Sending Inband Replies
Correctly Generating an Inband Reply
Inband replies are sent from the RPL buffer. Unlike the process of sending "regular" TX data that requires you to use the sock.setdata and sock.send methods, the process of sending an inband reply only takes one step. You set and send (commit) the data with a single method: sock.setsendinband.
The sock.setsendinband method puts the data into the RPL buffer and immediately commits it for sending. The socket does not add necessary incapsulation automatically: it is the responsibility of your application to add the escape character, some other character after the escape, and the end character.
Your inband reply will only be stored into the RPL buffer if the latter has enough space to store your entire message. If there is not enough free space, then nothing will be stored. This is different from the TX buffer, for which whatever can fit is stored. You can check the free space in the RPL buffer by using the sock.rplfree read-only property. The amount of unsent data in the RPL buffer can be checked through the sock.rpllen read-only property.
You must not split your inband reply — it must be placed in the RPL buffer with a single invocation of sock.setsendinband. In the example below, we reply "OK" to each inband command we receive:
sub on_sock_inband
dim s as string 'we will keep the data from the CMD buffer here
dim s2 as string 'this will keep individual inband commands
dim x as byte
s=sock.getinband 'we get entire CMD buffer contents into the s
x=instr(1,s,chr(sock.endchar))
while x<>0
s2=left(s,x-1) 's2 now contains a single inband command
s=right(s,len(s)-x) 'cut out this command
'reply back with OK
sock.setsendinband(chr(sock.escchar)+" OK"+chr(sock.endchar))
'any more inband commands to process now?
x=instr(1,s,chr(sock.endchar))
wend
end sub
For the above example to work well, the size of the CMD buffer must not exceed the capacity of string variable s. This way, whatever is extracted from the CMD buffer will always fit in s. A slightly more complex processing is needed if the buffer is larger than the capacity of s.
And this would be an incorrect way — do not split inband replies!
...
sock.setsendinband(chr(sock.escchar)) 'WRONG!
sock.setsendinband(" OK") 'WRONG!
sock.setsendinband(chr(sock.endchar)) 'WRONG!
...
Notice how we have created a character after the escape character — by adding a space in front of our "OK" reply. This will work fine, as long as our escape character is not space!