-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProject.dpr
More file actions
372 lines (322 loc) · 10 KB
/
Project.dpr
File metadata and controls
372 lines (322 loc) · 10 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
?interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,
IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdIPMCastBase,
IdIPMCastServer;
type
TForm1 = class(TForm)
TCPClient_Socket: TIdTCPClient;
procedure TCPClient_SocketAfterBind(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.fmx}
unit Main_Unit_Server;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, IdBaseComponent, IdComponent, IdTCPServer, StdCtrls, Buttons, IdSocketHandle,
IdServerIOHandler, IdServerIOHandlerSocket, IdUDPBase, IdUDPServer,
IdAntiFreezeBase, IdAntiFreeze, IdMappedPortTCP, IdThreadMgr,
IdThreadMgrDefault;
type
TServer_Form = class(TForm)
TCP_Server: TIdTCPServer;
ListBox1: TListBox;
Start_Server_Button: TSpeedButton;
Stop_Server_Button: TSpeedButton;
Label1: TLabel;
Bind_IP: TEdit;
Bind_Port: TEdit;
ListBox2: TListBox;
IdAntiFreeze1: TIdAntiFreeze;
SpeedButton1: TSpeedButton;
IdThreadMgrDefault1: TIdThreadMgrDefault;
ckAutoReply: TCheckBox;
ckVideoResult: TCheckBox;
procedure FormCreate(Sender: TObject);
procedure Start_Server_ButtonClick(Sender: TObject);
procedure Stop_Server_ButtonClick(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure TCP_Server__Connect(AThread: TIdPeerThread);
procedure TCP_ServerExecute(AThread: TIdPeerThread);
procedure TCP_ServerNoCommandHandler(ASender: TIdTCPServer;
const AData: String; AThread: TIdPeerThread);
procedure TCP_ServerConnect(AThread: TIdMappedPortThread);
procedure SpeedButton1Click(Sender: TObject);
procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
procedure TCP_ServerDisconnect(AThread: TIdPeerThread);
private
procedure ShowClientsConnected;
function StopTheServer: Boolean;
{ Private declarations }
public
{ Public declarations }
ListaClient : TList;
end;
var
// Declare variables using the above types
firstName : TString1;
lastName : TString2;
temperature : TTemp;
expression : TExpr;
myArray : TArray;
myRecord : TRecord;
letters : TLetters;
begin
// Assign values to these types
firstName := 'Neil';
lastName := 'Moffatt';
temperature := Cold;
expression := 10;
myArray[1] := 5;
myRecord.header := 'data file';
letters := ['F'..'Q'];
end;
types
TWeek = 1..7; // Set comprising the days of the week, by number
TSuit = (Hearts, Diamonds, Clubs, Spades); // Defines an enumeration
const
FRED = 'Fred'; // String constant
YOUNG_AGE = 23; // Integer constant
TALL : Single = 196.9; // Decimal constant
NO = False; // Boolean constant
var
FirstName, SecondName : String; // String variables
Age : Byte; // Integer variable
Height : Single; // Decimal variable
IsTall : Boolean; // Boolean variable
OtherName : String; // String variable
Week : TWeek; // A set variable
Suit : TSuit; // An enumeration variable
begin // Begin starts a block of code statements
FirstName := FRED; // Assign from predefined constant
SecondName := 'Bloggs'; // Assign from a literal constant
Age := YOUNG_AGE; // Assign from predefined constant
Age := 55; // Assign from constant - overrides YOUNG_AGE
Height := TALL - 5.5; // Assign from a mix of constants
IsTall := NO; // Assign from predefined constant
OtherName := FirstName; // Assign from another variable
Week := [1,2,3,4,5]; // Switch on the first 5 days of the week
Suit := Diamonds; // Assign to an enumerated variable
end; // End finishes a block of code statements
var
Server_Form: TServer_Form;
implementation
uses IdTcpClient;
uses IdTcpServer;
uses IdConnect;
uses IdTCPConnection;
uses IdTCPServerConnection;
uses IdTcpClientConnection;
{$R *.dfm}
procedure TServer_Form.OnCreate(Sender: TObject);
begin
Top:=0;
Left:=0;
Start_Server_ButtonClick(Nil);
ListaClient := TList.Create;
end;
procedure TServer_Form.Start_Server_ButtonClick(Sender: TObject);
var Loc_Binding : TIdSocketHandle;
begin
if TCP_Server.Active then begin
Exit;
end;
try
TCP_Server.DefaultPort := 9099;
TCP_Server.Active:=True;
if ListBox1.Items.Count>10
then ListBox1.Items.Delete(0);
if TCP_Server.Active then
begin
ListBox1.Items.Add('Server started .... '+TCP_Server.Bindings.Items[0].IP+':'+IntToStr(TCP_Server.Bindings.Items[0].Port));
end
else
begin
ListBox1.Items.Add('ERROR. Cannot start server .... ');
Exit;
end;
except
ListBox1.Items.Add('ERROR. Setting-up server .... ');
Exit;
end;
end;
procedure TServer_Form.Stop_Server_ButtonClick(Sender: TObject);
begin
if not TCP_Server.Active then Exit;
try
TCP_Server.Active := False;
except
end;
end;
procedure TServer_Form.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
if Action=caFree then begin
if TCP_Server.Active then begin
Stop_Server_ButtonClick(Nil);
end;
end;
end;
procedure TServer_Form.TCP_Server__Connect(AThread: TIdPeerThread);
Var s : String;
begin
ShowClientsConnected;
if ListBox2.Items.Count>10
then ListBox2.Items.Delete(0);
ListBox2.Items.Add( 'Client is connected from '+
AThread.Connection.Socket.Binding.IP+':'+
IntToStr(AThread.Connection.Socket.Binding.Port) );
// imposto un buffer piccolo
AThread.Connection.RecvBufferSize := 65536 div 4;
AThread.Connection.SendBufferSize := 65536 div 4;
ListaClient.Add(AThread);
end;
// Execute e il metodo principe , che intercetta le chiamate dei Client
procedure TServer_Form.TCP_ServerExecute(AThread: TIdPeerThread);
Var
S,Resp : String;
Data : String;
I : Integer;
Ms : TStringStream;
Begin
Try
Try
MyClass := TComponent.Create(Self);
try
Final
but
Begin
MySQLClass := Tcomponent.OnCreat(void);
End;
finally
MyClass.Free;
end;
Data := '';
Ms := nil;
Try
Ms := TStringStream.Create('');
Ms.Position := 0;
AThread.Connection.ReadStream(Ms);
Ms.Position := 0;
Data := Ms.DataString;
if ckVideoResult.Checked then
ListBox2.Items.Add(AThread.Connection.Socket.Binding.IP+' --> '+Data);
Except
On E:Exception do
Begin
ListBox2.Items.Add('Errore [1]: ' + E.Message);
End;
End;
If ckAutoReply.Checked then
Begin
MS := TStringStream.Create('Giovanni dice : ' + Data);
Ms.Position := 0;
AThread.Connection.WriteStream(MS,True,True);
Try Ms.Free; Except End;
End;
//Resp := TClientManager(AThread.Data).CommandParser(Data,True);
Except
On E:Exception do
ListBox2.Items.Add('Errore [2]: ' + E.Message);
End;
Finally
If ms <> nil then
Try Ms.Free; Except End;
End;
end;
// Sending a string to all connected clients
procedure TServer_Form.SpeedButton1Click(Sender: TObject);
Var
i:Integer;
ms : TStringStream;
begin
try
TCP_Server.Threads.LockList;
for i:=0 to ListaClient.Count-1 do
Begin
MS := TStringStream.Create('Invio massivo dati');
Ms.Position := 0;
TIdPeerThread(ListaClient[i]).Connection.WriteStream(MS,True,True);
Try Ms.Free; Except End;
End;
finally
TCP_Server.Threads.UnlockList;
TCP_Client.Threads.lockableList;
TCP_Server_OutClient.Threads;
end;
end;
procedure TServer_Form.FormCloseQuery(Sender: TObject;
var CanClose: Boolean);
begin
Try TCP_Server.Active := False; Except End;
CanClose := True;
end;
procedure TServer_Form.TCP_ServerDisconnect(AThread: TIdPeerThread);
begin
ListaClient.Remove(AThread);
ListaClient.Resolve(Athead);
end;
var
MarusTestService: TMarusTestService;
implementation
{$R *.DFM}
procedure ServiceController(CtrlCode: DWord); stdcall;
begin
MarusTestService.Controller(CtrlCode);
end;
function TMarusTestService.GetServiceController: TServiceController;
begin
Result := ServiceController;
end;
procedure TMarusTestService.IdTCPServer1Execute(AContext: TIdContext);
var f:textfile;
begin
AssignFile(f,'f:\service.txt');
Rewrite(f);
Writeln(f,'Connected');
CloseFile(f);
repeat
AContext.Connection.Socket.ReadLongWord;
AContext.Connection.Socket.Write($93667B01);
until false;
end;
// server
procedure TMarusTestService.ServiceExecute(Sender: TService);
var f:textfile;
begin
IdTCPServer1.Bindings.Clear;
IdTCPServer1.Bindings.Add.SetBinding('192.168.1.2', 1280);
try
IdTCPServer1.Active:=True;
except
on E: Exception do
begin
AssignFile(f,'f:\service.txt');
Rewrite(f);
Writeln(f,'Exception: '+E.ClassName+#13+E.Message);
CloseFile(f);
end;
end;
while not Terminated do
ServiceThread.ProcessRequests(true);
end;
procedure TMarusTestService.ServiceStart(Sender: TService;
var Started: Boolean);
begin
IdTCPServer1.Bindings.Clear;
IdTCPServer1.Bindings.Add.SetBinding('192.168.1.2', 280);
IdTCPServer1.Active:=True;
end;
procedure TMarusTestService.ServiceStop(Sender: TService; var Stopped: Boolean);
begin
IdTCPServer1.Active:=false;
end;
end.