Added upload and download statistics

This commit is contained in:
Eduard Urbach 2024-01-26 19:00:59 +01:00
parent 8370afd9a1
commit 252d55b30f
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
10 changed files with 54 additions and 6 deletions

View File

@ -3,7 +3,11 @@ extends NetworkNode
@export var host: String
@export var port: int
signal download_changed(down: int)
signal upload_changed(up: int)
var socket := PacketPeerUDP.new()
var upload := 0
func _enter_tree():
socket.connect_to_host(host, port)
@ -17,4 +21,14 @@ func _enter_tree():
func _process(_delta):
while socket.get_available_packet_count() > 0:
var packet := socket.get_packet()
handle_packet(packet, socket)
handle_packet(packet, socket)
func update_statistics():
download_changed.emit(download)
upload_changed.emit(upload)
download = 0
upload = 0
func send(data: PackedByteArray):
socket.put_packet(data)
upload += data.size()

View File

@ -32,7 +32,7 @@ func send_login():
var buffer := StreamPeerBuffer.new()
buffer.put_8(Packet.LOGIN)
buffer.put_data(JSON.stringify([Global.username, password]).to_utf8_buffer())
%Client.socket.put_packet(buffer.data_array)
%Client.send(buffer.data_array)
print("[%s] Connecting..." % Global.username)
func is_logged_in() -> bool:

View File

@ -19,7 +19,7 @@ func send_ping():
var buffer := StreamPeerBuffer.new()
buffer.put_8(Packet.PING)
buffer.put_8(count)
%Client.socket.put_packet(buffer.data_array)
%Client.send(buffer.data_array)
history[count] = get_time()
count += 1

View File

@ -40,4 +40,4 @@ func send_position():
buffer.put_float(Global.player.position.x)
# buffer.put_float(Global.player.position.y)
buffer.put_float(Global.player.position.z)
%Client.socket.put_packet(buffer.data_array)
%Client.send(buffer.data_array)

View File

@ -2,6 +2,7 @@ class_name NetworkNode
extends Node
var handlers: Array[Node] = []
var download := 0
func _init():
handlers.resize(256)
@ -21,3 +22,4 @@ func handle_packet(packet: PackedByteArray, peer: PacketPeer):
return
handler.handle_packet(packet.slice(1), peer)
download += packet.size()

View File

@ -1,6 +1,10 @@
extends Control
var on_ping_changed: Signal
var on_download_changed: Signal
var on_upload_changed: Signal
func _enter_tree():
on_ping_changed = %Ping.changed
on_ping_changed = %Ping.changed
on_download_changed = %Client.download_changed
on_upload_changed = %Client.upload_changed

View File

@ -1,4 +1,4 @@
[gd_scene load_steps=7 format=3 uid="uid://dagn5bf7ou3sd"]
[gd_scene load_steps=9 format=3 uid="uid://dagn5bf7ou3sd"]
[ext_resource type="PackedScene" uid="uid://cch67vqpsmtej" path="res://ui/debug/DebugLabel.tscn" id="1_7s8uu"]
[ext_resource type="Script" path="res://ui/UI.gd" id="1_l5b6o"]
@ -6,6 +6,8 @@
[ext_resource type="Script" path="res://ui/debug/PingLabel.gd" id="3_xjdws"]
[ext_resource type="Script" path="res://ui/debug/PositionLabel.gd" id="4_beqf6"]
[ext_resource type="Script" path="res://ui/debug/VelocityLabel.gd" id="5_8lm6a"]
[ext_resource type="Script" path="res://ui/debug/UploadLabel.gd" id="7_cfnpx"]
[ext_resource type="Script" path="res://ui/debug/DownloadLabel.gd" id="8_ogt38"]
[node name="UI" type="Control"]
layout_mode = 3
@ -47,3 +49,11 @@ script = ExtResource("4_beqf6")
[node name="Velocity" parent="CanvasLayer/BottomLeftMargin/VBoxContainer" instance=ExtResource("1_7s8uu")]
layout_mode = 2
script = ExtResource("5_8lm6a")
[node name="Send" parent="CanvasLayer/BottomLeftMargin/VBoxContainer" instance=ExtResource("1_7s8uu")]
layout_mode = 2
script = ExtResource("7_cfnpx")
[node name="Receive" parent="CanvasLayer/BottomLeftMargin/VBoxContainer" instance=ExtResource("1_7s8uu")]
layout_mode = 2
script = ExtResource("8_ogt38")

View File

@ -0,0 +1,7 @@
extends DebugLabel
func _ready():
owner.on_download_changed.connect(on_download_changed)
func on_download_changed(download):
text = "%d bytes" % download

View File

@ -0,0 +1,7 @@
extends DebugLabel
func _ready():
owner.on_upload_changed.connect(on_upload_changed)
func on_upload_changed(upload):
text = "%d bytes" % upload

View File

@ -57,6 +57,9 @@ packet_type = 10
script = ExtResource("7_rjgcp")
packet_type = 12
[node name="Statistics" type="Timer" parent="Client"]
autostart = true
[node name="World" type="Node3D" parent="."]
[node name="Sun" type="DirectionalLight3D" parent="World"]
@ -130,3 +133,4 @@ mesh = SubResource("QuadMesh_7yiqd")
[connection signal="timeout" from="Client/Ping/Timer" to="Client/Ping" method="send_ping"]
[connection signal="timeout" from="Client/Login/Timer" to="Client/Login" method="send_login"]
[connection signal="timeout" from="Client/Statistics" to="Client" method="update_statistics"]