Added thread safe packet handlers
This commit is contained in:
parent
bfec08ff02
commit
ded6f51c5d
@ -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()
|
||||
|
||||
|
@ -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()
|
||||
|
@ -16,5 +16,7 @@ enum Packet {
|
||||
|
||||
@export var packet_type: Packet
|
||||
|
||||
var thread_safe: bool
|
||||
|
||||
func handle_packet(_data: PackedByteArray):
|
||||
pass
|
@ -8,4 +8,4 @@ func set_download(download: int):
|
||||
%Receive.text = "%d bytes" % download
|
||||
|
||||
func set_upload(upload: int):
|
||||
%Send.text = "%d bytes" % upload
|
||||
%Send.text = "%d bytes" % upload
|
||||
|
Loading…
x
Reference in New Issue
Block a user