Details | Last modification | View Log | SVN | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 969 | runge | 1 | /*************************************************************************** |
| 2 | * Copyright (C) December 6, 2008 by Mattias Runge * |
||
| 3 | * mattias@runge.se * |
||
| 4 | * asyncsocket.h * |
||
| 5 | * * |
||
| 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 * |
||
| 8 | * the Free Software Foundation; either version 2 of the License, or * |
||
| 9 | * (at your option) any later version. * |
||
| 10 | * * |
||
| 11 | * This program is distributed in the hope that it will be useful, * |
||
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
||
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
||
| 14 | * GNU General Public License for more details. * |
||
| 15 | * * |
||
| 16 | * You should have received a copy of the GNU General Public License * |
||
| 17 | * along with this program; if not, write to the * |
||
| 18 | * Free Software Foundation, Inc., * |
||
| 19 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
||
| 20 | ***************************************************************************/ |
||
| 21 | |||
| 22 | #ifndef _ASYNCSOCKET_H |
||
| 23 | #define _ASYNCSOCKET_H |
||
| 24 | |||
| 25 | using namespace std; |
||
| 26 | |||
| 27 | #include <string> |
||
| 28 | #include <queue> |
||
| 981 | runge | 29 | #include <map> |
| 969 | runge | 30 | |
| 31 | #include <sys/types.h> |
||
| 32 | #include <sys/socket.h> |
||
| 33 | #include <netinet/in.h> |
||
| 34 | #include <netdb.h> |
||
| 35 | #include <unistd.h> |
||
| 36 | #include <string> |
||
| 37 | #include <arpa/inet.h> |
||
| 38 | #include <string.h> |
||
| 39 | #include <errno.h> |
||
| 40 | #include <fcntl.h> |
||
| 41 | #include <sys/ioctl.h> |
||
| 42 | #include <signal.h> |
||
| 974 | runge | 43 | #include <netinet/tcp.h> |
| 969 | runge | 44 | |
| 974 | runge | 45 | |
| 969 | runge | 46 | #include "../Threads/thread.h" |
| 47 | #include "../Threads/semaphore.h" |
||
| 48 | #include "../Threads/threadsafequeue.h" |
||
| 49 | #include "../Tools/tools.h" |
||
| 50 | |||
| 51 | #include "socketexception.h" |
||
| 984 | runge | 52 | #include "socketevent.h" |
| 969 | runge | 53 | |
| 54 | const int MAXBUFFER = 1024; |
||
| 981 | runge | 55 | const int MAXCONNECTIONS = 10; |
| 969 | runge | 56 | |
| 57 | class AsyncSocket : public Thread<AsyncSocket> |
||
| 58 | { |
||
| 59 | public: |
||
| 60 | AsyncSocket(); |
||
| 61 | ~AsyncSocket(); |
||
| 62 | |||
| 63 | void run(); |
||
| 64 | |||
| 984 | runge | 65 | string getId() { return myId; }; |
| 66 | bool isConnected() { return mySocket != -1; }; |
||
| 969 | runge | 67 | |
| 984 | runge | 68 | void forceReconnect() { myForceReconnect = true; }; |
| 69 | void setReconnectTimeout(unsigned int reconnectTimeout) { myReconnectTimeout = reconnectTimeout; }; |
||
| 70 | unsigned int getReconnectTimeout() { return myReconnectTimeout; }; |
||
| 71 | void setAddress(string address) { myAddress = address; }; |
||
| 72 | string getAddress() { return myAddress; }; |
||
| 73 | void setPort(int port) { myPort = port; }; |
||
| 74 | int getPort() { return myPort; }; |
||
| 75 | void setSocket(int socket) { mySocket = socket; }; |
||
| 76 | int getSocket() { return mySocket; }; |
||
| 969 | runge | 77 | |
| 984 | runge | 78 | void eventStartListen() { myEventSemaphore.lock(); }; |
| 79 | void eventWait() { myEventSemaphore.wait(); }; |
||
| 80 | void eventStopListen() { myEventSemaphore.unlock(); }; |
||
| 81 | bool eventIsAvailable() { return myEventQueue.size() > 0; }; |
||
| 82 | SocketEvent eventFetch() { return myEventQueue.pop(); }; |
||
| 83 | |||
| 84 | |||
| 85 | void sendData(string data); |
||
| 86 | |||
| 87 | |||
| 981 | runge | 88 | void startListen(); |
| 89 | bool accept(AsyncSocket* newSocket); |
||
| 984 | runge | 90 | |
| 969 | runge | 91 | |
| 984 | runge | 92 | |
| 981 | runge | 93 | |
| 969 | runge | 94 | protected: |
| 95 | void reconnectLoop(); |
||
| 984 | runge | 96 | void create(); |
| 97 | |||
| 969 | runge | 98 | void connect(); |
| 984 | runge | 99 | |
| 100 | void silentClose(); |
||
| 969 | runge | 101 | void close(); |
| 102 | |||
| 103 | |||
| 104 | private: |
||
| 981 | runge | 105 | string myId; |
| 969 | runge | 106 | string myAddress; |
| 107 | int myPort; |
||
| 108 | unsigned int myReconnectTimeout; |
||
| 984 | runge | 109 | int mySocket; |
| 110 | bool myForceReconnect; |
||
| 111 | sockaddr_in myAddressStruct; |
||
| 112 | |||
| 113 | Semaphore myEventSemaphore; |
||
| 114 | ThreadSafeQueue<SocketEvent> myEventQueue; |
||
| 115 | void eventAdd(unsigned int eventType, string eventData); |
||
| 116 | void eventAdd(unsigned int eventType); |
||
| 117 | |||
| 118 | bool receiveData(); |
||
| 969 | runge | 119 | }; |
| 120 | |||
| 121 | #endif /* _ASYNCSOCKET_H */ |
||
| 122 |