summaryrefslogtreecommitdiff
path: root/server-beta/src/service.lua
blob: 114ca6d04cf1f3bbdf6b3ec990faa3154a6c8148 (plain)
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
local pub        = require 'pub'
local subprocess = require 'bee.subprocess'
local thread     = require 'bee.thread'
local task       = require 'task'
local utility    = require 'utility'
local timer      = require 'timer'

local m = {}
m.type = 'service'

function m.listenProto()
    subprocess.filemode(io.stdin,  'b')
    subprocess.filemode(io.stdout, 'b')
    io.stdin:setvbuf  'no'
    io.stdout:setvbuf 'no'
    task.create(function ()
        while true do
            local proto = pub.task('loadProto')
            log.debug('proto:', utility.dump(proto))
        end
    end)
end

function m.listenPub()
    task.create(function ()
        while true do
            pub.checkDead()
            pub.recieve()
            task.sleep(0)
        end
    end)
end

function m.startTimer()
    local last = os.clock()
    while true do
        thread.sleep(0.001)
        local current = os.clock()
        local delta = current - last
        last = current
        timer.update(delta)
    end
end

function m.start()
    pub.recruitBraves(4)
    task.setErrorHandle(log.error)
    m.listenProto()
    m.listenPub()

    m.startTimer()
end

return m