Darran Shepherd
/
AutoIpNetStack
Net stack with AutoIP enabled
Embed:
(wiki syntax)
Show/hide line numbers
TCPSocket.cpp
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 #include "TCPSocket.h" 00025 00026 TCPSocket::TCPSocket() : m_pNetTcpSocket(NULL), m_pCbItem(NULL), m_pCbMeth(NULL), m_pCb(NULL) 00027 { 00028 00029 } 00030 00031 TCPSocket::TCPSocket(NetTcpSocket* pNetTcpSocket) : m_pNetTcpSocket(pNetTcpSocket), m_pCbItem(NULL), m_pCbMeth(NULL), m_pCb(NULL) 00032 { 00033 m_pNetTcpSocket->setOnEvent(this, &TCPSocket::onNetTcpSocketEvent); 00034 } 00035 00036 TCPSocket::~TCPSocket() //close() 00037 { 00038 close(); 00039 } 00040 00041 TCPSocketErr TCPSocket::bind(const Host& me) 00042 { 00043 TCPSocketErr tcpSocketErr = checkInst(); 00044 if(tcpSocketErr) 00045 return tcpSocketErr; 00046 return (TCPSocketErr) m_pNetTcpSocket->bind(me); 00047 } 00048 00049 TCPSocketErr TCPSocket::listen() 00050 { 00051 TCPSocketErr tcpSocketErr = checkInst(); 00052 if(tcpSocketErr) 00053 return tcpSocketErr; 00054 return (TCPSocketErr) m_pNetTcpSocket->listen(); 00055 } 00056 00057 TCPSocketErr TCPSocket::connect(const Host& host) 00058 { 00059 TCPSocketErr tcpSocketErr = checkInst(); 00060 if(tcpSocketErr) 00061 return tcpSocketErr; 00062 return (TCPSocketErr) m_pNetTcpSocket->connect(host); 00063 } 00064 00065 TCPSocketErr TCPSocket::accept(Host* pClient, TCPSocket** ppNewTCPSocket) 00066 { 00067 TCPSocketErr tcpSocketErr = checkInst(); 00068 if(tcpSocketErr) 00069 return tcpSocketErr; 00070 NetTcpSocket* pNewNetTcpSocket; 00071 tcpSocketErr = (TCPSocketErr) m_pNetTcpSocket->accept(pClient, &pNewNetTcpSocket); 00072 if(pNewNetTcpSocket) 00073 *ppNewTCPSocket = new TCPSocket(pNewNetTcpSocket); 00074 return tcpSocketErr; 00075 } 00076 00077 int /*if < 0 : TCPSocketErr*/ TCPSocket::send(const char* buf, int len) 00078 { 00079 TCPSocketErr tcpSocketErr = checkInst(); 00080 if(tcpSocketErr) 00081 return tcpSocketErr; 00082 return m_pNetTcpSocket->send(buf, len); 00083 } 00084 00085 int /*if < 0 : TCPSocketErr*/ TCPSocket::recv(char* buf, int len) 00086 { 00087 TCPSocketErr tcpSocketErr = checkInst(); 00088 if(tcpSocketErr) 00089 return tcpSocketErr; 00090 return m_pNetTcpSocket->recv(buf, len); 00091 } 00092 00093 TCPSocketErr TCPSocket::close() 00094 { 00095 if(!m_pNetTcpSocket) 00096 return TCPSOCKET_SETUP; 00097 m_pNetTcpSocket->resetOnEvent(); 00098 TCPSocketErr tcpSocketErr = (TCPSocketErr) m_pNetTcpSocket->close(); //Close (can already be closed) 00099 Net::releaseTcpSocket(m_pNetTcpSocket); //And release it so it can be freed when properly removed 00100 m_pNetTcpSocket = NULL; 00101 return tcpSocketErr; 00102 } 00103 00104 //Callbacks 00105 void TCPSocket::setOnEvent( void (*pMethod)(TCPSocketEvent) ) 00106 { 00107 m_pCb = pMethod; 00108 } 00109 00110 #if 0 //For info only 00111 template<class T> 00112 void TCPSocket::setOnEvent( T* pItem, void (T::*pMethod)(TCPSocketEvent) ) 00113 { 00114 m_pCbItem = (CDummy*) pItem; 00115 m_pCbMeth = (void (CDummy::*)(TCPSocketEvent)) pMethod; 00116 } 00117 #endif 00118 00119 void TCPSocket::resetOnEvent() //Disable callback 00120 { 00121 m_pCb = NULL; 00122 m_pCbItem = NULL; 00123 m_pCbMeth = NULL; 00124 } 00125 00126 void TCPSocket::onNetTcpSocketEvent(NetTcpSocketEvent e) 00127 { 00128 if(m_pCbItem && m_pCbMeth) 00129 (m_pCbItem->*m_pCbMeth)((TCPSocketEvent) e); 00130 else if(m_pCb) 00131 m_pCb((TCPSocketEvent) e); 00132 } 00133 00134 TCPSocketErr TCPSocket::checkInst() 00135 { 00136 if(!m_pNetTcpSocket) 00137 { 00138 m_pNetTcpSocket = Net::tcpSocket(); 00139 if(!m_pNetTcpSocket) 00140 { 00141 return TCPSOCKET_IF; //Interface did not return a socket (usually because a default interface does not exist) 00142 } 00143 m_pNetTcpSocket->setOnEvent(this, &TCPSocket::onNetTcpSocketEvent); 00144 } 00145 return TCPSOCKET_OK; 00146 }
Generated on Tue Jul 12 2022 15:37:05 by 1.7.2