ReceiveAndParse (Socket function): Difference between revisions

From m204wiki
Jump to navigation Jump to search
m (1 revision)
mNo edit summary
 
(10 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{Template:Socket:ReceiveAndParse subtitle}}


<span class="pageSubtitle"><section begin=dpl_desc/><section end=dpl_desc/></span>
This method receives a string over a <var class="product">Janus Sockets</var> connection.
[[Category:Socket methods|ReceiveAndParse function]]
<p>
<var>ReceiveAndParse</var> is a member of the <var>[[Socket class|Socket]]</var> class.
</p>
 
This method
receives a string over a <var class="product">Janus Sockets</var> connection.
It is designed for a string whose length
It is designed for a string whose length
is not known beforehand but which is terminated by a known string or by
is not known beforehand but which is terminated by a known string or by
one of a known set of strings.
one of a known set of strings.
The <var>ReceiveAndParse</var> function has an effect similar to <var>[[$Sock_Recvprs]]</var>.
The <var>ReceiveAndParse</var> function has an effect similar to <var>[[$Sock_RecvPrs]]</var>.
 
==Syntax==
==Syntax==
<p class="syntax">%len = %socko:ReceiveAndParse(%target, [maxrecvp], [%prsindx], [opts])
{{Template:Socket:ReceiveAndParse syntax}}
</p>


===Syntax terms===
===Syntax terms===
<table class="syntaxTable">
<table class="syntaxTable">
<tr><th>%len</th>
<tr><th>%BytesReceived</th>
<td>A numeric string to contain the count of the number of bytes received, or to contain 0, if the operation cannot be performed.
<td>A numeric string to contain the count of the number of bytes received, or to contain 0, if the operation cannot be performed.
</td></tr>
</td></tr>
<tr><th>%socko</th>
 
<tr><th>socket</th>
<td>A variable or an expression that is a reference to a <var>Socket</var> object.
<td>A variable or an expression that is a reference to a <var>Socket</var> object.
</td></tr>
</td></tr>
<tr><th>%target</th>
 
