From 073d0bbfc09ed8b068e2ad9b7da92a15006b7bf8 Mon Sep 17 00:00:00 2001 From: Aleteoryx Date: Mon, 2 Sep 2024 16:21:43 -0400 Subject: [PATCH] that's a basic nex guy done --- nex.nelua | 15 +++++++++++++++ socket.nelua | 37 ++++++++++++++++++++++++------------- 2 files changed, 39 insertions(+), 13 deletions(-) create mode 100644 nex.nelua diff --git a/nex.nelua b/nex.nelua new file mode 100644 index 0000000..9379752 --- /dev/null +++ b/nex.nelua @@ -0,0 +1,15 @@ +local socket = require 'socket' + +socket.listen_tcp("127.0.0.1:1900", function() + local line = socket.recv_line():gsub("\r", "") + + if line:subview(1, 1) == "/" then line = line:subview(2) end + + if line == "" then + socket.send_line("Hello!\nThis is the index page!\n\nThe protocol's originator:\n=> nex://nightfall.city") + else + socket.send_line("Page not Found.") + end + + socket.end_conn() +end) diff --git a/socket.nelua b/socket.nelua index 58e89cc..c53a74b 100644 --- a/socket.nelua +++ b/socket.nelua @@ -4,7 +4,6 @@ but for now it's a standalone function. ]] --- ## pragmas.nogc = true -- c imports ## cinclude '' @@ -69,6 +68,9 @@ local function c_close(fd: cint): cint end -- end c imports + +local export = @record{} + require 'string' require 'hashmap' require 'stringbuilder' @@ -195,7 +197,7 @@ function handler_state:step(epfd: cint, key: uint32): (boolean) return true end -local function listen_sock(sock: cint, handler: function(): void): void +function export.listen_sock(sock: cint, handler: function(): void): void die_errno(c_listen(sock, 32) ~= 0, "couldn't listen on TCP socket") local epfd = c_epoll_create(0) die_errno(epfd == -1, "couldn't create epoll instance") @@ -253,7 +255,7 @@ local function listen_sock(sock: cint, handler: function(): void): void end end -local function listen_tcp(address: string, handler: function(): void): void +function export.listen_tcp(address: string, handler: function(): void): void local matched, matches = string.match(address, inet_re) if matched then local c_err: cint = 0 @@ -282,17 +284,26 @@ local function listen_tcp(address: string, handler: function(): void): void defer c_close(fd) end - listen_sock(fd, handler) + export.listen_sock(fd, handler) end end -listen_tcp("127.0.0.1:1901", function() - local line: string - repeat - coroutine.yield(handler_req.read_line) - coroutine.running():pop(&line) - coroutine.running():push(line.."\n") - coroutine.yield(handler_req.write) - until line == "bye" +function export.end_conn(): void coroutine.yield(handler_req.close) -end) +end +function export.send(line: string): void + coroutine.running():push(line) + coroutine.yield(handler_req.write) +end +function export.send_line(line: string): void + coroutine.running():push(line.."\n") + coroutine.yield(handler_req.write) +end +function export.recv_line(): string + local line: string + coroutine.yield(handler_req.read_line) + coroutine.running():pop(&line) + return line +end + +return export -- 2.45.2