# This file is part of sunder. # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as # published by the Free Software Foundation, either version 3 of the # License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . class_name Network extends Node ## The maximum number of concurrent connections to accept @export_range(1, 4095) var max_peers:int = 32 ## Emitted when a [MultiplayerPeer] sends a [member leave] rpc, right before it is disconnected signal peer_disconnecting(peer_id:int) ## Creates a server peer and listens for connections on all intefaces for specifed [param port] func serve(port:int = 9000) -> void: var peer:ENetMultiplayerPeer = ENetMultiplayerPeer.new() var err:Error = peer.create_server(port, max_peers) if err != Error.OK: Game.change_scene(Game.GameType.NONE) return multiplayer.peer_connected.connect(_on_peer_connected) multiplayer.peer_disconnected.connect(_on_peer_disconnected) multiplayer.multiplayer_peer = peer print("listening for connections on *:%s" % port) ## Creates a client peer and connects to specified [param host] and [param port] func join(host:String = "localhost", port:int = 9000) -> void: var peer := ENetMultiplayerPeer.new() peer.create_client(host, port) multiplayer.multiplayer_peer = peer #@rpc("any_peer", "call_remote", "reliable") #func leave() -> void: #if multiplayer.is_server(): #var peer_id:int = multiplayer.get_remote_sender_id() #peer_disconnecting.emit(peer_id) #multiplayer.disconnect_peer(peer_id) ## Emitted when a multiplayer peer successfully connects to a server. Only emitted on the server. func _on_peer_connected(peer_id: int) -> void: print("peer `%d` connected" % peer_id) func _on_peer_disconnected(peer_id:int) -> void: print("peer `%d` disconnected" % peer_id)