ReceiveAndParse (Socket function): Difference between revisions

From m204wiki
Jump to navigation Jump to search
m (Automatically generated page update)
 
Line 1: Line 1:
{{Template:Socket:ReceiveAndParse subtitle}}


<span class="pageSubtitle"><section begin=dpl_desc/><section end=dpl_desc/></span>
[[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
is not known beforehand but which is terminated by a known string or by
one of a known set of strings.
The <var>ReceiveAndParse</var> function has an effect similar to <var>[[$Sock_Recvprs]]</var>.
==Syntax==
==Syntax==
{{Template:Socket:ReceiveAndParse syntax}}
<p class="syntax">%len = %socko:ReceiveAndParse(%target, [maxrecvp], [%prsindx], [opts])
</p>
 
===Syntax terms===
===Syntax terms===
<table class="syntaxTable">
<table class="syntaxTable">
<tr><th>%BytesReceived</th><td>number</td></tr>
<tr><th>%len</th>
<tr><th>socket</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>Socket object</td></tr>
</td></tr>
<tr><th><var>Target</var></th>
<tr><th>%socko</th>
<td>string</td></tr>
<td>A variable or an expression that is a reference to a <var>Socket</var> object.
<tr><th><var>MaxBytes</var></th>
</td></tr>
<td>number</td></tr>
<tr><th>%target</th>
<tr><th><var>ParseTokenIndex</var></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.
<td>number</td></tr>
</td></tr>
<tr><th><var>Options</var></th>
<tr><th>maxrecvp</th>
<td>string</td></tr>
<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.
</table>
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.
<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.
</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>.
</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.
</td></tr>
<tr><th>PRSTOK [AMBIG|]<i>hexstr</i>[|<i>hexstr</i>]...
</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.
</td></tr></table>
 
</td></tr></table>
 
==Usage notes==
==Usage notes==
==Examples==
<ul>
==See also==
<li><var>ReceiveAndParse</var> return values:
{{Template:Socket:ReceiveAndParse footer}}
<ul>
<li><var>ReceiveAndParse</var> returns the value -1 if the socket is not open
and <code>ONRESET CONTINUE</code> is in effect for the socket.
<li><var>ReceiveAndParse</var> returns a number greater than 0
which is the number of bytes received from of the socket stream.
[[#Lengths: maximum, truncation, RECVLIM|"Lengths: maximum, truncation, RECVLIM"]]
explains the use of the term
<code>number of bytes received</code>, and it discusses the <var class="term">maxrecvp</var>
argument and other values affecting the received string.
<li><var>ReceiveAndParse</var> returns zero to indicate there is no data remaining
to be received on the connection, that is, if <var>FIN</var> has been received or
the <var>RECVLIM</var> has been reached.
</ul>
<li>In general, you use <var>ReceiveAndParse</var> 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, <var>Receive</var>, when
the protocol in use establishes the length of a data item before it
appears in the socket stream.
<li>The <var class="term">%prsindx</var> argument is used if there is more than one
alternative in the <var>PRSTOK</var> set, and you need to distinguish which alternative
terminated the received value.
For example, with the input stream of
<code>X'36370D38390D'</code>, the following
expression will store the value <code>2</code> in the variable <code>%prs</code>.
<p class="code">%sk:ReceiveAndParse(%str, , %prs, 'PRSTOK AMBIG|0D0A|0D|0A')
</p>
Note that this example uses ambiguous <var>PRSTOK</var> strings (<code>0D</code> is a prefix of <code>0D0A</code>).
Considerations for this are discussed in [[Sample Janus Sockets programs#Ambiguous PRSTOK strings|"Ambiguous PRSTOK strings"]].
<li>The <var>PRSTOK</var> 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 <code>ESC</code> 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 <code>CR</code> and <code>ESC</code>:
<p class="code">%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
  ...
</p>
</ul>
 
==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 <var>ReceiveAndParse</var> is <code>-1</code>, 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 <var class="product">User Language</var> program.
<p class="code">%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
</p>

Revision as of 20:52, 8 November 2011

<section begin=dpl_desc/><section end=dpl_desc/>

ReceiveAndParse is a member of the 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

%len = %socko:ReceiveAndParse(%target, [maxrecvp], [%prsindx], [opts])

Syntax terms

%len A numeric string to contain the count of the number of bytes received, or to contain 0, if the operation cannot be performed.
%socko A variable or an expression that is a reference to a Socket object.
%target 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 %len value.
maxrecvp 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 maxrecvp is 0 and target is a Longstring, the received data is limited to 2,147,483,647 bytes. The maxrecvp value must not exceed 2,147,483,647. maxrecvp may have the special value -1 to indicate there is no limit to the number of bytes received.

%prsindx This optional argument is a %variable or IMAGE item to store the index number (positive integer) of the parse token found in the received data. %prsindx 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 maxrecvp.
opt An option string, which can contain either of the following:
BINARY 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
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 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 ...

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