-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetworkLib.lua
More file actions
129 lines (105 loc) · 2.52 KB
/
Copy pathNetworkLib.lua
File metadata and controls
129 lines (105 loc) · 2.52 KB
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
local socket = require("socket")
local SET = 1
local GET = 2
local TEMP = 3
local CONST = 4
local SIGNAL = 5
local SUB = 6
local DEL = string.char(0x1F)
local END = string.char(0x00)
local nl = {
is_connected = false,
conn = nil,
buffer = ""
}
function nl:init(host, port)
local conn, err = socket.tcp()
if not conn then
return false, "Error creating socket: " .. tostring(err)
end
conn:settimeout(1.0)
local res, conn_err = conn:connect(host, port)
if not res then
self.is_connected = false
return false, "ERROR connectiong: " .. tostring(conn_err)
end
conn:settimeout(0)
self.conn = conn
self.is_connected = true
self.buffer = ""
return true, "Connected Sucessfully"
end
function nl:sendCmd(opcode, args, is_signal)
if not self.is_connected or not self.conn then
return
end
local msg = string.char(opcode)
for _, a in ipairs(args) do
msg = msg .. tostring(a) .. DEL
end
if is_signal then
msg = msg .. END
end
self.conn:send(msg)
end
function nl:GET(key)
if not self.is_connected or not self.conn then
return nil
end
self:sendCmd(GET, {key})
self.conn:settimeout(0.1)
local temp_buffer = ""
while true do
local byte, err = self.conn:receive(1)
if err then
self.conn:settimeout(0)
return nil
end
if byte == DEL then
self.conn:settimeout(0)
return temp_buffer
else
temp_buffer = temp_buffer .. byte
end
end
end
function nl:SET(key, val)
self:sendCmd(SET, {key, val})
end
function nl:TEMP(key, val)
self:sendCmd(TEMP, {key, val})
end
function nl:CONST(key, val)
self:sendCmd(CONST, {key, val})
end
function nl:SUB(key)
self:sendCmd(SUB, {key})
end
function nl:SIGNAL(...)
self:sendCmd(SIGNAL, {...}, true)
end
function nl:update_game_network()
if not self.is_connected or not self.conn then
return nil, "Not Connected"
end
while true do
local byte, err = self.conn:receive(1)
if err == "timeout" then
return nil
elseif err then
self.is_connected = false
if self.conn then
self.conn:close()
end
return nil, err
end
if byte == DEL then
local msg = self.buffer
self.buffer = ""
return msg
else
self.buffer = self.buffer .. byte
end
end
end
return nl