Dependents:   SimpleLCDClock readCard2Twitter_http AnalogClock_StepperMotor_NTP ServoCamV1

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 enum TcpSocketErr
00031 {
00032   __TCPSOCKET_MIN = -0xFFFF,
00033   TCPSOCKET_SETUP, //NetTcpSocket not properly configured
00034   TCPSOCKET_TIMEOUT,
00035   TCPSOCKET_IF, //If has problems, does not exist or is not initialized
00036   TCPSOCKET_MEM, //Not enough mem
00037   TCPSOCKET_INUSE, //If/Port is in use
00038   TCPSOCKET_EMPTY, //Connections queue is empty
00039   TCPSOCKET_RST, // Connection was reset by remote host
00040 //...
00041   TCPSOCKET_OK = 0
00042 };
00043 
00044 enum TcpSocketEvent
00045 {
00046   TCPSOCKET_CONNECTED, //Connected to host, must call accept() if we were listening
00047   TCPSOCKET_ACCEPT,  //Connected to client
00048   TCPSOCKET_READABLE, //Data in buf
00049   TCPSOCKET_WRITEABLE, //Can write data to buf
00050   TCPSOCKET_CONTIMEOUT,
00051   TCPSOCKET_CONRST,
00052   TCPSOCKET_CONABRT,
00053   TCPSOCKET_ERROR,
00054   TCPSOCKET_DISCONNECTED
00055 };
00056 
00057 
00058 class TcpSocket
00059 {
00060 public:
00061   TcpSocket();
00062 protected:
00063   TcpSocket(NetTcpSocket* pNetTcpSocket);
00064 public:
00065   ~TcpSocket(); //close()
00066   
00067   TcpSocketErr bind(const Host& me);
00068   TcpSocketErr listen();
00069   TcpSocketErr connect(const Host& host);
00070   TcpSocketErr accept(Host* pClient, TcpSocket** ppNewTcpSocket);
00071   
00072   int /*if < 0 : TcpSocketErr*/ send(const char* buf, int len);
00073   int /*if < 0 : TcpSocketErr*/ recv(char* buf, int len);
00074 
00075   /* TODO NTH : printf / scanf helpers that call send/recv */
00076 
00077   TcpSocketErr close();
00078 
00079   class CDummy;
00080   //Callbacks
00081   void setOnEvent( void (*pMethod)(TcpSocketEvent) );
00082   template<class T> 
00083   //Linker bug : Must be defined here :(
00084   void setOnEvent( T* pItem, void (T::*pMethod)(TcpSocketEvent) )
00085   {
00086     m_pCbItem = (CDummy*) pItem;
00087     m_pCbMeth = (void (CDummy::*)(TcpSocketEvent)) pMethod;
00088   }
00089   
00090   void resetOnEvent(); //Disable callback
00091   
00092 protected:
00093   void onNetTcpSocketEvent(NetTcpSocketEvent e);
00094   TcpSocketErr checkInst();
00095 
00096 private:
00097   NetTcpSocket* m_pNetTcpSocket;
00098   
00099   CDummy* m_pCbItem;
00100   void (CDummy::*m_pCbMeth)(TcpSocketEvent);
00101   
00102   void (*m_pCb)(TcpSocketEvent);
00103   
00104 };
00105 
00106 #endif