This library is stripped down version of NetServices library. HTTP server and client function is NOT supported.

Dependents:   imu-daq-eth

Committer:
idinor
Date:
Wed Jul 20 11:45:39 2011 +0000
Revision:
0:dcf3c92487ca

        

Who changed what in which revision?

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