-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_handler.gd
86 lines (63 loc) · 2.25 KB
/
server_handler.gd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
extends Node
enum ListenerTypes {
NONE = 0,
REST,
WEBSOCKET,
UDP
}
var logger := Logger.new("RemoteControlServerHandler")
var server
var connection
var thread: Thread
var stop_server := true
var poll_interval: int = 100
var _scene_tree: SceneTree
#-----------------------------------------------------------------------------#
# Builtin functions #
#-----------------------------------------------------------------------------#
func _init(p_server: GDScript, port: int, p_poll_interval: float) -> void:
_scene_tree = Engine.get_main_loop()
poll_interval = int(p_poll_interval * 1000)
thread = Thread.new()
stop_server = false
server = p_server.new(logger)
server.listen(port)
thread.start(self, "_perform_reception")
func _exit_tree() -> void:
if thread != null:
stop(true)
#-----------------------------------------------------------------------------#
# Connections #
#-----------------------------------------------------------------------------#
#-----------------------------------------------------------------------------#
# Private functions #
#-----------------------------------------------------------------------------#
func _perform_reception() -> void:
while not stop_server:
server.poll()
OS.delay_msec(poll_interval)
server.shutdown()
func _perform_reception_websocket() -> void:
while not stop_server:
server.poll()
yield(_scene_tree.create_timer(poll_interval), "timeout")
func _perform_reception_udp() -> void:
while not stop_server:
server.poll()
yield(_scene_tree.create_timer(poll_interval), "timeout")
func _perform_reception_tcp() -> void:
while not stop_server:
pass
pass
#-----------------------------------------------------------------------------#
# Public functions #
#-----------------------------------------------------------------------------#
func stop(force: bool = false) -> void:
stop_server = true
if not force:
yield(get_tree(), "idle_frame")
while thread.is_alive():
yield(get_tree(), "idle_frame")
thread.wait_to_finish()
thread = null
server = null