AsyncTcpConnection.h

Go to the documentation of this file.
00001 
00032 #ifndef ASYNC_TCP_CONNECTION_INCLUDED
00033 #define ASYNC_TCP_CONNECTION_INCLUDED
00034 
00035 
00036 /****************************************************************************
00037  *
00038  * System Includes
00039  *
00040  ****************************************************************************/
00041 
00042 #include <sigc++/sigc++.h>
00043 #include <stdint.h>
00044 
00045 #include <string>
00046 
00047 
00048 /****************************************************************************
00049  *
00050  * Project Includes
00051  *
00052  ****************************************************************************/
00053 
00054 #include <AsyncIpAddress.h>
00055 
00056 
00057 /****************************************************************************
00058  *
00059  * Local Includes
00060  *
00061  ****************************************************************************/
00062 
00063 
00064 
00065 /****************************************************************************
00066  *
00067  * Forward declarations
00068  *
00069  ****************************************************************************/
00070 
00071 
00072 
00073 /****************************************************************************
00074  *
00075  * Namespace
00076  *
00077  ****************************************************************************/
00078 
00079 namespace Async
00080 {
00081 
00082 /****************************************************************************
00083  *
00084  * Forward declarations of classes inside of the declared namespace
00085  *
00086  ****************************************************************************/
00087 
00088 class FdWatch;
00089 class IpAddress;
00090 
00091 
00092 /****************************************************************************
00093  *
00094  * Defines & typedefs
00095  *
00096  ****************************************************************************/
00097 
00098 
00099 
00100 /****************************************************************************
00101  *
00102  * Exported Global Variables
00103  *
00104  ****************************************************************************/
00105 
00106 
00107 
00108 /****************************************************************************
00109  *
00110  * Class definitions
00111  *
00112  ****************************************************************************/
00113 
00123 class TcpConnection : public SigC::Object
00124 {
00125   public:
00129     typedef enum
00130     { 
00131       DR_HOST_NOT_FOUND,       
00132       DR_REMOTE_DISCONNECTED,  
00133       DR_SYSTEM_ERROR,         
00134       DR_RECV_BUFFER_OVERFLOW, 
00135       DR_ORDERED_DISCONNECT    
00136     } DisconnectReason;
00137     
00141     static const int DEFAULT_RECV_BUF_LEN = 1024;
00142     
00146     static const char *disconnectReasonStr(DisconnectReason reason);
00147     
00152     explicit TcpConnection(size_t recv_buf_len = DEFAULT_RECV_BUF_LEN);
00153     
00161     TcpConnection(int sock, const IpAddress& remote_addr,
00162                   uint16_t remote_port,
00163                   size_t recv_buf_len = DEFAULT_RECV_BUF_LEN);
00164     
00168     ~TcpConnection(void);
00169     
00177     void disconnect(void);
00178     
00185     int write(const void *buf, int count);
00186     
00193     const IpAddress& remoteHost(void) const { return remote_addr; }
00194     
00199     uint16_t remotePort(void) const { return remote_port; }
00200     
00206     bool isConnected(void) const { return sock != -1; }
00207     
00213     SigC::Signal2<void, TcpConnection *, DisconnectReason> disconnected;
00214     
00229     SigC::Signal3<int, TcpConnection *, void *, int> dataReceived;
00230     
00236     SigC::Signal1<void, bool> sendBufferFull;
00237 
00238         
00239   protected:
00246     void setSocket(int sock);
00247     
00254     void setRemoteAddr(const IpAddress& remote_addr);
00255     
00262     void setRemotePort(uint16_t remote_port);
00263     
00271     int socket(void) const { return sock; }
00272     
00273     
00274   private:
00275     IpAddress remote_addr;
00276     uint16_t  remote_port;
00277     size_t    recv_buf_len;
00278     int       sock;
00279     FdWatch * rd_watch;
00280     FdWatch * wr_watch;
00281     char *    recv_buf;
00282     size_t    recv_buf_cnt;
00283     
00284     void recvHandler(FdWatch *watch);
00285     void writeHandler(FdWatch *watch);
00286 
00287 };  /* class TcpConnection */
00288 
00289 
00290 } /* namespace */
00291 
00292 #endif /* ASYNC_TCP_CONNECTION_INCLUDED */
00293 
00294 
00295 
00296 /*
00297  * This file has not been truncated
00298  */
00299 
Generated by  doxygen 1.6.2-20100208