Added thread safe packet handlers

This commit is contained in:
Eduard Urbach 2024-02-28 12:14:27 +01:00
parent bfec08ff02
commit ded6f51c5d
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
4 changed files with 14 additions and 7 deletions

View File

@ -9,10 +9,14 @@ var history: Array[float] = []
func _init():
history.resize(HISTORY_SIZE)
thread_safe = true
func handle_packet(data: PackedByteArray):
var id := data[0]
var ping := get_time() - history[id]
var ping := Time.get_unix_time_from_system() - history[id]
emit.call_deferred(ping)
func emit(ping: float):
changed.emit(ping)
func send_ping():
@ -20,9 +24,6 @@ func send_ping():
buffer.put_u8(Packet.PING)
buffer.put_u8(count)
%Client.send(buffer.data_array)
history[count] = get_time()
history[count] = Time.get_unix_time_from_system()
count = (count + 1) % HISTORY_SIZE
func get_time() -> float:
return Time.get_unix_time_from_system()

View File

@ -21,5 +21,9 @@ func handle_packet(packet: PackedByteArray):
push_warning("Unknown packet type %d" % type)
return
handler.handle_packet.call_deferred(packet.slice(1))
if handler.thread_safe:
handler.handle_packet(packet.slice(1))
else:
handler.handle_packet.call_deferred(packet.slice(1))
download += packet.size()

View File

@ -16,5 +16,7 @@ enum Packet {
@export var packet_type: Packet
var thread_safe: bool
func handle_packet(_data: PackedByteArray):
pass