<td>A %variable or an <var>IMAGE</var> item that is the target of the receive operation. It is referred to as the "receive target." Since you may request that some bytes from the socket stream be discarded, the length of the string stored in this target may be less than the <var class="term">%len</var> value.
<tr><th><var>Target</var></th>
<td>This [[Notation conventions for methods#Named parameters|name allowed]] parameter is a %variable or an <var>IMAGE</var> item that is the target of the receive operation. It is referred to as the "receive target." Since you may request that some bytes from the socket stream be discarded, the length of the string stored in this target may be less than the <var class="term">%BytesReceived</var> value.
</td></tr>
</td></tr>
<tr><th>maxrecvp</th>
 
<td>The maximum number of bytes of data to receive, '''less''' the length of the separator string. This optional argument defaults to 0, which means limit the received data (less separator) to the declared length of the receive target.
<tr><th><var>MaxBytes</var></th>
If <var class="term">maxrecvp</var> is 0 and <var class="term">target</var> is a <var>Longstring</var>, the received data is limited to 2,147,483,647 bytes. The <var class="term">maxrecvp</var> value must not exceed 2,147,483,647.
<td>This optional, name allowed, parameter is the maximum number of bytes of data to receive, '''less''' the length of the separator string. This optional argument defaults to 0, which means limit the received data (less separator) to the declared length of the receive target.
<var class="term">maxrecvp</var> may have the special value <code>-1</code> to indicate there is no limit to the number of bytes received.
If the <var>MaxBytes</var> argument is 0 and the <var>Target</var> argument value is a <var>Longstring</var>, the received data is limited to 2,147,483,647 bytes. The <var>MaxBytes</var> argument value must not exceed 2,147,483,647.
<var>MaxBytes</var> may have the special value <code>-1</code> to indicate there is no limit to the number of bytes received.
</td></tr>
</td></tr>
<tr><th>%prsindx</th>
 
<td>This optional argument is a %variable or <var>IMAGE</var> item to store the index number (positive integer) of the parse token found in the received data. <var class="term">%prsindx</var> is set to 0 if no token is found ''within'' <var>RECVLIM</var> bytes, or if it is not found ''after'' more than the number of data bytes specified by the effective value of <var class="term">maxrecvp</var>.
<tr><th><var>ParseTokenIndex</var></th>
<td>This optional, name allowed, parameter is a %variable or <var>IMAGE</var> item to store the index number (positive integer) of the parse token found in the received data. The <var>ParseTokenIndex</var> value is set to 0 if no token is found ''within'' <var>[[#Lengths: maximum, truncation, RECVLIM|RECVLIM]]</var> bytes, or if it is not found ''after'' more than the number of data bytes specified by the effective value of the <var>MaxBytes</var> argument.
</td></tr>
</td></tr>
<tr><th>opt</th>
 
<td>An option string, which can contain either of the following: <table class="syntaxTable"> <tr><th><var>BINARY</var> </th><td>Regardless of the socket's BINARY or CHAR parameter (see [[??]] refid=binary.), data '''is not translated''' when saved in the receive target. The received string can be translated later in the program using the TranIn method. :Ih1.BINARY key</td></tr> <tr><th><var>CHAR</var> </th><td>Regardless of the socket's BINARY or CHAR parameter, data '''is translated''' when saved in the receive target. The translation is specified by the input table defined by the socket's <var>[[XTAB]]</var> parameter.
<tr><th><var>Options</var></th>
<td>This optional, name allowed, parameter is an option string, which can contain either of the following:  
 
<table class="syntaxTable">  
<tr><th><var>BINARY</var></th>
<td>Regardless of the socket's <var>[[BINARY (JANUS DEFINE parameter)|BINARY]]</var> or <var>[[CHAR (JANUS DEFINE parameter)|CHAR]]</var> port parameter, data '''is not translated''' when saved in the receive target. The received string can be translated later in the program using the <var>[[TranIn (Socket function)|TranIn]]</var> method. </td></tr>  
 
<tr><th><var>CHAR</var> </th><td>Regardless of the socket's <var>BINARY</var> or <var>CHAR</var> parameter, data '''is translated''' when saved in the receive target. The translation is specified by the input table defined by the socket's <var>[[XTAB (JANUS DEFINE parameter)|XTAB]]</var> port parameter.
</td></tr>
</td></tr>
<tr><th>PRSTOK [AMBIG|]<i>hexstr</i>[|<i>hexstr</i>]...
 
<tr><th nowrap><var>PRSTOK [AMBIG|]</var>hexstr<var>[|</var>hexstr<var>]...</var>
</th><td>A set of parse tokens that override the current parse tokens on the socket.
</th><td>A set of parse tokens that override the current parse tokens on the socket.


Whether it is set on the port definition or by the <var>Set</var> or <var>ReceiveAndParse</var> methods, <var>PRSTOK</var> must '''not''' be <code>NONE</code> or <var>ReceiveAndParse</var> will fail.
Whether it is set on the port definition or by the <var>[[Set (Socket function)|Set]]</var> or <var>ReceiveAndParse</var> methods, <var>PRSTOK</var> must '''not''' be <code>NONE</code>, or <var>ReceiveAndParse</var> will fail.
</td></tr></table>
</td></tr></table>
</td></tr></table>
</td></tr></table>


Line 98: Line 103:
</ul>
</ul>


==Lengths: maximum, truncation, RECVLIM==
The <var>ReceiveAndParse</var> function allows you to locate a separator string, and
to store all or part of the string before the separator into the
receive target.
It is important to understand that when we say
"the string that is received in a call to <var>ReceiveAndParse</var>",
it may be more than the string that <var>ReceiveAndParse</var> stores in the receive target.
This is for the following two reasons:
<ol>
<li>The separator string is part of the string received, but it is not
stored in the receive target.
<li>The string before the separator may exceed the size
of the target, and thus can be partially discarded (or "truncated").
</ol>
References to the string received by <var>ReceiveAndParse</var> are to the
string stored in the target plus any subsequent bytes that are discarded,
plus the separator string.
There are several values that control the length of the string received.
They will be discussed in this section, using the following terms:
<table class="syntaxTable">
<tr><th><var>RECVLIM</var></th>
<td>This specifies the number of bytes remaining in the "receive window".
A value of zero means there is no limit to the window.
The purpose of this window is to allow you to limit a series of receive
operations (using, if desired, a mixture of <var>Receive</var> and <var>ReceiveAndParse</var>)
to a predetermined total number of bytes.
<var>RECVLIM</var> can be set in a <var>[[Set (Socket function)|Set]]</var> call, and subsequent receive operations
decrement from <var>RECVLIM</var> the number of bytes received, until it reaches
zero, at which point a <var>Receive</var> call will return a 0 just as if a <code>FIN</code>
had been received.
Another <var>Receive</var> call after one returns a 0 will result in request
cancellation unless <var>RECVLIM</var> is reset.
When <var>RECVLIM</var> is greater than zero, no receive operation will use any
bytes beyond the window.
A good example of the use of <var>RECVLIM</var> is obtaining a web page, whose length
is (usually) specified in the <code>Content-length</code> field of the HTTP response header. </td></tr>
<tr><th>len_to_end</th>
<td>When a receive operation is performed, the received string is
of course limited to the total remaining bytes in the stream that the
remote end sent before issuing a close operation.
Although this value is not known ahead of time, when describing the
operation of receive we use this term to indicate various cases. </td></tr>
<tr><th>max_recvp</th>
<td>This corresponds to the <var>Maxbytes</var> argument of the <var>ReceiveAndParse</var> function call.
It needs to be specified only if you wish to allow truncation,
or if you wish to limit the string stored in the received
target to less than the declared the size of the target.
You can specify that an unlimited number of bytes can be discarded
by specifying -1 as the <var>Maxbytes</var> argument, making the effective value "infinite".
The number of bytes received by <var>ReceiveAndParse</var> will not exceed the
effective value of this argument plus the length of the separator string.
The effective value of <var class="term">max_recvp</var>, if <var>Maxbytes</var> is 0 (its default) or omitted,
is <var class="term">targ_size</var>.
'''Truncation''' can only occur if <var class="term">max_recvp</var> > <var class="term">targ_size</var>, which
cannot happen if the target is a <var>Longstring</var>.
If a separator string is not found due to a limit imposed by either <var>RECVLIM</var>,
<var class="term">len_to_end</var>, or <var class="term">max_recvp</var>, then zero is stored in the parse index target
(the <var>ParseTokenIndex</var> argument).
</td></tr>
<tr><th>targ_size</th>
<td>The size of the target is used in calculating various defaults
of the other values here, and it also obviously limits the number of
bytes stored in the target and so affects the number of bytes discarded. </td></tr>
<tr><th>discard_len</th>
<td>This is the number of bytes received, minus the length of the
separator string, minus <var class="term">targ_size</var>.
It will always be zero, unless
<var class="term">max_recvp</var> is greater than <var class="term">targ_size</var> </td></tr>
</table>
==Example==
==Example==


Line 117: Line 198:
%rc = %sock:Close
%rc = %sock:Close
</p>
</p>
==See also==
<ul>
<li><var>[[Receive (Socket function)|Receive]]</var>
<li><var>[[ReceiveAsynchronous (Socket function)|ReceiveAsynchronous]]</var>
</ul>
{{Template:Socket:ReceiveAndParse footer}}

Latest revision as of 20:43, 23 September 2014

Receive bytes up to a given string (Socket class)


This method receives a string over a Janus Sockets connection. It is designed for a string whose length is not known beforehand but which is terminated by a known string or by one of a known set of strings. The ReceiveAndParse function has an effect similar to $Sock_RecvPrs.

Syntax

%bytesReceived = socket:ReceiveAndParse( [Target=] string, - [[MaxBytes=] number], - [[ParseTokenIndex=] number], - [[Options=] string])

Syntax terms

%BytesReceived A numeric string to contain the count of the number of bytes received, or to contain 0, if the operation cannot be performed.
socket A variable or an expression that is a reference to a Socket object.
Target This name allowed parameter is a %variable or an IMAGE item that is the target of the receive operation. It is referred to as the "receive target." Since you may request that some bytes from the socket stream be discarded, the length of the string stored in this target may be less than the %BytesReceived value.
MaxBytes This optional, name allowed, parameter is the maximum number of bytes of data to receive, less the length of the separator string. This optional argument defaults to 0, which means limit the received data (less separator) to the declared length of the receive target.

If the MaxBytes argument is 0 and the Target argument value is a Longstring, the received data is limited to 2,147,483,647 bytes. The MaxBytes argument value must not exceed 2,147,483,647. MaxBytes may have the special value -1 to indicate there is no limit to the number of bytes received.

ParseTokenIndex This optional, name allowed, parameter is a %variable or IMAGE item to store the index number (positive integer) of the parse token found in the received data. The ParseTokenIndex value is set to 0 if no token is found within RECVLIM bytes, or if it is not found after more than the number of data bytes specified by the effective value of the MaxBytes argument.
Options This optional, name allowed, parameter is an option string, which can contain either of the following:
BINARY Regardless of the socket's BINARY or CHAR port parameter, data is not translated when saved in the receive target. The received string can be translated later in the program using the TranIn method.
CHAR Regardless of the socket's BINARY or CHAR parameter, data is translated when saved in the receive target. The translation is specified by the input table defined by the socket's XTAB port parameter.
PRSTOK [AMBIG|]hexstr[|hexstr]... A set of parse tokens that override the current parse tokens on the socket.

Whether it is set on the port definition or by the Set or ReceiveAndParse methods, PRSTOK must not be NONE, or ReceiveAndParse will fail.

Usage notes

  • ReceiveAndParse return values:
    • ReceiveAndParse returns the value -1 if the socket is not open and ONRESET CONTINUE is in effect for the socket.
    • ReceiveAndParse returns a number greater than 0 which is the number of bytes received from of the socket stream. "Lengths: maximum, truncation, RECVLIM" explains the use of the term number of bytes received, and it discusses the maxrecvp argument and other values affecting the received string.
    • ReceiveAndParse returns zero to indicate there is no data remaining to be received on the connection, that is, if FIN has been received or the RECVLIM has been reached.
  • In general, you use ReceiveAndParse for a data item whose length is unknown beforehand, but rather is terminated by a known string (or one of a known set of strings). You use the alternate receive operation, Receive, when the protocol in use establishes the length of a data item before it appears in the socket stream.
  • The %prsindx argument is used if there is more than one alternative in the PRSTOK set, and you need to distinguish which alternative terminated the received value. For example, with the input stream of X'36370D38390D', the following expression will store the value 2 in the variable %prs.

    %sk:ReceiveAndParse(%str, , %prs, 'PRSTOK AMBIG|0D0A|0D|0A')

    Note that this example uses ambiguous PRSTOK strings (0D is a prefix of 0D0A). Considerations for this are discussed in "Ambiguous PRSTOK strings".

  • The PRSTOK separators are chosen to match the protocol and data streams that are used with your application. One of the considerations, of course, is that a separator should not occur in the "normal" data received by your application. If that is unavoidable, you will need to use an "escaping" mechanism; for example, you could use the ASCII ESC character (hexadecimal 1B) to indicate that it precedes a character that is to be taken literally. For example, if the data contains non-separator instances of CR and ESC:

    %junk string len 1 %sok:Set('PRSTOK', '0D|1B') Repeat %r = %sok:ReceiveAndParse(%targ, %sep) If %sep eq 2 then %r = %sok:Receive(%junk) %targ = %targ With %junk End If ...

Lengths: maximum, truncation, RECVLIM

The ReceiveAndParse function allows you to locate a separator string, and to store all or part of the string before the separator into the receive target. It is important to understand that when we say "the string that is received in a call to ReceiveAndParse", it may be more than the string that ReceiveAndParse stores in the receive target. This is for the following two reasons:

  1. The separator string is part of the string received, but it is not stored in the receive target.
  2. The string before the separator may exceed the size of the target, and thus can be partially discarded (or "truncated").

References to the string received by ReceiveAndParse are to the string stored in the target plus any subsequent bytes that are discarded, plus the separator string.

There are several values that control the length of the string received. They will be discussed in this section, using the following terms:

RECVLIM This specifies the number of bytes remaining in the "receive window".

A value of zero means there is no limit to the window. The purpose of this window is to allow you to limit a series of receive operations (using, if desired, a mixture of Receive and ReceiveAndParse) to a predetermined total number of bytes. RECVLIM can be set in a Set call, and subsequent receive operations decrement from RECVLIM the number of bytes received, until it reaches zero, at which point a Receive call will return a 0 just as if a FIN had been received. Another Receive call after one returns a 0 will result in request cancellation unless RECVLIM is reset. When RECVLIM is greater than zero, no receive operation will use any bytes beyond the window.

A good example of the use of RECVLIM is obtaining a web page, whose length

is (usually) specified in the Content-length field of the HTTP response header.
len_to_end When a receive operation is performed, the received string is

of course limited to the total remaining bytes in the stream that the remote end sent before issuing a close operation. Although this value is not known ahead of time, when describing the

operation of receive we use this term to indicate various cases.
max_recvp This corresponds to the Maxbytes argument of the ReceiveAndParse function call.

It needs to be specified only if you wish to allow truncation, or if you wish to limit the string stored in the received target to less than the declared the size of the target. You can specify that an unlimited number of bytes can be discarded by specifying -1 as the Maxbytes argument, making the effective value "infinite".

The number of bytes received by ReceiveAndParse will not exceed the effective value of this argument plus the length of the separator string. The effective value of max_recvp, if Maxbytes is 0 (its default) or omitted, is targ_size.

Truncation can only occur if max_recvp > targ_size, which cannot happen if the target is a Longstring.

If a separator string is not found due to a limit imposed by either RECVLIM, len_to_end, or max_recvp, then zero is stored in the parse index target (the ParseTokenIndex argument).

targ_size The size of the target is used in calculating various defaults

of the other values here, and it also obviously limits the number of

bytes stored in the target and so affects the number of bytes discarded.
discard_len This is the number of bytes received, minus the length of the

separator string, minus targ_size. It will always be zero, unless

max_recvp is greater than targ_size

Example

The following example shows part of a program that communicates with a remote web server and which parses and prints the returned HTML stream. Note that the second argument to ReceiveAndParse is -1, which means that there is no limit to the length of each line parsed, hence no limit to the number of bytes discarded at the end of the line: only the first 78 bytes (the size of %s) of each line of HTML is examined by the User Language program.

%s = %sock:Set('PRSTOK', '0D0A|0A|0D') Repeat %rc = %sock:ReceiveAndParse(%s, -1) Print %s If %rc le 0 then Loop End End If End Repeat %rc = %sock:Close

See also