Skip to content

Commit

Permalink
Files for ExternalNet
Browse files Browse the repository at this point in the history
New files for ExternalNet

Adds: UDP (server & client)
[Server freezes without connections.]
  • Loading branch information
Malte0621 committed Dec 14, 2021
1 parent a9f03a2 commit e86d9fb
Show file tree
Hide file tree
Showing 4 changed files with 251 additions and 25 deletions.
4 changes: 2 additions & 2 deletions info.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
name = ExternalHttp V2
name = ExternalNet
author = Malte0621
description = A prototype http requesting mod for teardown. (V2)
description = A prototype http requesting & udp communication mod for teardown.
88 changes: 67 additions & 21 deletions main.lua
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -48,4 +93,5 @@ function draw()
UiText("ExternalHttp (V2) by Malte0621, Loaded.")
showTicks = showTicks - 1
end
end
end

5 changes: 3 additions & 2 deletions mhttp.lua
Original file line number Diff line number Diff line change
@@ -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
--------------------------
Expand Down
179 changes: 179 additions & 0 deletions mudp.lua
Original file line number Diff line number Diff line change
@@ -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(<port:int>) -> {
close = function() -> <success:bool>
send = function(<ip:string>,<port:int>,<data:string>) -> <success:bool>
recv = function() -> {addr = {ip = <ip:string>, port = <port:int>, str = <ipandport:string>}, data = <data:string> OR <data:nil>}
}
connect = function(<ip:string>,<port:int>) -> {
close = function() -> <success:bool>
send = function(<data:string>) -> <success:bool>
recv = function() -> <data:string> OR <data:nil>
}
}
--------------------------
]]

#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;
}

0 comments on commit e86d9fb

Please sign in to comment.