NetServices Stack source

Dependents:   HelloWorld ServoInterfaceBoardExample1 4180_Lab4

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers TCPSocket.cpp Source File

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 #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   TCPSocketErr tcpSocketErr = checkInst();
00045   if(tcpSocketErr)
00046     return tcpSocketErr;
00047   return (TCPSocketErr) m_pNetTcpSocket->bind(me); 
00048 }
00049 
00050 TCPSocketErr TCPSocket::listen()
00051 {
00052   TCPSocketErr tcpSocketErr = checkInst();
00053   if(tcpSocketErr)
00054     return tcpSocketErr;
00055   return (TCPSocketErr) m_pNetTcpSocket->listen(); 
00056 }
00057 
00058 TCPSocketErr TCPSocket::connect(const Host& host)
00059 {
00060   TCPSocketErr tcpSocketErr = checkInst();
00061   if(tcpSocketErr)
00062     return tcpSocketErr;
00063   return (TCPSocketErr) m_pNetTcpSocket->connect(host); 
00064 }
00065 
00066 TCPSocketErr TCPSocket::accept(Host* pClient, TCPSocket** ppNewTCPSocket)
00067 {
00068   TCPSocketErr tcpSocketErr = checkInst();
00069   if(tcpSocketErr)
00070     return tcpSocketErr;
00071   NetTcpSocket* pNewNetTcpSocket;
00072   tcpSocketErr = (TCPSocketErr) m_pNetTcpSocket->accept(pClient, &pNewNetTcpSocket);
00073   if(pNewNetTcpSocket)
00074     *ppNewTCPSocket = new TCPSocket(pNewNetTcpSocket);
00075   return tcpSocketErr;
00076 }
00077   
00078 int /*if < 0 : TCPSocketErr*/ TCPSocket::send(const char* buf, int len)
00079 {
00080   TCPSocketErr tcpSocketErr = checkInst();
00081   if(tcpSocketErr)
00082     return tcpSocketErr;
00083   return m_pNetTcpSocket->send(buf, len);  
00084 }
00085 
00086 int /*if < 0 : TCPSocketErr*/ TCPSocket::recv(char* buf, int len)
00087 {
00088   TCPSocketErr tcpSocketErr = checkInst();
00089   if(tcpSocketErr)
00090     return tcpSocketErr;
00091   return m_pNetTcpSocket->recv(buf, len);
00092 }
00093 
00094 TCPSocketErr TCPSocket::close()
00095 {
00096   if(!m_pNetTcpSocket)
00097     return TCPSOCKET_SETUP;
00098   m_pNetTcpSocket->resetOnEvent();
00099   TCPSocketErr tcpSocketErr = (TCPSocketErr) m_pNetTcpSocket->close(); //Close (can already be closed)
00100   Net::releaseTcpSocket(m_pNetTcpSocket); //And release it so it can be freed when properly removed
00101   m_pNetTcpSocket = NULL;
00102   return tcpSocketErr;
00103 }
00104 
00105 //Callbacks
00106 void TCPSocket::setOnEvent( void (*pMethod)(TCPSocketEvent) )
00107 {
00108   m_pCb = pMethod;
00109 }
00110 
00111 #if 0 //For info only
00112 template<class T> 
00113 void TCPSocket::setOnEvent( T* pItem, void (T::*pMethod)(TCPSocketEvent) )
00114 {
00115   m_pCbItem = (CDummy*) pItem;
00116   m_pCbMeth = (void (CDummy::*)(TCPSocketEvent)) pMethod;
00117 }
00118 #endif
00119   
00120 void TCPSocket::resetOnEvent() //Disable callback
00121 {
00122   m_pCb = NULL;
00123   m_pCbItem = NULL;
00124   m_pCbMeth = NULL;
00125 }
00126 
00127 void TCPSocket::onNetTcpSocketEvent(NetTcpSocketEvent e)
00128 {
00129   if(m_pCbItem && m_pCbMeth)
00130     (m_pCbItem->*m_pCbMeth)((TCPSocketEvent) e);
00131   else if(m_pCb)
00132     m_pCb((TCPSocketEvent) e);
00133 }
00134 
00135 TCPSocketErr TCPSocket::checkInst()
00136 {
00137   if(!m_pNetTcpSocket)
00138   {
00139     m_pNetTcpSocket = Net::tcpSocket();
00140     if(!m_pNetTcpSocket)
00141     {
00142       return TCPSOCKET_IF; //Interface did not return a socket (usually because a default interface does not exist)
00143     }
00144     m_pNetTcpSocket->setOnEvent(this, &TCPSocket::onNetTcpSocketEvent);
00145   }
00146   return TCPSOCKET_OK;
00147 }