HTTPClient using static IP

Dependencies:   mbed

Committer:
mr_q
Date:
Mon May 30 11:53:37 2011 +0000
Revision:
0:d8f2f7d5f31b
v0.01 Draft

Who changed what in which revision?

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