-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserverlib.hws
112 lines (99 loc) · 3.47 KB
/
serverlib.hws
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
; Start a new server instance
Function p_StartServer(port$)
If prefs.port$ = "" Then prefs.port$ = 2550
p_Log("Starting Server on port " .. prefs.port$)
; Create a new Server instance with ID=1
CreateServer(1, prefs.port$)
p_Log("Server started, now waiting for connections.")
p_SetUIState("started")
EndFunction
; Send Disconnect Command to client
Function p_SendDisconnectCommand(index, clientId)
p_SendMessage("/DISCONNECT")
EndFunction
; Disconnect a client from the server
Function p_CloseClientConnection(index, clientId)
p_RemoveConnection(clientId)
CloseConnection(clientId)
EndFunction
; Stop the running server instance
Function p_StopServer()
; Disconnect all clients
p_Log("Disconnecting all clients...")
ForEach(clients, p_SendDisconnectCommand)
ForEach(clients, p_CloseClientConnection)
; Close Server with ID=1
CloseServer(1)
p_Log("Server stopped.")
p_SetUIState("stopped")
EndFunction
; Build a comma separated string of connected Clients
Function p_ClientsToString(index, clientId)
listOfClients = listOfClients .. "," .. GetConnectionIP(clientId)
EndFunction
; Broadcast the list of connected clients
Function p_BroadcastListOfClients()
listOfClients = "/CLIENTS "
ForEach(clients, p_ClientsToString)
p_SendMessage(listOfClients)
EndFunction
; Handle a new client connection
Function p_ClientConnected(msg)
p_AddConnection(msg.clientId)
p_Log("A client connected! Client IP: " .. GetConnectionIP(msg.clientid))
p_SendMessage("Client " .. GetConnectionIP(msg.clientid) .. " is now connected!")
p_BroadcastListOfClients()
EndFunction
; Handle a client disconnection
Function p_ClientDisconnected(msg)
p_RemoveConnection(msg.Id)
p_Log("A client disconnected! Client IP was: " .. GetConnectionIP(msg.id))
p_SendMessage("Client " .. GetConnectionIP(msg.id) .. " is now disconnecting!")
p_BroadcastListOfClients()
CloseConnection(msg.Id)
EndFunction
; Handle received data from a connected client
Function p_ReceiveData(msg)
Local data$ = ReceiveData(msg.id, #RECEIVELINE)
p_Log("Message received: " .. data$)
p_ProcessMessagesByType(data$)
Local index, clientId = NextItem(clients)
; Broadcast the message to all other clients
While GetType(index) <> #NIL And clientId <> msg.id
p_Log("Broadcasting message to " .. GetConnectionIP(clientId))
p_SendData(index, clientId, data$ .. "\r\n")
index, clientId = NextItem(clients, index)
Wend
EndFunction
; Disable and enable UI elements according to current state
Function p_SetUIState(state$)
If state$ = "started"
moai.Set("CmdDir", "disabled", True)
moai.Set("ProjectsDir", "disabled", True)
moai.Set("txtPort", "disabled", True)
moai.Set("start", "disabled", True)
moai.Set("Nick", "disabled", True)
moai.Set("AltNick", "disabled", True)
moai.Set("stop", "disabled", False)
ElseIf state$ = "stopped"
moai.Set("CmdDir", "disabled", False)
moai.Set("ProjectsDir", "disabled", False)
moai.Set("txtPort", "disabled", False)
moai.Set("start", "disabled", False)
moai.Set("Nick", "disabled", False)
moai.Set("AltNick", "disabled", False)
moai.Set("stop", "disabled", True)
EndIf
EndFunction
Function p_ApplySettings()
prefs.port$ = moai.Get("txtPort", "Text")
prefs.nick$ = moai.Get("Nick", "Text")
prefs.lwsn$ = moai.Get("LWSN", "File")
prefs.commandDir$ = moai.Get("CmdDir", "Path")
EndFunction
Function p_PrefsToGui()
moai.Set("txtPort", "Text", prefs.port$)
moai.Set("Nick", "Text", prefs.nick$)
moai.Set("LWSN", "File", prefs.lwsn$)
moai.Set("CmdDir", "Path", prefs.commandDir$)
EndFunction