-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathudpserver.pas
More file actions
executable file
·190 lines (148 loc) · 3.57 KB
/
udpserver.pas
File metadata and controls
executable file
·190 lines (148 loc) · 3.57 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
//
// Copyright 2012 Shaun Simpson
// shauns2029@gmail.com
//
unit udpserver;
{$mode objfpc}{$H+}
// {$DEFINE DEBUG}
interface
uses
Classes, SysUtils, LCLProc, SyncObjs,
// synapse
blcksock;
type
{ TUDPServerThread }
TUDPServerThread = class(TThread)
protected
FData: TStringList;
procedure Execute; override;
public
Critical: TCriticalSection;
constructor Create(Data: string);
destructor Destroy; override;
published
property Data: TStringList read FData;
end;
TUDPServer = class
private
FUDPServerThread: TUDPServerThread;
procedure Stop;
procedure Start(Data: string);
function GetRunning: Boolean;
public
constructor Create;
procedure Send(Data: string);
property Running: Boolean read GetRunning;
end;
implementation
procedure Log(Message: string);
begin
DebugLn(Message);
end;
{ TUDPServer }
function TUDPServer.GetRunning: Boolean;
begin
Result := FUDPServerThread <> nil;
end;
constructor TUDPServer.Create;
begin
FUDPServerThread := nil;
end;
procedure TUDPServer.Send(Data: string);
begin
if FUDPServerThread = nil then
Start(Data)
else
begin
FUDPServerThread.Critical.Enter;
FUDPServerThread.Data.Text := Data;
FUDPServerThread.Critical.Leave;
end;
end;
procedure TUDPServer.Start(Data: string);
begin
FUDPServerThread := TUDPServerThread.Create(Data);
end;
procedure TUDPServer.Stop;
begin
if FUDPServerThread <> nil then
begin
FUDPServerThread.Terminate;
FUDPServerThread.WaitFor;
FreeAndNil(FUDPServerThread);
end;
end;
{ TUDPServerThread }
procedure TUDPServerThread.Execute;
var
Socket: TUDPBlockSocket;
Buffer: string;
DataBuff: string;
Pos: integer;
Total: integer;
PakNo: integer;
begin
{$IFDEF DEBUG} Log('UPDSRV: Running ...'); {$ENDIF}
Socket := TUDPBlockSocket.Create;
try
Socket.Bind('0.0.0.0', '44559');
if Socket.LastError <> 0 then
begin
Log(Format('Bind failed with error code %d', [Socket.LastError]));
while not Terminated do Sleep(100);
end
else
begin
try
while not Terminated do
begin
// wait one second for new packet
Buffer := Socket.RecvPacket(1000);
if Socket.LastError = 0 then
begin
if Buffer = 'REQUEST REMINDERS' then
begin
{$IFDEF DEBUG} Log('UPDSRV: Received REQUEST REMINDERS ...'); {$ENDIF}
Critical.Enter;
Total := FData.Count;
DataBuff := FData.Text;
Critical.Leave;
// Send packet with reminder total
Socket.SendString('REMINDERS:' + IntToStr(Total));
Pos := 1;
PakNo := 1;
// Send the reminders in packets of <= 512 bytes
while Pos < Length(DataBuff) do
begin
Buffer := 'REMPAK:' + IntToStr(PakNo) + ';' + Copy(DataBuff, Pos, 500);
Socket.SendString(Buffer);
Inc(Pos, 500);
Inc(PakNo);
end;
{$IFDEF DEBUG} Log('UPDSRV: Sent REMINDERS ...'); {$ENDIF}
end;
end;
end;
finally
Socket.CloseSocket;
end;
end;
finally
Socket.Free;
end;
{$IFDEF DEBUG} Log('UPDSRV: Stopped ...'); {$ENDIF}
end;
constructor TUDPServerThread.Create(Data: string);
begin
inherited Create(False);
Critical := TCriticalSection.Create;
FData := TStringList.Create;
FData.Text := Data;
end;
destructor TUDPServerThread.Destroy;
begin
FData.Free;
Critical.Free;
inherited Destroy;
end;
end.