Subversion Repositories HomeAutomation

Rev

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