1313#include < NetAPI.h>
1414#include < debug.hh>
1515#include < endianness.hh>
16- #include < function_wrapper.hh>
1716#include < limits>
1817#include < locks.hh>
1918#include < platform-ethernet.hh>
@@ -108,7 +107,8 @@ namespace
108107 * stack.
109108 */
110109 template <bool IsCloseOperation = false >
111- int with_sealed_socket (auto operation, Sealed<SealedSocket> sealedSocket)
110+ int with_sealed_socket (FunctionWrapper<int (SealedSocket *socket)> operation,
111+ Sealed<SealedSocket> sealedSocket)
112112 {
113113 return with_restarting_checks (
114114 [&]() {
@@ -145,8 +145,8 @@ namespace
145145 * (which must not try to release the lock after the lock has been
146146 * deallocated).
147147 */
148- int with_sealed_socket (Timeout *timeout,
149- auto operation,
148+ int with_sealed_socket (Timeout *timeout,
149+ FunctionWrapper< int (SealedSocket *socket)> operation,
150150 Sealed<SealedSocket> sealedSocket)
151151 {
152152 return with_sealed_socket (
@@ -248,10 +248,10 @@ namespace
248248 * timeout on the socket (for the direction identified by `directionFlag`),
249249 * calls `fn`, and then updates the timeout with the number of ticks taken.
250250 */
251- auto with_freertos_timeout (Timeout *timeout,
252- FreeRTOS_Socket_t *socket,
253- auto directionFlag,
254- auto && fn)
251+ auto with_freertos_timeout (Timeout *timeout,
252+ FreeRTOS_Socket_t *socket,
253+ auto directionFlag,
254+ FunctionWrapper< int ( void )> fn)
255255 {
256256 auto startTick = thread_systemtick_get ();
257257 TickType_t remaining = timeout->remaining ;
@@ -468,8 +468,9 @@ Socket network_socket_create_and_bind(Timeout *timeout,
468468 bool isListening,
469469 uint16_t maxConnections)
470470{
471- return with_restarting_checks (
472- [&]() -> Socket {
471+ Socket ret = nullptr ;
472+ with_restarting_checks (
473+ [&]() {
473474 // TODO: This should have nice RAII wrappers!
474475 // Add the socket lock to the linked list and make sure that the RAII
475476 // wrapper removes it from there.
@@ -478,7 +479,7 @@ Socket network_socket_create_and_bind(Timeout *timeout,
478479 if (socketWrapper == nullptr )
479480 {
480481 Debug::log (" Failed to allocate socket wrapper." );
481- return nullptr ;
482+ return -ENOMEM ;
482483 }
483484
484485 // Set the socket epoch
@@ -498,7 +499,7 @@ Socket network_socket_create_and_bind(Timeout *timeout,
498499 // allocated the token with this malloc capability. Same for
499500 // other calls to `token_obj_destroy` in this function.
500501 token_obj_destroy (mallocCapability, socket_key (), sealedSocket);
501- return nullptr ;
502+ return -ENOMEM ;
502503 }
503504 socketWrapper->socket = socket;
504505
@@ -518,7 +519,7 @@ Socket network_socket_create_and_bind(Timeout *timeout,
518519 close_socket_retry (timeout, socket);
519520 // Cannot fail, see above.
520521 token_obj_destroy (mallocCapability, socket_key (), sealedSocket);
521- return nullptr ;
522+ return -ENOMEM ;
522523 }
523524
524525 // Acquire the lock until we complete the bind to ensure that
@@ -550,7 +551,7 @@ Socket network_socket_create_and_bind(Timeout *timeout,
550551 close_socket_retry (timeout, socket);
551552 token_obj_destroy (
552553 mallocCapability, socket_key (), sealedSocket);
553- return nullptr ;
554+ return -EAGAIN ;
554555 }
555556
556557 if (isListening)
@@ -578,7 +579,7 @@ Socket network_socket_create_and_bind(Timeout *timeout,
578579 close_socket_retry (timeout, socket);
579580 token_obj_destroy (
580581 mallocCapability, socket_key (), sealedSocket);
581- return nullptr ;
582+ return -EAGAIN ;
582583 }
583584
584585 FreeRTOS_setsockopt (
@@ -595,13 +596,15 @@ Socket network_socket_create_and_bind(Timeout *timeout,
595596 Debug::log (" Failed to add socket to the socket reset list." );
596597 close_socket_retry (timeout, socket);
597598 token_obj_destroy (mallocCapability, socket_key (), sealedSocket);
598- return nullptr ;
599+ return -EAGAIN ;
599600 }
600601
601602 c.release ();
602- return sealedSocket;
603+ ret = sealedSocket;
604+ return 0 ;
603605 },
604- static_cast <Socket>(nullptr ) /* return nullptr if we are restarting */ );
606+ -EAGAIN /* this will be returned if we are restarting */ );
607+ return ret;
605608}
606609
607610Socket network_socket_accept_tcp (Timeout *timeout,
0 commit comments