Subversion Repositories HomeAutomation

Rev

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.  
  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>
  42.  
  43. #include "../Threads/thread.h"
  44. #include "../Threads/semaphore.h"
  45. #include "../Threads/threadsafequeue.h"
  46. #include "../Tools/tools.h"
  47. #include "../SyslogStream/syslogstream.h"
  48.  
  49. #include "socketexception.h"
  50.  
  51. #define ASYNCSOCKET_EVENT_NONE 0
  52. #define ASYNCSOCKET_EVENT_DATA 1
  53. #define ASYNCSOCKET_EVENT_CLOSED 2
  54. #define ASYNCSOCKET_EVENT_DIED 2
  55.  
  56. const int MAXBUFFER = 1024;
  57.  
  58. class AsyncSocket : public Thread<AsyncSocket>
  59. {
  60. public:
  61.     AsyncSocket();
  62.     ~AsyncSocket();
  63.  
  64.     void run();
  65.  
  66.     void setReconnectTimeout(unsigned int timeout);
  67.     void setAddress(string address, int port);
  68.  
  69.     void startEvent();
  70.     int getEvent();
  71.     void waitForEvent();
  72.     void stopEvent();
  73.  
  74.     bool availableData();
  75.     string getData();
  76.     bool sendData(string data);
  77.  
  78.     static void signalHandler(int signum);
  79.    
  80. protected:
  81.     void reconnectLoop();
  82.     void connect();
  83.     void close();
  84.     void setEvent(int event);
  85.  
  86.     static Semaphore mySemaphore;
  87.  
  88. private:
  89.     Semaphore myEventSemaphore;
  90.     int myEvent;
  91.    
  92.     ThreadSafeQueue<string> myInQueue;
  93.     ThreadSafeQueue<string> myOutQueue;
  94.  
  95.     int mySocket;
  96.     sockaddr_in myAddressStruct;
  97.     string myAddress;
  98.     int myPort;
  99.     unsigned int myReconnectTimeout;
  100. };
  101.  
  102. #endif  /* _ASYNCSOCKET_H */
  103.  
  104.