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