-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathzeroconf_mp.lua
114 lines (82 loc) · 3.69 KB
/
zeroconf_mp.lua
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
local json = require("json")
local function tableLength(table)
local count = 0
for _ in pairs(table) do
count = count + 1
end
return count
end
zcrmmp = {}
-- Variables.
--Default values for options being sent in.
zcrmmp.numberOfPlayers = 2 --Number of players in a game.
zcrmmp.gameName = "AwesomeGame" --Name of game.
zcrmmp.serverName = "MyAwesomeServer" --Name of service.
zcrmmp.searchForLength = 5000 --Number of mili-seconds to search for players.
--Others
zcrmmp.searchTimer = nil -- variable to hold the search timer.
zcrmmp.serverFound = false -- set to true once we find a server.
zcrmmp.serverInfo = {} -- table to store the server information.
zcrmmp.amServer = false -- to know if I cam client or server.
--References
zcrmmp.zeroconf = require( "plugin.zeroconf" ) -- zeroconf plugin.
zcrmmp.server = require( "zeroconf_mp.server") -- rob miracle original server (with changes).
zcrmmp.client = require( "zeroconf_mp.client") -- rob miracle original client (with changes).
zcrmmp.notifier = require( "zeroconf_mp.notifier") -- notification lib.
zcrmmp.buffer = require( "zeroconf_mp.dataBuffer") -- copy and send buffer.
zcrmmp.zeroconfListener = function(event)
if event.phase == "found" then -- Found a server.
zcrmmp.serverFound = true -- Set variable to found so that timer doesn't start a server.
local client = zcrmmp.client.connectToServer( event.addresses[1], event.port) -- Start the client.
zcrmmp.client.createClientLoop( client, event.addresses[1], event.port, zcrmmp.notifier, zcrmmp.buffer ) --Start the client loop.
zcrmmp.zeroconf.stopBrowseAll() -- Stop the browser.
end
end
zcrmmp.onEvent = function( event )
if event.phase == "playerAdded" then -- Found a server.
local numberOfPlayers = tableLength(event.data)
if numberOfPlayers == (zcrmmp.numberOfPlayers - 1 ) then -- If we have our number of players.
zcrmmp.server.stopAccepts() -- Stop accepting new clients.
zcrmmp.zeroconf.unpublishAll() -- Stop server publish.
end
end
end
zcrmmp.onSearchTimerOver = function(event)
if zcrmmp.serverFound == false then --Found nothing. Start a server.
zcrmmp.amServer = true
zcrmmp.zeroconf.stopBrowseAll() -- Stop the browswer.
zcrmmp.serverInfo = zcrmmp.server.createServer(zcrmmp.notifier, zcrmmp.buffer) -- Create the server.
zcrmmp.zeroconf.publish( {port = zcrmmp.server.port * 1, type="_corona._tcp", name=zcrmmp.serverName} ) -- Publish server.
end
end
zcrmmp.init = function(eventListener, optionsTable)
zcrmmp.notifier.eventDispatcher:addEventListener( "zeroconf_mp", eventListener)
zcrmmp.notifier.eventDispatcher:addEventListener( "zeroconf_mp", zcrmmp.onEvent )
zcrmmp.zeroconf.init( zcrmmp.zeroconfListener )
-- Values from option table.
zcrmmp.gameName = optionsTable["gameName"]
zcrmmp.serverName = optionsTable["serverName"]
zcrmmp.searchForLength = optionsTable["searchForLength"]
zcrmmp.numberOfPlayers = optionsTable["numberOfPlayers"]
end
zcrmmp.startGame = function()
-- Check for servers with zeroconf
zcrmmp.amServer = false
zcrmmp.serverFound = false
zcrmmp.zeroconf.browse( {type="_corona._tcp"} )
-- Create a timer to stop searching for server.
if zcrmmp.searchForLength > 0 then
zcrmmp.searchTimer = timer.performWithDelay(zcrmmp.searchForLength, zcrmmp.onSearchTimerOver)
end
end
zcrmmp.endGame = function()
if zcrmmp.amServer == true then
zcrmmp.server.stopServer()
else
zcrmmp.client.stopClient()
end
end
zcrmmp.sendData = function( tableOfData )
zcrmmp.buffer.addDataToBuffer( tableOfData)
end
return zcrmmp