Experimental HTTPClient with proxy support
api/TCPSocket.h@0:b56b6a05cad4, 2011-06-29 (annotated)
- Committer:
- igorsk
- Date:
- Wed Jun 29 16:01:58 2011 +0000
- Revision:
- 0:b56b6a05cad4
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
igorsk | 0:b56b6a05cad4 | 1 | |
igorsk | 0:b56b6a05cad4 | 2 | /* |
igorsk | 0:b56b6a05cad4 | 3 | Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) |
igorsk | 0:b56b6a05cad4 | 4 | |
igorsk | 0:b56b6a05cad4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy |
igorsk | 0:b56b6a05cad4 | 6 | of this software and associated documentation files (the "Software"), to deal |
igorsk | 0:b56b6a05cad4 | 7 | in the Software without restriction, including without limitation the rights |
igorsk | 0:b56b6a05cad4 | 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
igorsk | 0:b56b6a05cad4 | 9 | copies of the Software, and to permit persons to whom the Software is |
igorsk | 0:b56b6a05cad4 | 10 | furnished to do so, subject to the following conditions: |
igorsk | 0:b56b6a05cad4 | 11 | |
igorsk | 0:b56b6a05cad4 | 12 | The above copyright notice and this permission notice shall be included in |
igorsk | 0:b56b6a05cad4 | 13 | all copies or substantial portions of the Software. |
igorsk | 0:b56b6a05cad4 | 14 | |
igorsk | 0:b56b6a05cad4 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
igorsk | 0:b56b6a05cad4 | 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
igorsk | 0:b56b6a05cad4 | 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
igorsk | 0:b56b6a05cad4 | 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
igorsk | 0:b56b6a05cad4 | 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
igorsk | 0:b56b6a05cad4 | 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
igorsk | 0:b56b6a05cad4 | 21 | THE SOFTWARE. |
igorsk | 0:b56b6a05cad4 | 22 | */ |
igorsk | 0:b56b6a05cad4 | 23 | |
igorsk | 0:b56b6a05cad4 | 24 | /** \file |
igorsk | 0:b56b6a05cad4 | 25 | TCP Socket header file |
igorsk | 0:b56b6a05cad4 | 26 | */ |
igorsk | 0:b56b6a05cad4 | 27 | |
igorsk | 0:b56b6a05cad4 | 28 | #ifndef TCPSOCKET_H |
igorsk | 0:b56b6a05cad4 | 29 | #define TCPSOCKET_H |
igorsk | 0:b56b6a05cad4 | 30 | |
igorsk | 0:b56b6a05cad4 | 31 | #include "core/net.h" |
igorsk | 0:b56b6a05cad4 | 32 | #include "core/host.h" |
igorsk | 0:b56b6a05cad4 | 33 | //Essentially it is a safe interface to NetTcpSocket |
igorsk | 0:b56b6a05cad4 | 34 | |
igorsk | 0:b56b6a05cad4 | 35 | ///TCP Socket error codes |
igorsk | 0:b56b6a05cad4 | 36 | enum TCPSocketErr |
igorsk | 0:b56b6a05cad4 | 37 | { |
igorsk | 0:b56b6a05cad4 | 38 | __TCPSOCKET_MIN = -0xFFFF, |
igorsk | 0:b56b6a05cad4 | 39 | TCPSOCKET_SETUP, ///<TCPSocket not properly configured |
igorsk | 0:b56b6a05cad4 | 40 | TCPSOCKET_TIMEOUT, ///<Connection timed out |
igorsk | 0:b56b6a05cad4 | 41 | TCPSOCKET_IF, ///<Interface has problems, does not exist or is not initialized |
igorsk | 0:b56b6a05cad4 | 42 | TCPSOCKET_MEM, ///<Not enough mem |
igorsk | 0:b56b6a05cad4 | 43 | TCPSOCKET_INUSE, ///<Interface / Port is in use |
igorsk | 0:b56b6a05cad4 | 44 | TCPSOCKET_EMPTY, ///<Connections queue is empty |
igorsk | 0:b56b6a05cad4 | 45 | TCPSOCKET_RST, ///<Connection was reset by remote host |
igorsk | 0:b56b6a05cad4 | 46 | //... |
igorsk | 0:b56b6a05cad4 | 47 | TCPSOCKET_OK = 0 ///<Success |
igorsk | 0:b56b6a05cad4 | 48 | }; |
igorsk | 0:b56b6a05cad4 | 49 | |
igorsk | 0:b56b6a05cad4 | 50 | ///TCP Socket Events |
igorsk | 0:b56b6a05cad4 | 51 | enum TCPSocketEvent |
igorsk | 0:b56b6a05cad4 | 52 | { |
igorsk | 0:b56b6a05cad4 | 53 | TCPSOCKET_CONNECTED, ///<Connected to host |
igorsk | 0:b56b6a05cad4 | 54 | TCPSOCKET_ACCEPT, ///<Client is connected, must call accept() to get a new Socket |
igorsk | 0:b56b6a05cad4 | 55 | TCPSOCKET_READABLE, ///<Data in buf |
igorsk | 0:b56b6a05cad4 | 56 | TCPSOCKET_WRITEABLE, ///<Can write data to buf |
igorsk | 0:b56b6a05cad4 | 57 | TCPSOCKET_CONTIMEOUT, ///<Connection timed out |
igorsk | 0:b56b6a05cad4 | 58 | TCPSOCKET_CONRST, ///<Connection was reset by remote host |
igorsk | 0:b56b6a05cad4 | 59 | TCPSOCKET_CONABRT, ///<Connection was aborted |
igorsk | 0:b56b6a05cad4 | 60 | TCPSOCKET_ERROR, ///<Unknown error |
igorsk | 0:b56b6a05cad4 | 61 | TCPSOCKET_DISCONNECTED ///<Disconnected |
igorsk | 0:b56b6a05cad4 | 62 | }; |
igorsk | 0:b56b6a05cad4 | 63 | |
igorsk | 0:b56b6a05cad4 | 64 | class NetTcpSocket; |
igorsk | 0:b56b6a05cad4 | 65 | enum NetTcpSocketEvent; |
igorsk | 0:b56b6a05cad4 | 66 | |
igorsk | 0:b56b6a05cad4 | 67 | ///This is a simple TCP Socket class |
igorsk | 0:b56b6a05cad4 | 68 | /** |
igorsk | 0:b56b6a05cad4 | 69 | This class exposes an API to deal with TCP Sockets |
igorsk | 0:b56b6a05cad4 | 70 | */ |
igorsk | 0:b56b6a05cad4 | 71 | class TCPSocket |
igorsk | 0:b56b6a05cad4 | 72 | { |
igorsk | 0:b56b6a05cad4 | 73 | public: |
igorsk | 0:b56b6a05cad4 | 74 | ///Creates a new socket |
igorsk | 0:b56b6a05cad4 | 75 | TCPSocket(); |
igorsk | 0:b56b6a05cad4 | 76 | protected: |
igorsk | 0:b56b6a05cad4 | 77 | TCPSocket(NetTcpSocket* pNetTcpSocket); |
igorsk | 0:b56b6a05cad4 | 78 | public: |
igorsk | 0:b56b6a05cad4 | 79 | ///Closes if needed and destroys the socket |
igorsk | 0:b56b6a05cad4 | 80 | ~TCPSocket(); //close() |
igorsk | 0:b56b6a05cad4 | 81 | |
igorsk | 0:b56b6a05cad4 | 82 | ///Binds the socket to (local) host |
igorsk | 0:b56b6a05cad4 | 83 | TCPSocketErr bind(const Host& me); |
igorsk | 0:b56b6a05cad4 | 84 | |
igorsk | 0:b56b6a05cad4 | 85 | ///Starts listening |
igorsk | 0:b56b6a05cad4 | 86 | TCPSocketErr listen(); |
igorsk | 0:b56b6a05cad4 | 87 | |
igorsk | 0:b56b6a05cad4 | 88 | ///Connects socket to host |
igorsk | 0:b56b6a05cad4 | 89 | TCPSocketErr connect(const Host& host); |
igorsk | 0:b56b6a05cad4 | 90 | |
igorsk | 0:b56b6a05cad4 | 91 | ///Accepts connection from client and gets connected socket |
igorsk | 0:b56b6a05cad4 | 92 | TCPSocketErr accept(Host* pClient, TCPSocket** ppNewTcpSocket); |
igorsk | 0:b56b6a05cad4 | 93 | |
igorsk | 0:b56b6a05cad4 | 94 | ///Sends data |
igorsk | 0:b56b6a05cad4 | 95 | /* |
igorsk | 0:b56b6a05cad4 | 96 | @return a negative error code or the number of bytes transmitted |
igorsk | 0:b56b6a05cad4 | 97 | */ |
igorsk | 0:b56b6a05cad4 | 98 | int /*if < 0 : TCPSocketErr*/ send(const char* buf, int len); |
igorsk | 0:b56b6a05cad4 | 99 | |
igorsk | 0:b56b6a05cad4 | 100 | ///Receives data |
igorsk | 0:b56b6a05cad4 | 101 | /* |
igorsk | 0:b56b6a05cad4 | 102 | @return a negative error code or the number of bytes received |
igorsk | 0:b56b6a05cad4 | 103 | */ |
igorsk | 0:b56b6a05cad4 | 104 | int /*if < 0 : TCPSocketErr*/ recv(char* buf, int len); |
igorsk | 0:b56b6a05cad4 | 105 | |
igorsk | 0:b56b6a05cad4 | 106 | /* TODO NTH : printf / scanf helpers that call send/recv */ |
igorsk | 0:b56b6a05cad4 | 107 | |
igorsk | 0:b56b6a05cad4 | 108 | ///Closes socket |
igorsk | 0:b56b6a05cad4 | 109 | TCPSocketErr close(); |
igorsk | 0:b56b6a05cad4 | 110 | |
igorsk | 0:b56b6a05cad4 | 111 | //Callbacks |
igorsk | 0:b56b6a05cad4 | 112 | ///Setups callback |
igorsk | 0:b56b6a05cad4 | 113 | /** |
igorsk | 0:b56b6a05cad4 | 114 | @param pMethod : callback function |
igorsk | 0:b56b6a05cad4 | 115 | */ |
igorsk | 0:b56b6a05cad4 | 116 | void setOnEvent( void (*pMethod)(TCPSocketEvent) ); |
igorsk | 0:b56b6a05cad4 | 117 | |
igorsk | 0:b56b6a05cad4 | 118 | class CDummy; |
igorsk | 0:b56b6a05cad4 | 119 | ///Setups callback |
igorsk | 0:b56b6a05cad4 | 120 | /** |
igorsk | 0:b56b6a05cad4 | 121 | @param pItem : instance of class on which to execute the callback method |
igorsk | 0:b56b6a05cad4 | 122 | @param pMethod : callback method |
igorsk | 0:b56b6a05cad4 | 123 | */ |
igorsk | 0:b56b6a05cad4 | 124 | template<class T> |
igorsk | 0:b56b6a05cad4 | 125 | void setOnEvent( T* pItem, void (T::*pMethod)(TCPSocketEvent) ) |
igorsk | 0:b56b6a05cad4 | 126 | { |
igorsk | 0:b56b6a05cad4 | 127 | m_pCbItem = (CDummy*) pItem; |
igorsk | 0:b56b6a05cad4 | 128 | m_pCbMeth = (void (CDummy::*)(TCPSocketEvent)) pMethod; |
igorsk | 0:b56b6a05cad4 | 129 | } |
igorsk | 0:b56b6a05cad4 | 130 | |
igorsk | 0:b56b6a05cad4 | 131 | ///Disables callback |
igorsk | 0:b56b6a05cad4 | 132 | void resetOnEvent(); |
igorsk | 0:b56b6a05cad4 | 133 | |
igorsk | 0:b56b6a05cad4 | 134 | protected: |
igorsk | 0:b56b6a05cad4 | 135 | void onNetTcpSocketEvent(NetTcpSocketEvent e); |
igorsk | 0:b56b6a05cad4 | 136 | TCPSocketErr checkInst(); |
igorsk | 0:b56b6a05cad4 | 137 | |
igorsk | 0:b56b6a05cad4 | 138 | private: |
igorsk | 0:b56b6a05cad4 | 139 | NetTcpSocket* m_pNetTcpSocket; |
igorsk | 0:b56b6a05cad4 | 140 | |
igorsk | 0:b56b6a05cad4 | 141 | CDummy* m_pCbItem; |
igorsk | 0:b56b6a05cad4 | 142 | void (CDummy::*m_pCbMeth)(TCPSocketEvent); |
igorsk | 0:b56b6a05cad4 | 143 | |
igorsk | 0:b56b6a05cad4 | 144 | void (*m_pCb)(TCPSocketEvent); |
igorsk | 0:b56b6a05cad4 | 145 | |
igorsk | 0:b56b6a05cad4 | 146 | }; |
igorsk | 0:b56b6a05cad4 | 147 | |
igorsk | 0:b56b6a05cad4 | 148 | #endif |