Skip to content

Commit 83f65c0

Browse files
gekyc1728p9
authored andcommitted
Add rudimentary support for server side
mirrored from: https://developer.mbed.org/teams/NetworkSocketAPI/code/LWIPInterface/
1 parent 9e415df commit 83f65c0

File tree

1 file changed

+63
-2
lines changed

1 file changed

+63
-2
lines changed

net/LWIPInterface/LWIPInterface.cpp

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ struct lwip_socket {
139139
struct tcp_pcb *tcp;
140140
};
141141

142+
struct tcp_pcb *npcb;
142143
struct pbuf *rx_chain;
143144
Semaphore *sem;
144145

@@ -238,9 +239,25 @@ int LWIPInterface::socket_bind(void *handle, const SocketAddress &address)
238239
return NSAPI_ERROR_DEVICE_ERROR;
239240
}
240241

242+
static err_t tcp_accept_irq(void *arg, struct tcp_pcb *tpcb, err_t err);
243+
241244
int LWIPInterface::socket_listen(void *handle, int backlog)
242245
{
243-
return NSAPI_ERROR_UNSUPPORTED;
246+
struct lwip_socket *s = (struct lwip_socket *)handle;
247+
248+
if (s->tcp->state != LISTEN) {
249+
struct tcp_pcb *server = tcp_listen(s->tcp);
250+
if (!server) {
251+
return NSAPI_ERROR_NO_SOCKET;
252+
}
253+
254+
s->tcp = server;
255+
s->npcb = 0;
256+
}
257+
258+
tcp_arg(s->tcp, s);
259+
tcp_accept(s->tcp, tcp_accept_irq);
260+
return 0;
244261
}
245262

246263
static err_t tcp_sent_irq(void *arg, struct tcp_pcb *tpcb, uint16_t len);
@@ -277,9 +294,53 @@ int LWIPInterface::socket_connect(void *handle, const SocketAddress &addr)
277294
return 0;
278295
}
279296

297+
static err_t tcp_refuse_irq(void *handle, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
298+
{
299+
return ERR_WOULDBLOCK;
300+
}
301+
302+
static err_t tcp_accept_irq(void *handle, struct tcp_pcb *npcb, err_t err)
303+
{
304+
struct lwip_socket *s = (struct lwip_socket *)handle;
305+
if (s->npcb) {
306+
tcp_abort(npcb);
307+
return ERR_ABRT;
308+
}
309+
310+
tcp_recv(npcb, tcp_refuse_irq);
311+
s->npcb = npcb;
312+
313+
if (s->callback) {
314+
s->callback(s->data);
315+
}
316+
317+
return ERR_OK;
318+
}
319+
280320
int LWIPInterface::socket_accept(void **handle, void *server)
281321
{
282-
return NSAPI_ERROR_UNSUPPORTED;
322+
struct lwip_socket *s = (struct lwip_socket *)server;
323+
if (!s->npcb) {
324+
return NSAPI_ERROR_WOULD_BLOCK;
325+
}
326+
327+
struct lwip_socket *ns = new struct lwip_socket;
328+
if (!ns) {
329+
return NSAPI_ERROR_NO_SOCKET;
330+
}
331+
332+
memset(ns, 0, sizeof *ns);
333+
334+
ns->tcp = s->npcb;
335+
s->npcb = 0;
336+
337+
tcp_accepted(ns->tcp);
338+
tcp_arg(ns->tcp, ns);
339+
//tcp_err(ns->tcp, tcp_error_irq);
340+
tcp_sent(ns->tcp, tcp_sent_irq);
341+
tcp_recv(ns->tcp, tcp_recv_irq);
342+
*handle = ns;
343+
return 0;
283344
}
284345

285346
static struct pbuf *pbuf_consume(struct pbuf *p, size_t consume, bool free_partial)

0 commit comments

Comments
 (0)