root / branches / test / CCV_Select_Camera / addons / ofxNetwork / src / ofxTCPServer.h @ 12
View | Annotate | Download (2.2 KB)
| 1 | #ifndef _OFX_TCP_SERVER_
|
|---|---|
| 2 | #define _OFX_TCP_SERVER_
|
| 3 | |
| 4 | #include "ofConstants.h" |
| 5 | #include "ofxThread.h" |
| 6 | #include "ofxTCPManager.h" |
| 7 | #include <map> |
| 8 | |
| 9 | #define TCP_MAX_CLIENTS 32 |
| 10 | |
| 11 | //forward decleration
|
| 12 | class ofxTCPClient; |
| 13 | |
| 14 | class ofxTCPServer : public ofxThread{
|
| 15 | |
| 16 | public:
|
| 17 | |
| 18 | ofxTCPServer(); |
| 19 | ~ofxTCPServer(); |
| 20 | void setVerbose(bool _verbose); |
| 21 | bool setup(int _port, bool blocking = false); |
| 22 | bool close();
|
| 23 | bool disconnectClient(int clientID); |
| 24 | |
| 25 | int getNumClients();
|
| 26 | int getPort();
|
| 27 | bool isConnected();
|
| 28 | |
| 29 | int getClientPort(int clientID); |
| 30 | string getClientIP(int clientID);
|
| 31 | |
| 32 | bool isClientSetup(int clientID); |
| 33 | bool isClientConnected(int clientID); |
| 34 | |
| 35 | //send data as a string - a short message
|
| 36 | //is added to the end of the string which is
|
| 37 | //used to indicate the end of the message to
|
| 38 | //the receiver see: STR_END_MSG (ofTCPClient.h)
|
| 39 | bool send(int clientID, string message); |
| 40 | bool sendToAll(string message);
|
| 41 | |
| 42 | //send and receive raw bytes lets you send and receive
|
| 43 | //byte (char) arrays without modifiying or appending the data.
|
| 44 | //Strings terminate on null bytes so this is the better option
|
| 45 | //if you are trying to send something other than just ascii strings
|
| 46 | |
| 47 | bool sendRawBytes(int clientID, const char * rawBytes, const int numBytes); |
| 48 | bool sendRawBytesToAll(const char * rawBytes, const int numBytes); |
| 49 | |
| 50 | //the received message length in bytes
|
| 51 | int getNumReceivedBytes(int clientID); |
| 52 | |
| 53 | //get the message as a string
|
| 54 | //this will only work with messages coming via
|
| 55 | //send() and sendToAll()
|
| 56 | //or from messages terminating with the STR_END_MSG
|
| 57 | //which by default is [/TCP]
|
| 58 | //eg: if you want to send "Hello World" from other
|
| 59 | //software and want to receive it as a string
|
| 60 | //sender should send "Hello World[/TCP]"
|
| 61 | string receive(int clientID);
|
| 62 | |
| 63 | //pass in buffer to be filled - make sure the buffer
|
| 64 | //is at least as big as numBytes
|
| 65 | int receiveRawBytes(int clientID, char * receiveBytes, int numBytes); |
| 66 | |
| 67 | |
| 68 | //don't call this
|
| 69 | //--------------------------
|
| 70 | void threadedFunction();
|
| 71 | |
| 72 | |
| 73 | ofxTCPManager TCPServer; |
| 74 | map<int,ofxTCPClient> TCPConnections;
|
| 75 | |
| 76 | protected:
|
| 77 | bool connected, verbose;
|
| 78 | string str; |
| 79 | int idCount, port;
|
| 80 | bool bClientBlocking;
|
| 81 | |
| 82 | }; |
| 83 | |
| 84 | |
| 85 | #endif
|
| 86 | |
| 87 | |
| 88 |
