Subversion Repositories HomeAutomation

Rev

Rev 974 | Blame | Last modification | View Log | SVN | RSS feed

  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. #include <map>
  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>
  43. #include <netinet/tcp.h>
  44.  
  45.  
  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
  57. #define ASYNCSOCKET_EVENT_DIED 3
  58. #define ASYNCSOCKET_EVENT_INACTIVITY 4
  59.  
  60. const int MAXBUFFER = 1024;
  61. const int MAXCONNECTIONS = 10;
  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);
  73.     void setPort(int port);
  74.     void setSocket(int socket);
  75.     int getSocket();
  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);
  85.     void sendDataDirect(string data);
  86.     void forceReconnect();
  87.     void startListen();
  88.     bool accept(AsyncSocket* newSocket);
  89.     bool isConnected();
  90.  
  91.     static void signalHandler(int signum);
  92.     string getId() { return myId; };
  93.  
  94.     static Mutex mySocketsMutex;
  95.     static map<string, AsyncSocket*> mySockets;
  96.     Semaphore mySemaphore;
  97.  
  98. protected:
  99.     void reconnectLoop();
  100.     void connect();
  101.     void create();
  102.     void close();
  103.     void setEvent(int event);
  104.  
  105.  
  106.  
  107. private:
  108.     string myId;
  109.     Semaphore myEventSemaphore;
  110.     int myEvent;
  111.  
  112.     bool myForceReconnect;
  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.  
  126.