#pragma once
#include "ofConstants.h"
#include "ofThread.h"
#include "ofxTCPManager.h"
#include "ofxTCPSettings.h"
#include <map>
#include <condition_variable>
#define TCP_MAX_CLIENTS 32
class ofxTCPClient;
class ofxTCPServer : public ofThread{
public:
ofxTCPServer();
~ofxTCPServer();
ofxTCPServer(const ofxTCPServer & mom) = delete;
ofxTCPServer & operator=(const ofxTCPServer & mom) = delete;
void setVerbose(bool _verbose);
bool setup(int _port, bool blocking = false);
bool setup(const ofxTCPSettings & settings);
void setMessageDelimiter(std::string delim);
bool close();
bool disconnectClient(int clientID);
bool disconnectAllClients();
int getNumClients();
int getLastID();
int getPort();
bool isConnected();
int getClientPort(int clientID);
std::string getClientIP(int clientID);
bool isClientConnected(int clientID);
bool send(int clientID, std::string message);
bool sendToAll(std::string message);
bool sendRawMsg(int clientID, const char * rawMsg, const int numBytes);
bool sendRawMsgToAll(const char * rawMsg, const int numBytes);
bool sendRawBytes(int clientID, const char * rawBytes, const int numBytes);
bool sendRawBytesToAll(const char * rawBytes, const int numBytes);
int getNumReceivedBytes(int clientID);
std::string receive(int clientID);
int receiveRawMsg(int clientID, char * receiveBytes, int numBytes);
int receiveRawBytes(int clientID, char * receiveBytes, int numBytes);
int peekReceiveRawBytes(int clientID, char * receiveBytes, int numBytes);
void waitConnectedClient();
void waitConnectedClient(int ms);
private:
ofxTCPClient & getClient(int clientID);
bool isClientSetup(int clientID);
void threadedFunction();
ofxTCPManager TCPServer;
std::map<int,std::shared_ptr<ofxTCPClient> > TCPConnections;
std::mutex mConnectionsLock;
std::condition_variable serverReady;
bool connected;
std::string str;
int idCount, port;
bool bClientBlocking;
std::string messageDelimiter;
};
Comments