MCPcopy Create free account
hub / github.com/RT-Thread/rt-thread / alloc_socket

Function alloc_socket

components/net/lwip/lwip-2.1.2/src/api/sockets.c:519–560  ·  view source on GitHub ↗

* Allocate a new socket for a given netconn. * * @param newconn the netconn for which to allocate a socket * @param accepted 1 if socket has been created by accept(), * 0 if socket has been created by socket() * @return the index of the new socket; -1 on error */

Source from the content-addressed store, hash-verified

517 * @return the index of the new socket; -1 on error
518 */
519static int
520alloc_socket(struct netconn *newconn, int accepted)
521{
522 int i;
523 SYS_ARCH_DECL_PROTECT(lev);
524 LWIP_UNUSED_ARG(accepted);
525
526 /* allocate a new socket identifier */
527 for (i = 0; i < NUM_SOCKETS; ++i) {
528 /* Protect socket array */
529 SYS_ARCH_PROTECT(lev);
530 if (!sockets[i].conn) {
531#if LWIP_NETCONN_FULLDUPLEX
532 if (sockets[i].fd_used) {
533 SYS_ARCH_UNPROTECT(lev);
534 continue;
535 }
536 sockets[i].fd_used = 1;
537 sockets[i].fd_free_pending = 0;
538#endif
539 sockets[i].conn = newconn;
540 /* The socket is not yet known to anyone, so no need to protect
541 after having marked it as used. */
542 SYS_ARCH_UNPROTECT(lev);
543 sockets[i].lastdata.pbuf = NULL;
544#if LWIP_SOCKET_SELECT || LWIP_SOCKET_POLL
545 LWIP_ASSERT("sockets[i].select_waiting == 0", sockets[i].select_waiting == 0);
546 sockets[i].rcvevent = 0;
547 /* TCP sendbuf is empty, but the socket is not yet writable until connected
548 * (unless it has been created by accept()). */
549 sockets[i].sendevent = (NETCONNTYPE_GROUP(newconn->type) == NETCONN_TCP ? (accepted != 0) : 1);
550 sockets[i].errevent = 0;
551#endif /* LWIP_SOCKET_SELECT || LWIP_SOCKET_POLL */
552#ifdef SAL_USING_POSIX
553 rt_wqueue_init(&sockets[i].wait_head);
554#endif
555 return i + LWIP_SOCKET_OFFSET;
556 }
557 SYS_ARCH_UNPROTECT(lev);
558 }
559 return -1;
560}
561
562/** Free a socket (under lock)
563 *

Callers 2

lwip_acceptFunction · 0.70
lwip_socketFunction · 0.70

Calls 1

rt_wqueue_initFunction · 0.85

Tested by

no test coverage detected