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 ++++- 2 files changed, 259 insertions(+), 20 deletions(-) (limited to 'erts/doc') 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.

+
-- cgit v1.2.3 From 043cb47a1967a63b316c7bd4cb4232f2e9b0f895 Mon Sep 17 00:00:00 2001 From: Micael Karlberg Date: Tue, 30 Apr 2019 15:06:15 +0200 Subject: [esock|doc] Add text about the abort message If another process closes the socket, the caller will receive an abort message instead (of the select message). OTP-15731 --- erts/doc/src/socket.xml | 16 ++++++++++++++-- erts/doc/src/socket_usage.xml | 13 ++++++++++++- 2 files changed, 26 insertions(+), 3 deletions(-) (limited to 'erts/doc') diff --git a/erts/doc/src/socket.xml b/erts/doc/src/socket.xml index 3ff0eb4e1b..f9dd9575f4 100644 --- a/erts/doc/src/socket.xml +++ b/erts/doc/src/socket.xml @@ -44,15 +44,27 @@

Some functions allow for an asynchronous call. This is achieved by setting the Timeout argument to nowait. For instance, if calling the - recv/3 + 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 + + {'$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).

+

Another message the user must be prepared for (when making asynchronous + calls) is the abort message: + + {'$socket', Sock, abort, Reason} + + This message indicates + that the (asynchronous) operation has been aborted. + If, for instance, the socket has been + closed (by another process), Reason will be {Ref, closed}.

There is currently no support for Windows.

diff --git a/erts/doc/src/socket_usage.xml b/erts/doc/src/socket_usage.xml index 3d18bd1af7..fcfe9b54ec 100644 --- a/erts/doc/src/socket_usage.xml +++ b/erts/doc/src/socket_usage.xml @@ -56,8 +56,19 @@ 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 + + {'$socket', Sock, select, Info} + + The caller can then make another call to the recv function and now expect data.

+

The user must also be prepared to receive an abort message: + + {'$socket', Sock, abort, Reason} + + If the operation is aborted + for whatever reason (e.g. if the socket is closed "by someone else"). + The Reason part contains the abort reason (in case the socket + has been closed Reason = {Ref, closed}).

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 -- cgit v1.2.3 From 64c4668a65195283c7dbc24a677f8d3d46ac5513 Mon Sep 17 00:00:00 2001 From: Micael Karlberg Date: Thu, 2 May 2019 15:08:00 +0200 Subject: [esock|doc] Update with regard to the new select returns --- erts/doc/src/socket.xml | 30 ++++++++++++++++----- erts/doc/src/socket_usage.xml | 63 +++++++++++++++++++++++++++++++++++++------ 2 files changed, 78 insertions(+), 15 deletions(-) (limited to 'erts/doc') diff --git a/erts/doc/src/socket.xml b/erts/doc/src/socket.xml index f9dd9575f4..48e91e93a7 100644 --- a/erts/doc/src/socket.xml +++ b/erts/doc/src/socket.xml @@ -47,24 +47,40 @@ 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: + {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).

Another message the user must be prepared for (when making asynchronous - calls) is the abort message: + calls) is the abort message:

+ + + + + {'$socket', Sock, abort, Info} + +

This message indicates that the (asynchronous) operation has been aborted. If, for instance, the socket has been - closed (by another process), Reason will be {Ref, closed}.

+ closed (by another process), Info will be {Ref, closed}.

There is currently no support for Windows.

diff --git a/erts/doc/src/socket_usage.xml b/erts/doc/src/socket_usage.xml index fcfe9b54ec..9d8141b822 100644 --- a/erts/doc/src/socket_usage.xml +++ b/erts/doc/src/socket_usage.xml @@ -54,25 +54,72 @@ 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: + {ok, + SelectInfo}. + When data eventually arrives a 'select message' + will be sent to the caller:

+ + + + + {'$socket', Sock, select, Info} + +

The caller can then make another call to the recv function and now expect data.

-

The user must also be prepared to receive an abort message: +

The user must also be prepared to receive an abort message:

+ + + + + {'$socket', Sock, abort, Info} + +

If the operation is aborted for whatever reason (e.g. if the socket is closed "by someone else"). - The Reason part contains the abort reason (in case the socket - has been closed Reason = {Ref, closed}).

+ The Info part contains the abort reason (in this case that + the socket has been closed Info = {Ref, closed}).

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.

+

The general form of the 'socket' message is:

+ + + + + {'$socket', Sock :: socket(), Tag :: atom(), Info :: term()} + +

Where the format of Info is a function of Tag:

+ + + Tag + Info value type + + + select + reference() + + + abort + {reference(), Reason :: term()} + + socket message info value type +
+

The reference() is the same as was received in the + SelectInfo.

-- cgit v1.2.3 From 88a1140f46b47e8a3526a7239f343ca344e9a3e1 Mon Sep 17 00:00:00 2001 From: Micael Karlberg Date: Mon, 6 May 2019 10:44:07 +0200 Subject: [esock|doc] Make select_ref opaque OTP-15731 --- erts/doc/src/socket.xml | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) (limited to 'erts/doc') diff --git a/erts/doc/src/socket.xml b/erts/doc/src/socket.xml index 48e91e93a7..47e84090ee 100644 --- a/erts/doc/src/socket.xml +++ b/erts/doc/src/socket.xml @@ -51,15 +51,10 @@ SelectInfo}. When data eventually arrives a 'select' message will be sent to the caller:

