Donatien Garnier / EthernetNetIfBeta
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers TCPSocket.h Source File

TCPSocket.h

00001 
00002 /*
00003 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
00004  
00005 Permission is hereby granted, free of charge, to any person obtaining a copy
00006 of this software and associated documentation files (the "Software"), to deal
00007 in the Software without restriction, including without limitation the rights
00008 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00009 copies of the Software, and to permit persons to whom the Software is
00010 furnished to do so, subject to the following conditions:
00011  
00012 The above copyright notice and this permission notice shall be included in
00013 all copies or substantial portions of the Software.
00014  
00015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00016 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00017 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00018 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00019 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00020 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00021 THE SOFTWARE.
00022 */
00023 
00024 #ifndef TCPSOCKET_H
00025 #define TCPSOCKET_H
00026 
00027 #include "if/net/net.h"
00028 //Essentially it is a safe interface to NetTcpSocket
00029 
00030 //!TCP Socket Errors
00031 enum TCPSocketErr
00032 {
00033   __TCPSOCKET_MIN = -0xFFFF,
00034   TCPSOCKET_SETUP, //!TCPSocket not properly configured
00035   TCPSOCKET_TIMEOUT,
00036   TCPSOCKET_IF, //!Interface has problems, does not exist or is not initialized
00037   TCPSOCKET_MEM, //!Not enough mem
00038   TCPSOCKET_INUSE, //!Interface / Port is in use
00039   TCPSOCKET_EMPTY, //!Connections queue is empty
00040   TCPSOCKET_RST, //!Connection was reset by remote host
00041 //...
00042   TCPSOCKET_OK = 0
00043 };
00044 
00045 //!TCP Socket Events
00046 enum TCPSocketEvent
00047 {
00048   TCPSOCKET_CONNECTED, //!Connected to host, must call accept() if we were listening
00049   TCPSOCKET_ACCEPT,  //!Connected to client
00050   TCPSOCKET_READABLE, //!Data in buf
00051   TCPSOCKET_WRITEABLE, //!Can write data to buf
00052   TCPSOCKET_CONTIMEOUT, //!Connection timed out
00053   TCPSOCKET_CONRST, //!Connection was reset by remote host
00054   TCPSOCKET_CONABRT, //!Connection was aborted
00055   TCPSOCKET_ERROR, //!Unknown error
00056   TCPSOCKET_DISCONNECTED //!Disconnected
00057 };
00058 
00059 //!This is a simple TCP Socket class
00060 /*!
00061   This class exposes an API to deal with TCP Sockets
00062 */
00063 class TCPSocket
00064 {
00065 public:
00066   //!Creates a new socket
00067   TCPSocket();
00068 protected:
00069   TCPSocket(NetTcpSocket* pNetTcpSocket);
00070 public:
00071   //!Closes if needed and destroys the socket
00072   ~TCPSocket(); //close()
00073   
00074   //!Binds the socket to (local) host
00075   TCPSocketErr bind(const Host& me);
00076   
00077   //!Starts listening
00078   TCPSocketErr listen();
00079   
00080   //!Connects socket to host
00081   TCPSocketErr connect(const Host& host);
00082   
00083   //!Accepts connection from client and gets connected socket
00084   TCPSocketErr accept(Host* pClient, TCPSocket** ppNewTcpSocket);
00085   
00086   //!Sends data
00087   /*
00088   \return a negative error code or the number of bytes transmitted
00089   */
00090   int /*if < 0 : TCPSocketErr*/ send(const char* buf, int len);
00091   
00092   //!Receives data
00093   /*
00094   \return a negative error code or the number of bytes received
00095   */
00096   int /*if < 0 : TCPSocketErr*/ recv(char* buf, int len);
00097 
00098   /* TODO NTH : printf / scanf helpers that call send/recv */
00099 
00100   //!Closes socket
00101   TCPSocketErr close();
00102 
00103   //Callbacks
00104   //!Setups callback
00105   /*!
00106   \param pMethod : callback function
00107   */
00108   void setOnEvent( void (*pMethod)(TCPSocketEvent) );
00109   
00110   //!Setups callback
00111   /*!
00112   \param pItem : instance of class on which to execute the callback method
00113   \param pMethod : callback method
00114   */
00115   class CDummy;
00116   template<class T> 
00117   void setOnEvent( T* pItem, void (T::*pMethod)(TCPSocketEvent) )
00118   {
00119     m_pCbItem = (CDummy*) pItem;
00120     m_pCbMeth = (void (CDummy::*)(TCPSocketEvent)) pMethod;
00121   }
00122   
00123   //!Disables callback
00124   void resetOnEvent(); 
00125   
00126 protected:
00127   void onNetTcpSocketEvent(NetTcpSocketEvent e);
00128   TCPSocketErr checkInst();
00129 
00130 private:
00131   NetTcpSocket* m_pNetTcpSocket;
00132   
00133   CDummy* m_pCbItem;
00134   void (CDummy::*m_pCbMeth)(TCPSocketEvent);
00135   
00136   void (*m_pCb)(TCPSocketEvent);
00137   
00138 };
00139 
00140 #endif