From 2a040cf7c0cc6415b4bb5152b58d8a98a59d6a81 Mon Sep 17 00:00:00 2001 From: Micael Karlberg Date: Thu, 25 Apr 2019 11:34:50 +0200 Subject: [esock|doc] Document the new asynchronous feature Document the new way to make asynchronous calls to some of the functions: accept, all recv and all send. OTP-15731 --- erts/doc/src/socket.xml | 253 +++++++++++++++++++++++++++++++++++++--- erts/doc/src/socket_usage.xml | 26 ++++- erts/preloaded/ebin/socket.beam | Bin 74584 -> 74628 bytes erts/preloaded/src/socket.erl | 8 +- 4 files changed, 263 insertions(+), 24 deletions(-) diff --git a/erts/doc/src/socket.xml b/erts/doc/src/socket.xml index 9b34bf1df1..3ff0eb4e1b 100644 --- a/erts/doc/src/socket.xml +++ b/erts/doc/src/socket.xml @@ -40,6 +40,20 @@ the functions, e.g. recv/3, has a timeout argument.

+ +

Some functions allow for an asynchronous call. + This is achieved by setting the Timeout argument to + nowait. For instance, if calling the + recv/3 + function with Timeout set to nowait (recv(Sock, 0, nowait)) + when there is actually nothing to read, it will return with + {ok, SelectInfo}. When data eventually arrives a 'select' message + will be sent to the caller: + {'$socket', Sock, select, Info}. The caller can now make another + call to the recv function and now expect data.

+

Note that all other users are locked out until the + 'current user' has called the function (recv in this case).

+

There is currently no support for Windows.

Support for IPv6 has been implemented but not tested.

@@ -64,6 +78,15 @@ accept/1,2.

+ + + + + + + + + @@ -257,7 +280,7 @@ - + Accept a connection on a socket.

Accept a connection on a socket.

@@ -268,6 +291,26 @@
+ + + Accept a connection on a socket. + +

Accept a connection on a socket.

+ +

This call is used with connection-based socket types + (stream or seqpacket). It extracs the first pending + connection request for the listen socket and returns the (newly) + connected socket.

+ +

In the case when there is no connections waiting, the function + will return with the SelectInfo. The caller can then await a + select message, {'$socket', Sock, select, Info} (where + Info is the select_ref() from the SelectInfo), + when a client connects (a subsequent call to accept will then return + the socket).

+
+
+ Bind a name to a socket. @@ -281,6 +324,18 @@ + + + Cancel an asynchronous request. + +

Cancel an asynchronous request.

+ +

Call this function in order to cancel a previous + asynchronous call to, e.g. + recv/3.

+
+
+ Close a socket. @@ -406,48 +461,136 @@ - - + + + Receive a message from a socket. + +

Receive a message from a socket.

+

There is a special case for the argument Length. + If it is set to zero (0), it means "give me everything you + currently have".

+
+
+ + + + Receive a message from a socket.

Receive a message from a socket.

+

There is a special case for the argument Length. If it is set to zero (0), it means "give me everything you currently have".

+ +

In the case when there is no data waiting, the function + will return with the SelectInfo. The caller can then await a + select message, {'$socket', Sock, select, Info} (where + Info is the select_ref() from the SelectInfo), + when data has arrived (a subsequent call to recv will then return + the data).

+

