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