-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGServer.gd
78 lines (58 loc) · 1.87 KB
/
GServer.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
extends Node
class_name GServer
var server : TCPServer
var clients : Array[StreamPeerTCP] = []
var clients_disconnected : Array[StreamPeerTCP] = []
var _master : GMaster
signal on_receive_move_cursor_request(offset: Vector2)
func initialize(master: GMaster):
_master = master
server = TCPServer.new()
var err = server.listen(GConfig.port)
if err:
push_error(err)
return
print("Godouse server started at port: ", GConfig.port)
func _process(_delta):
if server == null:
return
if server.is_connection_available():
print("new client connected")
clients.append(server.take_connection())
for c in clients:
c.poll()
# Check if disconnected
var conn_status = c.get_status();
if conn_status != StreamPeerTCP.STATUS_CONNECTED:
if conn_status == StreamPeerTCP.STATUS_NONE:
clients_disconnected.append(c)
continue
if c.get_available_bytes() == 0:
continue
handle_action(c.get_var())
if clients_disconnected.size() > 0:
for c in clients_disconnected:
clients.erase(c)
print("Client disconnected")
clients_disconnected.clear()
func handle_action(action: Dictionary):
if not action.has("type"):
push_error("action does not contain type")
return
match action["type"] as int:
GConstants.TCP_MESSAGE_TYPE_MOVE_CURSOR:
# Move cursor
#var next_position = DisplayServer.mouse_get_position() as Vector2 + (offset * _master.move_multiplier)
var next_position = GXInput.get_cursor_position() + action["relative"] * 4
if not _master.initialize_client:
GXInput.set_cursor_position(next_position)
else:
on_receive_move_cursor_request.emit(action["relative"])
GConstants.TCP_MESSAGE_TYPE_PRESS_CURSOR_BUTTON:
GXInput.simulate_click(action["button"])
pass
GConstants.TCP_MESSAGE_TYPE_VIRTUAL_KEY:
GXInput.simulate_virtual_key_click(action["button"])
pass
_:
push_warning("unknown type received: ", action["type"])