Subversion Repositories HomeAutomation

Rev

Rev 1026 | Details | Compare with Previous | 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
 
1033 runge 45
#include "../Threads/mutex.h"
969 runge 46
#include "../Threads/thread.h"
47
#include "../Threads/semaphore.h"
48
#include "../Threads/threadsafequeue.h"
49
#include "../Tools/tools.h"
1033 runge 50
#include "../Logger/logger.h"
969 runge 51
 
52
#include "socketexception.h"
984 runge 53
#include "socketevent.h"
999 runge 54
#include "socketeventcallback.h"
969 runge 55
 
56
const int MAXBUFFER = 1024;
981 runge 57
const int MAXCONNECTIONS = 10;
969 runge 58
 
59
class AsyncSocket : public Thread<AsyncSocket>
60
{
61
public:
62
    AsyncSocket();
63
    ~AsyncSocket();
64
 
65
    void run();
66
 
999 runge 67
    int getId() { return myId; };
984 runge 68
    bool isConnected() { return mySocket != -1; };
969 runge 69
 
984 runge 70
    void forceReconnect() { myForceReconnect = true; };
71
    void setReconnectTimeout(unsigned int reconnectTimeout) { myReconnectTimeout = reconnectTimeout; };
72
    unsigned int getReconnectTimeout() { return myReconnectTimeout; };
73
    void setAddress(string address) { myAddress = address; };
74
    string getAddress() { return myAddress; };
75
    void setPort(int port) { myPort = port; };
76
    int getPort() { return myPort; };
77
    void setSocket(int socket) { mySocket = socket; };
78
    int getSocket() { return mySocket; };
969 runge 79
 
984 runge 80
    void eventStartListen() { myEventSemaphore.lock(); };
81
    void eventWait() { myEventSemaphore.wait(); };
82
    void eventStopListen() { myEventSemaphore.unlock(); };
83
    bool eventIsAvailable() { return myEventQueue.size() > 0; };
84
    SocketEvent eventFetch() { return myEventQueue.pop(); };
85
 
86
 
87
    void sendData(string data);
88
 
89
 
981 runge 90
    void startListen();
91
    bool accept(AsyncSocket* newSocket);
984 runge 92
 
969 runge 93
 
999 runge 94
    void eventSetCallback(SocketEventCallback *eventCallback);
981 runge 95
 
969 runge 96
protected:
97
    void reconnectLoop();
984 runge 98
    void create();
99
 
969 runge 100
    void connect();
984 runge 101
 
102
    void silentClose();
969 runge 103
    void close();
104
 
105
 
106
private:
999 runge 107
    int myId;
969 runge 108
    string myAddress;
109
    int myPort;
110
    unsigned int myReconnectTimeout;
984 runge 111
    int mySocket;
112
    bool myForceReconnect;
113
    sockaddr_in myAddressStruct;
114
 
1033 runge 115
    Mutex mySendMutex;
999 runge 116
 
984 runge 117
    Semaphore myEventSemaphore;
118
    ThreadSafeQueue<SocketEvent> myEventQueue;
999 runge 119
    SocketEventCallback *myEventCallback;
984 runge 120
    void eventAdd(unsigned int eventType, string eventData);
121
    void eventAdd(unsigned int eventType);
122
 
123
    bool receiveData();
969 runge 124
};
125
 
126
#endif  /* _ASYNCSOCKET_H */
127