Subversion Repositories HomeAutomation

Rev

Rev 1026 | Blame | Compare with Previous | 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. #include "../Threads/mutex.h"
  46. #include "../Threads/thread.h"
  47. #include "../Threads/semaphore.h"
  48. #include "../Threads/threadsafequeue.h"
  49. #include "../Tools/tools.h"
  50. #include "../Logger/logger.h"
  51.  
  52. #include "socketexception.h"
  53. #include "socketevent.h"
  54. #include "socketeventcallback.h"
  55.  
  56. const int MAXBUFFER = 1024;
  57. const int MAXCONNECTIONS = 10;
  58.  
  59. class AsyncSocket : public Thread<AsyncSocket>
  60. {
  61. public:
  62.     AsyncSocket();
  63.     ~AsyncSocket();
  64.  
  65.     void run();
  66.  
  67.     int getId() { return myId; };
  68.     bool isConnected() { return mySocket != -1; };
  69.  
  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; };
  79.  
  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.    
  90.     void startListen();
  91.     bool accept(AsyncSocket* newSocket);
  92.    
  93.  
  94.     void eventSetCallback(SocketEventCallback *eventCallback);
  95.  
  96. protected:
  97.     void reconnectLoop();
  98.     void create();
  99.  
  100.     void connect();
  101.    
  102.     void silentClose();
  103.     void close();
  104.  
  105.  
  106. private:
  107.     int myId;
  108.     string myAddress;
  109.     int myPort;
  110.     unsigned int myReconnectTimeout;
  111.     int mySocket;
  112.     bool myForceReconnect;
  113.     sockaddr_in myAddressStruct;
  114.  
  115.     Mutex mySendMutex;
  116.  
  117.     Semaphore myEventSemaphore;
  118.     ThreadSafeQueue<SocketEvent> myEventQueue;
  119.     SocketEventCallback *myEventCallback;
  120.     void eventAdd(unsigned int eventType, string eventData);
  121.     void eventAdd(unsigned int eventType);
  122.  
  123.     bool receiveData();
  124. };
  125.  
  126. #endif  /* _ASYNCSOCKET_H */
  127.  
  128.