Rev 974 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 974 | Rev 981 | ||
|---|---|---|---|
| 1 | /*************************************************************************** |
1 | /*************************************************************************** |
| 2 | * Copyright (C) December 6, 2008 by Mattias Runge * |
2 | * Copyright (C) December 6, 2008 by Mattias Runge * |
| 3 | * mattias@runge.se * |
3 | * mattias@runge.se * |
| 4 | * asyncsocket.h * |
4 | * asyncsocket.h * |
| 5 | * * |
5 | * * |
| 6 | * This program is free software; you can redistribute it and/or modify * |
6 | * This program is free software; you can redistribute it and/or modify * |
| 7 | * it under the terms of the GNU General Public License as published by * |
7 | * it under the terms of the GNU General Public License as published by * |
| 8 | * the Free Software Foundation; either version 2 of the License, or * |
8 | * the Free Software Foundation; either version 2 of the License, or * |
| 9 | * (at your option) any later version. * |
9 | * (at your option) any later version. * |
| 10 | * * |
10 | * * |
| 11 | * This program is distributed in the hope that it will be useful, * |
11 | * This program is distributed in the hope that it will be useful, * |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 | * GNU General Public License for more details. * |
14 | * GNU General Public License for more details. * |
| 15 | * * |
15 | * * |
| 16 | * You should have received a copy of the GNU General Public License * |
16 | * You should have received a copy of the GNU General Public License * |
| 17 | * along with this program; if not, write to the * |
17 | * along with this program; if not, write to the * |
| 18 | * Free Software Foundation, Inc., * |
18 | * Free Software Foundation, Inc., * |
| 19 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
19 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
| 20 | ***************************************************************************/ |
20 | ***************************************************************************/ |
| 21 | 21 | ||
| 22 | #ifndef _ASYNCSOCKET_H |
22 | #ifndef _ASYNCSOCKET_H |
| 23 | #define _ASYNCSOCKET_H |
23 | #define _ASYNCSOCKET_H |
| 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> |
| 34 | #include <unistd.h> |
35 | #include <unistd.h> |
| 35 | #include <string> |
36 | #include <string> |
| 36 | #include <arpa/inet.h> |
37 | #include <arpa/inet.h> |
| 37 | #include <string.h> |
38 | #include <string.h> |
| 38 | #include <errno.h> |
39 | #include <errno.h> |
| 39 | #include <fcntl.h> |
40 | #include <fcntl.h> |
| 40 | #include <sys/ioctl.h> |
41 | #include <sys/ioctl.h> |
| 41 | #include <signal.h> |
42 | #include <signal.h> |
| 42 | #include <netinet/tcp.h> |
43 | #include <netinet/tcp.h> |
| 43 | 44 | ||
| 44 | 45 | ||
| 45 | #include "../Threads/thread.h" |
46 | #include "../Threads/thread.h" |
| 46 | #include "../Threads/semaphore.h" |
47 | #include "../Threads/semaphore.h" |
| 47 | #include "../Threads/threadsafequeue.h" |
48 | #include "../Threads/threadsafequeue.h" |
| 48 | #include "../Tools/tools.h" |
49 | #include "../Tools/tools.h" |
| 49 | #include "../SyslogStream/syslogstream.h" |
50 | #include "../SyslogStream/syslogstream.h" |
| 50 | 51 | ||
| 51 | #include "socketexception.h" |
52 | #include "socketexception.h" |
| 52 | 53 | ||
| 53 | #define ASYNCSOCKET_EVENT_NONE 0 |
54 | #define ASYNCSOCKET_EVENT_NONE 0 |
| 54 | #define ASYNCSOCKET_EVENT_DATA 1 |
55 | #define ASYNCSOCKET_EVENT_DATA 1 |
| 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(); |
| 65 | ~AsyncSocket(); |
67 | ~AsyncSocket(); |
| 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 | ||
| 98 | ThreadSafeQueue<string> myInQueue; |
114 | ThreadSafeQueue<string> myInQueue; |
| 99 | ThreadSafeQueue<string> myOutQueue; |
115 | ThreadSafeQueue<string> myOutQueue; |
| 100 | 116 | ||
| 101 | int mySocket; |
117 | int mySocket; |
| 102 | sockaddr_in myAddressStruct; |
118 | sockaddr_in myAddressStruct; |
| 103 | string myAddress; |
119 | string myAddress; |
| 104 | int myPort; |
120 | int myPort; |
| 105 | unsigned int myReconnectTimeout; |
121 | unsigned int myReconnectTimeout; |
| 106 | }; |
122 | }; |
| 107 | 123 | ||
| 108 | #endif /* _ASYNCSOCKET_H */ |
124 | #endif /* _ASYNCSOCKET_H */ |
| 109 | 125 | ||
| 110 | 126 | ||