test.tscn 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. [gd_scene load_steps=3 format=3 uid="uid://cvfeet1dqmpoe"]
  2. [ext_resource type="PackedScene" uid="uid://cdt4rn31xeo85" path="res://microserver.tscn" id="1_0r1oj"]
  3. [sub_resource type="GDScript" id="GDScript_loein"]
  4. script/source = "extends Node
  5. @export
  6. var server_path:NodePath
  7. var server
  8. var service_id_1:int
  9. var service_id_2:int
  10. const ECHO_FREQ:int = 4
  11. var echo:Thread
  12. func _ready():
  13. server = get_node(server_path)
  14. service_id_1 = server.add_service(callback_1)
  15. service_id_2 = server.add_service(callback_2)
  16. echo = Thread.new()
  17. echo.start(func():
  18. while server != null:
  19. server.send_message(service_id_2, '')
  20. OS.delay_msec(ECHO_FREQ)
  21. )
  22. func _exit_tree():
  23. server.remove_service(callback_1)
  24. server.remove_service(callback_2)
  25. server = null
  26. echo.wait_to_finish()
  27. echo = null
  28. func callback_1(message):
  29. print('message received {0} by [thread: {1}]'.format([message, OS.get_thread_caller_id()]))
  30. var callback_2_counter:int
  31. var callback_2_tick:int = Time.get_ticks_msec()
  32. func callback_2(_message):
  33. const COUNT:int = 250
  34. callback_2_counter += 1
  35. if callback_2_counter >= COUNT:
  36. callback_2_counter = 0
  37. print('processed {0} messages cost {1} ms (design: {2} ms)'.format([COUNT, Time.get_ticks_msec() - callback_2_tick, ECHO_FREQ * COUNT]))
  38. callback_2_tick = Time.get_ticks_msec()
  39. func _on_button_button_up():
  40. var message = 'click at {0} [thread: {1}]'.format([Time.get_time_string_from_system(), OS.get_thread_caller_id()])
  41. server.send_message(service_id_1, message)
  42. func _on_timer_timeout():
  43. var message = 'timer triggerd at {0} [thread: {1}]'.format([Time.get_time_string_from_system(), OS.get_thread_caller_id()])
  44. server.send_message(service_id_1, message)
  45. "
  46. [node name="test" type="Node"]
  47. [node name="microserver" parent="." instance=ExtResource("1_0r1oj")]
  48. [node name="sub1" type="Node" parent="."]
  49. script = SubResource("GDScript_loein")
  50. server_path = NodePath("../microserver")
  51. [node name="Button" type="Button" parent="sub1"]
  52. offset_right = 8.0
  53. offset_bottom = 8.0
  54. text = "Send Message !"
  55. [node name="Timer" type="Timer" parent="sub1"]
  56. autostart = true
  57. [connection signal="button_up" from="sub1/Button" to="sub1" method="_on_button_button_up"]
  58. [connection signal="timeout" from="sub1/Timer" to="sub1" method="_on_timer_timeout"]