Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- rednet.open("left")
- local connected = {}
- local keepAliveTimer = os.startTimer(1)
- local configFile = fs.open("server.cfg", "r")
- local config = textutils.unserialise(configFile.readAll())
- configFile.close()
- local map = paintutils.loadImage(config.mapFile)
- local function serverLoop(eventData)
- local e, p1, p2, p3, p4, p5 = unpack(eventData)
- if e == "rednet_message" then
- local id, tbl = p1, p2
- if tbl.type ~= "connect" and connected[id] == nil then
- print("Rejected unconnected message from " .. id)
- return true
- end
- if tbl.type == "connect" then
- connected[id] = {
- keepAliveResponded = true,
- suspicious = false
- }
- print("Connection from " .. id)
- rednet.send(id, {
- type = "accepted"
- })
- elseif tbl.type == "disconnect" then
- print(id .. " has disconnected.")
- connected[id] = nil
- elseif tbl.type == "keepalive" then
- connected[id].keepAliveResponded = true
- elseif tbl.type == "suspicious_computer" then
- if connected[tbl.id] == nil then
- print(id .. " marked unknown computer suspicious.")
- print("Cheating attempt?")
- else
- print(id .. " marked " .. tbl.id .. " suspicious.")
- connected[tbl.id].suspicious = true
- end
- elseif tbl.type == "map" then
- rednet.send(id, {
- type = "map",
- data = map
- })
- end
- elseif e == "timer" and p1 == keepAliveTimer then
- local toDelete = {}
- for k,v in pairs(connected) do
- if not v.keepAliveResponded then
- table.insert(toDelete, k)
- print(k .. " timed out.")
- rednet.send(k, { type = "disconnect", reason = "You timed out." }) -- just to make sure
- end
- rednet.send(k, {
- type = "keepalive"
- })
- v.keepAliveResponded = false
- end
- for _,v in pairs(toDelete) do
- connected[v] = nil
- end
- keepAliveTimer = os.startTimer(1)
- elseif e == "terminate" then
- for k,v in pairs(connected) do
- rednet.send(k, {
- type = "disconnect",
- reason = "Server shutting down"
- })
- end
- return false
- end
- return true
- end
- print("Hosting server on ID " .. os.getComputerID())
- while true do
- local eventData = { os.pullEventRaw() }
- if not serverLoop(eventData) then break end
- end
Advertisement
Add Comment
Please, Sign In to add comment