Subversion Repositories HomeAutomation

Rev

Rev 983 | Rev 999 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 983 Rev 984
Line 45... Line 45...
45
 
45
 
46
#include "../Threads/thread.h"
46
#include "../Threads/thread.h"
47
#include "../Threads/semaphore.h"
47
#include "../Threads/semaphore.h"
48
#include "../Threads/threadsafequeue.h"
48
#include "../Threads/threadsafequeue.h"
49
#include "../Tools/tools.h"
49
#include "../Tools/tools.h"
50
#include "../SyslogStream/syslogstream.h"
-
 
51
 
50
 
52
#include "socketexception.h"
51
#include "socketexception.h"
53
 
-
 
54
#define ASYNCSOCKET_EVENT_NONE 0
-
 
55
#define ASYNCSOCKET_EVENT_DATA 1
52
#include "socketevent.h"
56
#define ASYNCSOCKET_EVENT_CONNECTED 2
-
 
57
#define ASYNCSOCKET_EVENT_CONNECT_FAILED 3
-
 
58
#define ASYNCSOCKET_EVENT_WAITING_TO_RECONNECT 4
-
 
59
#define ASYNCSOCKET_EVENT_CLOSED 5
-
 
60
#define ASYNCSOCKET_EVENT_RESET 6
-
 
61
#define ASYNCSOCKET_EVENT_DIED 7
-
 
62
#define ASYNCSOCKET_EVENT_INACTIVITY 8
-
 
63
 
53
 
64
const int MAXBUFFER = 1024;
54
const int MAXBUFFER = 1024;
65
const int MAXCONNECTIONS = 10;
55
const int MAXCONNECTIONS = 10;
66
 
56
 
67
class AsyncSocket : public Thread<AsyncSocket>
57
class AsyncSocket : public Thread<AsyncSocket>
68
{
58
{
69
public:
59
public:
70
    AsyncSocket();
60
    AsyncSocket();
71
    ~AsyncSocket();
61
    ~AsyncSocket();
72
 
62
 
73
    void run();
63
    void run();
74
 
64
 
-
 
65
    string getId() { return myId; };
-
 
66
    bool isConnected() { return mySocket != -1; };
-
 
67
 
-
 
68
    void forceReconnect() { myForceReconnect = true; };
-
 
69
    void setReconnectTimeout(unsigned int reconnectTimeout) { myReconnectTimeout = reconnectTimeout; };
75
    void setReconnectTimeout(unsigned int timeout);
70
    unsigned int getReconnectTimeout() { return myReconnectTimeout; };
76
    void setAddress(string address, int port);
71
    void setAddress(string address) { myAddress = address; };
-
 
72
    string getAddress() { return myAddress; };
77
    void setPort(int port);
73
    void setPort(int port) { myPort = port; };
-
 
74
    int getPort() { return myPort; };
78
    void setSocket(int socket);
75
    void setSocket(int socket) { mySocket = socket; };
79
    int getSocket();
76
    int getSocket() { return mySocket; };
-
 
77
 
-
 
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);
80
 
86
 
81
    bool availableEvent();
-
 
82
    void startEvent();
-
 
83
    int getEvent();
-
 
84
    void waitForEvent();
-
 
85
    void stopEvent();
-
 
86
 
87
   
87
    bool availableData();
-
 
88
    string getData();
-
 
89
    bool sendData(string data);
-
 
90
    void sendDataDirect(string data);
-
 
91
    void forceReconnect();
-
 
92
    void startListen();
88
    void startListen();
93
    bool accept(AsyncSocket* newSocket);
89
    bool accept(AsyncSocket* newSocket);
94
    bool isConnected();
90
   
95
 
91
 
96
    //static void signalHandler(int signum);
-
 
97
    string getId() { return myId; };
-
 
98
 
92
   
99
    //static Mutex mySocketsMutex;
-
 
100
    //static map<string, AsyncSocket*> mySockets;
-
 
101
    //static Semaphore mySemaphore;
-
 
102
 
93
 
103
protected:
94
protected:
104
    void reconnectLoop();
95
    void reconnectLoop();
105
    void connect();
-
 
106
    void create();
96
    void create();
-
 
97
 
-
 
98
    void connect();
-
 
99
   
-
 
100
    void silentClose();
107
    void close();
101
    void close();
108
    void setEvent(int event);
-
 
109
 
-
 
110
 
102
 
111
 
103
 
112
private:
104
private:
113
    string myId;
105
    string myId;
114
    Semaphore myEventSemaphore;
-
 
115
    ThreadSafeQueue<int> myEventQueue;
-
 
116
 
-
 
117
    bool myForceReconnect;
-
 
118
   
-
 
119
    ThreadSafeQueue<string> myInQueue;
-
 
120
    //ThreadSafeQueue<string> myOutQueue;
-
 
121
 
-
 
122
    int mySocket;
-
 
123
    sockaddr_in myAddressStruct;
-
 
124
    string myAddress;
106
    string myAddress;
125
    int myPort;
107
    int myPort;
126
    unsigned int myReconnectTimeout;
108
    unsigned int myReconnectTimeout;
-
 
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();
127
};
119
};
128
 
120
 
129
#endif  /* _ASYNCSOCKET_H */
121
#endif  /* _ASYNCSOCKET_H */
130
 
122