Fork of NetServicesMin with some warnings removed
Fork of NetServicesMin by
TCPSocket.h
00001 00002 /* 00003 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) 00004 00005 Permission is hereby granted, free of charge, to any person obtaining a copy 00006 of this software and associated documentation files (the "Software"), to deal 00007 in the Software without restriction, including without limitation the rights 00008 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00009 copies of the Software, and to permit persons to whom the Software is 00010 furnished to do so, subject to the following conditions: 00011 00012 The above copyright notice and this permission notice shall be included in 00013 all copies or substantial portions of the Software. 00014 00015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00016 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00017 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00018 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00019 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00020 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 00021 THE SOFTWARE. 00022 */ 00023 00024 /** \file 00025 TCP Socket header file 00026 */ 00027 00028 #ifndef TCPSOCKET_H 00029 #define TCPSOCKET_H 00030 00031 #include "core/net.h" 00032 #include "core/host.h" 00033 //Essentially it is a safe interface to NetTcpSocket 00034 00035 #include "if/net/nettcpsocket.h" 00036 00037 ///TCP Socket error codes 00038 enum TCPSocketErr 00039 { 00040 __TCPSOCKET_MIN = -0xFFFF, 00041 TCPSOCKET_SETUP, ///<TCPSocket not properly configured 00042 TCPSOCKET_TIMEOUT, ///<Connection timed out 00043 TCPSOCKET_IF, ///<Interface has problems, does not exist or is not initialized 00044 TCPSOCKET_MEM, ///<Not enough mem 00045 TCPSOCKET_INUSE, ///<Interface / Port is in use 00046 TCPSOCKET_EMPTY, ///<Connections queue is empty 00047 TCPSOCKET_RST, ///<Connection was reset by remote host 00048 //... 00049 TCPSOCKET_OK = 0 ///<Success 00050 }; 00051 00052 ///TCP Socket Events 00053 enum TCPSocketEvent 00054 { 00055 TCPSOCKET_CONNECTED, ///<Connected to host 00056 TCPSOCKET_ACCEPT, ///<Client is connected, must call accept() to get a new Socket 00057 TCPSOCKET_READABLE, ///<Data in buf 00058 TCPSOCKET_WRITEABLE, ///<Can write data to buf 00059 TCPSOCKET_CONTIMEOUT, ///<Connection timed out 00060 TCPSOCKET_CONRST, ///<Connection was reset by remote host 00061 TCPSOCKET_CONABRT, ///<Connection was aborted 00062 TCPSOCKET_ERROR, ///<Unknown error 00063 TCPSOCKET_DISCONNECTED ///<Disconnected 00064 }; 00065 00066 //class NetTcpSocket; 00067 //enum NetTcpSocketEvent; 00068 00069 ///This is a simple TCP Socket class 00070 /** 00071 This class exposes an API to deal with TCP Sockets 00072 */ 00073 class TCPSocket 00074 { 00075 public: 00076 ///Creates a new socket 00077 TCPSocket(); 00078 protected: 00079 TCPSocket(NetTcpSocket* pNetTcpSocket); 00080 public: 00081 ///Closes if needed and destroys the socket 00082 ~TCPSocket(); //close() 00083 00084 ///Binds the socket to (local) host 00085 TCPSocketErr bind(const Host& me); 00086 00087 ///Starts listening 00088 TCPSocketErr listen(); 00089 00090 ///Connects socket to host 00091 TCPSocketErr connect(const Host& host); 00092 00093 ///Accepts connection from client and gets connected socket 00094 TCPSocketErr accept(Host* pClient, TCPSocket** ppNewTcpSocket); 00095 00096 ///Sends data 00097 /* 00098 @return a negative error code or the number of bytes transmitted 00099 */ 00100 int /*if < 0 : TCPSocketErr*/ send(const char* buf, int len); 00101 00102 ///Receives data 00103 /* 00104 @return a negative error code or the number of bytes received 00105 */ 00106 int /*if < 0 : TCPSocketErr*/ recv(char* buf, int len); 00107 00108 /* TODO NTH : printf / scanf helpers that call send/recv */ 00109 00110 ///Closes socket 00111 TCPSocketErr close(); 00112 00113 //Callbacks 00114 ///Setups callback 00115 /** 00116 @param pMethod : callback function 00117 */ 00118 void setOnEvent( void (*pMethod)(TCPSocketEvent) ); 00119 00120 class CDummy; 00121 ///Setups callback 00122 /** 00123 @param pItem : instance of class on which to execute the callback method 00124 @param pMethod : callback method 00125 */ 00126 template<class T> 00127 void setOnEvent( T* pItem, void (T::*pMethod)(TCPSocketEvent) ) 00128 { 00129 m_pCbItem = (CDummy*) pItem; 00130 m_pCbMeth = (void (CDummy::*)(TCPSocketEvent)) pMethod; 00131 } 00132 00133 ///Disables callback 00134 void resetOnEvent(); 00135 00136 protected: 00137 void onNetTcpSocketEvent(NetTcpSocketEvent e); 00138 TCPSocketErr checkInst(); 00139 00140 private: 00141 NetTcpSocket* m_pNetTcpSocket; 00142 00143 CDummy* m_pCbItem; 00144 void (CDummy::*m_pCbMeth)(TCPSocketEvent); 00145 00146 void (*m_pCb)(TCPSocketEvent); 00147 00148 }; 00149 00150 #endif
Generated on Wed Jul 13 2022 04:33:03 by 1.7.2