Skip to content

Commit 24efa4c

Browse files
author
Cruz Monrreal
authored
Merge pull request #7192 from SeppoTakalo/socket-refactor
Create abstract Socket interface
2 parents dbc109d + 1ca9ccd commit 24efa4c

13 files changed

+790
-270
lines changed

features/netsocket/Socket.cpp renamed to features/netsocket/InternetSocket.cpp

Lines changed: 46 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Socket
1+
/* InternetSocket
22
* Copyright (c) 2015 ARM Limited
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -14,17 +14,19 @@
1414
* limitations under the License.
1515
*/
1616

17-
#include "Socket.h"
18-
#include "mbed.h"
17+
#include "InternetSocket.h"
18+
#include "platform/Callback.h"
1919

20-
Socket::Socket()
21-
: _stack(0)
22-
, _socket(0)
23-
, _timeout(osWaitForever)
20+
using namespace mbed;
21+
22+
InternetSocket::InternetSocket()
23+
: _stack(0), _socket(0), _timeout(osWaitForever),
24+
_readers(0), _writers(0), _factory_allocated(false),
25+
_pending(0)
2426
{
2527
}
2628

27-
nsapi_error_t Socket::open(NetworkStack *stack)
29+
nsapi_error_t InternetSocket::open(NetworkStack *stack)
2830
{
2931
_lock.lock();
3032

@@ -42,14 +44,14 @@ nsapi_error_t Socket::open(NetworkStack *stack)
4244
}
4345

4446
_socket = socket;
45-
_event = callback(this, &Socket::event);
47+
_event = callback(this, &InternetSocket::event);
4648
_stack->socket_attach(_socket, Callback<void()>::thunk, &_event);
4749

4850
_lock.unlock();
4951
return NSAPI_ERROR_OK;
5052
}
5153

52-
nsapi_error_t Socket::close()
54+
nsapi_error_t InternetSocket::close()
5355
{
5456
_lock.lock();
5557

@@ -66,11 +68,23 @@ nsapi_error_t Socket::close()
6668
// on this socket
6769
event();
6870

71+
// Wait until all readers and writers are gone
72+
while (_readers || _writers) {
73+
_lock.unlock();
74+
_event_flag.wait_any(FINISHED_FLAG, osWaitForever);
75+
_lock.lock();
76+
}
77+
6978
_lock.unlock();
79+
80+
// When allocated by accept() call, will self desctruct on close();
81+
if (_factory_allocated) {
82+
delete this;
83+
}
7084
return ret;
7185
}
7286

73-
int Socket::modify_multicast_group(const SocketAddress &address, nsapi_socket_option_t socketopt)
87+
int InternetSocket::modify_multicast_group(const SocketAddress &address, nsapi_socket_option_t socketopt)
7488
{
7589
nsapi_ip_mreq_t mreq;
7690

@@ -81,32 +95,32 @@ int Socket::modify_multicast_group(const SocketAddress &address, nsapi_socket_op
8195
return this->setsockopt(NSAPI_SOCKET, socketopt, &mreq, sizeof(mreq));
8296
}
8397

84-
int Socket::join_multicast_group(const SocketAddress &address)
98+
int InternetSocket::join_multicast_group(const SocketAddress &address)
8599
{
86100
return modify_multicast_group(address, NSAPI_ADD_MEMBERSHIP);
87101
}
88102

89-
int Socket::leave_multicast_group(const SocketAddress &address)
103+
int InternetSocket::leave_multicast_group(const SocketAddress &address)
90104
{
91105
return modify_multicast_group(address, NSAPI_DROP_MEMBERSHIP);
92106
}
93107

94108

95-
nsapi_error_t Socket::bind(uint16_t port)
109+
nsapi_error_t InternetSocket::bind(uint16_t port)
96110
{
97111
// Underlying bind is thread safe
98112
SocketAddress addr(0, port);
99113
return bind(addr);
100114
}
101115

102-
nsapi_error_t Socket::bind(const char *address, uint16_t port)
116+
nsapi_error_t InternetSocket::bind(const char *address, uint16_t port)
103117
{
104118
// Underlying bind is thread safe
105119
SocketAddress addr(address, port);
106120
return bind(addr);
107121
}
108122

109-
nsapi_error_t Socket::bind(const SocketAddress &address)
123+
nsapi_error_t InternetSocket::bind(const SocketAddress &address)
110124
{
111125
_lock.lock();
112126
nsapi_error_t ret;
@@ -121,13 +135,13 @@ nsapi_error_t Socket::bind(const SocketAddress &address)
121135
return ret;
122136
}
123137

124-
void Socket::set_blocking(bool blocking)
138+
void InternetSocket::set_blocking(bool blocking)
125139
{
126-
// Socket::set_timeout is thread safe
140+
// InternetSocket::set_timeout is thread safe
127141
set_timeout(blocking ? -1 : 0);
128142
}
129143

130-
void Socket::set_timeout(int timeout)
144+
void InternetSocket::set_timeout(int timeout)
131145
{
132146
_lock.lock();
133147

@@ -140,7 +154,7 @@ void Socket::set_timeout(int timeout)
140154
_lock.unlock();
141155
}
142156

143-
nsapi_error_t Socket::setsockopt(int level, int optname, const void *optval, unsigned optlen)
157+
nsapi_error_t InternetSocket::setsockopt(int level, int optname, const void *optval, unsigned optlen)
144158
{
145159
_lock.lock();
146160
nsapi_error_t ret;
@@ -155,7 +169,7 @@ nsapi_error_t Socket::setsockopt(int level, int optname, const void *optval, uns
155169
return ret;
156170
}
157171

158-
nsapi_error_t Socket::getsockopt(int level, int optname, void *optval, unsigned *optlen)
172+
nsapi_error_t InternetSocket::getsockopt(int level, int optname, void *optval, unsigned *optlen)
159173
{
160174
_lock.lock();
161175
nsapi_error_t ret;
@@ -170,15 +184,24 @@ nsapi_error_t Socket::getsockopt(int level, int optname, void *optval, unsigned
170184
return ret;
171185

172186
}
187+
void InternetSocket::event()
188+
{
189+
_event_flag.set(READ_FLAG|WRITE_FLAG);
190+
191+
_pending += 1;
192+
if (_callback && _pending == 1) {
193+
_callback();
194+
}
195+
}
173196

174-
void Socket::sigio(Callback<void()> callback)
197+
void InternetSocket::sigio(Callback<void()> callback)
175198
{
176199
_lock.lock();
177200
_callback = callback;
178201
_lock.unlock();
179202
}
180203

181-
void Socket::attach(Callback<void()> callback)
204+
void InternetSocket::attach(Callback<void()> callback)
182205
{
183206
sigio(callback);
184207
}

0 commit comments

Comments
 (0)