~aleteoryx/nex.nelua

073d0bbfc09ed8b068e2ad9b7da92a15006b7bf8 — Aleteoryx 2 months ago a9dc10f
that's a basic nex guy done
2 files changed, 39 insertions(+), 13 deletions(-)

A nex.nelua
M socket.nelua
A nex.nelua => nex.nelua +15 -0
@@ 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)

M socket.nelua => socket.nelua +24 -13
@@ 4,7 4,6 @@
  but for now it's a standalone function.
]]

-- ## pragmas.nogc = true

-- c imports
## cinclude '<sys/socket.h>'


@@ 69,6 68,9 @@ local function c_close(fd: cint): cint <cimport 'close', nodecl> 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