1
- /* Socket
1
+ /* InternetSocket
2
2
* Copyright (c) 2015 ARM Limited
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
14
14
* limitations under the License.
15
15
*/
16
16
17
- #include " Socket .h"
18
- #include " mbed .h"
17
+ #include " InternetSocket .h"
18
+ #include " platform/Callback .h"
19
19
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 )
24
26
{
25
27
}
26
28
27
- nsapi_error_t Socket ::open (NetworkStack *stack)
29
+ nsapi_error_t InternetSocket ::open (NetworkStack *stack)
28
30
{
29
31
_lock.lock ();
30
32
@@ -42,14 +44,14 @@ nsapi_error_t Socket::open(NetworkStack *stack)
42
44
}
43
45
44
46
_socket = socket;
45
- _event = callback (this , &Socket ::event);
47
+ _event = callback (this , &InternetSocket ::event);
46
48
_stack->socket_attach (_socket, Callback<void ()>::thunk, &_event);
47
49
48
50
_lock.unlock ();
49
51
return NSAPI_ERROR_OK;
50
52
}
51
53
52
- nsapi_error_t Socket ::close ()
54
+ nsapi_error_t InternetSocket ::close ()
53
55
{
54
56
_lock.lock ();
55
57
@@ -66,11 +68,23 @@ nsapi_error_t Socket::close()
66
68
// on this socket
67
69
event ();
68
70
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
+
69
78
_lock.unlock ();
79
+
80
+ // When allocated by accept() call, will self desctruct on close();
81
+ if (_factory_allocated) {
82
+ delete this ;
83
+ }
70
84
return ret;
71
85
}
72
86
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)
74
88
{
75
89
nsapi_ip_mreq_t mreq;
76
90
@@ -81,32 +95,32 @@ int Socket::modify_multicast_group(const SocketAddress &address, nsapi_socket_op
81
95
return this ->setsockopt (NSAPI_SOCKET, socketopt, &mreq, sizeof (mreq));
82
96
}
83
97
84
- int Socket ::join_multicast_group (const SocketAddress &address)
98
+ int InternetSocket ::join_multicast_group (const SocketAddress &address)
85
99
{
86
100
return modify_multicast_group (address, NSAPI_ADD_MEMBERSHIP);
87
101
}
88
102
89
- int Socket ::leave_multicast_group (const SocketAddress &address)
103
+ int InternetSocket ::leave_multicast_group (const SocketAddress &address)
90
104
{
91
105
return modify_multicast_group (address, NSAPI_DROP_MEMBERSHIP);
92
106
}
93
107
94
108
95
- nsapi_error_t Socket ::bind (uint16_t port)
109
+ nsapi_error_t InternetSocket ::bind (uint16_t port)
96
110
{
97
111
// Underlying bind is thread safe
98
112
SocketAddress addr (0 , port);
99
113
return bind (addr);
100
114
}
101
115
102
- nsapi_error_t Socket ::bind (const char *address, uint16_t port)
116
+ nsapi_error_t InternetSocket ::bind (const char *address, uint16_t port)
103
117
{
104
118
// Underlying bind is thread safe
105
119
SocketAddress addr (address, port);
106
120
return bind (addr);
107
121
}
108
122
109
- nsapi_error_t Socket ::bind (const SocketAddress &address)
123
+ nsapi_error_t InternetSocket ::bind (const SocketAddress &address)
110
124
{
111
125
_lock.lock ();
112
126
nsapi_error_t ret;
@@ -121,13 +135,13 @@ nsapi_error_t Socket::bind(const SocketAddress &address)
121
135
return ret;
122
136
}
123
137
124
- void Socket ::set_blocking (bool blocking)
138
+ void InternetSocket ::set_blocking (bool blocking)
125
139
{
126
- // Socket ::set_timeout is thread safe
140
+ // InternetSocket ::set_timeout is thread safe
127
141
set_timeout (blocking ? -1 : 0 );
128
142
}
129
143
130
- void Socket ::set_timeout (int timeout)
144
+ void InternetSocket ::set_timeout (int timeout)
131
145
{
132
146
_lock.lock ();
133
147
@@ -140,7 +154,7 @@ void Socket::set_timeout(int timeout)
140
154
_lock.unlock ();
141
155
}
142
156
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)
144
158
{
145
159
_lock.lock ();
146
160
nsapi_error_t ret;
@@ -155,7 +169,7 @@ nsapi_error_t Socket::setsockopt(int level, int optname, const void *optval, uns
155
169
return ret;
156
170
}
157
171
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)
159
173
{
160
174
_lock.lock ();
161
175
nsapi_error_t ret;
@@ -170,15 +184,24 @@ nsapi_error_t Socket::getsockopt(int level, int optname, void *optval, unsigned
170
184
return ret;
171
185
172
186
}
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
+ }
173
196
174
- void Socket ::sigio (Callback<void ()> callback)
197
+ void InternetSocket ::sigio (Callback<void ()> callback)
175
198
{
176
199
_lock.lock ();
177
200
_callback = callback;
178
201
_lock.unlock ();
179
202
}
180
203
181
- void Socket ::attach (Callback<void ()> callback)
204
+ void InternetSocket ::attach (Callback<void ()> callback)
182
205
{
183
206
sigio (callback);
184
207
}
0 commit comments