This commit is contained in:
Hartmut Seichter 2025-06-27 00:24:43 +02:00
parent 3b790a7217
commit 9e60cbd0e9

View file

@ -7,20 +7,16 @@ signal error
@onready var _stream: StreamPeerTCP = StreamPeerTCP.new() @onready var _stream: StreamPeerTCP = StreamPeerTCP.new()
var _status: StreamPeerTCP.Status = StreamPeerTCP.STATUS_NONE
func _ready() -> void: func _ready() -> void:
_status = _stream.get_status()
self.connect_to_host("127.0.0.1",3883) self.connect_to_host("127.0.0.1",3883)
func _process(delta: float) -> void: func _process(delta: float) -> void:
var old_status = _stream.get_status()
_stream.poll() _stream.poll()
var new_status = _stream.get_status() var new_status = _stream.get_status()
if new_status != _status: if old_status != new_status:
_status = new_status match new_status:
match _status:
_stream.STATUS_NONE: _stream.STATUS_NONE:
print("Disconnected from host.")
emit_signal("disconnected") emit_signal("disconnected")
_stream.STATUS_CONNECTING: _stream.STATUS_CONNECTING:
print("Connecting.") print("Connecting.")
@ -32,46 +28,49 @@ func _process(delta: float) -> void:
print("Error with socket stream.") print("Error with socket stream.")
emit_signal("error") emit_signal("error")
if _status == _stream.STATUS_CONNECTED: if new_status == _stream.STATUS_CONNECTED:
if Input.is_action_just_pressed("ui_accept"):
print("Sending ...")
self.send("vrpn".to_ascii_buffer())
var available_bytes: int = _stream.get_available_bytes() var available_bytes: int = _stream.get_available_bytes()
if available_bytes > 0: if available_bytes > 0:
print("available bytes: ", available_bytes) print("available bytes: ", available_bytes)
var data: Array = _stream.get_partial_data(available_bytes) var res = _stream.get_partial_data(available_bytes)
# Check for read error. if res[0] != OK:
if data[0] != OK:
print("Error getting data from stream: ", data[0])
emit_signal("error") emit_signal("error")
else: else:
emit_signal("data", data[1]) emit_signal("data", res[1])
func connect_to_host(host: String, port: int) -> void: func connect_to_host(host: String, port: int) -> void:
print("Connecting to %s:%d" % [host, port]) 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: if _stream.connect_to_host(host, port) != OK:
print("Error connecting to host.") print("Error connecting to host.")
emit_signal("error") emit_signal("error")
func send(data: PackedByteArray) -> bool: func send(data: PackedByteArray) -> bool:
if _status != _stream.STATUS_CONNECTED: if _stream.get_status() != _stream.STATUS_CONNECTED:
print("Error: Stream is not currently connected.") print("Error: Stream is not currently connected.")
return false return false
var error: int = _stream.put_data(data) var error: int = _stream.put_data(data)
if error != OK: if error != OK:
print("Error writing to stream: ", error) print("Error writing to stream: ", error)
return false return false
return true return true
func _on_data(data : Array): func _on_data(data : Array):
var buf = PackedByteArray(data) var bytes = PackedByteArray(data)
var cookie = buf.get_string_from_ascii() var as_cookie = bytes.get_string_from_ascii()
print("Data\t",cookie) # Replace with function body.
self.send(buf)
# 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): func _on_connected(s : StreamPeerTCP):
print("Connected to",s.get_connected_host()) # Replace with function body. print("Connected to",s.get_connected_host()) # Replace with function body.