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