-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtmcImpl.pas
173 lines (140 loc) · 3.88 KB
/
tmcImpl.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
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
unit tmcImpl;
interface
uses IdTCPServer, System.SysUtils, IdContext, IdGlobal, IdTCPClient,
IdUDPServer, IdSocketHandle, IdUDPClient;
type
TMsgProc = procedure(Msg: String);
TMTUChecker = class
private
tcpListener: TIdTCPServer;
tcpClient: TIdTCPClient;
udpListener: TIdUDPServer;
udpClient: TIdUDPClient;
FOnMsg: TMsgProc;
FPort: UInt16;
FUseUDP: Boolean;
// TCP Client
FFirstPacketSize, FLastPacketSize, FStep: UInt32;
FHost: String;
// TCP Server
procedure OnExecute(AContext: TIdContext);
// UDP Server
procedure OnUDPRead(AThread: TIdUDPListenerThread; const AData: TIdBytes;
ABinding: TIdSocketHandle);
// TCP Client
procedure OnConnected(Sender: TObject);
public
procedure StartServer;
procedure SendRandomData;
procedure PrintMsg(Msg: String);
constructor Create(OnMsg: TMsgProc);
destructor Destroy; override;
property Port: UInt16 read FPort write FPort;
property UseUDP: Boolean read FUSeUDP write FUseUDP;
property FirstPacketSize: UInt32 read FFirstPacketSize write FFirstPacketSize;
property LastPacketSize: UInt32 read FLastPacketSize write FLastPacketSize;
property Step: UInt32 read FStep write FStep;
property Host: String read FHost write FHost;
end;
implementation
{ TMC }
constructor TMTUChecker.Create(OnMsg: TMsgProc);
begin
FOnMsg := OnMsg;
FUseUDP := False;
end;
destructor TMTUChecker.Destroy;
begin
if Assigned(tcpListener) then
begin
tcpListener.Active := False;
FreeAndNil(tcpListener);
end;
if Assigned(udpListener) then
begin
udpListener.Active := False;
FreeAndNil(udpListener);
end;
if Assigned(tcpClient) then
begin
tcpClient.Disconnect;
FreeAndNil(tcpClient);
end;
inherited;
end;
procedure TMTUChecker.OnConnected(Sender: TObject);
var i: UInt32;
outBytes: TIdBytes;
begin
PrintMsg('Connected to server');
i := FFirstPacketSize;
while i <= FLastPacketSize do
begin
SetLength(outBytes, i);
if (Sender is TIdTCPClient) then
tcpClient.Socket.Write(outBytes)
else
udpClient.SendBuffer(outBytes);
PrintMsg('Bytes sent to server: ' + IntToStr(i));
i := i + FStep;
end;
end;
procedure TMTUChecker.OnExecute(AContext: TIdContext);
var inBytes: TIdBytes;
begin
// Read received data
AContext.Connection.IOHandler.ReadBytes(inBytes, -1, False);
if Length(inBytes) > 0 then
PrintMsg('New packet received. IP: ' + AContext.Binding.PeerIP + '. Size: ' + IntToStr(Length(inBytes)));
end;
procedure TMTUChecker.OnUDPRead(AThread: TIdUDPListenerThread;
const AData: TIdBytes; ABinding: TIdSocketHandle);
begin
PrintMsg('New packet received. IP: ' + ABinding.PeerIP + '. Size: ' + IntToStr(Length(AData)));
end;
procedure TMTUChecker.PrintMsg(Msg: String);
begin
if Assigned(FOnMsg) then
FOnMsg(Msg);
end;
procedure TMTUChecker.SendRandomData;
begin
if FUseUDP then
begin
udpClient := TIdUDPClient.Create(nil);
udpClient.OnConnected := OnConnected;
udpClient.Host := FHost;
udpClient.Port := FPort;
udpClient.Active := True;
udpClient.Connect;
end else
begin
tcpClient := TIdTCPClient.Create(nil);
tcpClient.OnConnected := OnConnected;
tcpClient.Host := FHost;
tcpClient.Port := FPort;
tcpClient.Connect;
end;
end;
procedure TMTUChecker.StartServer;
var Protocol: String;
begin
if FUseUDP then
begin
udpListener := TIdUDPServer.Create(nil);
udpListener.DefaultPort := FPort;
udpListener.OnUDPRead := OnUDPRead;
udpListener.ThreadedEvent := True;
udpListener.Active := True;
Protocol := 'UDP';
end else
begin
tcpListener := TIdTCPServer.Create(nil);
tcpListener.OnExecute := OnExecute;
tcpListener.DefaultPort := FPort;
tcpListener.Active := True;
Protocol := 'TCP';
end;
PrintMsg('TMC Server Started at port ' + IntToStr(FPort) + ' (' + Protocol + ')');
end;
end.