This library is stripped down version of NetServices library. HTTP server and client function is NOT supported.

Dependents:   imu-daq-eth

Committer:
idinor
Date:
Wed Jul 20 11:45:39 2011 +0000
Revision:
0:dcf3c92487ca

        

Who changed what in which revision?

UserRevisionLine numberNew 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 #include "TCPSocket.h"
idinor 0:dcf3c92487ca 25 #include "if/net/nettcpsocket.h"
idinor 0:dcf3c92487ca 26
idinor 0:dcf3c92487ca 27 TCPSocket::TCPSocket() : m_pNetTcpSocket(NULL), m_pCbItem(NULL), m_pCbMeth(NULL), m_pCb(NULL)
idinor 0:dcf3c92487ca 28 {
idinor 0:dcf3c92487ca 29
idinor 0:dcf3c92487ca 30 }
idinor 0:dcf3c92487ca 31
idinor 0:dcf3c92487ca 32 TCPSocket::TCPSocket(NetTcpSocket* pNetTcpSocket) : m_pNetTcpSocket(pNetTcpSocket), m_pCbItem(NULL), m_pCbMeth(NULL), m_pCb(NULL)
idinor 0:dcf3c92487ca 33 {
idinor 0:dcf3c92487ca 34 m_pNetTcpSocket->setOnEvent(this, &TCPSocket::onNetTcpSocketEvent);
idinor 0:dcf3c92487ca 35 }
idinor 0:dcf3c92487ca 36
idinor 0:dcf3c92487ca 37 TCPSocket::~TCPSocket() //close()
idinor 0:dcf3c92487ca 38 {
idinor 0:dcf3c92487ca 39 close();
idinor 0:dcf3c92487ca 40 }
idinor 0:dcf3c92487ca 41
idinor 0:dcf3c92487ca 42 TCPSocketErr TCPSocket::bind(const Host& me)
idinor 0:dcf3c92487ca 43 {
idinor 0:dcf3c92487ca 44 TCPSocketErr tcpSocketErr = checkInst();
idinor 0:dcf3c92487ca 45 if(tcpSocketErr)
idinor 0:dcf3c92487ca 46 return tcpSocketErr;
idinor 0:dcf3c92487ca 47 return (TCPSocketErr) m_pNetTcpSocket->bind(me);
idinor 0:dcf3c92487ca 48 }
idinor 0:dcf3c92487ca 49
idinor 0:dcf3c92487ca 50 TCPSocketErr TCPSocket::listen()
idinor 0:dcf3c92487ca 51 {
idinor 0:dcf3c92487ca 52 TCPSocketErr tcpSocketErr = checkInst();
idinor 0:dcf3c92487ca 53 if(tcpSocketErr)
idinor 0:dcf3c92487ca 54 return tcpSocketErr;
idinor 0:dcf3c92487ca 55 return (TCPSocketErr) m_pNetTcpSocket->listen();
idinor 0:dcf3c92487ca 56 }
idinor 0:dcf3c92487ca 57
idinor 0:dcf3c92487ca 58 TCPSocketErr TCPSocket::connect(const Host& host)
idinor 0:dcf3c92487ca 59 {
idinor 0:dcf3c92487ca 60 TCPSocketErr tcpSocketErr = checkInst();
idinor 0:dcf3c92487ca 61 if(tcpSocketErr)
idinor 0:dcf3c92487ca 62 return tcpSocketErr;
idinor 0:dcf3c92487ca 63 return (TCPSocketErr) m_pNetTcpSocket->connect(host);
idinor 0:dcf3c92487ca 64 }
idinor 0:dcf3c92487ca 65
idinor 0:dcf3c92487ca 66 TCPSocketErr TCPSocket::accept(Host* pClient, TCPSocket** ppNewTCPSocket)
idinor 0:dcf3c92487ca 67 {
idinor 0:dcf3c92487ca 68 TCPSocketErr tcpSocketErr = checkInst();
idinor 0:dcf3c92487ca 69 if(tcpSocketErr)
idinor 0:dcf3c92487ca 70 return tcpSocketErr;
idinor 0:dcf3c92487ca 71 NetTcpSocket* pNewNetTcpSocket;
idinor 0:dcf3c92487ca 72 tcpSocketErr = (TCPSocketErr) m_pNetTcpSocket->accept(pClient, &pNewNetTcpSocket);
idinor 0:dcf3c92487ca 73 if(pNewNetTcpSocket)
idinor 0:dcf3c92487ca 74 *ppNewTCPSocket = new TCPSocket(pNewNetTcpSocket);
idinor 0:dcf3c92487ca 75 return tcpSocketErr;
idinor 0:dcf3c92487ca 76 }
idinor 0:dcf3c92487ca 77
idinor 0:dcf3c92487ca 78 int /*if < 0 : TCPSocketErr*/ TCPSocket::send(const char* buf, int len)
idinor 0:dcf3c92487ca 79 {
idinor 0:dcf3c92487ca 80 TCPSocketErr tcpSocketErr = checkInst();
idinor 0:dcf3c92487ca 81 if(tcpSocketErr)
idinor 0:dcf3c92487ca 82 return tcpSocketErr;
idinor 0:dcf3c92487ca 83 return m_pNetTcpSocket->send(buf, len);
idinor 0:dcf3c92487ca 84 }
idinor 0:dcf3c92487ca 85
idinor 0:dcf3c92487ca 86 int /*if < 0 : TCPSocketErr*/ TCPSocket::recv(char* buf, int len)
idinor 0:dcf3c92487ca 87 {
idinor 0:dcf3c92487ca 88 TCPSocketErr tcpSocketErr = checkInst();
idinor 0:dcf3c92487ca 89 if(tcpSocketErr)
idinor 0:dcf3c92487ca 90 return tcpSocketErr;
idinor 0:dcf3c92487ca 91 return m_pNetTcpSocket->recv(buf, len);
idinor 0:dcf3c92487ca 92 }
idinor 0:dcf3c92487ca 93
idinor 0:dcf3c92487ca 94 TCPSocketErr TCPSocket::close()
idinor 0:dcf3c92487ca 95 {
idinor 0:dcf3c92487ca 96 if(!m_pNetTcpSocket)
idinor 0:dcf3c92487ca 97 return TCPSOCKET_SETUP;
idinor 0:dcf3c92487ca 98 m_pNetTcpSocket->resetOnEvent();
idinor 0:dcf3c92487ca 99 TCPSocketErr tcpSocketErr = (TCPSocketErr) m_pNetTcpSocket->close(); //Close (can already be closed)
idinor 0:dcf3c92487ca 100 Net::releaseTcpSocket(m_pNetTcpSocket); //And release it so it can be freed when properly removed
idinor 0:dcf3c92487ca 101 m_pNetTcpSocket = NULL;
idinor 0:dcf3c92487ca 102 return tcpSocketErr;
idinor 0:dcf3c92487ca 103 }
idinor 0:dcf3c92487ca 104
idinor 0:dcf3c92487ca 105 //Callbacks
idinor 0:dcf3c92487ca 106 void TCPSocket::setOnEvent( void (*pMethod)(TCPSocketEvent) )
idinor 0:dcf3c92487ca 107 {
idinor 0:dcf3c92487ca 108 m_pCb = pMethod;
idinor 0:dcf3c92487ca 109 }
idinor 0:dcf3c92487ca 110
idinor 0:dcf3c92487ca 111 #if 0 //For info only
idinor 0:dcf3c92487ca 112 template<class T>
idinor 0:dcf3c92487ca 113 void TCPSocket::setOnEvent( T* pItem, void (T::*pMethod)(TCPSocketEvent) )
idinor 0:dcf3c92487ca 114 {
idinor 0:dcf3c92487ca 115 m_pCbItem = (CDummy*) pItem;
idinor 0:dcf3c92487ca 116 m_pCbMeth = (void (CDummy::*)(TCPSocketEvent)) pMethod;
idinor 0:dcf3c92487ca 117 }
idinor 0:dcf3c92487ca 118 #endif
idinor 0:dcf3c92487ca 119
idinor 0:dcf3c92487ca 120 void TCPSocket::resetOnEvent() //Disable callback
idinor 0:dcf3c92487ca 121 {
idinor 0:dcf3c92487ca 122 m_pCb = NULL;
idinor 0:dcf3c92487ca 123 m_pCbItem = NULL;
idinor 0:dcf3c92487ca 124 m_pCbMeth = NULL;
idinor 0:dcf3c92487ca 125 }
idinor 0:dcf3c92487ca 126
idinor 0:dcf3c92487ca 127 void TCPSocket::onNetTcpSocketEvent(NetTcpSocketEvent e)
idinor 0:dcf3c92487ca 128 {
idinor 0:dcf3c92487ca 129 if(m_pCbItem && m_pCbMeth)
idinor 0:dcf3c92487ca 130 (m_pCbItem->*m_pCbMeth)((TCPSocketEvent) e);
idinor 0:dcf3c92487ca 131 else if(m_pCb)
idinor 0:dcf3c92487ca 132 m_pCb((TCPSocketEvent) e);
idinor 0:dcf3c92487ca 133 }
idinor 0:dcf3c92487ca 134
idinor 0:dcf3c92487ca 135 TCPSocketErr TCPSocket::checkInst()
idinor 0:dcf3c92487ca 136 {
idinor 0:dcf3c92487ca 137 if(!m_pNetTcpSocket)
idinor 0:dcf3c92487ca 138 {
idinor 0:dcf3c92487ca 139 m_pNetTcpSocket = Net::tcpSocket();
idinor 0:dcf3c92487ca 140 if(!m_pNetTcpSocket)
idinor 0:dcf3c92487ca 141 {
idinor 0:dcf3c92487ca 142 return TCPSOCKET_IF; //Interface did not return a socket (usually because a default interface does not exist)
idinor 0:dcf3c92487ca 143 }
idinor 0:dcf3c92487ca 144 m_pNetTcpSocket->setOnEvent(this, &TCPSocket::onNetTcpSocketEvent);
idinor 0:dcf3c92487ca 145 }
idinor 0:dcf3c92487ca 146 return TCPSOCKET_OK;
idinor 0:dcf3c92487ca 147 }