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)
if/net/nettcpsocket.h@0:308f83189a3f, 2011-09-06 (annotated)
- Committer:
- mimil
- Date:
- Tue Sep 06 13:26:45 2011 +0000
- Revision:
- 0:308f83189a3f
Who changed what in which revision?
User | Revision | Line number | New 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 NETTCPSOCKET_H |
mimil | 0:308f83189a3f | 25 | #define NETTCPSOCKET_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 NetTcpSocketErr |
mimil | 0:308f83189a3f | 37 | { |
mimil | 0:308f83189a3f | 38 | __NETTCPSOCKET_MIN = -0xFFFF, |
mimil | 0:308f83189a3f | 39 | NETTCPSOCKET_SETUP, //NetTcpSocket not properly configured |
mimil | 0:308f83189a3f | 40 | NETTCPSOCKET_TIMEOUT, |
mimil | 0:308f83189a3f | 41 | NETTCPSOCKET_IF, //If has problems |
mimil | 0:308f83189a3f | 42 | NETTCPSOCKET_MEM, //Not enough mem |
mimil | 0:308f83189a3f | 43 | NETTCPSOCKET_INUSE, //If/Port is in use |
mimil | 0:308f83189a3f | 44 | NETTCPSOCKET_EMPTY, //Connections queue is empty |
mimil | 0:308f83189a3f | 45 | NETTCPSOCKET_RST, // Connection was reset by remote host |
mimil | 0:308f83189a3f | 46 | //... |
mimil | 0:308f83189a3f | 47 | NETTCPSOCKET_OK = 0 |
mimil | 0:308f83189a3f | 48 | }; |
mimil | 0:308f83189a3f | 49 | |
mimil | 0:308f83189a3f | 50 | enum NetTcpSocketEvent |
mimil | 0:308f83189a3f | 51 | { |
mimil | 0:308f83189a3f | 52 | NETTCPSOCKET_CONNECTED, //Connected to host, must call accept() if we were listening |
mimil | 0:308f83189a3f | 53 | NETTCPSOCKET_ACCEPT, //Connected to client |
mimil | 0:308f83189a3f | 54 | NETTCPSOCKET_READABLE, //Data in buf |
mimil | 0:308f83189a3f | 55 | NETTCPSOCKET_WRITEABLE, //Can write data to buf |
mimil | 0:308f83189a3f | 56 | NETTCPSOCKET_CONTIMEOUT, |
mimil | 0:308f83189a3f | 57 | NETTCPSOCKET_CONRST, |
mimil | 0:308f83189a3f | 58 | NETTCPSOCKET_CONABRT, |
mimil | 0:308f83189a3f | 59 | NETTCPSOCKET_ERROR, |
mimil | 0:308f83189a3f | 60 | NETTCPSOCKET_DISCONNECTED |
mimil | 0:308f83189a3f | 61 | }; |
mimil | 0:308f83189a3f | 62 | |
mimil | 0:308f83189a3f | 63 | |
mimil | 0:308f83189a3f | 64 | class NetTcpSocket |
mimil | 0:308f83189a3f | 65 | { |
mimil | 0:308f83189a3f | 66 | public: |
mimil | 0:308f83189a3f | 67 | NetTcpSocket(); |
mimil | 0:308f83189a3f | 68 | virtual ~NetTcpSocket(); //close() |
mimil | 0:308f83189a3f | 69 | |
mimil | 0:308f83189a3f | 70 | virtual NetTcpSocketErr bind(const Host& me) = 0; |
mimil | 0:308f83189a3f | 71 | virtual NetTcpSocketErr listen() = 0; |
mimil | 0:308f83189a3f | 72 | virtual NetTcpSocketErr connect(const Host& host) = 0; |
mimil | 0:308f83189a3f | 73 | virtual NetTcpSocketErr accept(Host* pClient, NetTcpSocket** ppNewNetTcpSocket) = 0; |
mimil | 0:308f83189a3f | 74 | |
mimil | 0:308f83189a3f | 75 | virtual int /*if < 0 : NetTcpSocketErr*/ send(const char* buf, int len) = 0; |
mimil | 0:308f83189a3f | 76 | virtual int /*if < 0 : NetTcpSocketErr*/ recv(char* buf, int len) = 0; |
mimil | 0:308f83189a3f | 77 | |
mimil | 0:308f83189a3f | 78 | virtual NetTcpSocketErr close() = 0; |
mimil | 0:308f83189a3f | 79 | |
mimil | 0:308f83189a3f | 80 | virtual NetTcpSocketErr poll() = 0; |
mimil | 0:308f83189a3f | 81 | |
mimil | 0:308f83189a3f | 82 | class CDummy; |
mimil | 0:308f83189a3f | 83 | //Callbacks |
mimil | 0:308f83189a3f | 84 | template<class T> |
mimil | 0:308f83189a3f | 85 | //Linker bug : Must be defined here :( |
mimil | 0:308f83189a3f | 86 | void setOnEvent( T* pItem, void (T::*pMethod)(NetTcpSocketEvent) ) |
mimil | 0:308f83189a3f | 87 | { |
mimil | 0:308f83189a3f | 88 | m_pCbItem = (CDummy*) pItem; |
mimil | 0:308f83189a3f | 89 | m_pCbMeth = (void (CDummy::*)(NetTcpSocketEvent)) pMethod; |
mimil | 0:308f83189a3f | 90 | } |
mimil | 0:308f83189a3f | 91 | |
mimil | 0:308f83189a3f | 92 | void resetOnEvent(); //Disable callback |
mimil | 0:308f83189a3f | 93 | |
mimil | 0:308f83189a3f | 94 | protected: |
mimil | 0:308f83189a3f | 95 | void queueEvent(NetTcpSocketEvent e); |
mimil | 0:308f83189a3f | 96 | void discardEvents(); |
mimil | 0:308f83189a3f | 97 | void flushEvents(); //to be called during polling |
mimil | 0:308f83189a3f | 98 | |
mimil | 0:308f83189a3f | 99 | Host m_host; |
mimil | 0:308f83189a3f | 100 | Host m_client; |
mimil | 0:308f83189a3f | 101 | |
mimil | 0:308f83189a3f | 102 | friend class Net; |
mimil | 0:308f83189a3f | 103 | int m_refs; |
mimil | 0:308f83189a3f | 104 | |
mimil | 0:308f83189a3f | 105 | bool m_closed; |
mimil | 0:308f83189a3f | 106 | bool m_removed; |
mimil | 0:308f83189a3f | 107 | |
mimil | 0:308f83189a3f | 108 | private: |
mimil | 0:308f83189a3f | 109 | //We do not want to execute user code in interrupt routines, so we queue events until the server is polled |
mimil | 0:308f83189a3f | 110 | //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 | 111 | void onEvent(NetTcpSocketEvent e); //To be called on poll |
mimil | 0:308f83189a3f | 112 | CDummy* m_pCbItem; |
mimil | 0:308f83189a3f | 113 | void (CDummy::*m_pCbMeth)(NetTcpSocketEvent); |
mimil | 0:308f83189a3f | 114 | queue<NetTcpSocketEvent> m_events; |
mimil | 0:308f83189a3f | 115 | |
mimil | 0:308f83189a3f | 116 | }; |
mimil | 0:308f83189a3f | 117 | |
mimil | 0:308f83189a3f | 118 | #endif |