gdvrpn/SocketClient.gd

131 lines
3.7 KiB
GDScript3
Raw Normal View History

2025-06-26 21:19:51 +02:00
extends Node
signal connected(s:StreamPeerTCP)
signal data(data:Array)
signal disconnected
signal error
@onready var _stream: StreamPeerTCP = StreamPeerTCP.new()
func _ready() -> void:
self.connect_to_host("127.0.0.1",3883)
func _process(delta: float) -> void:
2025-06-27 00:24:43 +02:00
var old_status = _stream.get_status()
2025-06-26 21:19:51 +02:00
_stream.poll()
var new_status = _stream.get_status()
2025-06-27 00:24:43 +02:00
if old_status != new_status:
match new_status:
2025-06-26 21:19:51 +02:00
_stream.STATUS_NONE:
emit_signal("disconnected")
_stream.STATUS_CONNECTING:
print("Connecting.")
_stream.STATUS_CONNECTED:
print("Connected.")
emit_signal("connected",_stream.poll()
)
_stream.STATUS_ERROR:
print("Error with socket stream.")
emit_signal("error")
2025-06-27 00:24:43 +02:00
if new_status == _stream.STATUS_CONNECTED:
2025-06-26 21:19:51 +02:00
var available_bytes: int = _stream.get_available_bytes()
if available_bytes > 0:
2025-06-27 00:24:43 +02:00
var res = _stream.get_partial_data(available_bytes)
if res[0] != OK:
2025-06-26 21:19:51 +02:00
emit_signal("error")
else:
2025-06-27 00:24:43 +02:00
emit_signal("data", res[1])
2025-06-26 21:19:51 +02:00
func connect_to_host(host: String, port: int) -> void:
print("Connecting to %s:%d" % [host, port])
if _stream.connect_to_host(host, port) != OK:
print("Error connecting to host.")
emit_signal("error")
func send(data: PackedByteArray) -> bool:
2025-06-27 00:24:43 +02:00
if _stream.get_status() != _stream.STATUS_CONNECTED:
2025-06-26 21:19:51 +02:00
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
2025-06-27 00:24:43 +02:00
2025-06-26 21:19:51 +02:00
func _on_data(data : Array):
2025-06-27 00:24:43 +02:00
var bytes = PackedByteArray(data)
var as_cookie = bytes.get_string_from_ascii()
2025-06-26 21:19:51 +02:00
2025-06-27 00:24:43 +02:00
# Cookie Hack!
if as_cookie.begins_with("vrpn: ver."):
2025-06-27 12:24:18 +02:00
# kaboom we just send back the same cookie :)
2025-06-27 00:24:43 +02:00
self.send(bytes)
else:
2025-06-27 16:04:08 +02:00
# non-cookie
var header := StreamPeerBuffer.new()
header.data_array = bytes.slice(0,aligned_size(20))
header.big_endian = true
2025-06-27 13:16:38 +02:00
2025-06-27 14:32:17 +02:00
# Header
2025-06-27 16:04:08 +02:00
var length = header.get_32() # length of message
var time_sec = header.get_32() # time sec
var time_msec = header.get_32() # time micro sec
var sender_id = header.get_32() # sender id
var message_type = header.get_32() # type of message (payload)
var sequence_num = header.get_32() # inofficial sequence number (padding)
2025-06-27 13:16:38 +02:00
2025-06-27 16:04:08 +02:00
if false:
print("length %d" % length)
print("time_sec %d" % time_sec)
print("time_msec %d" % time_msec)
print("sender_id %d" % sender_id)
print("message_type %d" % message_type)
print("sequence_num %d" % sequence_num)
2025-06-27 13:16:38 +02:00
2025-06-27 16:04:08 +02:00
#print(bytes)
# sender description
if message_type == -1:
var body := StreamPeerBuffer.new()
body.data_array = bytes.slice(24,length-24)
body.big_endian = true
var body_length = body.get_32()
print(body_length)
2025-06-27 13:16:38 +02:00
2025-06-27 16:04:08 +02:00
#var sender_name = peer.get_string(body_length)
#print("sender id {0} is {1}".format([sender_id,sender_name]))
2025-06-27 13:16:38 +02:00
2025-06-27 16:04:08 +02:00
# quat pos
if message_type == 4:
var body := StreamPeerBuffer.new()
2025-06-27 13:16:38 +02:00
2025-06-27 16:04:08 +02:00
body.data_array = bytes.slice(24,length-24)
body.big_endian = true
var sensor_id = body.get_32()
var padding = body.get_32()
var pos = Vector3(body.get_float(),body.get_float(),body.get_float())
var quat_w = body.get_float()
var quat_x = body.get_float()
var quat_y = body.get_float()
var quat_z = body.get_float()
var quat = Quaternion(quat_x,quat_y,quat_z,quat_w)
#print("Pos_Quat ",sensor_id,pos,quat)
static func aligned_size(actual_size : int,alignment : int = 8) -> int:
return (actual_size + alignment - 1) & ~(alignment - 1)
2025-06-27 00:24:43 +02:00
2025-06-26 21:19:51 +02:00
func _on_connected(s : StreamPeerTCP):
print("Connected to",s.get_connected_host()) # Replace with function body.
func _on_disconnected():
print("Disconnected") # Replace with function body.
func _on_error():
print("Error") # Replace with function body.