Rev 969 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 969 | Rev 974 | ||
|---|---|---|---|
| 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 | 29 | ||
| 30 | #include <sys/types.h> |
30 | #include <sys/types.h> |
| 31 | #include <sys/socket.h> |
31 | #include <sys/socket.h> |
| 32 | #include <netinet/in.h> |
32 | #include <netinet/in.h> |
| 33 | #include <netdb.h> |
33 | #include <netdb.h> |
| 34 | #include <unistd.h> |
34 | #include <unistd.h> |
| 35 | #include <string> |
35 | #include <string> |
| 36 | #include <arpa/inet.h> |
36 | #include <arpa/inet.h> |
| 37 | #include <string.h> |
37 | #include <string.h> |
| 38 | #include <errno.h> |
38 | #include <errno.h> |
| 39 | #include <fcntl.h> |
39 | #include <fcntl.h> |
| 40 | #include <sys/ioctl.h> |
40 | #include <sys/ioctl.h> |
| 41 | #include <signal.h> |
41 | #include <signal.h> |
| - | 42 | #include <netinet/tcp.h> |
|
| - | 43 | ||
| 42 | 44 | ||
| 43 | #include "../Threads/thread.h" |
45 | #include "../Threads/thread.h" |
| 44 | #include "../Threads/semaphore.h" |
46 | #include "../Threads/semaphore.h" |
| 45 | #include "../Threads/threadsafequeue.h" |
47 | #include "../Threads/threadsafequeue.h" |
| 46 | #include "../Tools/tools.h" |
48 | #include "../Tools/tools.h" |
| 47 | #include "../SyslogStream/syslogstream.h" |
49 | #include "../SyslogStream/syslogstream.h" |
| 48 | 50 | ||
| 49 | #include "socketexception.h" |
51 | #include "socketexception.h" |
| 50 | 52 | ||
| 51 | #define ASYNCSOCKET_EVENT_NONE 0 |
53 | #define ASYNCSOCKET_EVENT_NONE 0 |
| 52 | #define ASYNCSOCKET_EVENT_DATA 1 |
54 | #define ASYNCSOCKET_EVENT_DATA 1 |
| 53 | #define ASYNCSOCKET_EVENT_CLOSED 2 |
55 | #define ASYNCSOCKET_EVENT_CLOSED 2 |
| 54 | #define ASYNCSOCKET_EVENT_DIED |
56 | #define ASYNCSOCKET_EVENT_DIED 3 |
| - | 57 | #define ASYNCSOCKET_EVENT_INACTIVITY 4 |
|
| 55 | 58 | ||
| 56 | const int MAXBUFFER = 1024; |
59 | const int MAXBUFFER = 1024; |
| 57 | 60 | ||
| 58 | class AsyncSocket : public Thread<AsyncSocket> |
61 | class AsyncSocket : public Thread<AsyncSocket> |
| 59 | { |
62 | { |
| 60 | public: |
63 | public: |
| 61 | AsyncSocket(); |
64 | AsyncSocket(); |
| 62 | ~AsyncSocket(); |
65 | ~AsyncSocket(); |
| 63 | 66 | ||
| 64 | void run(); |
67 | void run(); |
| 65 | 68 | ||
| 66 | void setReconnectTimeout(unsigned int timeout); |
69 | void setReconnectTimeout(unsigned int timeout); |
| 67 | void setAddress(string address, int port); |
70 | void setAddress(string address, int port); |
| 68 | 71 | ||
| 69 | void startEvent(); |
72 | void startEvent(); |
| 70 | int getEvent(); |
73 | int getEvent(); |
| 71 | void waitForEvent(); |
74 | void waitForEvent(); |
| 72 | void stopEvent(); |
75 | void stopEvent(); |
| 73 | 76 | ||
| 74 | bool availableData(); |
77 | bool availableData(); |
| 75 | string getData(); |
78 | string getData(); |
| 76 | bool sendData(string data); |
79 | bool sendData(string data); |
| - | 80 | void forceReconnect(); |
|
| 77 | 81 | ||
| 78 | static void signalHandler(int signum); |
82 | static void signalHandler(int signum); |
| 79 | 83 | ||
| 80 | protected: |
84 | protected: |
| 81 | void reconnectLoop(); |
85 | void reconnectLoop(); |
| 82 | void connect(); |
86 | void connect(); |
| 83 | void close(); |
87 | void close(); |
| 84 | void setEvent(int event); |
88 | void setEvent(int event); |
| 85 | 89 | ||
| 86 | static Semaphore mySemaphore; |
90 | static Semaphore mySemaphore; |
| 87 | 91 | ||
| 88 | private: |
92 | private: |
| 89 | Semaphore myEventSemaphore; |
93 | Semaphore myEventSemaphore; |
| 90 | int myEvent; |
94 | int myEvent; |
| - | 95 | ||
| - | 96 | bool myForceReconnect; |
|
| 91 | 97 | ||
| 92 | ThreadSafeQueue<string> myInQueue; |
98 | ThreadSafeQueue<string> myInQueue; |
| 93 | ThreadSafeQueue<string> myOutQueue; |
99 | ThreadSafeQueue<string> myOutQueue; |
| 94 | 100 | ||
| 95 | int mySocket; |
101 | int mySocket; |
| 96 | sockaddr_in myAddressStruct; |
102 | sockaddr_in myAddressStruct; |
| 97 | string myAddress; |
103 | string myAddress; |
| 98 | int myPort; |
104 | int myPort; |
| 99 | unsigned int myReconnectTimeout; |
105 | unsigned int myReconnectTimeout; |
| 100 | }; |
106 | }; |
| 101 | 107 | ||
| 102 | #endif /* _ASYNCSOCKET_H */ |
108 | #endif /* _ASYNCSOCKET_H */ |
| 103 | 109 | ||
| 104 | 110 | ||