LuaJIT-powered Simple Stupid Socket
A reimplementation of SSS powered by MoonScript and LuaJIT.
Install
sh$ make
sh$ make test
sh$ sudo make install
Before make install
, you can check if it’s ok by running:
sh$ make echoserver
And:
sh$ telnet localhost 32000
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
test
test
Connection closed by foreign host.
sh$
Use
#!moonscript
sss = assert require "sss"
socket = sss.socket!
The socket
function receives three parameters:
- domain: from
sss.AF
table, defaultsss.AF.inet
- type: from
sss.SOCK
table, defaultsss.SOCK.stream
- protocol: integer or string, see
/etc/protocols
, default 0
The socket object accepts the following messages:
-
socket.close!
– closes the socket. -
socket.connect address
– connects to the address. The address can be:- a table with the keys
host
andport
- an address object, got from
sss.tocaddress t
- a table with the keys
-
socket.bind address
– binds to the address. Adress as above. -
socket.listen backlog
– like Clisten
function. -
socket.accept!
– accepts new connections, returns the client socket and the C address. -
socket.receive!
– receives and returns one line from remote peer. -
socket.recv n
– receivesn
bytes from remote peer. Default 1024. -
socket.send data
– sends data to remote peer. -
socket.sendto data, address
– like Csendto
function. -
socket.setopt name, value
– sets socket option, value must be acdata
. -
socket.getopt name
– gets socket option value, returnscdata<unsgined char[?]>
and the length in bytes. -
socket.settimeout tmo
– sets the receiving and/or sending timeout.tmo
can be a number, or a table containing the keyssend
andreceive
.
Another useful functions:
tocaddress addr
– converts a table to C address.toladdress addr
– converts a C address to table.
The tables
sss.AF
: contains the socket address families.sss.SOCK
: contains the socket types.sss.SO
: contains the socket options.
To see the content:
#!moonscript
table.foreach sss.AF, print
The sample echo server
#!moonscript
#!/usr/bin/env moon
sss = assert require "sss"
s = with sss.socket!
\reuseaddr!
\bind
host: "*"
port: 32000
\listen!
while true
with s\accept!
got = \receive!
\send got
\close!
License
Copyright © 2015, Rodrigo Cacilhας <batalema@cacilhas.info>
All rights reserved.