Fork of NetServicesMin with some warnings removed

Dependencies:   lwip-sys lwip

Fork of NetServicesMin by Hendrik Lipka

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers UDPSocket.h Source File

UDPSocket.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 UDP Socket header file
00026 */
00027 
00028 #ifndef UDPSOCKET_H
00029 #define UDPSOCKET_H
00030 
00031 #include "core/net.h"
00032 #include "core/host.h"
00033 //Essentially it is a safe interface to NetUdpSocket
00034 #include "if/net/netudpsocket.h"
00035 
00036 ///UDP Socket error codes
00037 enum UDPSocketErr
00038 {
00039   __UDPSOCKET_MIN = -0xFFFF,
00040   UDPSOCKET_SETUP, ///<UDPSocket not properly configured
00041   UDPSOCKET_IF, ///<Interface has problems, does not exist or is not initialized
00042   UDPSOCKET_MEM, ///<Not enough mem
00043   UDPSOCKET_INUSE, ///<Interface / Port is in use
00044 //...
00045   UDPSOCKET_OK = 0 ///<Success
00046 };
00047 
00048 ///UDP Socket Event(s)
00049 enum UDPSocketEvent //Only one event here for now, but keeps that model in case we need to implement some others
00050 {
00051   UDPSOCKET_READABLE, ///<Data in buf
00052 };
00053 
00054 //class NetUdpSocket;
00055 //enum NetUdpSocketEvent;
00056 
00057 ///This is a simple UDP Socket class
00058 /**
00059   This class exposes an API to deal with UDP Sockets
00060 */
00061 class UDPSocket
00062 {
00063 public:
00064   ///Creates a new socket
00065   UDPSocket();
00066   
00067   ///Closes and destroys socket
00068   ~UDPSocket(); //close()
00069   
00070   ///Binds the socket to local host or a multicast address
00071   UDPSocketErr bind(const Host& me);
00072   
00073   ///Sends data
00074   /*
00075   @param pHost : host to send data to
00076   @return a negative error code or the number of bytes transmitted
00077   */
00078   int /*if < 0 : UDPSocketErr*/ sendto(const char* buf, int len, Host* pHost);
00079   
00080   ///Receives data
00081   /*
00082   @param pHost : host from which this piece of data comes from
00083   @return a negative error code or the number of bytes received
00084   */
00085   int /*if < 0 : UDPSocketErr*/ recvfrom(char* buf, int len, Host* pHost);
00086 
00087   /* TODO NTH : printf / scanf helpers that call send/recv */
00088 
00089   ///Closes socket
00090   UDPSocketErr close();
00091 
00092   //Callbacks
00093   ///Setups callback
00094   /**
00095   @param pMethod : callback function
00096   */  
00097   void setOnEvent( void (*pMethod)(UDPSocketEvent) );
00098   
00099   class CDummy;
00100   ///Setups callback
00101   /**
00102   @param pItem : instance of class on which to execute the callback method
00103   @param pMethod : callback method
00104   */
00105   template<class T> 
00106   void setOnEvent( T* pItem, void (T::*pMethod)(UDPSocketEvent) )
00107   {
00108     m_pCbItem = (CDummy*) pItem;
00109     m_pCbMeth = (void (CDummy::*)(UDPSocketEvent)) pMethod;
00110   }
00111   
00112   ///Disables callback
00113   void resetOnEvent();
00114 
00115 protected:
00116   void onNetUdpSocketEvent(NetUdpSocketEvent e);
00117   UDPSocketErr checkInst();
00118 
00119 private:
00120   NetUdpSocket* m_pNetUdpSocket;
00121   
00122   CDummy* m_pCbItem;
00123   void (CDummy::*m_pCbMeth)(UDPSocketEvent);
00124   
00125   void (*m_pCb)(UDPSocketEvent);
00126 
00127 };
00128 
00129 #endif