Added thread safe packet handlers
This commit is contained in:
parent
bfec08ff02
commit
ded6f51c5d
@ -9,10 +9,14 @@ var history: Array[float] = []
|
|||||||
|
|
||||||
func _init():
|
func _init():
|
||||||
history.resize(HISTORY_SIZE)
|
history.resize(HISTORY_SIZE)
|
||||||
|
thread_safe = true
|
||||||
|
|
||||||
func handle_packet(data: PackedByteArray):
|
func handle_packet(data: PackedByteArray):
|
||||||
var id := data[0]
|
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)
|
changed.emit(ping)
|
||||||
|
|
||||||
func send_ping():
|
func send_ping():
|
||||||
@ -20,9 +24,6 @@ func send_ping():
|
|||||||
buffer.put_u8(Packet.PING)
|
buffer.put_u8(Packet.PING)
|
||||||
buffer.put_u8(count)
|
buffer.put_u8(count)
|
||||||
%Client.send(buffer.data_array)
|
%Client.send(buffer.data_array)
|
||||||
history[count] = get_time()
|
history[count] = Time.get_unix_time_from_system()
|
||||||
count = (count + 1) % HISTORY_SIZE
|
count = (count + 1) % HISTORY_SIZE
|
||||||
|
|
||||||
func get_time() -> float:
|
|
||||||
return Time.get_unix_time_from_system()
|
|
||||||
|
|
||||||
|
@ -21,5 +21,9 @@ func handle_packet(packet: PackedByteArray):
|
|||||||
push_warning("Unknown packet type %d" % type)
|
push_warning("Unknown packet type %d" % type)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
if handler.thread_safe:
|
||||||
|
handler.handle_packet(packet.slice(1))
|
||||||
|
else:
|
||||||
handler.handle_packet.call_deferred(packet.slice(1))
|
handler.handle_packet.call_deferred(packet.slice(1))
|
||||||
|
|
||||||
download += packet.size()
|
download += packet.size()
|
||||||
|
@ -16,5 +16,7 @@ enum Packet {
|
|||||||
|
|
||||||
@export var packet_type: Packet
|
@export var packet_type: Packet
|
||||||
|
|
||||||
|
var thread_safe: bool
|
||||||
|
|
||||||
func handle_packet(_data: PackedByteArray):
|
func handle_packet(_data: PackedByteArray):
|
||||||
pass
|
pass
|
Loading…
x
Reference in New Issue
Block a user