-
Notifications
You must be signed in to change notification settings - Fork 1
/
server_udp.moon
212 lines (166 loc) · 5.73 KB
/
server_udp.moon
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package.cpath = ";./?51.dll;./debug/?.dll;"..package.cpath
package.path = ";./socket/?.lua;"..package.path
--You can download the iup library from:
--http://sourceforge.net/projects/iup
require "iuplua"
import iup from _G
import emu, console from _G
--Using luasocket-2.0.2, you can download the socket library from:
--http://files.luaforge.net/releases/luasocket/luasocket
export socket = require('socket')
import prettyPrint, dumpPrint from require("utils")
export remExecBuilder = require('deserializeData')
export serverSocket = socket.udp()
export bindname = "*"
--export bindname = "127.0.0.1"
--export bindname = "localhost"
export bindport = 51424
export peername = "localhost"
--export peername = "127.0.0.1"
export peerport = 51425
serverSocket\setsockname(bindname, bindport)
serverSocket\settimeout(0)
serverSocket\setpeername(peername, peerport)
dumpPrint serverSocket
export queueInput=nil
export queueRcvCmd={}
export dataToSend="New command executed."
class ServerSpawn
--timer = nil
--new:(queueInput)=>
-- queueInput=queueInput
-- print "newnew"
new:()=>
print "new"
setInputQueue:(self, queue)->
queueInput=queue
print "setQueue"
--dumpPrint queueInput
addInputQueue:(self, msg)->
--print "current queue length: "..(#queueInput)
--print "new queue length: "..(#queue)
--print "new queue msg: "..tostring(msg)
--console.log msg
--dumpPrint queueInput
table.insert(queueInput, msg)
--queueInput={msg}
--print("added queueInp: "..(#queueInput))
--@run()
--print ("addedQueue: "..(#queueInput))
getSocketInfo:()->
print("Using sockname: "..bindname..":"..bindport)
print(serverSocket\getsockname())
print("Using peername: "..peername..":"..peerport)
print(serverSocket\getpeername())
cmpStartsString = (fullString,startString)->
return string.sub(fullString,1,string.len(startString))==startString
@fnTick:()->
--print("tick")
--print("tick")
@start()
--poll()
return iup.DEFAULT
@fnTock:()->
print("tock")
return iup.DEFAULT
@start:()->
--canread=nil
canread = socket.select({serverSocket}, nil, 0)
for i,inSocket in ipairs(canread) do
--iup.Close()
line, err = inSocket\receive()
if err then print "error: "..tostring(err)
rcptValue = tostring(line)
print("got: "..rcptValue)
if(rcptValue=="send") then
serverSocket\send(dataToSend)
print("done ".. rcptValue)
elseif(rcptValue~=nil and rcptValue~="nil") then
if not cmpStartsString(rcptValue,"confirm")
confirmMsg = "confirm: "..rcptValue
print("sending: "..confirmMsg)
serverSocket\send(confirmMsg)
--serverSocket\send(dataToSend);
print("added "..rcptValue)
gg = remExecBuilder(rcptValue)
table.insert(queueRcvCmd, gg)
@startTimer:()->
print("start timer")
fps = 111
timerTick = 1000/fps
timerTick=100
print("timer set at: ".. timerTick)
timer = iup.timer({time:timerTick, action_cb: @fnTock})
timer = iup.timer({time:timerTick, action_cb: @fnTick})
--timer.run="NO" --stop the timer
timer.run="YES" --start the timer
--iup.SetIdle(@fnTick) --this can be used as timer as well...
--dg = iup.dialog({title:"Timer example"})
--dg\show()
--iup.SetIdle(@fnTock)
--iup.MainLoop()
--run:()=>
-- print("inside run")
--testRun:()=>
run:()->
print "ok"
if(queueInput == nil) then print("queueInp: nil")
if(queueInput ~= nil) then
print("queueInp: "..(#queueInput))
for i, msg in ipairs(queueInput) do
print "msg to send: "..msg
--msg = io.read("*l")
--_,msg = iup.GetParam("Title", nil, "Msg to send: %s\n","")
--print socket.select({serverSocket}, nil, 0)
if msg == nil then
print("msg is null")
continue
print("SEND MSG: "..msg)
--queueInput[i]=nil
serverSocket\send(msg)
--serverSocket\sendto(msg, peername, peerport) --this may cause to crash here
--serverSocket\sendto(msg, "127.0.0.1", peerport) -- so this works
--serverSocket\sendto(msg, peername, peerport) -- so this works
print("SENDED MSG: "..msg)
--serverSocket\sendto("lol", "127.0.0.1", 51420) -- so this works
--serverSocket\sendto("lol", "127.0.0.1", 51420) -- so this works...
for i=0, #queueInput do queueInput[i]=nil --clear the queue
--queueInput= [nil for i,v in ipairs queueInput] --clear the queue
--print("queue cleared: "..(#queueInput))
--while true do
if false then
if (#queueRcvCmd > 0) then
--TODO: Send back the actual data returned.
--example:
--dataToSend= "game name: " .. gameinfo.getromname();
dataToSend = "New Command command executed with success."
for i, value in ipairs(queueRcvCmd) do
remCmd=queueRcvCmd[i]
queueRcvCmd[i]=nil
if(remCmd.multiArgs=="ping") then
print("pong")
dataToSend="pong"
else
if remCmd\isParsable() then
remCmd\parse()
--remCmd:print() -- show all the params and function used.
--else print("lib not supported")
if remCmd\isSupported() then
print("execute function...")
remCmd\exec()
--else print("function not supported")
queueRcvCmd={} --clear the queue
--emu.yield()
--emu.pause()
--@run()
--if _G.emu==nil and _G.tastudio==nil then
--print("using console mode")
--dg = iup.dialog({title:"Timer example"})
--dg\show()
--iup.MainLoop() --start a loop
testRun = ()->
queueInput={"hi"}
ServerSpawn\run()
queueInput=nil
testRun()
return {:ServerSpawn, :serverSocket}