simple tester example >>> osc destination & receive
Fork of myOSC_test by
EthernetNetIf/LPC1768/if/net/netudpsocket.h@2:44f1e5803762, 2013-08-06 (annotated)
- Committer:
- sfjmt
- Date:
- Tue Aug 06 10:12:56 2013 +0000
- Revision:
- 2:44f1e5803762
- Parent:
- 0:f76708a93fb3
revision_01
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbedalvaro | 0:f76708a93fb3 | 1 | |
mbedalvaro | 0:f76708a93fb3 | 2 | /* |
mbedalvaro | 0:f76708a93fb3 | 3 | Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) |
mbedalvaro | 0:f76708a93fb3 | 4 | |
mbedalvaro | 0:f76708a93fb3 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy |
mbedalvaro | 0:f76708a93fb3 | 6 | of this software and associated documentation files (the "Software"), to deal |
mbedalvaro | 0:f76708a93fb3 | 7 | in the Software without restriction, including without limitation the rights |
mbedalvaro | 0:f76708a93fb3 | 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
mbedalvaro | 0:f76708a93fb3 | 9 | copies of the Software, and to permit persons to whom the Software is |
mbedalvaro | 0:f76708a93fb3 | 10 | furnished to do so, subject to the following conditions: |
mbedalvaro | 0:f76708a93fb3 | 11 | |
mbedalvaro | 0:f76708a93fb3 | 12 | The above copyright notice and this permission notice shall be included in |
mbedalvaro | 0:f76708a93fb3 | 13 | all copies or substantial portions of the Software. |
mbedalvaro | 0:f76708a93fb3 | 14 | |
mbedalvaro | 0:f76708a93fb3 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
mbedalvaro | 0:f76708a93fb3 | 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
mbedalvaro | 0:f76708a93fb3 | 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
mbedalvaro | 0:f76708a93fb3 | 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
mbedalvaro | 0:f76708a93fb3 | 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
mbedalvaro | 0:f76708a93fb3 | 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
mbedalvaro | 0:f76708a93fb3 | 21 | THE SOFTWARE. |
mbedalvaro | 0:f76708a93fb3 | 22 | */ |
mbedalvaro | 0:f76708a93fb3 | 23 | |
mbedalvaro | 0:f76708a93fb3 | 24 | #ifndef NETUDPSOCKET_H |
mbedalvaro | 0:f76708a93fb3 | 25 | #define NETUDPSOCKET_H |
mbedalvaro | 0:f76708a93fb3 | 26 | |
mbedalvaro | 0:f76708a93fb3 | 27 | #include "net.h" |
mbedalvaro | 0:f76708a93fb3 | 28 | #include "host.h" |
mbedalvaro | 0:f76708a93fb3 | 29 | |
mbedalvaro | 0:f76708a93fb3 | 30 | #include <queue> |
mbedalvaro | 0:f76708a93fb3 | 31 | using std::queue; |
mbedalvaro | 0:f76708a93fb3 | 32 | |
mbedalvaro | 0:f76708a93fb3 | 33 | //Implements a Berkeley-like socket if |
mbedalvaro | 0:f76708a93fb3 | 34 | //Can be interfaced either to lwip or a Telit module |
mbedalvaro | 0:f76708a93fb3 | 35 | |
mbedalvaro | 0:f76708a93fb3 | 36 | enum NetUdpSocketErr |
mbedalvaro | 0:f76708a93fb3 | 37 | { |
mbedalvaro | 0:f76708a93fb3 | 38 | __NETUDPSOCKET_MIN = -0xFFFF, |
mbedalvaro | 0:f76708a93fb3 | 39 | NETUDPSOCKET_SETUP, //NetUdpSocket not properly configured |
mbedalvaro | 0:f76708a93fb3 | 40 | NETUDPSOCKET_IF, //If has problems |
mbedalvaro | 0:f76708a93fb3 | 41 | NETUDPSOCKET_MEM, //Not enough mem |
mbedalvaro | 0:f76708a93fb3 | 42 | NETUDPSOCKET_INUSE, //If/Port is in use |
mbedalvaro | 0:f76708a93fb3 | 43 | //... |
mbedalvaro | 0:f76708a93fb3 | 44 | NETUDPSOCKET_OK = 0 |
mbedalvaro | 0:f76708a93fb3 | 45 | }; |
mbedalvaro | 0:f76708a93fb3 | 46 | |
mbedalvaro | 0:f76708a93fb3 | 47 | enum NetUdpSocketEvent //Only one lonely event here... but who knows, maybe some day there'll be another one! |
mbedalvaro | 0:f76708a93fb3 | 48 | { |
mbedalvaro | 0:f76708a93fb3 | 49 | NETUDPSOCKET_READABLE, //Data in buf |
mbedalvaro | 0:f76708a93fb3 | 50 | }; |
mbedalvaro | 0:f76708a93fb3 | 51 | |
mbedalvaro | 0:f76708a93fb3 | 52 | |
mbedalvaro | 0:f76708a93fb3 | 53 | class NetUdpSocket |
mbedalvaro | 0:f76708a93fb3 | 54 | { |
mbedalvaro | 0:f76708a93fb3 | 55 | public: |
mbedalvaro | 0:f76708a93fb3 | 56 | NetUdpSocket(); |
mbedalvaro | 0:f76708a93fb3 | 57 | virtual ~NetUdpSocket(); //close() |
mbedalvaro | 0:f76708a93fb3 | 58 | |
mbedalvaro | 0:f76708a93fb3 | 59 | virtual NetUdpSocketErr bind(const Host& me) = 0; |
mbedalvaro | 0:f76708a93fb3 | 60 | |
mbedalvaro | 0:f76708a93fb3 | 61 | virtual int /*if < 0 : NetUdpSocketErr*/ sendto(const char* buf, int len, Host* pHost) = 0; |
mbedalvaro | 0:f76708a93fb3 | 62 | virtual int /*if < 0 : NetUdpSocketErr*/ recvfrom(char* buf, int len, Host* pHost) = 0; |
mbedalvaro | 0:f76708a93fb3 | 63 | |
mbedalvaro | 0:f76708a93fb3 | 64 | /* TODO NTH : printf / scanf helpers that call send/recv */ |
mbedalvaro | 0:f76708a93fb3 | 65 | |
mbedalvaro | 0:f76708a93fb3 | 66 | virtual NetUdpSocketErr close() = 0; |
mbedalvaro | 0:f76708a93fb3 | 67 | |
mbedalvaro | 0:f76708a93fb3 | 68 | virtual NetUdpSocketErr poll() = 0; |
mbedalvaro | 0:f76708a93fb3 | 69 | |
mbedalvaro | 0:f76708a93fb3 | 70 | class CDummy; |
mbedalvaro | 0:f76708a93fb3 | 71 | //Callbacks |
mbedalvaro | 0:f76708a93fb3 | 72 | template<class T> |
mbedalvaro | 0:f76708a93fb3 | 73 | //Linker bug : Must be defined here :( |
mbedalvaro | 0:f76708a93fb3 | 74 | void setOnEvent( T* pItem, void (T::*pMethod)(NetUdpSocketEvent) ) |
mbedalvaro | 0:f76708a93fb3 | 75 | { |
mbedalvaro | 0:f76708a93fb3 | 76 | m_pCbItem = (CDummy*) pItem; |
mbedalvaro | 0:f76708a93fb3 | 77 | m_pCbMeth = (void (CDummy::*)(NetUdpSocketEvent)) pMethod; |
mbedalvaro | 0:f76708a93fb3 | 78 | } |
mbedalvaro | 0:f76708a93fb3 | 79 | |
mbedalvaro | 0:f76708a93fb3 | 80 | void resetOnEvent(); //Disable callback |
mbedalvaro | 0:f76708a93fb3 | 81 | |
mbedalvaro | 0:f76708a93fb3 | 82 | protected: |
mbedalvaro | 0:f76708a93fb3 | 83 | void queueEvent(NetUdpSocketEvent e); |
mbedalvaro | 0:f76708a93fb3 | 84 | void discardEvents(); |
mbedalvaro | 0:f76708a93fb3 | 85 | void flushEvents(); //to be called during polling |
mbedalvaro | 0:f76708a93fb3 | 86 | |
mbedalvaro | 0:f76708a93fb3 | 87 | Host m_host; |
mbedalvaro | 0:f76708a93fb3 | 88 | Host m_client; |
mbedalvaro | 0:f76708a93fb3 | 89 | |
mbedalvaro | 0:f76708a93fb3 | 90 | friend class Net; |
mbedalvaro | 0:f76708a93fb3 | 91 | int m_refs; |
mbedalvaro | 0:f76708a93fb3 | 92 | |
mbedalvaro | 0:f76708a93fb3 | 93 | bool m_closed; |
mbedalvaro | 0:f76708a93fb3 | 94 | bool m_removed; |
mbedalvaro | 0:f76708a93fb3 | 95 | |
mbedalvaro | 0:f76708a93fb3 | 96 | private: |
mbedalvaro | 0:f76708a93fb3 | 97 | //We do not want to execute user code in interrupt routines, so we queue events until the server is polled |
mbedalvaro | 0:f76708a93fb3 | 98 | //If we port this to a multithreaded OS, we could avoid this (however some functions here are not thread-safe, so beware ;) ) |
mbedalvaro | 0:f76708a93fb3 | 99 | void onEvent(NetUdpSocketEvent e); //To be called on poll |
mbedalvaro | 0:f76708a93fb3 | 100 | CDummy* m_pCbItem; |
mbedalvaro | 0:f76708a93fb3 | 101 | void (CDummy::*m_pCbMeth)(NetUdpSocketEvent); |
mbedalvaro | 0:f76708a93fb3 | 102 | queue<NetUdpSocketEvent> m_events; |
mbedalvaro | 0:f76708a93fb3 | 103 | |
mbedalvaro | 0:f76708a93fb3 | 104 | }; |
mbedalvaro | 0:f76708a93fb3 | 105 | |
mbedalvaro | 0:f76708a93fb3 | 106 | #endif |