Donatien Garnier / EthernetNetIfBeta
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers UDPSocket.h Source File

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