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