Dirk-Willem van Gulik (NXP/mbed)
/
Bonjour
Bonjour/Zerconf library
api/TCPSocket.cpp@5:8e53abda9900, 2010-08-14 (annotated)
- Committer:
- dirkx
- Date:
- Sat Aug 14 15:54:31 2010 +0000
- Revision:
- 5:8e53abda9900
- Parent:
- 0:355018f44c9f
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dirkx | 0:355018f44c9f | 1 | |
dirkx | 0:355018f44c9f | 2 | /* |
dirkx | 0:355018f44c9f | 3 | Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) |
dirkx | 0:355018f44c9f | 4 | |
dirkx | 0:355018f44c9f | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy |
dirkx | 0:355018f44c9f | 6 | of this software and associated documentation files (the "Software"), to deal |
dirkx | 0:355018f44c9f | 7 | in the Software without restriction, including without limitation the rights |
dirkx | 0:355018f44c9f | 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
dirkx | 0:355018f44c9f | 9 | copies of the Software, and to permit persons to whom the Software is |
dirkx | 0:355018f44c9f | 10 | furnished to do so, subject to the following conditions: |
dirkx | 0:355018f44c9f | 11 | |
dirkx | 0:355018f44c9f | 12 | The above copyright notice and this permission notice shall be included in |
dirkx | 0:355018f44c9f | 13 | all copies or substantial portions of the Software. |
dirkx | 0:355018f44c9f | 14 | |
dirkx | 0:355018f44c9f | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
dirkx | 0:355018f44c9f | 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
dirkx | 0:355018f44c9f | 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
dirkx | 0:355018f44c9f | 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
dirkx | 0:355018f44c9f | 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
dirkx | 0:355018f44c9f | 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
dirkx | 0:355018f44c9f | 21 | THE SOFTWARE. |
dirkx | 0:355018f44c9f | 22 | */ |
dirkx | 0:355018f44c9f | 23 | |
dirkx | 0:355018f44c9f | 24 | #include "TCPSocket.h" |
dirkx | 0:355018f44c9f | 25 | |
dirkx | 0:355018f44c9f | 26 | TCPSocket::TCPSocket() : m_pNetTcpSocket(NULL), m_pCbItem(NULL), m_pCbMeth(NULL), m_pCb(NULL) |
dirkx | 0:355018f44c9f | 27 | { |
dirkx | 0:355018f44c9f | 28 | |
dirkx | 0:355018f44c9f | 29 | } |
dirkx | 0:355018f44c9f | 30 | |
dirkx | 0:355018f44c9f | 31 | TCPSocket::TCPSocket(NetTcpSocket* pNetTcpSocket) : m_pNetTcpSocket(pNetTcpSocket), m_pCbItem(NULL), m_pCbMeth(NULL), m_pCb(NULL) |
dirkx | 0:355018f44c9f | 32 | { |
dirkx | 0:355018f44c9f | 33 | m_pNetTcpSocket->setOnEvent(this, &TCPSocket::onNetTcpSocketEvent); |
dirkx | 0:355018f44c9f | 34 | } |
dirkx | 0:355018f44c9f | 35 | |
dirkx | 0:355018f44c9f | 36 | TCPSocket::~TCPSocket() //close() |
dirkx | 0:355018f44c9f | 37 | { |
dirkx | 0:355018f44c9f | 38 | close(); |
dirkx | 0:355018f44c9f | 39 | } |
dirkx | 0:355018f44c9f | 40 | |
dirkx | 0:355018f44c9f | 41 | TCPSocketErr TCPSocket::bind(const Host& me) |
dirkx | 0:355018f44c9f | 42 | { |
dirkx | 0:355018f44c9f | 43 | TCPSocketErr tcpSocketErr = checkInst(); |
dirkx | 0:355018f44c9f | 44 | if(tcpSocketErr) |
dirkx | 0:355018f44c9f | 45 | return tcpSocketErr; |
dirkx | 0:355018f44c9f | 46 | return (TCPSocketErr) m_pNetTcpSocket->bind(me); |
dirkx | 0:355018f44c9f | 47 | } |
dirkx | 0:355018f44c9f | 48 | |
dirkx | 0:355018f44c9f | 49 | TCPSocketErr TCPSocket::listen() |
dirkx | 0:355018f44c9f | 50 | { |
dirkx | 0:355018f44c9f | 51 | TCPSocketErr tcpSocketErr = checkInst(); |
dirkx | 0:355018f44c9f | 52 | if(tcpSocketErr) |
dirkx | 0:355018f44c9f | 53 | return tcpSocketErr; |
dirkx | 0:355018f44c9f | 54 | return (TCPSocketErr) m_pNetTcpSocket->listen(); |
dirkx | 0:355018f44c9f | 55 | } |
dirkx | 0:355018f44c9f | 56 | |
dirkx | 0:355018f44c9f | 57 | TCPSocketErr TCPSocket::connect(const Host& host) |
dirkx | 0:355018f44c9f | 58 | { |
dirkx | 0:355018f44c9f | 59 | TCPSocketErr tcpSocketErr = checkInst(); |
dirkx | 0:355018f44c9f | 60 | if(tcpSocketErr) |
dirkx | 0:355018f44c9f | 61 | return tcpSocketErr; |
dirkx | 0:355018f44c9f | 62 | return (TCPSocketErr) m_pNetTcpSocket->connect(host); |
dirkx | 0:355018f44c9f | 63 | } |
dirkx | 0:355018f44c9f | 64 | |
dirkx | 0:355018f44c9f | 65 | TCPSocketErr TCPSocket::accept(Host* pClient, TCPSocket** ppNewTCPSocket) |
dirkx | 0:355018f44c9f | 66 | { |
dirkx | 0:355018f44c9f | 67 | TCPSocketErr tcpSocketErr = checkInst(); |
dirkx | 0:355018f44c9f | 68 | if(tcpSocketErr) |
dirkx | 0:355018f44c9f | 69 | return tcpSocketErr; |
dirkx | 0:355018f44c9f | 70 | NetTcpSocket* pNewNetTcpSocket; |
dirkx | 0:355018f44c9f | 71 | tcpSocketErr = (TCPSocketErr) m_pNetTcpSocket->accept(pClient, &pNewNetTcpSocket); |
dirkx | 0:355018f44c9f | 72 | if(pNewNetTcpSocket) |
dirkx | 0:355018f44c9f | 73 | *ppNewTCPSocket = new TCPSocket(pNewNetTcpSocket); |
dirkx | 0:355018f44c9f | 74 | return tcpSocketErr; |
dirkx | 0:355018f44c9f | 75 | } |
dirkx | 0:355018f44c9f | 76 | |
dirkx | 0:355018f44c9f | 77 | int /*if < 0 : TCPSocketErr*/ TCPSocket::send(const char* buf, int len) |
dirkx | 0:355018f44c9f | 78 | { |
dirkx | 0:355018f44c9f | 79 | TCPSocketErr tcpSocketErr = checkInst(); |
dirkx | 0:355018f44c9f | 80 | if(tcpSocketErr) |
dirkx | 0:355018f44c9f | 81 | return tcpSocketErr; |
dirkx | 0:355018f44c9f | 82 | return m_pNetTcpSocket->send(buf, len); |
dirkx | 0:355018f44c9f | 83 | } |
dirkx | 0:355018f44c9f | 84 | |
dirkx | 0:355018f44c9f | 85 | int /*if < 0 : TCPSocketErr*/ TCPSocket::recv(char* buf, int len) |
dirkx | 0:355018f44c9f | 86 | { |
dirkx | 0:355018f44c9f | 87 | TCPSocketErr tcpSocketErr = checkInst(); |
dirkx | 0:355018f44c9f | 88 | if(tcpSocketErr) |
dirkx | 0:355018f44c9f | 89 | return tcpSocketErr; |
dirkx | 0:355018f44c9f | 90 | return m_pNetTcpSocket->recv(buf, len); |
dirkx | 0:355018f44c9f | 91 | } |
dirkx | 0:355018f44c9f | 92 | |
dirkx | 0:355018f44c9f | 93 | TCPSocketErr TCPSocket::close() |
dirkx | 0:355018f44c9f | 94 | { |
dirkx | 0:355018f44c9f | 95 | if(!m_pNetTcpSocket) |
dirkx | 0:355018f44c9f | 96 | return TCPSOCKET_SETUP; |
dirkx | 0:355018f44c9f | 97 | m_pNetTcpSocket->resetOnEvent(); |
dirkx | 0:355018f44c9f | 98 | TCPSocketErr tcpSocketErr = (TCPSocketErr) m_pNetTcpSocket->close(); //Close (can already be closed) |
dirkx | 0:355018f44c9f | 99 | Net::releaseTcpSocket(m_pNetTcpSocket); //And release it so it can be freed when properly removed |
dirkx | 0:355018f44c9f | 100 | m_pNetTcpSocket = NULL; |
dirkx | 0:355018f44c9f | 101 | return tcpSocketErr; |
dirkx | 0:355018f44c9f | 102 | } |
dirkx | 0:355018f44c9f | 103 | |
dirkx | 0:355018f44c9f | 104 | //Callbacks |
dirkx | 0:355018f44c9f | 105 | void TCPSocket::setOnEvent( void (*pMethod)(TCPSocketEvent) ) |
dirkx | 0:355018f44c9f | 106 | { |
dirkx | 0:355018f44c9f | 107 | m_pCb = pMethod; |
dirkx | 0:355018f44c9f | 108 | } |
dirkx | 0:355018f44c9f | 109 | |
dirkx | 0:355018f44c9f | 110 | #if 0 //For info only |
dirkx | 0:355018f44c9f | 111 | template<class T> |
dirkx | 0:355018f44c9f | 112 | void TCPSocket::setOnEvent( T* pItem, void (T::*pMethod)(TCPSocketEvent) ) |
dirkx | 0:355018f44c9f | 113 | { |
dirkx | 0:355018f44c9f | 114 | m_pCbItem = (CDummy*) pItem; |
dirkx | 0:355018f44c9f | 115 | m_pCbMeth = (void (CDummy::*)(TCPSocketEvent)) pMethod; |
dirkx | 0:355018f44c9f | 116 | } |
dirkx | 0:355018f44c9f | 117 | #endif |
dirkx | 0:355018f44c9f | 118 | |
dirkx | 0:355018f44c9f | 119 | void TCPSocket::resetOnEvent() //Disable callback |
dirkx | 0:355018f44c9f | 120 | { |
dirkx | 0:355018f44c9f | 121 | m_pCb = NULL; |
dirkx | 0:355018f44c9f | 122 | m_pCbItem = NULL; |
dirkx | 0:355018f44c9f | 123 | m_pCbMeth = NULL; |
dirkx | 0:355018f44c9f | 124 | } |
dirkx | 0:355018f44c9f | 125 | |
dirkx | 0:355018f44c9f | 126 | void TCPSocket::onNetTcpSocketEvent(NetTcpSocketEvent e) |
dirkx | 0:355018f44c9f | 127 | { |
dirkx | 0:355018f44c9f | 128 | if(m_pCbItem && m_pCbMeth) |
dirkx | 0:355018f44c9f | 129 | (m_pCbItem->*m_pCbMeth)((TCPSocketEvent) e); |
dirkx | 0:355018f44c9f | 130 | else if(m_pCb) |
dirkx | 0:355018f44c9f | 131 | m_pCb((TCPSocketEvent) e); |
dirkx | 0:355018f44c9f | 132 | } |
dirkx | 0:355018f44c9f | 133 | |
dirkx | 0:355018f44c9f | 134 | TCPSocketErr TCPSocket::checkInst() |
dirkx | 0:355018f44c9f | 135 | { |
dirkx | 0:355018f44c9f | 136 | if(!m_pNetTcpSocket) |
dirkx | 0:355018f44c9f | 137 | { |
dirkx | 0:355018f44c9f | 138 | m_pNetTcpSocket = Net::tcpSocket(); |
dirkx | 0:355018f44c9f | 139 | if(!m_pNetTcpSocket) |
dirkx | 0:355018f44c9f | 140 | { |
dirkx | 0:355018f44c9f | 141 | return TCPSOCKET_IF; //Interface did not return a socket (usually because a default interface does not exist) |
dirkx | 0:355018f44c9f | 142 | } |
dirkx | 0:355018f44c9f | 143 | m_pNetTcpSocket->setOnEvent(this, &TCPSocket::onNetTcpSocketEvent); |
dirkx | 0:355018f44c9f | 144 | } |
dirkx | 0:355018f44c9f | 145 | return TCPSOCKET_OK; |
dirkx | 0:355018f44c9f | 146 | } |