Dependents:   SNMPAgent HTTPServer think_speak_a cyassl-client ... more

Committer:
mamezu
Date:
Thu Dec 09 01:33:56 2010 +0000
Revision:
0:0f6c82fcde82

        

Who changed what in which revision?

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