Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
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 #include "mbed.h" 00024 #include "TCPSocket.h" 00025 #include "if/net/nettcpsocket.h" 00026 00027 TCPSocket::TCPSocket() : m_pNetTcpSocket(NULL), m_pCbItem(NULL), m_pCbMeth(NULL), m_pCb(NULL) 00028 { 00029 00030 } 00031 00032 TCPSocket::TCPSocket(NetTcpSocket* pNetTcpSocket) : m_pNetTcpSocket(pNetTcpSocket), m_pCbItem(NULL), m_pCbMeth(NULL), m_pCb(NULL) 00033 { 00034 m_pNetTcpSocket->setOnEvent(this, &TCPSocket::onNetTcpSocketEvent); 00035 } 00036 00037 TCPSocket::~TCPSocket() //close() 00038 { 00039 close(); 00040 } 00041 00042 TCPSocketErr TCPSocket::bind(const Host& me) 00043 { 00044 printf("Enter TCPSocket::bind()\r\n"); 00045 TCPSocketErr tcpSocketErr = checkInst(); 00046 if(tcpSocketErr) 00047 return tcpSocketErr; 00048 return (TCPSocketErr) m_pNetTcpSocket->bind(me); 00049 } 00050 00051 TCPSocketErr TCPSocket::listen() 00052 { 00053 printf("Enter TCPSocket::listen()\r\n"); 00054 TCPSocketErr tcpSocketErr = checkInst(); 00055 if(tcpSocketErr) 00056 return tcpSocketErr; 00057 return (TCPSocketErr) m_pNetTcpSocket->listen(); 00058 } 00059 00060 TCPSocketErr TCPSocket::connect(const Host& host) 00061 { 00062 printf("Enter TCPSocket::connect()\r\n"); 00063 TCPSocketErr tcpSocketErr = checkInst(); 00064 if(tcpSocketErr) 00065 return tcpSocketErr; 00066 return (TCPSocketErr) m_pNetTcpSocket->connect(host); 00067 } 00068 00069 TCPSocketErr TCPSocket::accept(Host* pClient, TCPSocket** ppNewTCPSocket) 00070 { 00071 printf("Enter TCPSocket::accept()\r\n"); 00072 TCPSocketErr tcpSocketErr = checkInst(); 00073 if(tcpSocketErr) 00074 return tcpSocketErr; 00075 NetTcpSocket* pNewNetTcpSocket; 00076 tcpSocketErr = (TCPSocketErr) m_pNetTcpSocket->accept(pClient, &pNewNetTcpSocket); 00077 if(pNewNetTcpSocket) 00078 *ppNewTCPSocket = new TCPSocket(pNewNetTcpSocket); 00079 return tcpSocketErr; 00080 } 00081 00082 int /*if < 0 : TCPSocketErr*/ TCPSocket::send(const char* buf, int len) 00083 { 00084 printf("Enter TCPSocket::send()\r\n"); 00085 TCPSocketErr tcpSocketErr = checkInst(); 00086 if(tcpSocketErr) 00087 return tcpSocketErr; 00088 return m_pNetTcpSocket->send(buf, len); 00089 } 00090 00091 int /*if < 0 : TCPSocketErr*/ TCPSocket::recv(char* buf, int len) 00092 { 00093 printf("Enter TCPSocket::receive()\r\n"); 00094 TCPSocketErr tcpSocketErr = checkInst(); 00095 if(tcpSocketErr) 00096 return tcpSocketErr; 00097 return m_pNetTcpSocket->recv(buf, len); 00098 } 00099 00100 TCPSocketErr TCPSocket::close() 00101 { 00102 printf("Enter TCPSocket::close()\r\n"); 00103 if(!m_pNetTcpSocket) 00104 return TCPSOCKET_SETUP; 00105 m_pNetTcpSocket->resetOnEvent(); 00106 TCPSocketErr tcpSocketErr = (TCPSocketErr) m_pNetTcpSocket->close(); //Close (can already be closed) 00107 Net::releaseTcpSocket(m_pNetTcpSocket); //And release it so it can be freed when properly removed 00108 m_pNetTcpSocket = NULL; 00109 return tcpSocketErr; 00110 } 00111 00112 //Callbacks 00113 void TCPSocket::setOnEvent( void (*pMethod)(TCPSocketEvent) ) 00114 { 00115 printf("Enter TCPSocket::setOnEvent()\r\n"); 00116 m_pCb = pMethod; 00117 } 00118 00119 #if 0 //For info only 00120 template<class T> 00121 void TCPSocket::setOnEvent( T* pItem, void (T::*pMethod)(TCPSocketEvent) ) 00122 { 00123 printf("Enter TCPSocket::setOnEvent()\r\n"); 00124 m_pCbItem = (CDummy*) pItem; 00125 m_pCbMeth = (void (CDummy::*)(TCPSocketEvent)) pMethod; 00126 } 00127 #endif 00128 00129 void TCPSocket::resetOnEvent() //Disable callback 00130 { 00131 printf("Enter TCPSocket::resetOnEvent()\r\n"); 00132 m_pCb = NULL; 00133 m_pCbItem = NULL; 00134 m_pCbMeth = NULL; 00135 } 00136 00137 void TCPSocket::onNetTcpSocketEvent(NetTcpSocketEvent e) 00138 { 00139 printf("Enter TCPSocket::onNetTcpSpcketEvent()\r\n"); 00140 if(m_pCbItem && m_pCbMeth) 00141 (m_pCbItem->*m_pCbMeth)((TCPSocketEvent) e); 00142 else if(m_pCb) 00143 m_pCb((TCPSocketEvent) e); 00144 } 00145 00146 TCPSocketErr TCPSocket::checkInst() 00147 { 00148 printf("Enter TCPSocket::checkInst()\r\n"); 00149 if(!m_pNetTcpSocket) 00150 { 00151 m_pNetTcpSocket = Net::tcpSocket(); 00152 if(!m_pNetTcpSocket) 00153 { 00154 return TCPSOCKET_IF; //Interface did not return a socket (usually because a default interface does not exist) 00155 } 00156 printf("!m_pNetTcpSocket\r\n"); 00157 m_pNetTcpSocket->setOnEvent(this, &TCPSocket::onNetTcpSocketEvent); 00158 } 00159 return TCPSOCKET_OK; 00160 }
Generated on Tue Jul 12 2022 19:12:17 by
1.7.2