Note that if a length (> 0) is specified, and only part + of that amount of data is available, the function will return with + that data and the SelectInfo (if the caller don't + want to wait for the remaining data, it must immediately call + the cancel/2 function.)

- - + + Receive a message from a socket.

Receive a message from a socket.

This function reads "messages", which means that regardless of - how much we want to read, it returns when we get a message.

+ how much we want to read, it returns when we get a message + (if the buffer size is to small, the message will be truncated).

+

The BufSz argument basically defines the size of the + receive buffer. By setting the value to zero (0), the configured + size (setopt with Level = otp and + Key = rcvbuf) is used.

+

It may be impossible to know what (buffer) size is appropriate + "in advance", and in those cases it may be convenient to use the + (recv) 'peek' flag. When this flag is provided, the message is *not* + "consumed" from the underlying buffers, so another recvfrom call + is needed, possibly with a then adjusted buffer size.

+
+
+ + + + + + Receive a message from a socket. + +

Receive a message from a socket.

+

This function reads "messages", which means that regardless of + how much we want to read, it returns when we get a message + (if the buffer size is to small, the message will be truncated).

The BufSz argument basically defines the size of the receive buffer. By setting the value to zero (0), the configured - size (setopt with Level = otp and Key = rcvbuf) - is used.

+ size (setopt with Level = otp and + Key = rcvbuf) is used.

It may be impossible to know what (buffer) size is appropriate "in advance", and in those cases it may be convenient to use the (recv) 'peek' flag. When this flag is provided, the message is *not* "consumed" from the underlying buffers, so another recvfrom call is needed, possibly with a then adjusted buffer size.

+ +

In the case when there is no data waiting, the function + will return with the SelectInfo. The caller can then await a + select message, {'$socket', Sock, select, Info} (where + Info is the select_ref() from the SelectInfo), + when data has arrived (a subsequent call to recvfrom will then return + the data).

- - + - + + + Receive a message from a socket. + +

Receive a message from a socket.

+

This function reads "messages", which means that regardless of + how much we want to read, it returns when we get a message.

+

The message will be delivered in the form of a msghdr(), + which may contain the source address (if socket not connected), + a list of cmsghdr_recv() (depends on what socket options have + been set and what the protocol and platform supports) and + also a set of flags, providing further info about the read.

+ +

The BufSz argument basically defines the size of the + receive buffer. By setting the value to zero (0), the configured + size (setopt with Level = otp and + Key = rcvbuf) is used.

+ +

The CtrlSz argument basically defines the size of the + receive buffer for the control messages. + By setting the value to zero (0), the configured size (setopt + with Level = otp) is used.

+ +

It may be impossible to know what (buffer) size is appropriate + "in advance", and in those cases it may be convenient to use the + (recv) 'peek' flag. When this flag is provided, the message is *not* + "consumed" from the underlying buffers, so another recvmsg call + is needed, possibly with a then adjusted buffer size.

+
+
+ + + + + Receive a message from a socket.

Receive a message from a socket.

@@ -461,8 +604,8 @@

The BufSz argument basically defines the size of the receive buffer. By setting the value to zero (0), the configured - size (setopt with Level = otp and Key = rcvbuf) - is used.

+ size (setopt with Level = otp and + Key = rcvbuf) is used.

The CtrlSz argument basically defines the size of the receive buffer for the control messages. @@ -474,25 +617,75 @@ (recv) 'peek' flag. When this flag is provided, the message is *not* "consumed" from the underlying buffers, so another recvmsg call is needed, possibly with a then adjusted buffer size.

+ +

In the case when there is no data waiting, the function + will return with the SelectInfo. The caller can then await a + select message, {'$socket', Sock, select, Info} (where + Info is the select_ref() from the SelectInfo), + when data has arrived (a subsequent call to recvmsg will then return + the data).

- - + + + Send a message on a socket. + +

Send a message on a connected socket.

+
+
+ + + + Send a message on a socket.

Send a message on a connected socket.

+ +

In the case when there is no room in the (system-) buffers, + the function will return with the SelectInfo. The caller + can then await a select message, {'$socket', Sock, select, Info} + (where Info is the select_ref() from the + SelectInfo), when there is room for more data (a subsequent + call to send will then send the data).

+

Note that if not all the data was sent, the function will return + with the remaining data and the SelectInfo + (if the caller don't + want to wait to be able to send the rest, it should immediately call + the cancel/2 function.)

- - + + + Send a message on a socket. + +

Send a message on a socket. The destination, if needed + (socket not connected) is provided in the MsgHdr, + which also contains the message to send, + The MsgHdr may also contain an list of optional cmsghdr_send() + (depends on what the protocol and platform supports).

+ +

Unlike the send function, + this one sends one message. + This means that if, for whatever reason, its not possible to send the + message in one go, the function will instead return with the + remaining data ({ok, Remaining}). Thereby leaving it + up to the caller to decide what to do (retry with the remaining data + of give up).

+ +
+
+ + + + Send a message on a socket.

Send a message on a socket. The destination, if needed @@ -509,20 +702,42 @@ up to the caller to decide what to do (retry with the remaining data of give up).

+

In the case when there is no room in the (system-) buffers, + the function will return with the SelectInfo. The caller + can then await a select message, {'$socket', Sock, select, Info} + (where Info is the select_ref() from the + SelectInfo), when there is room for more data (a subsequent + call to sendmsg will then send the data).

- - + + Send a message on a socket.

Send a message on a socket, to the specified destination.

+ + + + Send a message on a socket. + +

Send a message on a socket, to the specified destination.

+ +

In the case when there is no room in the (system-) buffers, + the function will return with the SelectInfo. The caller + can then await a select message, {'$socket', Sock, select, Info} + (where Info is the select_ref() from the + SelectInfo), when there is room for more data (a subsequent + call to sendto will then send the data).

+
+
+ diff --git a/erts/doc/src/socket_usage.xml b/erts/doc/src/socket_usage.xml index 4b3872d7e3..3d18bd1af7 100644 --- a/erts/doc/src/socket_usage.xml +++ b/erts/doc/src/socket_usage.xml @@ -34,11 +34,35 @@ Introduction

The socket interface (module) is basically an "thin" layer on top of the OS socket interface. It is assumed that, unless you have special needs, - gen_[tcp|udp|sctp] should be sufficent.

+ gen_[tcp|udp|sctp] should be sufficent (when they become available).

Note that just because we have a documented and described option, it does not mean that the OS supports it. So its recommended that the user reads the platform specific documentation for the option used.

+
+ Asynchronous calls +

Some functions allow for an asynchronous call + (accept/2, + recv/3,4, + recvfrom/3,4, + recvmsg/2,3,5, + send/3,4, + sendmsg/3,4 and + sendto/4,5). + This is achieved by setting the Timeout argument to + nowait. For instance, if calling the + recv/3 + function with Timeout set to nowait (i.e. recv(Sock, 0, nowait)) + when there is actually nothing to read, it will return with + {ok, SelectInfo}. When data eventually arrives a 'select message' + will be sent to the caller: + {'$socket', Sock, select, Info}. The caller can make another + call to the recv function and now expect data.

+

Note that all other users are locked out until the + 'current user' has called the function (recv in this case). So either + immediately call the function or + cancel.

+
diff --git a/erts/preloaded/ebin/socket.beam b/erts/preloaded/ebin/socket.beam index b8983eb8e8..892aab1fa0 100644 Binary files a/erts/preloaded/ebin/socket.beam and b/erts/preloaded/ebin/socket.beam differ diff --git a/erts/preloaded/src/socket.erl b/erts/preloaded/src/socket.erl index f4d8f175f8..59bef4b3b7 100644 --- a/erts/preloaded/src/socket.erl +++ b/erts/preloaded/src/socket.erl @@ -1359,7 +1359,7 @@ send(Socket, Data) -> Data :: iodata(), Flags :: send_flags(), Reason :: term() - ; (Socket, Data, nowait) -> ok | + ; (Socket, Data, Timeout :: nowait) -> ok | {ok, SelectInfo} | {ok, {RestData, SelectInfo}} | {error, Reason} when @@ -1488,7 +1488,7 @@ sendto(Socket, Data, Dest) -> Dest :: null | sockaddr(), Flags :: send_flags(), Reason :: term() - ; (Socket, Data, Dest, nowait) -> ok | + ; (Socket, Data, Dest, Timeout :: nowait) -> ok | {ok, SelectInfo} | {error, Reason} when Socket :: socket(), @@ -1630,7 +1630,7 @@ sendmsg(Socket, MsgHdr) -> MsgHdr :: msghdr(), Flags :: send_flags(), Reason :: term() - ; (Socket, MsgHdr, nowait) -> ok | + ; (Socket, MsgHdr, Timeout :: nowait) -> ok | {ok, SelectInfo} | {error, Reason} when Socket :: socket(), @@ -2162,7 +2162,7 @@ recvmsg(Socket) -> Flags :: recv_flags(), MsgHdr :: msghdr(), Reason :: term() - ; (Socket, nowait) -> {ok, MsgHdr} | + ; (Socket, Timeout :: nowait) -> {ok, MsgHdr} | {ok, SelectInfo} | {error, Reason} when Socket :: socket(), -- cgit v1.2.3