-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.pas
78 lines (60 loc) · 1.77 KB
/
server.pas
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
uses System;
uses System.Net;
uses System.Net.Sockets;
uses Timers;
uses Utility;
uses App;
var server, handler: System.Net.Sockets.Socket;
s: string;
handlers: array of System.Net.Sockets.Socket;
timers: array of Timer;
procedure processUser(timer_id, user_id: integer);
begin
timers[timer_id].Stop();
writeln('user ', user_id, ' is connected');
while(true) do begin
// Receive message, process, answer, repeat
var message: string := '';
while(true) do begin
if(
(length(message) > 0) and (
(message[length(message)] = chr(10)) or
(message[length(message)] = chr(13))
)
) then
break;
if(not handlers[user_id].Connected) then
break;
message := message + sreceive(handlers[user_id], 1024);
end;
if(length(message) = 0) then
continue;
var answer: string := App.process(message.Trim);
if(not handlers[user_id].Connected) then begin
break;
end;
handlers[user_id].send(prepare(answer + chr(10)));
end;
end;
begin
server := new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
server.bind(new IPEndPoint(IPAddress.Parse('0.0.0.0'), 4120));
server.listen(100);
WriteLn('Bound to the port 4120. Ready!');
setLength(handlers, 0);
setLength(timers, 0);
while(true) do
begin
handler := server.accept();
setLength(handlers, handlers.Length + 1);
setLength(timers, timers.Length + 1);
var user_id: integer := handlers.Length - 1;
var timer_id: integer := timers.Length - 1;
handlers[user_id] := handler;
// Now, start a timer-thread for this handler
var processUserWrapper: () -> () := () -> processUser(timer_id, user_id);
var t: Timer := new Timer(1, processUserWrapper);
timers[timer_id] := t;
t.Start();
end;
end.