Renamed globals to autoloads for clarity
This commit is contained in:
72
autoloads/instance_launcher.gd
Normal file
72
autoloads/instance_launcher.gd
Normal file
@@ -0,0 +1,72 @@
|
||||
extends Node
|
||||
|
||||
|
||||
var port_range_start: int = 5000
|
||||
var port_range_end: int = 5010
|
||||
var threads: Dictionary = Dictionary()
|
||||
|
||||
|
||||
func _wait_for_child(port: String, mode: String, id: String) -> void:
|
||||
if port == "-1":
|
||||
call_deferred("_child_finished", id, -1)
|
||||
return
|
||||
|
||||
var output := []
|
||||
var exit_code := OS.execute(
|
||||
"./game.sh",
|
||||
[
|
||||
"--headless", "--", "server_" + port, "game", mode
|
||||
],
|
||||
output,
|
||||
true
|
||||
)
|
||||
|
||||
call_deferred("_child_finished", id, exit_code)
|
||||
|
||||
func _child_finished(id: String, exit_code: int) -> void:
|
||||
var thread: Thread = threads.get(id)
|
||||
if not thread: return
|
||||
|
||||
push_warning("Game Server [ %s ] Exited: %d" % [id, exit_code])
|
||||
|
||||
thread.wait_to_finish()
|
||||
threads.erase(id)
|
||||
|
||||
func is_port_free(port: int) -> bool:
|
||||
var server := TCPServer.new()
|
||||
var state := server.listen(port)
|
||||
|
||||
match state:
|
||||
OK:
|
||||
server.stop()
|
||||
return true
|
||||
_:
|
||||
return false
|
||||
|
||||
func find_free_port(start_port: int = 1024, end_port: int = 65535) -> int:
|
||||
for port in range(start_port, end_port + 1):
|
||||
if is_port_free(port):
|
||||
return port
|
||||
|
||||
return -1
|
||||
|
||||
func launch_game_server(match_data: Dictionary) -> String:
|
||||
var thread := Thread.new()
|
||||
var _thread_id := str( thread.get_instance_id() )
|
||||
var port := str(
|
||||
find_free_port(
|
||||
port_range_start,
|
||||
port_range_end
|
||||
)
|
||||
)
|
||||
|
||||
thread.start(
|
||||
_wait_for_child.bind(
|
||||
port,
|
||||
match_data.type,
|
||||
match_data.match_id
|
||||
)
|
||||
)
|
||||
|
||||
threads.set(match_data.match_id, thread)
|
||||
return port
|
||||
Reference in New Issue
Block a user