This commit is contained in:
Hartmut Seichter 2025-06-26 21:19:51 +02:00
parent 127eef5a8b
commit 3b790a7217
5 changed files with 131 additions and 24 deletions

3
Notes.md Normal file
View file

@ -0,0 +1,3 @@
# Resources
- [vrpn-rs description of VRPN protocol](https://github.com/vrpn/vrpn-rs/blob/main/Protocol.md)

View file

@ -1,6 +1,11 @@
[gd_scene load_steps=2 format=3 uid="uid://bj5ykdjle10tt"]
[ext_resource type="Script" uid="uid://ca5psnjx63ua8" path="res://uvrpn.gd" id="1_gxo8o"]
[ext_resource type="Script" uid="uid://vnywsf0rn1ax" path="res://SocketClient.gd" id="1_gxo8o"]
[node name="Node3D" type="Node3D"]
script = ExtResource("1_gxo8o")
[connection signal="connected" from="." to="." method="_on_connected"]
[connection signal="data" from="." to="." method="_on_data"]
[connection signal="disconnected" from="." to="." method="_on_disconnected"]
[connection signal="error" from="." to="." method="_on_error"]

84
SocketClient.gd Normal file
View file

@ -0,0 +1,84 @@
extends Node
signal connected(s:StreamPeerTCP)
signal data(data:Array)
signal disconnected
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:
_stream.poll()
var new_status = _stream.get_status()
if new_status != _status:
_status = new_status
match _status:
_stream.STATUS_NONE:
print("Disconnected from host.")
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")
if _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()
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])
emit_signal("error")
else:
emit_signal("data", data[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:
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)
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.

1
SocketClient.gd.uid Normal file
View file

@ -0,0 +1 @@
uid://vnywsf0rn1ax

View file

@ -3,34 +3,48 @@ extends Node
@export var url : String = "localhost"
@export var port : int = 3883
var server : UDPServer = null
var socket : StreamPeerTCP = StreamPeerTCP.new()
var vrpn_cookie : String = "vrpn: ver. 07.35"
#var server : UDPServer = null
@onready var socket : StreamPeerTCP = StreamPeerTCP.new()
func _ready() -> void:
if StreamPeerTCP.STATUS_NONE == socket.get_status():
if socket.connect_to_host("localhost",3883) == OK:
send_data(socket,"localhost".to_utf8_buffer())
server = UDPServer.new()
server.listen(3883)
#if StreamPeerTCP.STATUS_NONE == socket.get_status():
if socket.connect_to_host(url,port) == OK:
print("Socket connected ...")
socket.set_no_delay(true)
send_data(socket,vrpn_cookie.to_utf8_buffer())
#server = UDPServer.new()
#server.listen(3883)
else:
print("Error connecting to server")
#else:
#print("Stream not ready")
func _process(delta: float) -> void:
server.poll()
if server.is_connection_available():
var peer = server.take_connection()
var packet = peer.get_packet()
print("Accepted peer: %s:%s" % [peer.get_packet_ip(), peer.get_packet_port()])
print("Received data: %s" % [packet.get_string_from_utf8()])
# Reply so it knows we received the message.
peer.put_packet(packet)
if socket:
socket.poll()
##return
##if server:
##server.poll()
##if server.is_connection_available():
##var peer = server.take_connection()
##var packet = peer.get_packet()
##print("Accepted peer: %s:%s" % [peer.get_packet_ip(), peer.get_packet_port()])
##print("Received data: %s" % [packet.get_string_from_utf8()])
### Reply so it knows we received the message.
##peer.put_packet(packet)
static func send_data(socket : StreamPeerTCP, data: PackedByteArray) -> bool:
if socket.get_status() != StreamPeerTCP.STATUS_CONNECTED:
print("Error: Stream is not currently connected.")
return false
var error: int = socket.put_data(data)
if error != OK:
print("Error writing to stream: ", error)
return false
return true
print(socket.get_status())
if socket.get_status() == StreamPeerTCP.STATUS_CONNECTED:
if socket.put_data(data) == OK:
return true
else:
print("Error writing data ...")
return false
else:
print("Error connecting")
return false