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