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

Committer:
mamezu
Date:
Thu Dec 09 01:33:56 2010 +0000
Revision:
0:0f6c82fcde82

        

Who changed what in which revision?

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