0% found this document useful (0 votes)
46 views

(Private Declarations) (Public Declarations) : ($R .DFM)

1. The document defines classes for a reading thread (TReadingThread) and log (TLog) that are used by a client form (TForm1) to connect to a TCP server and log received messages. 2. The TReadingThread class executes in a separate thread to continuously read messages from the TCP connection and pass them to TLog to be synchronized and logged. 3. The TForm1 class handles button clicks to initiate and terminate the TCP connection, and uses the reading thread and log classes to receive and log messages from the server.

Uploaded by

didier_o
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

(Private Declarations) (Public Declarations) : ($R .DFM)

1. The document defines classes for a reading thread (TReadingThread) and log (TLog) that are used by a client form (TForm1) to connect to a TCP server and log received messages. 2. The TReadingThread class executes in a separate thread to continuously read messages from the TCP connection and pass them to TLog to be synchronized and logged. 3. The TForm1 class handles button clicks to initiate and terminate the TCP connection, and uses the reading thread and log classes to receive and log messages from the server.

Uploaded by

didier_o
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

1 unit Unit1;

2
3 interface
4
5 uses
6 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
7 Dialogs, StdCtrls, IdCustomTransparentProxy, IdSocks, IdBaseComponent,
8 IdComponent, IdIOHandler, IdIOHandlerSocket, IdIOHandlerStack,
9 IdTCPConnection, IdTCPClient, IdSync;
10
11 type
12
13 TReadingThread = class(TThread)
14 protected
15 FConn: TIdTCPConnection;
16 procedure Execute; override;
17 public
18 constructor Create(AConn: TIdTCPConnection); reintroduce;
19 end;
20
21 TLog = class(TIdSync)
22 protected
23 FMsg: String;
24 procedure DoSynchronize; override;
25 public
26 constructor Create(const AMsg: String);
27 class procedure AddMsg(const AMsg: String);
28 end;
29
30
31 TForm1 = class(TForm)
32 Memo1: TMemo;
33 Button1: TButton;
34 IdIOHandlerStack1: TIdIOHandlerStack;
35 client: TIdTCPClient;
36 IdSocksInfo1: TIdSocksInfo;
37 procedure Button1Click(Sender: TObject);
38 procedure clientConnected(Sender: TObject);
39 procedure clientDisconnected(Sender: TObject);
40 procedure FormCreate(Sender: TObject);
41
42 private
43 { Private declarations }
44 public
45 { Public declarations }
46 end;
47
48 var
49 Form1: TForm1;
50 rt: TReadingThread = nil;
51
52 implementation
53
54 {$R *.dfm}
55
56 constructor TReadingThread.Create(AConn: TIdTCPConnection);
57 begin
58 Form1.Memo1.Lines.Add('DEBUG: TReadingThread.Create'); // Debug
59
60 FConn := AConn;
61 inherited Create(False);
62 end;
63
64 procedure TReadingThread.Execute;
65 begin
66 Form1.Memo1.Lines.Add('DEBUG: TReadingThread.Execute'); // Debug
67
68 while not Terminated and FConn.Connected do
69 begin
70 Form1.Memo1.Lines.Add('DEBUG: TReadingThread.Execute - FConn.Connected'); // Debug
71
72 TLog.AddMsg(FConn.IOHandler.ReadLn);
73 end;
74 end;
75
76 constructor TLog.Create(const AMsg: String);
77 begin
78 Form1.Memo1.Lines.Add('DEBUG: TLog.Create'); // Debug
79
80 FMsg := AMsg;
81 inherited Create;
82 end;
83
84 procedure TLog.DoSynchronize;
85 var
86 cmd : string;
87 begin
88 Form1.Memo1.Lines.Add('DEBUG: TLog.DoSynchronize'); // Debug
89
90 cmd := copy(FMsg, 1, 1);
91
92 if cmd='PING' then
93 begin
94 Form1.client.Socket.WriteLn('PONG');
95 end
96
97 end;
98
99 class procedure TLog.AddMsg(const AMsg: String);
100 begin
101 Form1.Memo1.Lines.Add('DEBUG: TLog.AddMsg'); // Debug
102
103 with Create(AMsg) do try
104 Synchronize;
105 finally
106 Free;
107 end;
108 end;
109
110 procedure TForm1.FormCreate(Sender: TObject);
111 begin
112 Memo1.Clear;
113 end;
114
115 procedure TForm1.Button1Click(Sender: TObject);
116 var
117 Host : String;
118 Port : Integer;
119 begin
120
121 Host := '127.0.0.1';
122 Port := StrToInt('1234');
123
124 client.Host := Host;
125 client.Port := Port;
126
127 with client do
128 begin
129 try
130 Connect;
131 except
132 on E: Exception do
133 Memo1.Lines.Add('Error: ' + E.Message);
134 end;
135 end;
136
137 end;
138
139 procedure TForm1.clientConnected(Sender: TObject);
140 begin
141 Form1.Memo1.Lines.Add('DEBUG: TForm1.clientConnected'); // Debug
142
143 rt := TReadingThread.Create(client);
144 end;
145
146 procedure TForm1.clientDisconnected(Sender: TObject);
147 begin
148 Form1.Memo1.Lines.Add('DEBUG: TForm1.clientDisconnected'); // Debug
149
150 if rt <> nil then
151 begin
152 rt.Terminate;
153 rt.WaitFor;
154 FreeAndNil(rt);
155 end;
156 end;
157
158 end.

You might also like