30 lines
620 B
GDScript

class_name NetworkNode
extends Node
var handlers: Array[Node] = []
var download := 0
func _init():
handlers.resize(256)
func get_handler(index: int) -> PacketHandler:
return handlers[index]
func set_handler(index: int, node: PacketHandler):
handlers[index] = node
func handle_packet(packet: PackedByteArray):
var type := packet.decode_u8(0)
var handler := get_handler(type)
if handler == null:
push_warning("Unknown packet type %d" % type)
return
if handler.thread_safe:
handler.handle_packet(packet.slice(1))
else:
handler.handle_packet.call_deferred(packet.slice(1))
download += packet.size()