add more structure
This commit is contained in:
parent
a4d0e8f33b
commit
36c7517cb8
6 changed files with 26 additions and 28 deletions
|
@ -16,7 +16,7 @@ var messages : Dictionary[int,String] = {}
|
|||
signal connected(s:StreamPeerTCP)
|
||||
signal data(data:Array)
|
||||
signal disconnected
|
||||
signal error
|
||||
signal error(msg:String)
|
||||
|
||||
|
||||
@onready var _stream: StreamPeerTCP = StreamPeerTCP.new()
|
||||
|
@ -40,45 +40,44 @@ func _ready() -> void:
|
|||
error.connect(self._on_error)
|
||||
|
||||
func _process(delta: float) -> void:
|
||||
# store old state
|
||||
var old_status = _stream.get_status()
|
||||
# poll
|
||||
_stream.poll()
|
||||
# receive all errors
|
||||
var new_status = _stream.get_status()
|
||||
# process if something changed
|
||||
if old_status != new_status:
|
||||
match new_status:
|
||||
_stream.STATUS_NONE:
|
||||
emit_signal("disconnected")
|
||||
_stream.STATUS_CONNECTING:
|
||||
print("Connecting.")
|
||||
pass
|
||||
_stream.STATUS_CONNECTED:
|
||||
print("Connected.")
|
||||
_stream.poll()
|
||||
emit_signal("connected",_stream)
|
||||
_stream.STATUS_ERROR:
|
||||
print("Error with socket stream.")
|
||||
emit_signal("error")
|
||||
emit_signal("error","error with socket stream from {0}:{1}".format([_stream.get_connected_host(),_stream.get_connected_port()]))
|
||||
|
||||
if new_status == _stream.STATUS_CONNECTED:
|
||||
var available_bytes: int = _stream.get_available_bytes()
|
||||
if available_bytes > 0:
|
||||
var res = _stream.get_partial_data(available_bytes)
|
||||
if res[0] != OK:
|
||||
emit_signal("error")
|
||||
emit_signal("error","error receiving data {0}".format([res[0]]))
|
||||
else:
|
||||
emit_signal("data", res[1])
|
||||
|
||||
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")
|
||||
emit_signal("error","error connecting to host '{0}:{1}'".format([host,port]))
|
||||
|
||||
func send(data: PackedByteArray) -> bool:
|
||||
if _stream.get_status() != _stream.STATUS_CONNECTED:
|
||||
print("Error: Stream is not currently connected.")
|
||||
emit_signal("error","stream not connected!")
|
||||
return false
|
||||
var error: int = _stream.put_data(data)
|
||||
if error != OK:
|
||||
print("Error writing to stream: ", error)
|
||||
var res: int = _stream.put_data(data)
|
||||
if res != OK:
|
||||
emit_signal("error","error writing to stream {0}!".format([res]))
|
||||
return false
|
||||
return true
|
||||
|
||||
|
@ -99,10 +98,10 @@ 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.
|
||||
push_warning("Disconnected") # Replace with function body.
|
||||
|
||||
func _on_error():
|
||||
print("Error") # Replace with function body.
|
||||
func _on_error(msg:String):
|
||||
push_warning(msg) # Replace with function body.
|
||||
|
||||
|
||||
static func marshall_block(data : PackedByteArray,session : VRPN) -> void:
|
||||
|
@ -122,7 +121,7 @@ static func marshall_block(data : PackedByteArray,session : VRPN) -> void:
|
|||
var length := header.get_32() as int # length of message
|
||||
var time_sec := header.get_32() as int # datetime sec
|
||||
var time_msec := header.get_32() as int # datetime micro sec
|
||||
var sender_id := header.get_32() as int # sender id
|
||||
var sender_id := header.get_32() as int # sender id (tracker or interfaces)
|
||||
var message_type := header.get_32() as int # type of message (payload)
|
||||
var sequence_num := header.get_32() as int # inofficial sequence number (padding)
|
||||
|
||||
|
|
|
@ -7,6 +7,7 @@ class_name VRPN_Receiver
|
|||
@export var tracker_use_position : bool = true
|
||||
@export var tracker_use_rotation : bool = true
|
||||
|
||||
|
||||
func _on_pos_quat(tracker_data : Dictionary):
|
||||
|
||||
if tracker_data['tracker'] == tracker_name and tracker_data['sensor'] == tracker_sensor:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue