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