Dependencies:   mbed

Committer:
gbeardall
Date:
Mon Oct 17 10:39:17 2011 +0000
Revision:
0:715023d993d6

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gbeardall 0:715023d993d6 1
gbeardall 0:715023d993d6 2 /*
gbeardall 0:715023d993d6 3 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
gbeardall 0:715023d993d6 4
gbeardall 0:715023d993d6 5 Permission is hereby granted, free of charge, to any person obtaining a copy
gbeardall 0:715023d993d6 6 of this software and associated documentation files (the "Software"), to deal
gbeardall 0:715023d993d6 7 in the Software without restriction, including without limitation the rights
gbeardall 0:715023d993d6 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
gbeardall 0:715023d993d6 9 copies of the Software, and to permit persons to whom the Software is
gbeardall 0:715023d993d6 10 furnished to do so, subject to the following conditions:
gbeardall 0:715023d993d6 11
gbeardall 0:715023d993d6 12 The above copyright notice and this permission notice shall be included in
gbeardall 0:715023d993d6 13 all copies or substantial portions of the Software.
gbeardall 0:715023d993d6 14
gbeardall 0:715023d993d6 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
gbeardall 0:715023d993d6 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
gbeardall 0:715023d993d6 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
gbeardall 0:715023d993d6 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
gbeardall 0:715023d993d6 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
gbeardall 0:715023d993d6 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
gbeardall 0:715023d993d6 21 THE SOFTWARE.
gbeardall 0:715023d993d6 22 */
gbeardall 0:715023d993d6 23
gbeardall 0:715023d993d6 24 /** \file
gbeardall 0:715023d993d6 25 UDP Socket header file
gbeardall 0:715023d993d6 26 */
gbeardall 0:715023d993d6 27
gbeardall 0:715023d993d6 28 #ifndef UDPSOCKET_H
gbeardall 0:715023d993d6 29 #define UDPSOCKET_H
gbeardall 0:715023d993d6 30
gbeardall 0:715023d993d6 31 #include "core/net.h"
gbeardall 0:715023d993d6 32 #include "core/host.h"
gbeardall 0:715023d993d6 33 //Essentially it is a safe interface to NetUdpSocket
gbeardall 0:715023d993d6 34
gbeardall 0:715023d993d6 35 ///UDP Socket error codes
gbeardall 0:715023d993d6 36 enum UDPSocketErr
gbeardall 0:715023d993d6 37 {
gbeardall 0:715023d993d6 38 __UDPSOCKET_MIN = -0xFFFF,
gbeardall 0:715023d993d6 39 UDPSOCKET_SETUP, ///<UDPSocket not properly configured
gbeardall 0:715023d993d6 40 UDPSOCKET_IF, ///<Interface has problems, does not exist or is not initialized
gbeardall 0:715023d993d6 41 UDPSOCKET_MEM, ///<Not enough mem
gbeardall 0:715023d993d6 42 UDPSOCKET_INUSE, ///<Interface / Port is in use
gbeardall 0:715023d993d6 43 //...
gbeardall 0:715023d993d6 44 UDPSOCKET_OK = 0 ///<Success
gbeardall 0:715023d993d6 45 };
gbeardall 0:715023d993d6 46
gbeardall 0:715023d993d6 47 ///UDP Socket Event(s)
gbeardall 0:715023d993d6 48 enum UDPSocketEvent //Only one event here for now, but keeps that model in case we need to implement some others
gbeardall 0:715023d993d6 49 {
gbeardall 0:715023d993d6 50 UDPSOCKET_READABLE, ///<Data in buf
gbeardall 0:715023d993d6 51 };
gbeardall 0:715023d993d6 52
gbeardall 0:715023d993d6 53 class NetUdpSocket;
gbeardall 0:715023d993d6 54 enum NetUdpSocketEvent;
gbeardall 0:715023d993d6 55
gbeardall 0:715023d993d6 56 ///This is a simple UDP Socket class
gbeardall 0:715023d993d6 57 /**
gbeardall 0:715023d993d6 58 This class exposes an API to deal with UDP Sockets
gbeardall 0:715023d993d6 59 */
gbeardall 0:715023d993d6 60 class UDPSocket
gbeardall 0:715023d993d6 61 {
gbeardall 0:715023d993d6 62 public:
gbeardall 0:715023d993d6 63 ///Creates a new socket
gbeardall 0:715023d993d6 64 UDPSocket();
gbeardall 0:715023d993d6 65
gbeardall 0:715023d993d6 66 ///Closes and destroys socket
gbeardall 0:715023d993d6 67 ~UDPSocket(); //close()
gbeardall 0:715023d993d6 68
gbeardall 0:715023d993d6 69 ///Binds the socket to local host or a multicast address
gbeardall 0:715023d993d6 70 UDPSocketErr bind(const Host& me);
gbeardall 0:715023d993d6 71
gbeardall 0:715023d993d6 72 ///Sends data
gbeardall 0:715023d993d6 73 /*
gbeardall 0:715023d993d6 74 @param pHost : host to send data to
gbeardall 0:715023d993d6 75 @return a negative error code or the number of bytes transmitted
gbeardall 0:715023d993d6 76 */
gbeardall 0:715023d993d6 77 int /*if < 0 : UDPSocketErr*/ sendto(const char* buf, int len, Host* pHost);
gbeardall 0:715023d993d6 78
gbeardall 0:715023d993d6 79 ///Receives data
gbeardall 0:715023d993d6 80 /*
gbeardall 0:715023d993d6 81 @param pHost : host from which this piece of data comes from
gbeardall 0:715023d993d6 82 @return a negative error code or the number of bytes received
gbeardall 0:715023d993d6 83 */
gbeardall 0:715023d993d6 84 int /*if < 0 : UDPSocketErr*/ recvfrom(char* buf, int len, Host* pHost);
gbeardall 0:715023d993d6 85
gbeardall 0:715023d993d6 86 /* TODO NTH : printf / scanf helpers that call send/recv */
gbeardall 0:715023d993d6 87
gbeardall 0:715023d993d6 88 ///Closes socket
gbeardall 0:715023d993d6 89 UDPSocketErr close();
gbeardall 0:715023d993d6 90
gbeardall 0:715023d993d6 91 //Callbacks
gbeardall 0:715023d993d6 92 ///Setups callback
gbeardall 0:715023d993d6 93 /**
gbeardall 0:715023d993d6 94 @param pMethod : callback function
gbeardall 0:715023d993d6 95 */
gbeardall 0:715023d993d6 96 void setOnEvent( void (*pMethod)(UDPSocketEvent) );
gbeardall 0:715023d993d6 97
gbeardall 0:715023d993d6 98 class CDummy;
gbeardall 0:715023d993d6 99 ///Setups callback
gbeardall 0:715023d993d6 100 /**
gbeardall 0:715023d993d6 101 @param pItem : instance of class on which to execute the callback method
gbeardall 0:715023d993d6 102 @param pMethod : callback method
gbeardall 0:715023d993d6 103 */
gbeardall 0:715023d993d6 104 template<class T>
gbeardall 0:715023d993d6 105 void setOnEvent( T* pItem, void (T::*pMethod)(UDPSocketEvent) )
gbeardall 0:715023d993d6 106 {
gbeardall 0:715023d993d6 107 m_pCbItem = (CDummy*) pItem;
gbeardall 0:715023d993d6 108 m_pCbMeth = (void (CDummy::*)(UDPSocketEvent)) pMethod;
gbeardall 0:715023d993d6 109 }
gbeardall 0:715023d993d6 110
gbeardall 0:715023d993d6 111 ///Disables callback
gbeardall 0:715023d993d6 112 void resetOnEvent();
gbeardall 0:715023d993d6 113
gbeardall 0:715023d993d6 114 protected:
gbeardall 0:715023d993d6 115 void onNetUdpSocketEvent(NetUdpSocketEvent e);
gbeardall 0:715023d993d6 116 UDPSocketErr checkInst();
gbeardall 0:715023d993d6 117
gbeardall 0:715023d993d6 118 private:
gbeardall 0:715023d993d6 119 NetUdpSocket* m_pNetUdpSocket;
gbeardall 0:715023d993d6 120
gbeardall 0:715023d993d6 121 CDummy* m_pCbItem;
gbeardall 0:715023d993d6 122 void (CDummy::*m_pCbMeth)(UDPSocketEvent);
gbeardall 0:715023d993d6 123
gbeardall 0:715023d993d6 124 void (*m_pCb)(UDPSocketEvent);
gbeardall 0:715023d993d6 125
gbeardall 0:715023d993d6 126 };
gbeardall 0:715023d993d6 127
gbeardall 0:715023d993d6 128 #endif