Dependents:   SNMPAgent HTTPServer think_speak_a cyassl-client ... more

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