From 9e60cbd0e97b2e038b961dd42731f147192b57e7 Mon Sep 17 00:00:00 2001 From: Hartmut Seichter Date: Fri, 27 Jun 2025 00:24:43 +0200 Subject: [PATCH] WIP --- SocketClient.gd | 49 ++++++++++++++++++++++++------------------------- 1 file changed, 24 insertions(+), 25 deletions(-) diff --git a/SocketClient.gd b/SocketClient.gd index c19994e..7d73917 100644 --- a/SocketClient.gd +++ b/SocketClient.gd @@ -7,20 +7,16 @@ signal error @onready var _stream: StreamPeerTCP = StreamPeerTCP.new() -var _status: StreamPeerTCP.Status = StreamPeerTCP.STATUS_NONE - func _ready() -> void: - _status = _stream.get_status() self.connect_to_host("127.0.0.1",3883) func _process(delta: float) -> void: + var old_status = _stream.get_status() _stream.poll() var new_status = _stream.get_status() - if new_status != _status: - _status = new_status - match _status: + if old_status != new_status: + match new_status: _stream.STATUS_NONE: - print("Disconnected from host.") emit_signal("disconnected") _stream.STATUS_CONNECTING: print("Connecting.") @@ -32,47 +28,50 @@ func _process(delta: float) -> void: print("Error with socket stream.") emit_signal("error") - if _status == _stream.STATUS_CONNECTED: - if Input.is_action_just_pressed("ui_accept"): - print("Sending ...") - self.send("vrpn".to_ascii_buffer()) - + if new_status == _stream.STATUS_CONNECTED: var available_bytes: int = _stream.get_available_bytes() if available_bytes > 0: print("available bytes: ", available_bytes) - var data: Array = _stream.get_partial_data(available_bytes) - # Check for read error. - if data[0] != OK: - print("Error getting data from stream: ", data[0]) + var res = _stream.get_partial_data(available_bytes) + if res[0] != OK: emit_signal("error") else: - emit_signal("data", data[1]) + emit_signal("data", res[1]) func connect_to_host(host: String, port: int) -> void: print("Connecting to %s:%d" % [host, port]) - # Reset status so we can tell if it changes to error again. - _status = _stream.STATUS_NONE if _stream.connect_to_host(host, port) != OK: print("Error connecting to host.") emit_signal("error") func send(data: PackedByteArray) -> bool: - if _status != _stream.STATUS_CONNECTED: + if _stream.get_status() != _stream.STATUS_CONNECTED: print("Error: Stream is not currently connected.") return false var error: int = _stream.put_data(data) if error != OK: print("Error writing to stream: ", error) return false - return true + func _on_data(data : Array): - var buf = PackedByteArray(data) - var cookie = buf.get_string_from_ascii() - print("Data\t",cookie) # Replace with function body. - self.send(buf) + var bytes = PackedByteArray(data) + var as_cookie = bytes.get_string_from_ascii() + + # Cookie Hack! + if as_cookie.begins_with("vrpn: ver."): + print("VRPN magic cookie '{0}'".format([as_cookie])) + self.send(bytes) + else: + if true: + var header_bytes = bytes.slice(0,24) + var header_i32 = header_bytes.to_int32_array() + #var vrpn_h = VRPN_Header.new(header_bytes) + print(header_i32) + print(header_bytes) + func _on_connected(s : StreamPeerTCP): print("Connected to",s.get_connected_host()) # Replace with function body.