- - {'$socket', Sock, select, Info} + {'$socket', socket(), select, select_ref()}

The caller can now make another call to the recv function and now expect data.

@@ -67,20 +62,15 @@ 'current user' has called the function (recv in this case).

Another message the user must be prepared for (when making asynchronous calls) is the abort message:

- - {'$socket', Sock, abort, Info} + {'$socket', socket(), abort, Info}

This message indicates that the (asynchronous) operation has been aborted. - If, for instance, the socket has been - closed (by another process), Info will be {Ref, closed}.

+ If, for instance, the socket has been closed (by another process), + Info will be {select_ref(), closed}.

There is currently no support for Windows.

@@ -111,6 +101,8 @@ +

A reference that uniquely identifies the (select) operation.

+
-- cgit v1.2.3 From d6dddf8ec963d34c91ec92b4772ff34238f2c718 Mon Sep 17 00:00:00 2001 From: Micael Karlberg Date: Mon, 6 May 2019 12:19:41 +0200 Subject: [esock] Connect can now also take a nowait Timeout Had forgot about the connect function. But it can now also handle the Timeout = nowait, maybe resulting in a select. Required some nif work also... OTP-15731 --- erts/doc/src/socket.xml | 37 +++++++++++++++++++++++++++++-------- erts/doc/src/socket_usage.xml | 29 ++++++++++------------------- 2 files changed, 39 insertions(+), 27 deletions(-) (limited to 'erts/doc') diff --git a/erts/doc/src/socket.xml b/erts/doc/src/socket.xml index 47e84090ee..e5cf77663a 100644 --- a/erts/doc/src/socket.xml +++ b/erts/doc/src/socket.xml @@ -324,7 +324,7 @@

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 + select message, {'$socket', Socket, 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).

@@ -377,7 +377,7 @@ - + Initiate a connection on a socket.

This function connects the socket to the address @@ -385,6 +385,24 @@ + + + Initiate a connection on a socket. + +

This function connects the socket to the address + specied by the SockAddr argument.

+ +

In the case when its not possible to immediately establish a + connection, the function will return with the + SelectInfo. + The caller can then await a + select message, {'$socket', Socket, select, Info} (where + Info is the select_ref() from the SelectInfo, + a subsequent call to connect will then + establish the connection).

+
+
+ @@ -505,7 +523,7 @@

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 + select message, {'$socket', Socket, 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).

@@ -564,7 +582,7 @@

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 + select message, {'$socket', Socket, 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).

@@ -640,7 +658,7 @@

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 + select message, {'$socket', Socket, 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).

@@ -667,7 +685,8 @@

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} + can then await a select message, + {'$socket', Socket, 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).

@@ -724,7 +743,8 @@

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} + can then await a select message, + {'$socket', Socket, 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).

@@ -751,7 +771,8 @@

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} + can then await a select message, + {'$socket', Socket, 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 9d8141b822..7e65bcbf70 100644 --- a/erts/doc/src/socket_usage.xml +++ b/erts/doc/src/socket_usage.xml @@ -43,6 +43,7 @@ Asynchronous calls

Some functions allow for an asynchronous call (accept/2, + connect/3, recv/3,4, recvfrom/3,4, recvmsg/2,3,5, @@ -52,21 +53,17 @@ 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)) + 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} + {'$socket', socket(), select, select_ref()}

The caller can then make another call to the recv function and now expect data.

@@ -79,24 +76,18 @@ - {'$socket', Sock, abort, Info} + {'$socket', socket(), abort, Info}

If the operation is aborted for whatever reason (e.g. if the socket is closed "by someone else"). The Info part contains the abort reason (in this case that - the socket has been closed Info = {Ref, closed}).

+ the socket has been closed Info = {select_ref(), closed}).

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.

+

The general form of the 'socket' message is:

- @@ -110,15 +101,15 @@ select - reference() + select_ref() abort - {reference(), Reason :: term()} + {select_ref(), Reason :: term()} socket message info value type -

The reference() is the same as was received in the +

The select_ref() is the same as was received in the SelectInfo.

-- cgit v1.2.3 From 6ede678f9e7a2ba1b15c276e0e401d7990e88c51 Mon Sep 17 00:00:00 2001 From: Micael Karlberg Date: Mon, 6 May 2019 18:21:38 +0200 Subject: [esock] Types and cleanup Changed tag type tpo opaque. Also added connect (monitor and process) cleanup. OTP-15731 --- erts/doc/src/socket.xml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'erts/doc') diff --git a/erts/doc/src/socket.xml b/erts/doc/src/socket.xml index e5cf77663a..b4e22e86a8 100644 --- a/erts/doc/src/socket.xml +++ b/erts/doc/src/socket.xml @@ -98,10 +98,14 @@
+ +

A tag that describes the (select) operation.

+
-

A reference that uniquely identifies the (select) operation.

+ +

A reference that uniquely identifies the (select) operation.

-- cgit v1.2.3