Subversion Repositories HomeAutomation

Rev

Rev 999 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 999 Rev 1026
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
#include <map>
30
 
30
 
31
#include <sys/types.h>
31
#include <sys/types.h>
32
#include <sys/socket.h>
32
#include <sys/socket.h>
33
#include <netinet/in.h>
33
#include <netinet/in.h>
34
#include <netdb.h>
34
#include <netdb.h>
35
#include <unistd.h>
35
#include <unistd.h>
36
#include <string>
36
#include <string>
37
#include <arpa/inet.h>
37
#include <arpa/inet.h>
38
#include <string.h>
38
#include <string.h>
39
#include <errno.h>
39
#include <errno.h>
40
#include <fcntl.h>
40
#include <fcntl.h>
41
#include <sys/ioctl.h>
41
#include <sys/ioctl.h>
42
#include <signal.h>
42
#include <signal.h>
43
#include <netinet/tcp.h>
43
#include <netinet/tcp.h>
44
 
44
 
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
 
50
 
51
#include "socketexception.h"
51
#include "socketexception.h"
52
#include "socketevent.h"
52
#include "socketevent.h"
53
#include "socketeventcallback.h"
53
#include "socketeventcallback.h"
54
 
54
 
55
const int MAXBUFFER = 1024;
55
const int MAXBUFFER = 1024;
56
const int MAXCONNECTIONS = 10;
56
const int MAXCONNECTIONS = 10;
57
 
57
 
58
class AsyncSocket : public Thread<AsyncSocket>
58
class AsyncSocket : public Thread<AsyncSocket>
59
{
59
{
60
public:
60
public:
61
    AsyncSocket();
61
    AsyncSocket();
62
    ~AsyncSocket();
62
    ~AsyncSocket();
63
 
63
 
64
    void run();
64
    void run();
65
 
65
 
66
    int getId() { return myId; };
66
    int getId() { return myId; };
67
    bool isConnected() { return mySocket != -1; };
67
    bool isConnected() { return mySocket != -1; };
68
 
68
 
69
    void forceReconnect() { myForceReconnect = true; };
69
    void forceReconnect() { myForceReconnect = true; };
70
    void setReconnectTimeout(unsigned int reconnectTimeout) { myReconnectTimeout = reconnectTimeout; };
70
    void setReconnectTimeout(unsigned int reconnectTimeout) { myReconnectTimeout = reconnectTimeout; };
71
    unsigned int getReconnectTimeout() { return myReconnectTimeout; };
71
    unsigned int getReconnectTimeout() { return myReconnectTimeout; };
72
    void setAddress(string address) { myAddress = address; };
72
    void setAddress(string address) { myAddress = address; };
73
    string getAddress() { return myAddress; };
73
    string getAddress() { return myAddress; };
74
    void setPort(int port) { myPort = port; };
74
    void setPort(int port) { myPort = port; };
75
    int getPort() { return myPort; };
75
    int getPort() { return myPort; };
76
    void setSocket(int socket) { mySocket = socket; };
76
    void setSocket(int socket) { mySocket = socket; };
77
    int getSocket() { return mySocket; };
77
    int getSocket() { return mySocket; };
78
 
78
 
79
    void eventStartListen() { myEventSemaphore.lock(); };
79
    void eventStartListen() { myEventSemaphore.lock(); };
80
    void eventWait() { myEventSemaphore.wait(); };
80
    void eventWait() { myEventSemaphore.wait(); };
81
    void eventStopListen() { myEventSemaphore.unlock(); };
81
    void eventStopListen() { myEventSemaphore.unlock(); };
82
    bool eventIsAvailable() { return myEventQueue.size() > 0; };
82
    bool eventIsAvailable() { return myEventQueue.size() > 0; };
83
    SocketEvent eventFetch() { return myEventQueue.pop(); };
83
    SocketEvent eventFetch() { return myEventQueue.pop(); };
84
 
84
 
85
 
85
 
86
    void sendData(string data);
86
    void sendData(string data);
87
 
87
 
88
   
88
   
89
    void startListen();
89
    void startListen();
90
    bool accept(AsyncSocket* newSocket);
90
    bool accept(AsyncSocket* newSocket);
91
   
91
   
92
 
92
 
93
    void eventSetCallback(SocketEventCallback *eventCallback);
93
    void eventSetCallback(SocketEventCallback *eventCallback);
94
 
94
 
95
protected:
95
protected:
96
    void reconnectLoop();
96
    void reconnectLoop();
97
    void create();
97
    void create();
98
 
98
 
99
    void connect();
99
    void connect();
100
   
100
   
101
    void silentClose();
101
    void silentClose();
102
    void close();
102
    void close();
103
 
103
 
104
 
104
 
105
private:
105
private:
106
    int myId;
106
    int myId;
107
    string myAddress;
107
    string myAddress;
108
    int myPort;
108
    int myPort;
109
    unsigned int myReconnectTimeout;
109
    unsigned int myReconnectTimeout;
110
    int mySocket;
110
    int mySocket;
111
    bool myForceReconnect;
111
    bool myForceReconnect;
112
    sockaddr_in myAddressStruct;
112
    sockaddr_in myAddressStruct;
113
 
113
 
114
 
114
 
115
    Semaphore myEventSemaphore;
115
    Semaphore myEventSemaphore;
116
    ThreadSafeQueue<SocketEvent> myEventQueue;
116
    ThreadSafeQueue<SocketEvent> myEventQueue;
117
    SocketEventCallback *myEventCallback;
117
    SocketEventCallback *myEventCallback;
118
    void eventAdd(unsigned int eventType, string eventData);
118
    void eventAdd(unsigned int eventType, string eventData);
119
    void eventAdd(unsigned int eventType);
119
    void eventAdd(unsigned int eventType);
120
 
120
 
121
    bool receiveData();
121
    bool receiveData();
122
};
122
};
123
 
123
 
124
#endif  /* _ASYNCSOCKET_H */
124
#endif  /* _ASYNCSOCKET_H */
125
 
125
 
126
 
126