all repos — min @ 7ca23972c8059acc22e6cc139fefb1e36c576ad5

A small but practical concatenative programming language.

Added accept and listen.
h3rald h3rald@h3rald.com
Sun, 19 Nov 2017 12:49:02 +0100
commit

7ca23972c8059acc22e6cc139fefb1e36c576ad5

parent

41131df5c583a2645012883b9d62e8f61b0a9a01

1 files changed, 37 insertions(+), 4 deletions(-)

jump to
M lib/min_net.nimlib/min_net.nim

@@ -53,15 +53,48 @@ elif (sProtocol == "icmp"):

protocol = IPPROTO_ICMP var socket = newSocket(domain, sockettype, protocol) var skt = newSeq[MinValue](0).newVal(i.scope) - skt = i.dset(skt, "domain".newSym, sDomain.newVal) - skt = i.dset(skt, "type".newSym, sSockType.newVal) - skt = i.dset(skt, "protocol".newSym, sProtocol.newVal) + skt = i.dset(skt, "domain".newVal, sDomain.newVal) + skt = i.dset(skt, "type".newVal, sSockType.newVal) + skt = i.dset(skt, "protocol".newVal, sProtocol.newVal) skt.objType = "socket" skt.obj = socket[].addr i.push skt - def.symbol("socket-close") do (i: In): + def.symbol("close") do (i: In): let vals = i.expect("dict:socket") vals[0].toSocket.close() + def.symbol("listen") do (i: In): + let vals = i.expect("dict", "dict:socket") + let params = vals[0] + var skt = vals[1] + var socket = skt.toSocket + var address = "0.0.0.0" + var port: BiggestInt = 80 + if params.dhas("address".newVal): + address = params.dget("address".newVal).getString + if params.dhas("port".newVal): + port = params.dget("port".newVal).intVal + socket.bindAddr(Port(port), address) + skt = i.dset(skt, "address".newVal, address.newVal) + skt = i.dset(skt, "port".newVal, port.newVal) + skt.objType = "socket" + skt.obj = socket[].addr + socket.listen() + i.push skt + + def.symbol("accept") do (i: In): + let vals = i.expect("dict:socket", "dict:socket") + var client = vals[0] + var server = vals[1] + var address = "" + var serverSocket = server.toSocket + var clientSocket = client.toSocket + serverSocket.acceptAddr(clientSocket, address) + i.dset(client, "address".newVal, address.newVal) + client.objType = "socket" + client.obj = clientSocket[].addr + i.push client + + def.finalize("net")