ID the MBED processor on the network if you have more than one. View MBED name on DHCP of your Router - Example \"MBED PE\" Change your ID in Core / host.h

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers TCPSocket.h Source File

TCPSocket.h

Go to the documentation of this file.
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 /** \file
00025 TCP Socket header file
00026 */
00027 
00028 #ifndef TCPSOCKET_H
00029 #define TCPSOCKET_H
00030 
00031 #include "core/net.h"
00032 #include "core/host.h"
00033 //Essentially it is a safe interface to NetTcpSocket
00034 
00035 ///TCP Socket error codes
00036 enum TCPSocketErr
00037 {
00038   __TCPSOCKET_MIN = -0xFFFF,
00039   TCPSOCKET_SETUP, ///<TCPSocket not properly configured
00040   TCPSOCKET_TIMEOUT, ///<Connection timed out
00041   TCPSOCKET_IF, ///<Interface has problems, does not exist or is not initialized
00042   TCPSOCKET_MEM, ///<Not enough mem
00043   TCPSOCKET_INUSE, ///<Interface / Port is in use
00044   TCPSOCKET_EMPTY, ///<Connections queue is empty
00045   TCPSOCKET_RST, ///<Connection was reset by remote host
00046 //...
00047   TCPSOCKET_OK = 0 ///<Success
00048 };
00049 
00050 ///TCP Socket Events
00051 enum TCPSocketEvent
00052 {
00053   TCPSOCKET_CONNECTED, ///<Connected to host
00054   TCPSOCKET_ACCEPT,  ///<Client is connected, must call accept() to get a new Socket
00055   TCPSOCKET_READABLE, ///<Data in buf
00056   TCPSOCKET_WRITEABLE, ///<Can write data to buf
00057   TCPSOCKET_CONTIMEOUT, ///<Connection timed out
00058   TCPSOCKET_CONRST, ///<Connection was reset by remote host
00059   TCPSOCKET_CONABRT, ///<Connection was aborted
00060   TCPSOCKET_ERROR, ///<Unknown error
00061   TCPSOCKET_DISCONNECTED ///<Disconnected
00062 };
00063 
00064 class NetTcpSocket;
00065 enum NetTcpSocketEvent;
00066 
00067 ///This is a simple TCP Socket class
00068 /**
00069   This class exposes an API to deal with TCP Sockets
00070 */
00071 class TCPSocket
00072 {
00073 public:
00074   ///Creates a new socket
00075   TCPSocket();
00076 protected:
00077   TCPSocket(NetTcpSocket* pNetTcpSocket);
00078 public:
00079   ///Closes if needed and destroys the socket
00080   ~TCPSocket(); //close()
00081   
00082   ///Binds the socket to (local) host
00083   TCPSocketErr bind(const Host& me);
00084   
00085   ///Starts listening
00086   TCPSocketErr listen();
00087   
00088   ///Connects socket to host
00089   TCPSocketErr connect(const Host& host);
00090   
00091   ///Accepts connection from client and gets connected socket
00092   TCPSocketErr accept(Host* pClient, TCPSocket** ppNewTcpSocket);
00093   
00094   ///Sends data
00095   /*
00096   @return a negative error code or the number of bytes transmitted
00097   */
00098   int /*if < 0 : TCPSocketErr*/ send(const char* buf, int len);
00099   
00100   ///Receives data
00101   /*
00102   @return a negative error code or the number of bytes received
00103   */
00104   int /*if < 0 : TCPSocketErr*/ recv(char* buf, int len);
00105 
00106   /* TODO NTH : printf / scanf helpers that call send/recv */
00107 
00108   ///Closes socket
00109   TCPSocketErr close();
00110 
00111   //Callbacks
00112   ///Setups callback
00113   /**
00114   @param pMethod : callback function
00115   */
00116   void setOnEvent( void (*pMethod)(TCPSocketEvent) );
00117   
00118   class CDummy;
00119   ///Setups callback
00120   /**
00121   @param pItem : instance of class on which to execute the callback method
00122   @param pMethod : callback method
00123   */
00124   template<class T> 
00125   void setOnEvent( T* pItem, void (T::*pMethod)(TCPSocketEvent) )
00126   {
00127     m_pCbItem = (CDummy*) pItem;
00128     m_pCbMeth = (void (CDummy::*)(TCPSocketEvent)) pMethod;
00129   }
00130   
00131   ///Disables callback
00132   void resetOnEvent(); 
00133   
00134 protected:
00135   void onNetTcpSocketEvent(NetTcpSocketEvent e);
00136   TCPSocketErr checkInst();
00137 
00138 private:
00139   NetTcpSocket* m_pNetTcpSocket;
00140   
00141   CDummy* m_pCbItem;
00142   void (CDummy::*m_pCbMeth)(TCPSocketEvent);
00143   
00144   void (*m_pCb)(TCPSocketEvent);
00145   
00146 };
00147 
00148 #endif