Change how different sensor types ar handled
This commit is contained in:
@@ -5,7 +5,7 @@ extends Node
|
||||
var udp = PacketPeerUDP.new()
|
||||
var connected = false
|
||||
|
||||
var fast_broadcast_rate = 1.0
|
||||
var fast_broadcast_rate = 0.5
|
||||
var slow_broadcast_rate = 10.0
|
||||
var current_broadcast_rate = fast_broadcast_rate
|
||||
var last_broadcast = 0
|
||||
@@ -16,6 +16,7 @@ var thread: Thread
|
||||
var mutex: Mutex
|
||||
var signal_queue = []
|
||||
var exit_thread = false
|
||||
var use_thread = true
|
||||
|
||||
func _ready():
|
||||
_try_set_ip()
|
||||
@@ -23,44 +24,54 @@ func _ready():
|
||||
udp.set_dest_address("192.168.0.255", 5000)
|
||||
SensorSignals.new_sensor_connected.connect(_on_sensor_connected)
|
||||
|
||||
thread = Thread.new()
|
||||
use_thread = OS.get_processor_count() > 1
|
||||
mutex = Mutex.new()
|
||||
|
||||
thread.start(_thread_function)
|
||||
if use_thread:
|
||||
thread = Thread.new()
|
||||
print('Starting UDP Broadcast thread')
|
||||
thread.start(_thread_function)
|
||||
else:
|
||||
print('UDP server running in single threaded mode')
|
||||
|
||||
func _process(delta: float) -> void:
|
||||
mutex.lock()
|
||||
last_broadcast += delta
|
||||
mutex.unlock()
|
||||
if not use_thread:
|
||||
_process_broadcast()
|
||||
|
||||
func _exit_tree() -> void:
|
||||
mutex.lock()
|
||||
exit_thread = true
|
||||
mutex.unlock()
|
||||
thread.wait_to_finish()
|
||||
if use_thread:
|
||||
mutex.lock()
|
||||
exit_thread = true
|
||||
mutex.unlock()
|
||||
thread.wait_to_finish()
|
||||
|
||||
func _thread_function():
|
||||
print('UDP Broadcast thread started')
|
||||
while true:
|
||||
mutex.lock()
|
||||
var should_exit = exit_thread
|
||||
mutex.unlock()
|
||||
if should_exit:
|
||||
break
|
||||
|
||||
if ip_address.length() == 0:
|
||||
_try_set_ip()
|
||||
return
|
||||
mutex.lock()
|
||||
var do_broadcast = last_broadcast >= current_broadcast_rate
|
||||
mutex.unlock()
|
||||
if do_broadcast:
|
||||
# Try to contact server
|
||||
mutex.lock()
|
||||
last_broadcast = 0
|
||||
mutex.unlock()
|
||||
var broadcast_str = ip_address + "|%s" % Time.get_datetime_string_from_system(true)
|
||||
print('Broadcasting to UDP: ', broadcast_str)
|
||||
udp.put_packet(broadcast_str.to_utf8_buffer())
|
||||
_process_broadcast()
|
||||
|
||||
func _process_broadcast():
|
||||
if ip_address.length() == 0:
|
||||
_try_set_ip()
|
||||
return
|
||||
# Try to contact server
|
||||
var broadcast_str = ip_address + "|%s" % Time.get_datetime_string_from_system(true)
|
||||
print('Broadcasting to UDP: ', broadcast_str)
|
||||
udp.put_packet(broadcast_str.to_utf8_buffer())
|
||||
|
||||
func _try_set_ip():
|
||||
for address in IP.get_local_addresses():
|
||||
|
||||
Reference in New Issue
Block a user