-
Notifications
You must be signed in to change notification settings - Fork 0
/
mudp.lua
188 lines (176 loc) · 4.37 KB
/
mudp.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
--[[
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
local udp_host = udp_host
local udp_unhost = udp_unhost
local udp_send = udp_send
local udp_recv = udp_recv
local udp_connect = udp_connect
local udp_close = udp_close
udp_host, udp_unhost, udp_send, udp_recv, udp_connect, udp_close = nil
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;
}