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