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