/
spark_wiring_tcpserver.cpp
129 lines (106 loc) · 3.21 KB
/
spark_wiring_tcpserver.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/**
******************************************************************************
* @file spark_wiring_tcpserver.cpp
* @author Satish Nair
* @version V1.0.0
* @date 13-Nov-2013
* @brief
******************************************************************************
Copyright (c) 2013-2015 Particle Industries, Inc. All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation, either
version 3 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, see <http://www.gnu.org/licenses/>.
******************************************************************************
*/
#include "spark_wiring_tcpclient.h"
#include "spark_wiring_tcpserver.h"
#include "spark_wiring_network.h"
#include "spark_wiring_thread.h"
using namespace spark;
static TCPClient* s_invalid_client = NULL;
class TCPServerClient : public TCPClient
{
public:
TCPServerClient(sock_handle_t sock) : TCPClient(sock) {}
virtual IPAddress remoteIP() override
{
sock_peer_t peer;
memset(&peer, 0, sizeof(peer));
peer.size = sizeof(peer);
socket_peer(sock_handle(), &peer, NULL);
return peer.address;
}
};
TCPServer::TCPServer(uint16_t port, network_interface_t nif) : _port(port), _nif(nif), _sock(socket_handle_invalid()), _client(socket_handle_invalid())
{
SINGLE_THREADED_BLOCK() {
if (!s_invalid_client) {
s_invalid_client = new TCPClient(socket_handle_invalid());
}
}
}
bool TCPServer::begin()
{
stop();
if(!Network.from(_nif).ready())
{
return false;
}
if (socket_handle_valid(_sock)) {
return true;
}
sock_result_t result = socket_create_tcp_server(_port, _nif);
if (socket_handle_valid(result)) {
_sock = result;
return true;
}
stop();
return false;
}
void TCPServer::stop()
{
_client.stop();
socket_close(_sock);
_sock = socket_handle_invalid();
}
TCPClient TCPServer::available()
{
sock_handle_t SOCKET_INVALID = socket_handle_invalid();
if(_sock == SOCKET_INVALID)
{
begin();
}
if((!Network.from(_nif).ready()) || (_sock == SOCKET_INVALID))
{
_sock = SOCKET_INVALID;
_client = *s_invalid_client;
return _client;
}
int sock = socket_accept(_sock);
if (!socket_handle_valid(sock))
{
_client = *s_invalid_client;
}
else
{
TCPServerClient client = TCPServerClient(sock);
client.d_->remoteIP = client.remoteIP(); // fetch the peer IP ready for the copy operator
_client = client;
}
return _client;
}
size_t TCPServer::write(uint8_t b)
{
return write(&b, 1);
}
size_t TCPServer::write(const uint8_t *buffer, size_t size)
{
return _client.write(buffer, size);
}