Skip to content

Instantly share code, notes, and snippets.

@GiuseppeChillemi
Last active July 11, 2022 22:50
Show Gist options
  • Save GiuseppeChillemi/bdeb3378eb8d4975036de634751e1f8c to your computer and use it in GitHub Desktop.
Save GiuseppeChillemi/bdeb3378eb8d4975036de634751e1f8c to your computer and use it in GitHub Desktop.
A function to send a command to a server
send-to: func [
"Send a message and returns the response. NONE if no response (TBD:)"
server-address "The address of the server"
port-number [integer!] "The remote port number"
message [String! block!] "The message to send"
/local
out-data ;"The return data"
connection
server
data
data-buffer
dbg
loop-times
] [
connection: open/direct/no-wait to-url rejoin ["tcp://" server-address ":" port-number]
;Discover how to continuously search and avoid timeone if server is closed
;TBD: Try the effet of timeout waiting for the server
;TBD: ... Also errors on opening
;TBD: ... and all the combinations
;TBD: Try if sending nothing timeout is generated
server: connection
if block? message [message: mold/all message]
;+++++++++ If server crashes, client blocks!!! Check on exit!
; probe "//// send-to ///"
; probe message
; probe "//// send-to-end ///"
insert server message
wait server
dbg: does [
print [
" VAR1 -> data : " type? data
if series? data [rejoin ["length? : " length? data]] " || VAR2 -> data-buffer : " type? data-buffer
if series? data-buffer [rejoin ["length?: " length? data-buffer " round: " loop-times]]
]
]
loop-times: 0
data-buffer: copy ""
until [
loop-times: loop-times + 1
print ["Reading!"]
data: copy either port? server ;Returns string or block with distinct values if /lines
[
Print "An event has happened, connection is a port"
server
]
[
Print ["An event has happened, connection is not a door - its type is: " type? data]
;Copy on top, fails if there is a problem , so you have to pass an empty element
copy "" ;Use a block in case of /LINES
;[]
;It is needed for /LINES when the connection does not return nothing
; and it is in timeout
; or if you do things like WAIT [PORT 10]
]
print ["!!!!!!!!!!!!! -> Before append . " ] dbg
if all [not none? data series? data not empty? data] [
print "x-x> There is data on the port"
case [
block? data [if not empty? first data append data-buffer first data]
string? data [append data-buffer data]
]
]
print ["!!!!!!!!!!!!! -> After append APPEND, Before check on exist di check per uscita . " ] dbg
any [none? data empty? data];It you use /no-wait, you get STINGS or NONE in data
]
Print "!!!! I am out"
case [
all [none? data] [print "I have found NONE in DATA - Connection has been lost"]
all [string? data] [append data: copy [] data-buffer]
;all [block? data] [] ;Manages LINES mode and without NO-WAIT or DIRECT
]
case [
;Manage network error
;test TRY
data = none [print ["1 Connection has been closed"] break]
empty? data [print ["2 Connecction in timeout!!"] break]
block? data [
out-data: first data
Print "We have reached the end of DATA ---"
] ;Note, put it here or you will intercept the empty block, order is important!
]
;TBD: check the server ha not already been closed
; use Open? ???
probe "--- I am closing the server ---"
print ["xxx-send-to Data length is" length? out-data]
close server
probe "---- Server - Closed ---"
out-data
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment