-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy paththread.lua
More file actions
42 lines (33 loc) · 813 Bytes
/
thread.lua
File metadata and controls
42 lines (33 loc) · 813 Bytes
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
socket = require "socket"
love.timer = require "love.timer"
ch = love.thread.getChannel("args")
udp = socket.udp()
udp:settimeout(0)
local address = ch:demand()
local port = ch:demand()
local rate = ch:demand()
udp:setpeername(address, port)
print(address, port)
local startT = love.timer.getTime() - rate
local num = 0;
while true do
local tT = love.timer.getTime()
if (tT - startT) > rate then
startT = math.max(tT - rate, startT + rate)
udp:send(string.format("%d", num))
ch:push({num, tT, false})
num = num + 1
end
repeat
local data, msg = udp:receive()
tT = love.timer.getTime()
if data then
local m = tonumber(data)
ch:push({m, tT, true})
end
until not data
local sleepT = rate - (tT - startT)
if sleepT > 0 then
socket.select({ udp }, nil, sleepT)
end
end