From e86d9fb8babdcd021718e1132e9d1a071118c057 Mon Sep 17 00:00:00 2001 From: Malte0621 <50240920+Malte0621@users.noreply.github.com> Date: Tue, 14 Dec 2021 16:37:16 +0100 Subject: [PATCH] Files for ExternalNet New files for ExternalNet Adds: UDP (server & client) [Server freezes without connections.] --- info.txt | 4 +- main.lua | 88 ++++++++++++++++++++------- mhttp.lua | 5 +- mudp.lua | 179 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 251 insertions(+), 25 deletions(-) create mode 100644 mudp.lua diff --git a/info.txt b/info.txt index 2411e97..19cd0de 100644 --- a/info.txt +++ b/info.txt @@ -1,3 +1,3 @@ -name = ExternalHttp V2 +name = ExternalNet author = Malte0621 -description = A prototype http requesting mod for teardown. (V2) \ No newline at end of file +description = A prototype http requesting & udp communication mod for teardown. \ No newline at end of file diff --git a/main.lua b/main.lua index d9e0286..de64393 100644 --- a/main.lua +++ b/main.lua @@ -1,6 +1,10 @@ #include "mhttp.lua" +#include "mudp.lua" -local testingMode = false +local testingMode = true +local doHttpTest = false +local doUDPTest = false +local isUDPServer = false local loaded = false local showTicks = 60*1.5 @@ -9,30 +13,71 @@ function init() loaded = true end -function Split(s, delimiter) - result = {}; - for match in (s..delimiter):gmatch("(.-)"..delimiter) do - table.insert(result, match); - end - return result; -end - local c = 0 -function tick(dt) +local udpSocket = nil + +local udpServerSocket = nil + +function update(dt) if testingMode then - if c >= 60*1.25 then + if c == 25 then + c = c + 1 + if isUDPServer then + if not udpServerSocket then + udpServerSocket = udp.host(1621) + end + if udpServerSocket then + pcall(function() + local data = udpServerSocket.recv(1024) + DebugPrint('--------- Server ---------') + DebugPrint(data.addr.str) + DebugPrint(data.data) + DebugPrint('--------------------------') + local s,f = pcall(function() + udpServerSocket.send(data.addr.ip,data.addr.port,"Hello Server") + end) + if s then + DebugPrint('SENT RESPONSE') + else + DebugPrint('I FAILED TO SEND MY RESPONSE! :( : ' .. f) + end + end) + end + end + elseif c >= 60*1.25 then c = 0 - local resp = http.GetAsync("http://localhost/hi.txt",{["Test-Header"] = "Test"}) - -- local resp = http.PostAsyncA("http://localhost/test.php",{hi = "Hello"}) - -- local resp = http.PutAsync("http://localhost/test.php") - if resp.Success then - DebugPrint(resp.Body) - for i,v in pairs(resp.Headers) do - DebugPrint(i .. ", " .. v) + + -- HTTP + if doHttpTest then + local resp = http.GetAsync("http://localhost/hi.txt",{["Test-Header"] = "Test"}) + -- local resp = http.PostAsyncA("http://localhost/test.php",{hi = "Hello"}) + -- local resp = http.PutAsync("http://localhost/test.php") + if resp.Success then + DebugPrint(resp.Body) + for i,v in pairs(resp.Headers) do + DebugPrint(i .. ", " .. v) + end + else + DebugPrint("MTHTP_ERROR: " .. resp.Error) + end + end + -- UDP + if doUDPTest then + if not isUDPServer then + if not udpSocket then + udpSocket = udp.connect("127.0.0.1",1621) + DebugPrint("Connected") + end + if udpSocket then + DebugPrint('--------- Client ---------') + local sent = udpSocket.send("Hello Client") + DebugPrint(sent and "true" or "false") + local data = udpSocket.recv(1024) + DebugPrint(data) + DebugPrint('--------------------------') + end end - else - DebugPrint("MTHTP_ERROR: " .. resp.Error) end else c = c + 1 @@ -48,4 +93,5 @@ function draw() UiText("ExternalHttp (V2) by Malte0621, Loaded.") showTicks = showTicks - 1 end -end \ No newline at end of file +end + diff --git a/mhttp.lua b/mhttp.lua index 7a8fc8d..20f8b1f 100644 --- a/mhttp.lua +++ b/mhttp.lua @@ -1,13 +1,14 @@ --[[ Malte0621's ExternalHttp Module (V2). +(coroutine.wrap(function() end) is helpful to prevent crashes during the request.) (Requires json.lua) --> Usage #include "mhttp.lua" -local body = http.PostAsyncA("http://localhost/",{msg = "hello"}) -- PostAsyncA urlencodes for you. While PostAsync does not and requires manual urlencoding. --- local body = http.GetAsync("http://localhost/") +local resp = http.PostAsyncA("http://localhost/",{msg = "hello"}) -- PostAsyncA urlencodes for you. While PostAsync does not and requires manual urlencoding. +-- local resp = http.GetAsync("http://localhost/") --> API -------------------------- diff --git a/mudp.lua b/mudp.lua new file mode 100644 index 0000000..2437650 --- /dev/null +++ b/mudp.lua @@ -0,0 +1,179 @@ +--[[ +Malte0621's ExternalUdp Module. [EARLY PROTOTYPE] - Many bugs exists and you should use pcall in some places to prevent errors. +(coroutine.wrap(function() end) is also helpful to prevent crashes during the request.) + +(Requires json.lua) + +--> Usage +#include "mudp.lua" + +local udpSocket = udp.connect("127.0.0.1",1621) +DebugPrint("Connected") +local sent = udpSocket.send("Hello!") +DebugPrint(sent and "true" or "false") +local data = udpSocket.recv(1024) +DebugPrint(data) +udpSocket.close() + +--> API +-------------------------- +"mudp.lua" +-------------------------- +udp = { + host = function() -> { + close = function() -> + send = function(,,) -> + recv = function() -> {addr = {ip = , port = , str = }, data = OR } + } + connect = function(,) -> { + close = function() -> + send = function() -> + recv = function() -> OR + } +} +-------------------------- +]] + +#include "json.lua" + +local bug_fix_random_chars = false -- Fixes tons of random characters at the end of the recv data. + +local function Split(s, delimiter) + result = {}; + for match in (s..delimiter):gmatch("(.-)"..delimiter) do + table.insert(result, match); + end + return result; +end + +local unpack = table.unpack or unpack + +local function parseResponse(resp) + local index = string.find(resp,string.char(0)) + if index then + resp = resp:sub(1,index-1) + end + return resp +end + +udp = { + host = function(port) + local resp = udp_host(port) + if resp.Success then + local id = resp.Id + local closed = false + return { + isClosed = function() + return closed + end; + close = function() + if closed then + -- error("Request Failed : Socket Closed.") + return false + end + local resp = udp_unhost(id) + if resp.Success then + closed = true + return true + else + -- error(resp.Error) + return false + end + end; + send = function(ip,port,data) + if closed then + -- error("Request Failed : Socket Closed.") + return false + end + local resp = udp_send(id,true,ip,port,data) + if resp.Success then + return true + else + -- error(resp.Error) + return false + end + end; + recv = function(buffer) + if closed then + -- error("Request Failed : Socket Closed.") + return false + end + local resp = udp_recv(id,true,"",0,buffer) + if resp.Success then + local data = Split(resp.Data,"|") + local addr = table.remove(data,1) + data = table.concat(data,"|") + if bug_fix_random_chars then + data = parseResponse(data) + end + local ip,port = unpack(Split(addr,":")) + return {addr = {ip = ip, port = tonumber(port), str = addr}, data = data} + else + -- error(resp.Error) + return nil + end + end; + } + else + error(resp.Error) + end + end; + connect = function (ip,port) + local resp = udp_connect(ip,port) + if resp.Success then + local id = resp.Id + local closed = false + return { + isClosed = function() + return closed + end; + close = function() + if closed then + -- error("Request Failed : Socket Closed.") + return false + end + local resp = udp_close(id) + if resp.Success then + closed = true + return true + else + -- error(resp.Error) + return false + end + end; + send = function(data) + if closed then + -- error("Request Failed : Socket Closed.") + return false + end + local resp = udp_send(id,false,ip,port,data) + if resp.Success then + return true + else + -- error(resp.Error) + return false + end + end; + recv = function(buffer) + if closed then + -- error("Request Failed : Socket Closed.") + return false + end + local resp = udp_recv(id,false,ip,port,buffer) + if resp.Success then + local data = resp.Data + if bug_fix_random_chars then + data = parseResponse(data) + end + return data + else + -- error(resp.Error) + return nil + end + end; + } + else + error(resp.Error) + end + end; +} \ No newline at end of file