This library is stripped down version of NetServices library. HTTP server and client function is NOT supported.

Dependents:   imu-daq-eth

Committer:
idinor
Date:
Wed Jul 20 11:45:39 2011 +0000
Revision:
0:dcf3c92487ca

        

Who changed what in which revision?

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