Remove low level network handler
This commit is contained in:
@ -1,40 +0,0 @@
|
||||
extends Node
|
||||
|
||||
signal handle_local_id_assignment(local_id: int)
|
||||
signal handle_remote_id_assignment(remote_id: int)
|
||||
signal handle_player_position(player_position: PlayerPosition)
|
||||
|
||||
var id: int = -1
|
||||
var remote_ids: Array[int]
|
||||
|
||||
func _ready() -> void:
|
||||
LowLevelNetworkHandler.on_client_packet.connect(on_client_packet)
|
||||
|
||||
|
||||
func on_client_packet(data: PackedByteArray) -> void:
|
||||
var packet_type: int = data.decode_u8(0)
|
||||
match packet_type:
|
||||
PacketInfo.PACKET_TYPE.ID_ASSIGNMENT:
|
||||
manage_ids(IDAssignment.create_from_data(data))
|
||||
|
||||
PacketInfo.PACKET_TYPE.PLAYER_POSITION:
|
||||
handle_player_position.emit(PlayerPosition.create_from_data(data))
|
||||
|
||||
_:
|
||||
push_error("Packet type with index ", data[0], " unhandled!")
|
||||
|
||||
|
||||
func manage_ids(id_assignment: IDAssignment) -> void:
|
||||
# TODO: This will cause issues if two clients believe they are the target for the ID assignment
|
||||
if id == -1: # When id == -1, the id sent by the server is for us
|
||||
id = id_assignment.id
|
||||
handle_local_id_assignment.emit(id_assignment.id)
|
||||
|
||||
remote_ids = id_assignment.remote_ids
|
||||
for remote_id in remote_ids:
|
||||
if remote_id == id: continue
|
||||
handle_remote_id_assignment.emit(remote_id)
|
||||
|
||||
else: # When id != -1, we already own an id, and just append the remote ids by the sent id
|
||||
remote_ids.append(id_assignment.id)
|
||||
handle_remote_id_assignment.emit(id_assignment.id)
|
||||
@ -1 +0,0 @@
|
||||
uid://cja10oadwmjen
|
||||
@ -1,122 +0,0 @@
|
||||
extends Node
|
||||
|
||||
# Server signals
|
||||
signal on_peer_connected(peer_id: int)
|
||||
signal on_peer_disconnected(peer_id: int)
|
||||
signal on_server_packet(peer_id: int, data: PackedByteArray)
|
||||
|
||||
# Client signals
|
||||
signal on_connected_to_server()
|
||||
signal on_disconnected_from_server()
|
||||
signal on_client_packet(data: PackedByteArray)
|
||||
|
||||
# Server variables
|
||||
var available_peer_ids: Array = range(255, -1, -1)
|
||||
var client_peers: Dictionary[int, ENetPacketPeer] # key: peer_id (int), value: peer (ENetPacketPeer). The peer_id should hold "id" meta data. Use get_meta("id")
|
||||
|
||||
# Client variables
|
||||
var server_peer: ENetPacketPeer
|
||||
|
||||
# General variables
|
||||
var connection: ENetConnection
|
||||
var is_server: bool = false
|
||||
|
||||
func _process(delta: float) -> void:
|
||||
if connection == null: return
|
||||
|
||||
handle_events()
|
||||
|
||||
|
||||
func handle_events() -> void:
|
||||
var packet_event: Array = connection.service()
|
||||
var event_type: ENetConnection.EventType = packet_event[0]
|
||||
|
||||
while event_type != ENetConnection.EVENT_NONE:
|
||||
var peer: ENetPacketPeer = packet_event[1]
|
||||
|
||||
match (event_type):
|
||||
ENetConnection.EVENT_ERROR:
|
||||
push_warning("Package resulted in an unknown error!")
|
||||
return
|
||||
|
||||
ENetConnection.EVENT_CONNECT:
|
||||
if is_server:
|
||||
peer_connected(peer)
|
||||
else:
|
||||
connected_to_server()
|
||||
|
||||
ENetConnection.EVENT_DISCONNECT:
|
||||
if is_server:
|
||||
peer_disconnected(peer)
|
||||
else:
|
||||
disconnected_from_server()
|
||||
return # Return because connection was set to null
|
||||
|
||||
ENetConnection.EVENT_RECEIVE:
|
||||
if is_server:
|
||||
on_server_packet.emit(peer.get_meta("id"), peer.get_packet())
|
||||
else:
|
||||
on_client_packet.emit(peer.get_packet())
|
||||
|
||||
# Call service() again to handle remaining packets in current while loop
|
||||
packet_event = connection.service()
|
||||
event_type = packet_event[0]
|
||||
|
||||
|
||||
func start_server(ip_address: String = "127.0.0.1", port: int = 42069) -> void:
|
||||
connection = ENetConnection.new()
|
||||
var error: Error = connection.create_host_bound(ip_address, port)
|
||||
if error:
|
||||
print("Server starting failed: ", error_string(error))
|
||||
connection = null
|
||||
return
|
||||
|
||||
print("Server started")
|
||||
is_server = true
|
||||
|
||||
|
||||
func peer_connected(peer: ENetPacketPeer) -> void:
|
||||
var peer_id: int = available_peer_ids.pop_back()
|
||||
peer.set_meta("id", peer_id)
|
||||
client_peers[peer_id] = peer
|
||||
|
||||
print("Peer connected with assigned id: ", peer_id)
|
||||
on_peer_connected.emit(peer_id)
|
||||
|
||||
|
||||
func peer_disconnected(peer: ENetPacketPeer) -> void:
|
||||
var peer_id: int = peer.get_meta("id")
|
||||
available_peer_ids.push_back(peer_id)
|
||||
client_peers.erase(peer_id)
|
||||
|
||||
print("Succesfully disconnected: ", peer_id, " from server!")
|
||||
on_peer_disconnected.emit(peer_id)
|
||||
|
||||
|
||||
func start_client(ip_address: String = "127.0.0.1", port: int = 42069) -> void:
|
||||
connection = ENetConnection.new()
|
||||
var error: Error = connection.create_host(1)
|
||||
if error:
|
||||
print("Client starting failed: ", error_string(error))
|
||||
connection = null
|
||||
return
|
||||
|
||||
print("Client started")
|
||||
server_peer = connection.connect_to_host(ip_address, port)
|
||||
|
||||
|
||||
func disconnect_client() -> void:
|
||||
if is_server: return
|
||||
|
||||
server_peer.peer_disconnect()
|
||||
|
||||
|
||||
func connected_to_server() -> void:
|
||||
print("Successfully connected to server!")
|
||||
on_connected_to_server.emit()
|
||||
|
||||
|
||||
func disconnected_from_server() -> void:
|
||||
print("Successfully disconnected from server!")
|
||||
on_disconnected_from_server.emit()
|
||||
connection = null
|
||||
@ -1 +0,0 @@
|
||||
uid://yhu84qgcru8e
|
||||
@ -1,41 +0,0 @@
|
||||
extends CharacterBody2D
|
||||
|
||||
const SPEED: float = 500.0
|
||||
|
||||
var is_authority: bool:
|
||||
get: return !LowLevelNetworkHandler.is_server && owner_id == ClientNetworkGlobals.id
|
||||
|
||||
var owner_id: int
|
||||
|
||||
func _enter_tree() -> void:
|
||||
ServerNetworkGlobals.handle_player_position.connect(server_handle_player_position)
|
||||
ClientNetworkGlobals.handle_player_position.connect(client_handle_player_position)
|
||||
|
||||
|
||||
func _exit_tree() -> void:
|
||||
ServerNetworkGlobals.handle_player_position.disconnect(server_handle_player_position)
|
||||
ClientNetworkGlobals.handle_player_position.disconnect(client_handle_player_position)
|
||||
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
if !is_authority: return
|
||||
|
||||
velocity = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down") * SPEED
|
||||
|
||||
move_and_slide()
|
||||
|
||||
PlayerPosition.create(owner_id, global_position).send(LowLevelNetworkHandler.server_peer)
|
||||
|
||||
|
||||
func server_handle_player_position(peer_id: int, player_position: PlayerPosition) -> void:
|
||||
if owner_id != peer_id: return
|
||||
|
||||
global_position = player_position.position
|
||||
|
||||
PlayerPosition.create(owner_id, global_position).broadcast(LowLevelNetworkHandler.connection)
|
||||
|
||||
|
||||
func client_handle_player_position(player_position: PlayerPosition) -> void:
|
||||
if is_authority || owner_id != player_position.id: return
|
||||
|
||||
global_position = player_position.position
|
||||
@ -1 +0,0 @@
|
||||
uid://b0ye6wj30inr6
|
||||
@ -1,17 +0,0 @@
|
||||
extends Node
|
||||
|
||||
# const LOW_LEVEL_NETWORK_PLAYER = preload("res://low_level_example/scenes/low_level_network_player.tscn")
|
||||
|
||||
func _ready() -> void:
|
||||
LowLevelNetworkHandler.on_peer_connected.connect(spawn_player)
|
||||
ClientNetworkGlobals.handle_local_id_assignment.connect(spawn_player)
|
||||
ClientNetworkGlobals.handle_remote_id_assignment.connect(spawn_player)
|
||||
|
||||
|
||||
func spawn_player(id: int) -> void:
|
||||
pass
|
||||
# var player = LOW_LEVEL_NETWORK_PLAYER.instantiate()
|
||||
# player.owner_id = id
|
||||
# player.name = str(id) # Optional, but it beats the name "@CharacterBody2D@2/3/4..."
|
||||
|
||||
# call_deferred("add_child", player)
|
||||
@ -1 +0,0 @@
|
||||
uid://dxf8dqkg3c4ph
|
||||
@ -1,35 +0,0 @@
|
||||
class_name IDAssignment extends PacketInfo
|
||||
|
||||
var id: int
|
||||
var remote_ids: Array[int]
|
||||
|
||||
static func create(id: int, remote_ids: Array[int]) -> IDAssignment:
|
||||
var info: IDAssignment = IDAssignment.new()
|
||||
info.packet_type = PACKET_TYPE.ID_ASSIGNMENT
|
||||
info.flag = ENetPacketPeer.FLAG_RELIABLE
|
||||
info.id = id
|
||||
info.remote_ids = remote_ids
|
||||
return info
|
||||
|
||||
|
||||
static func create_from_data(data: PackedByteArray) -> IDAssignment:
|
||||
var info: IDAssignment = IDAssignment.new()
|
||||
info.decode(data)
|
||||
return info
|
||||
|
||||
|
||||
func encode() -> PackedByteArray:
|
||||
var data: PackedByteArray = super.encode()
|
||||
data.resize(2 + remote_ids.size())
|
||||
data.encode_u8(1, id)
|
||||
for i in remote_ids.size():
|
||||
var id: int = remote_ids[i]
|
||||
data.encode_u8(2 + i, id)
|
||||
return data
|
||||
|
||||
|
||||
func decode(data: PackedByteArray) -> void:
|
||||
super.decode(data)
|
||||
id = data.decode_u8(1)
|
||||
for i in range(2, data.size()):
|
||||
remote_ids.append(data.decode_u8(i))
|
||||
@ -1 +0,0 @@
|
||||
uid://bingu8h4ddect
|
||||
@ -1,32 +0,0 @@
|
||||
## BASECLASS ##
|
||||
|
||||
class_name PacketInfo
|
||||
|
||||
# Don't make values above 255, since we send "packet_type" as a single byte
|
||||
enum PACKET_TYPE {
|
||||
ID_ASSIGNMENT = 0,
|
||||
PLAYER_POSITION = 10,
|
||||
}
|
||||
|
||||
var packet_type: PACKET_TYPE
|
||||
var flag: int
|
||||
|
||||
# Override function in derived classes
|
||||
func encode() -> PackedByteArray:
|
||||
var data: PackedByteArray
|
||||
data.resize(1)
|
||||
data.encode_u8(0, packet_type)
|
||||
return data
|
||||
|
||||
|
||||
# Override function in derived classes
|
||||
func decode(data: PackedByteArray) -> void:
|
||||
packet_type = data.decode_u8(0)
|
||||
|
||||
|
||||
func send(target: ENetPacketPeer) -> void:
|
||||
target.send(0, encode(), flag)
|
||||
|
||||
|
||||
func broadcast(server: ENetConnection) -> void:
|
||||
server.broadcast(0, encode(), flag)
|
||||
@ -1 +0,0 @@
|
||||
uid://bjdyqwbttme7c
|
||||
@ -1,33 +0,0 @@
|
||||
class_name PlayerPosition extends PacketInfo
|
||||
|
||||
var id: int
|
||||
var position: Vector2
|
||||
|
||||
static func create(id: int, position: Vector2) -> PlayerPosition:
|
||||
var info: PlayerPosition = PlayerPosition.new()
|
||||
info.packet_type = PACKET_TYPE.PLAYER_POSITION
|
||||
info.flag = ENetPacketPeer.FLAG_UNSEQUENCED
|
||||
info.id = id
|
||||
info.position = position
|
||||
return info
|
||||
|
||||
|
||||
static func create_from_data(data: PackedByteArray) -> PlayerPosition:
|
||||
var info: PlayerPosition = PlayerPosition.new()
|
||||
info.decode(data)
|
||||
return info
|
||||
|
||||
|
||||
func encode() -> PackedByteArray:
|
||||
var data: PackedByteArray = super.encode()
|
||||
data.resize(10)
|
||||
data.encode_u8(1, id)
|
||||
data.encode_float(2, position.x)
|
||||
data.encode_float(6, position.y)
|
||||
return data
|
||||
|
||||
|
||||
func decode(data: PackedByteArray) -> void:
|
||||
super.decode(data)
|
||||
id = data.decode_u8(1)
|
||||
position = Vector2(data.decode_float(2), data.decode_float(6))
|
||||
@ -1 +0,0 @@
|
||||
uid://c87eugk8d8d6w
|
||||
@ -1,30 +0,0 @@
|
||||
extends Node
|
||||
|
||||
signal handle_player_position(peer_id: int, player_position: PlayerPosition)
|
||||
|
||||
var peer_ids: Array[int]
|
||||
|
||||
func _ready() -> void:
|
||||
LowLevelNetworkHandler.on_peer_connected.connect(on_peer_connected)
|
||||
LowLevelNetworkHandler.on_peer_disconnected.connect(on_peer_disconnected)
|
||||
LowLevelNetworkHandler.on_server_packet.connect(on_server_packet)
|
||||
|
||||
|
||||
func on_peer_connected(peer_id: int) -> void:
|
||||
peer_ids.append(peer_id)
|
||||
|
||||
IDAssignment.create(peer_id, peer_ids).broadcast(LowLevelNetworkHandler.connection)
|
||||
|
||||
|
||||
func on_peer_disconnected(peer_id: int) -> void:
|
||||
peer_ids.erase(peer_id)
|
||||
|
||||
# Create IDUnassignment to broadcast to all still connected peers
|
||||
|
||||
|
||||
func on_server_packet(peer_id: int, data: PackedByteArray) -> void:
|
||||
match data[0]:
|
||||
PacketInfo.PACKET_TYPE.PLAYER_POSITION:
|
||||
handle_player_position.emit(peer_id, PlayerPosition.create_from_data(data))
|
||||
_:
|
||||
push_error("Packet type with index ", data[0], " unhandled!")
|
||||
@ -1 +0,0 @@
|
||||
uid://bo2hh6e60ikth
|
||||
Reference in New Issue
Block a user