Basic TCP/IP commands: SEND, LISTEN, & REPLY
Created by: dvkt
This is one idea for how to approach #110. It adds SEND
, LISTEN
, and REPLY
to LDPL as basic socket statements. It's not ready to be merged yet, but I wanted to open this to track progress.
Send statement:
SEND <TEXT-EXPR> TO <TEXT-EXPR> AT <NUMBER-EXPR> IN <TEXT-VAR>
This will open a TCP connection to host/port, send the data, read the response into a variable, then close the connection.
You can use it to make basic HTTP requests currently, but people will want persistent connections for stuff like IRC. So the next step is probably adding something like CONNECT TO <host> AT <port>
and CLOSE CONNECTION TO <host> AT <port>
to be used in conjunction with SEND
.
Then if CONNECT
has been called, SEND
will re-use that connection and keep it open. Otherwise, it'll open and close a new connection.
Receive statement:
LISTEN ON <TEXT-EXPR> AT <NUMBER-EXPR> AND CALL <SUB-PROCEDURE> WITH <TEXT-VAR>
This starts a basic multi-client TCP server that uses select()
. It can only read 1024 byte requests, so we'll have to increase that if we want to support stuff like HTTP POST. Otherwise it should work fine. Currently it closes all connections after sending data.
I, uh, wouldn't call this "industrial strength" or anything like that. But once we get it working right, it'll probably be okay for little dev servers.
Right now the request body is put into the <TEXT-VAR>
so it can be accessed in the callback, but I'm going to change it to be a MAP
. That way you can get information about the specific client that made the request and all that.
Reply statement:
REPLY WITH <TEXT-EXPR>
This can be used in the LISTEN
callback. We may want to add a few more callback-specific statements in the future too, for managing the client connection.
Windows
The examples/echo.ldpl
program should work on Linux and MacOS. I haven't added Windows support yet, but I am going to work on that next. It should be possible to have things work the same way.
That's it
I'll update this PR when I make more progress. Any thoughts on the syntax, approach, or other things we're gonna need?