Rev 974 | Rev 983 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 974 | Rev 981 | ||
|---|---|---|---|
| Line 24... | Line 24... | ||
| 24 | 24 | ||
| 25 | using namespace std; |
25 | using namespace std; |
| 26 | 26 | ||
| 27 | #include <string> |
27 | #include <string> |
| 28 | #include <queue> |
28 | #include <queue> |
| - | 29 | #include <map> |
|
| 29 | 30 | ||
| 30 | #include <sys/types.h> |
31 | #include <sys/types.h> |
| 31 | #include <sys/socket.h> |
32 | #include <sys/socket.h> |
| 32 | #include <netinet/in.h> |
33 | #include <netinet/in.h> |
| 33 | #include <netdb.h> |
34 | #include <netdb.h> |
| Line 55... | Line 56... | ||
| 55 | #define ASYNCSOCKET_EVENT_CLOSED 2 |
56 | #define ASYNCSOCKET_EVENT_CLOSED 2 |
| 56 | #define ASYNCSOCKET_EVENT_DIED 3 |
57 | #define ASYNCSOCKET_EVENT_DIED 3 |
| 57 | #define ASYNCSOCKET_EVENT_INACTIVITY 4 |
58 | #define ASYNCSOCKET_EVENT_INACTIVITY 4 |
| 58 | 59 | ||
| 59 | const int MAXBUFFER = 1024; |
60 | const int MAXBUFFER = 1024; |
| - | 61 | const int MAXCONNECTIONS = 10; |
|
| 60 | 62 | ||
| 61 | class AsyncSocket : public Thread<AsyncSocket> |
63 | class AsyncSocket : public Thread<AsyncSocket> |
| 62 | { |
64 | { |
| 63 | public: |
65 | public: |
| 64 | AsyncSocket(); |
66 | AsyncSocket(); |
| Line 66... | Line 68... | ||
| 66 | 68 | ||
| 67 | void run(); |
69 | void run(); |
| 68 | 70 | ||
| 69 | void setReconnectTimeout(unsigned int timeout); |
71 | void setReconnectTimeout(unsigned int timeout); |
| 70 | void setAddress(string address, int port); |
72 | void setAddress(string address, int port); |
| - | 73 | void setPort(int port); |
|
| - | 74 | void setSocket(int socket); |
|
| - | 75 | int getSocket(); |
|
| 71 | 76 | ||
| 72 | void startEvent(); |
77 | void startEvent(); |
| 73 | int getEvent(); |
78 | int getEvent(); |
| 74 | void waitForEvent(); |
79 | void waitForEvent(); |
| 75 | void stopEvent(); |
80 | void stopEvent(); |
| 76 | 81 | ||
| 77 | bool availableData(); |
82 | bool availableData(); |
| 78 | string getData(); |
83 | string getData(); |
| 79 | bool sendData(string data); |
84 | bool sendData(string data); |
| - | 85 | void sendDataDirect(string data); |
|
| 80 | void forceReconnect(); |
86 | void forceReconnect(); |
| - | 87 | void startListen(); |
|
| - | 88 | bool accept(AsyncSocket* newSocket); |
|
| - | 89 | bool isConnected(); |
|
| 81 | 90 | ||
| 82 | static void signalHandler(int signum); |
91 | static void signalHandler(int signum); |
| - | 92 | string getId() { return myId; }; |
|
| - | 93 | ||
| - | 94 | static Mutex mySocketsMutex; |
|
| - | 95 | static map<string, AsyncSocket*> mySockets; |
|
| - | 96 | Semaphore mySemaphore; |
|
| 83 | 97 | ||
| 84 | protected: |
98 | protected: |
| 85 | void reconnectLoop(); |
99 | void reconnectLoop(); |
| 86 | void connect(); |
100 | void connect(); |
| - | 101 | void create(); |
|
| 87 | void close(); |
102 | void close(); |
| 88 | void setEvent(int event); |
103 | void setEvent(int event); |
| 89 | 104 | ||
| 90 | static Semaphore mySemaphore; |
- | |
| - | 105 | ||
| 91 | 106 | ||
| 92 | private: |
107 | private: |
| - | 108 | string myId; |
|
| 93 | Semaphore myEventSemaphore; |
109 | Semaphore myEventSemaphore; |
| 94 | int myEvent; |
110 | int myEvent; |
| 95 | 111 | ||
| 96 | bool myForceReconnect; |
112 | bool myForceReconnect; |
| 97 | 113 | ||