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