Skip to content
Permalink
b1393ff48b
Switch branches/tags

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?
Go to file
 
 
Cannot retrieve contributors at this time
80 lines (65 sloc) 1.96 KB
#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