Networking: Part 2 -> Added the initial roomname handshake.
This commit is contained in:
parent
cb24b0b629
commit
7aaacecec8
5 changed files with 66 additions and 43 deletions
|
@ -65,7 +65,3 @@ gdscript/warnings/confusable_capture_reassignment=2
|
||||||
gdscript/warnings/property_used_as_function=2
|
gdscript/warnings/property_used_as_function=2
|
||||||
gdscript/warnings/constant_used_as_function=2
|
gdscript/warnings/constant_used_as_function=2
|
||||||
gdscript/warnings/function_used_as_property=2
|
gdscript/warnings/function_used_as_property=2
|
||||||
|
|
||||||
[editor]
|
|
||||||
|
|
||||||
run/main_run_args="--testmode --logmode 4"
|
|
||||||
|
|
|
@ -1,13 +1,12 @@
|
||||||
extends Node
|
extends Node
|
||||||
|
|
||||||
var testmode: bool = true
|
|
||||||
var errortest: bool = true
|
|
||||||
|
|
||||||
var logmode: int = 4
|
var logmode: int = 4
|
||||||
|
|
||||||
const IP_ADDRESS: String = "127.0.0.1"
|
var ip: String = "127.0.0.1"
|
||||||
const PORT: int = 25262
|
var port: int = 25262
|
||||||
const MAX_CLIENTS: int = 1024
|
var max_clients: int = 1024
|
||||||
|
|
||||||
|
var room_name: String = "sample"
|
||||||
|
|
||||||
func _ready() -> void:
|
func _ready() -> void:
|
||||||
var args: PackedStringArray = OS.get_cmdline_args()
|
var args: PackedStringArray = OS.get_cmdline_args()
|
||||||
|
@ -16,21 +15,13 @@ func _ready() -> void:
|
||||||
if !skip:
|
if !skip:
|
||||||
Log.info("Argument %s passed." % args[i])
|
Log.info("Argument %s passed." % args[i])
|
||||||
match args[i]:
|
match args[i]:
|
||||||
"--testmode":
|
|
||||||
testmode = true
|
|
||||||
"--errortest":
|
|
||||||
errortest = true
|
|
||||||
"--logmode":
|
"--logmode":
|
||||||
logmode = int(args[i+1])
|
logmode = int(args[i+1])
|
||||||
skip = true
|
skip = true
|
||||||
|
"--server":
|
||||||
|
if Networking.start_server() != OK:
|
||||||
|
Log.error("Failed to start server! Closing application.", "Failed to start server!")
|
||||||
|
"--client":
|
||||||
|
Networking.join_room()
|
||||||
_:
|
_:
|
||||||
Log.warning("Unknown argument: %s!" % args[i])
|
Log.warning("Unknown argument: %s!" % args[i])
|
||||||
run_tests()
|
|
||||||
|
|
||||||
func run_tests() -> void:
|
|
||||||
if testmode:
|
|
||||||
Log.info("Starting singleton tests.")
|
|
||||||
Networking.test()
|
|
||||||
Log.test()
|
|
||||||
if errortest:
|
|
||||||
Log.error("Test", "Test")
|
|
||||||
|
|
|
@ -7,11 +7,6 @@ extends Node
|
||||||
# 3 -> Errors/Warnings/Infos
|
# 3 -> Errors/Warnings/Infos
|
||||||
# 4 -> All
|
# 4 -> All
|
||||||
|
|
||||||
func test() -> void:
|
|
||||||
debug("Test")
|
|
||||||
info("Test")
|
|
||||||
warning("Test")
|
|
||||||
|
|
||||||
# Used for errors.
|
# Used for errors.
|
||||||
func error(message: String, alert_message: String) -> void:
|
func error(message: String, alert_message: String) -> void:
|
||||||
if message == "":
|
if message == "":
|
||||||
|
|
|
@ -1,29 +1,70 @@
|
||||||
extends Node
|
extends Node
|
||||||
|
|
||||||
func test() -> void:
|
## Connection Code
|
||||||
start_server()
|
|
||||||
join_server()
|
# Connect the signals.
|
||||||
close_network()
|
func _ready() -> void:
|
||||||
|
if multiplayer.connected_to_server.connect(_on_connected_ok) != OK:
|
||||||
|
Log.error("FAILED to connect the connected_to_server signal to _on_connected_ok!", "Internal Error: Failed to connect signal.")
|
||||||
|
if multiplayer.connection_failed.connect(close_network) != OK:
|
||||||
|
Log.error("FAILED to connect the connection_failed signal to _on_connected_fail!", "Internal Error: Failed to connect signal.")
|
||||||
|
if multiplayer.server_disconnected.connect(close_network) != OK:
|
||||||
|
Log.error("FAILED to connect the server_disconnected signal to _on_server_disconnected!", "Internal Error: Failed to connect signal.")
|
||||||
|
|
||||||
# Start the network listener.
|
# Start the network listener.
|
||||||
func start_server() -> void:
|
func start_server() -> Error:
|
||||||
var peer: ENetMultiplayerPeer = ENetMultiplayerPeer.new()
|
var peer: ENetMultiplayerPeer = ENetMultiplayerPeer.new()
|
||||||
if peer.create_server(Game.PORT, Game.MAX_CLIENTS) != OK:
|
if peer.create_server(Game.port, Game.max_clients) != OK:
|
||||||
Log.warning("Couldn't create the server at port %d!" % Game.PORT)
|
Log.warning("Couldn't create the server at port %d!" % Game.port)
|
||||||
return
|
return FAILED
|
||||||
multiplayer.multiplayer_peer = peer
|
multiplayer.multiplayer_peer = peer
|
||||||
Log.info("Created the server at port %d." % Game.PORT)
|
Log.info("Created the server at port %d." % Game.port)
|
||||||
|
return OK
|
||||||
|
|
||||||
# Connect to a server.
|
# Connect to a server.
|
||||||
func join_server() -> void:
|
func join_server() -> Error:
|
||||||
var peer: ENetMultiplayerPeer = ENetMultiplayerPeer.new()
|
var peer: ENetMultiplayerPeer = ENetMultiplayerPeer.new()
|
||||||
if peer.create_client(Game.IP_ADDRESS, Game.PORT) != OK:
|
if peer.create_client(Game.ip, Game.port) != OK:
|
||||||
Log.warning("Couldn't connect to the server at %s:%d!" % [Game.IP_ADDRESS, Game.PORT])
|
Log.warning("Couldn't connect to the server at %s:%d!" % [Game.ip, Game.port])
|
||||||
return
|
return FAILED
|
||||||
multiplayer.multiplayer_peer = peer
|
multiplayer.multiplayer_peer = peer
|
||||||
Log.info("Connected to the server at %s:%d." % [Game.IP_ADDRESS, Game.PORT])
|
Log.info("Connected to the server at %s:%d." % [Game.ip, Game.port])
|
||||||
|
return OK
|
||||||
|
|
||||||
# Close all network connections.
|
# Close all network connections.
|
||||||
func close_network() -> void:
|
func close_network() -> void:
|
||||||
multiplayer.multiplayer_peer = null
|
multiplayer.multiplayer_peer = null
|
||||||
Log.info("Closed all network connections.")
|
Log.info("Closed all network connections.")
|
||||||
|
|
||||||
|
## Matchmaking Code
|
||||||
|
|
||||||
|
var mutex: Mutex = Mutex.new()
|
||||||
|
var rooms: Dictionary = {}
|
||||||
|
var peers: Dictionary = {}
|
||||||
|
|
||||||
|
func join_room() -> void:
|
||||||
|
if join_server() != OK:
|
||||||
|
Log.warning("Failed to connect to sever.")
|
||||||
|
|
||||||
|
func _on_connected_ok() -> void:
|
||||||
|
if rpc_id(1, "send_roomname", Game.room_name) != OK:
|
||||||
|
Log.warning("Failed to send roomname!")
|
||||||
|
|
||||||
|
@rpc("any_peer", "call_remote", "reliable")
|
||||||
|
func send_roomname(roomname: String) -> void:
|
||||||
|
mutex.lock()
|
||||||
|
if multiplayer.get_unique_id() == 1:
|
||||||
|
if !rooms.has(roomname) and !rooms.has(multiplayer.get_remote_sender_id()):
|
||||||
|
rooms[roomname] = multiplayer.get_remote_sender_id()
|
||||||
|
rooms[multiplayer.get_remote_sender_id()] = roomname
|
||||||
|
peers[roomname] = [multiplayer.get_remote_sender_id()]
|
||||||
|
Log.info("Room %s registered." % roomname)
|
||||||
|
elif peers[roomname] is Array:
|
||||||
|
# For some odd reason, Godot doesn't allow casting peers[roomname] to an Array, but this works?
|
||||||
|
var peer_array: Array = peers[roomname]
|
||||||
|
peer_array.append(multiplayer.get_remote_sender_id())
|
||||||
|
peers[roomname] = peer_array
|
||||||
|
Log.debug("Room %s joined by peer %d." % [roomname, multiplayer.get_remote_sender_id()])
|
||||||
|
else:
|
||||||
|
Log.warning("Peer %d tried to send this client a request meant for the authority!" % multiplayer.get_remote_sender_id())
|
||||||
|
mutex.unlock()
|
||||||
|
|
|
@ -1,3 +1,3 @@
|
||||||
[gd_scene format=3 uid="uid://dhi34ugxtgmlg"]
|
[gd_scene format=3 uid="uid://dhi34ugxtgmlg"]
|
||||||
|
|
||||||
[node name="Test" type="Node2D"]
|
[node name="Start" type="Node2D"]
|
||||||
|
|
Loading…
Reference in a new issue