Dependents:   SimpleLCDClock readCard2Twitter_http AnalogClock_StepperMotor_NTP ServoCamV1

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 enum UdpSocketErr
00031 {
00032   __UDPSOCKET_MIN = -0xFFFF,
00033   UDPSOCKET_SETUP, //NetUdpSocket not properly configured
00034   UDPSOCKET_IF, //If has problems, does not exist or is not initialized
00035   UDPSOCKET_MEM, //Not enough mem
00036   UDPSOCKET_INUSE, //If/Port is in use
00037 //...
00038   UDPSOCKET_OK = 0
00039 };
00040 
00041 enum UdpSocketEvent //Only one lonely event here... but who knows, maybe some day there'll be another one!
00042 {
00043   UDPSOCKET_READABLE, //Data in buf
00044 };
00045 
00046 
00047 class UdpSocket
00048 {
00049 public:
00050   UdpSocket();
00051   ~UdpSocket(); //close()
00052   
00053   UdpSocketErr bind(const Host& me);
00054   
00055   int /*if < 0 : UdpSocketErr*/ sendto(const char* buf, int len, Host* pHost);
00056   int /*if < 0 : UdpSocketErr*/ recvfrom(char* buf, int len, Host* pHost);
00057 
00058   /* TODO NTH : printf / scanf helpers that call send/recv */
00059 
00060   UdpSocketErr close();
00061 
00062   class CDummy;
00063   //Callbacks
00064   void setOnEvent( void (*pMethod)(UdpSocketEvent) );
00065   template<class T> 
00066   //Linker bug : Must be defined here :(
00067   void setOnEvent( T* pItem, void (T::*pMethod)(UdpSocketEvent) )
00068   {
00069     m_pCbItem = (CDummy*) pItem;
00070     m_pCbMeth = (void (CDummy::*)(UdpSocketEvent)) pMethod;
00071   }
00072   
00073   void resetOnEvent(); //Disable callback
00074 
00075 protected:
00076   void onNetUdpSocketEvent(NetUdpSocketEvent e);
00077   UdpSocketErr checkInst();
00078 
00079 private:
00080   NetUdpSocket* m_pNetUdpSocket;
00081   
00082   CDummy* m_pCbItem;
00083   void (CDummy::*m_pCbMeth)(UdpSocketEvent);
00084   
00085   void (*m_pCb)(UdpSocketEvent);
00086 
00087 };
00088 
00089 #endif