Skip to content

NEP21 Network library change

Greg Hewgill edited this page Nov 1, 2020 · 1 revision

This proposes a change to the Socket.recv() function in the net library.

Motivation

Currently, the function prototype is:

FUNCTION Socket.recv(self: Socket, count: Number): Bytes

When using nonblocking sockets, received data is signaled through the select() function. Whenever new data arrives, the select() function indicates that the socket is readable, and the Socket.recv() function will read a nonzero number of bytes. If the Socket.recv() function returns zero bytes after select() has signaled the socket as readable, this means the socket has closed.

It is desirable to change the interface to allow for more flexible options in socket implementation. For example, when using TLS over TCP, the socket may be signaled as readable but a call to recv() would not return any actual bytes (because the bytes ready to read might only be meaningful to the TLS layer).

Proposal

Change the function prototype to:

FUNCTION Socket.recv(self: Socket, count: Number, OUT buffer: Bytes): Boolean

With this change, the boolean return value of the function would indicate whether the socket has been closed or not (FALSE to indicate that the socket has been closed). So this gives implementations the option of returning immediately, even for a blocking socket, with a TRUE result to indicate the socket has not yet been closed, and an empty buffer.