A modified version of http://mbed.org/users/segundo/libraries/NetServices/ljhqix to add HTTP proxy feature (based on http://mbed.org/users/igorsk/programs/NetServicesSource/ltjpag)

Dependents:   SenseClient

Committer:
mimil
Date:
Tue Sep 06 13:26:45 2011 +0000
Revision:
0:308f83189a3f

        

Who changed what in which revision?

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