Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
bfs/bfs/include/DataSyncThread.hpp
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
80 lines (65 sloc)
1.96 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef DATA_SYNC_THREAD_HPP | |
#define DATA_SYNC_THREAD_HPP | |
#define WIN32_LEAN_AND_MEAN | |
#include <windows.h> | |
#include <winsock2.h> | |
#include <ws2tcpip.h> | |
#include <stdlib.h> | |
#include <stdio.h> | |
#include <vector> | |
#include <iostream> | |
#include "Attribute.hpp" | |
#include "CMakeConfig.h" | |
#include "Logger.hpp" | |
// Need to link with Ws2_32.lib, Mswsock.lib, and Advapi32.lib | |
#pragma comment (lib, "Ws2_32.lib") | |
#pragma comment (lib, "Mswsock.lib") | |
#pragma comment (lib, "AdvApi32.lib") | |
using namespace std; | |
/* | |
This class contains the thread for communicating back and forth along a socket to sync attributes | |
*/ | |
class DataSyncThread | |
{ | |
private: | |
SOCKET sock; | |
HANDLE hThread = INVALID_HANDLE_VALUE; | |
DWORD dwThreadId; | |
// Flag for signalling this thread to terminate | |
bool continueThread = false; | |
// Variable changed only by the data sync thread itself for other threads to determine | |
// if this one is still running | |
bool threadRunning = false; | |
vector<Attribute> incomingAttributes; | |
HANDLE attribMutex; | |
public: | |
DataSyncThread(SOCKET s) | |
{ | |
sock = s; | |
attribMutex = CreateMutex(NULL, false, NULL); | |
}; | |
// Synchronous functions (only called from thread) | |
void threadRuntime(); | |
bool readyToReceive(int interval = 1); | |
static DWORD threadInit(LPVOID pThreadArgs) | |
{ | |
DataSyncThread* pDataSyncThread = (DataSyncThread*)pThreadArgs; | |
pDataSyncThread->threadRuntime(); | |
return NULL; | |
} | |
int recvBytes(void* buffer, size_t numBytes); | |
int sendBytes(char* buffer, size_t numBytes); | |
void addIncomingAttribute(Attribute attrib); | |
// Instantiates a datasyncthread on the heap connected to given server. | |
int connectToAlgorithm(char* serverName); | |
// Async control (only called outside of thread) | |
void startComms(); | |
bool isRunning(); | |
bool stopComms(); | |
bool isClientConnected() { return continueThread; }; | |
unsigned int getSocketNumber() { return (unsigned int) socket; }; | |
bool areIncomingAttributesAvailable(); | |
vector<Attribute> getIncomingAttributes(); | |
bool sendAttribute(Attribute attrib); | |
}; | |
#endif |