-
Notifications
You must be signed in to change notification settings - Fork 2
/
SocketUnit.pas
460 lines (415 loc) · 10.6 KB
/
SocketUnit.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
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
unit SocketUnit2;
interface
uses Windows, Winsock;
type
TClientSocket = class(TObject)
private
FAddress: pansichar;
FData: pointer;
FTag: integer;
FConnected: boolean;
function GetLocalAddress: ansistring;
function GetLocalPort: integer;
function GetRemoteAddress: ansistring;
function GetRemotePort: integer;
protected
FSocket: TSocket;
public
function set_nonblocking(non_blocking: boolean): boolean;
function enable_keepalive(): boolean;
function set_timeout(timeout: integer): boolean;
procedure Connect(Address: ansistring; Port: integer);
property Connected: boolean read FConnected;
property Data: pointer read FData write FData;
destructor Destroy; override;
procedure Disconnect;
function Idle(Seconds: integer): boolean;
property LocalAddress: ansistring read GetLocalAddress;
property LocalPort: integer read GetLocalPort;
function ReceiveBuffer(var Buffer; BufferSize: integer): integer;
function ReceiveLength: integer;
function ReceiveString: ansistring;
property RemoteAddress: ansistring read GetRemoteAddress;
property RemotePort: integer read GetRemotePort;
function SendBuffer(var Buffer; BufferSize: integer): integer;
function SendString(Buffer: ansistring): integer;
property Socket: TSocket read FSocket;
property Tag: integer read FTag write FTag;
end;
TServerSocket = class(TObject)
private
FListening: boolean;
function GetLocalAddress: ansistring;
function GetLocalPort: integer;
protected
FSocket: TSocket;
public
function Accept: TClientSocket;
destructor Destroy; override;
procedure Disconnect;
procedure Idle;
procedure Listen(Port: integer);
property Listening: boolean read FListening;
property LocalAddress: ansistring read GetLocalAddress;
property LocalPort: integer read GetLocalPort;
end;
var
WSAData: TWSAData;
implementation
const
SO_KEEPALIVE = 1;
TCP_KEEPIDLE = 5;
TCP_KEEPINTVL = 3;
TCP_KEEPCNT = 3;
function TClientSocket.set_nonblocking(non_blocking: boolean): boolean;
var
mode: integer;
begin
if (non_blocking = true) then
begin
mode := 1;
end
else
begin
mode := 0;
end;
result := (ioctlsocket(FSocket, FIONBIO, mode) = 0);
end;
function TClientSocket.enable_keepalive(): boolean;
var
yes: integer;
Idle: integer;
interval: integer;
maxpkt: integer;
begin
result := false;
yes := 1;
if setsockopt(FSocket, SOL_SOCKET, SO_KEEPALIVE, @yes, 4) = 0 then
begin
Idle := 15;
if setsockopt(FSocket, IPPROTO_TCP, TCP_KEEPIDLE, @Idle, 4) = 0 then
begin
interval := 10;
if setsockopt(FSocket, IPPROTO_TCP, TCP_KEEPINTVL, @interval, 4) = 0 then
begin
maxpkt := 10;
if setsockopt(FSocket, IPPROTO_TCP, TCP_KEEPCNT, @maxpkt, 4) = 0 then
begin
result := true;
end;
end;
end;
end;
end;
function TClientSocket.set_timeout(timeout: integer): boolean;
var
tv: timeval;
begin
result := false;
tv.tv_usec := timeout;
tv.tv_sec := 0;
if setsockopt(FSocket, SOL_SOCKET, SO_RCVTIMEO, @timeout, sizeof(timeout)) = 0
then
begin
tv.tv_usec := timeout;
tv.tv_sec := 0;
if setsockopt(FSocket, SOL_SOCKET, SO_SNDTIMEO, @timeout, sizeof(timeout)) = 0
then
result := true;
end;
end;
procedure TClientSocket.Connect(Address: ansistring; Port: integer);
var
SockAddrIn: TSockAddrIn;
HostEnt: PHostEnt;
arg: integer;
FDSetW: TFDSet;
FDSetE: TFDSet;
timeval: TTimeVal;
const
FTimeOutConnect: Cardinal = 120000;
begin
Disconnect;
FAddress := pansichar(Address);
FSocket := Winsock.Socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
SockAddrIn.sin_family := AF_INET;
SockAddrIn.sin_port := htons(Port);
SockAddrIn.sin_addr.s_addr := inet_addr(FAddress);
if (DWORD(SockAddrIn.sin_addr.s_addr) = INADDR_NONE) then
begin
HostEnt := gethostbyname(FAddress);
if HostEnt = nil then
begin
Exit;
end;
SockAddrIn.sin_addr.s_addr := Longint(PLongint(HostEnt^.h_addr_list^)^);
end;
arg := 1;
if ioctlsocket(FSocket, FIONBIO, arg) = no_error then
begin
if Winsock.Connect(FSocket, SockAddrIn, sizeof(SockAddrIn)) <> 0 then
begin
arg := 0;
if ioctlsocket(FSocket, FIONBIO, arg) = no_error then
begin
FD_ZERO(FDSetW);
FD_ZERO(FDSetE);
FD_SET(FSocket, FDSetW);
FD_SET(FSocket, FDSetE);
timeval.tv_sec := 0;
timeval.tv_usec := FTimeOutConnect;
select(0, nil, @FDSetW, @FDSetE, @timeval);
if FD_ISSET(FSocket, FDSetW) then
begin
FConnected := true;
end;
end;
end;
end;
end;
procedure TClientSocket.Disconnect;
begin
closesocket(FSocket);
FConnected := false;
end;
function TClientSocket.GetLocalAddress: ansistring;
var
SockAddrIn: TSockAddrIn;
Size: integer;
begin
Size := sizeof(SockAddrIn);
getsockname(FSocket, SockAddrIn, Size);
result := inet_ntoa(SockAddrIn.sin_addr);
end;
function TClientSocket.GetLocalPort: integer;
var
SockAddrIn: TSockAddrIn;
Size: integer;
begin
Size := sizeof(SockAddrIn);
getsockname(FSocket, SockAddrIn, Size);
result := ntohs(SockAddrIn.sin_port);
end;
function TClientSocket.GetRemoteAddress: ansistring;
var
SockAddrIn: TSockAddrIn;
Size: integer;
begin
Size := sizeof(SockAddrIn);
getpeername(FSocket, SockAddrIn, Size);
result := inet_ntoa(SockAddrIn.sin_addr);
end;
function TClientSocket.GetRemotePort: integer;
var
SockAddrIn: TSockAddrIn;
Size: integer;
begin
Size := sizeof(SockAddrIn);
getpeername(FSocket, SockAddrIn, Size);
result := ntohs(SockAddrIn.sin_port);
end;
function TClientSocket.Idle(Seconds: integer): boolean;
var
FDset: TFDSet;
timeval: TTimeVal;
begin
if Seconds = 0 then
begin
FD_ZERO(FDset);
FD_SET(FSocket, FDset);
result := (select(0, @FDset, nil, nil, nil) > 0);
end
else
begin
timeval.tv_sec := 0;
timeval.tv_usec := Seconds * 1000;
FD_ZERO(FDset);
FD_SET(FSocket, FDset);
result := (select(0, @FDset, nil, nil, @timeval) > 0);
end;
end;
function TClientSocket.ReceiveLength: integer;
begin
result := ReceiveBuffer(pointer(nil)^, -1);
end;
function TClientSocket.ReceiveBuffer(var Buffer; BufferSize: integer): integer;
begin
if BufferSize = -1 then
begin
if ioctlsocket(FSocket, FIONREAD, result) = SOCKET_ERROR then
begin
result := SOCKET_ERROR;
// Disconnect;
end;
end
else
begin
result := recv(FSocket, Buffer, BufferSize, 0);
if result = 0 then
begin
// Disconnect;
end;
if result = SOCKET_ERROR then
begin
result := WSAGetLastError;
if result = WSAEWOULDBLOCK then
begin
result := 0;
end
else
begin
result := 0;
// Disconnect;
end;
end;
end;
end;
function TClientSocket.ReceiveString: ansistring;
var
t: ansistring;
answer: Integer;
sizebuffer: dword;
begin
result := '';
sizebuffer := 0;
ReceiveBuffer(sizebuffer, sizeof(sizebuffer));
if sizebuffer > 0 then begin
SetLength(t, 8192);
if Length(t) = 8192 then begin
while true do begin
answer := ReceiveBuffer(pointer(t)^, Length(t));
if (answer = 0) or (answer = -1) then begin
Break;
end;
if answer > 0 then begin
result := result + copy(t, 1, answer);
end;
if Length(result) >= sizebuffer then
Break;
end;
end;
end;
end;
function TClientSocket.SendBuffer(var Buffer; BufferSize: integer): integer;
var
ErrorCode: integer;
begin
result := send(FSocket, Buffer, BufferSize, 0);
if result = SOCKET_ERROR then
begin
ErrorCode := WSAGetLastError;
if (ErrorCode = WSAEWOULDBLOCK) then
begin
result := -1;
end
else
begin
result := -1;
// Disconnect;
end;
end;
end;
function TClientSocket.SendString(Buffer: ansistring): integer;
var
answer, totalansw: Integer;
l: DWORD;
begin
result := 0;
totalansw := 0;
l := Length(Buffer);
Buffer := PansiChar(@l)^ +
PansiChar(Pointer(dword(@l) + 1))^ +
PansiChar(Pointer(dword(@l) + 2))^ +
PansiChar(Pointer(dword(@l) + 3))^ + Buffer;
while true do begin
if Length(Buffer) > 0 then begin
answer := SendBuffer(pointer(Buffer)^, Length(Buffer));
if (answer = 0) or (answer = -1) then begin
Break;
end;
if answer > 0 then begin
totalansw := totalansw + answer;
Delete(Buffer, 1, answer);
end;
end else break;
end;
result := totalansw;
end;
destructor TClientSocket.Destroy;
begin
Disconnect;
inherited Destroy;
end;
procedure TServerSocket.Listen(Port: integer);
var
SockAddrIn: TSockAddrIn;
begin
FSocket := Socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
SockAddrIn.sin_family := AF_INET;
SockAddrIn.sin_addr.s_addr := htonl(INADDR_ANY);
SockAddrIn.sin_port := htons(Port);
if bind(FSocket, SockAddrIn, sizeof(SockAddrIn)) = 0 then
begin
if Winsock.Listen(FSocket, SOMAXCONN) = 0 then
FListening := true;
end;
end;
function TServerSocket.GetLocalAddress: ansistring;
var
SockAddrIn: TSockAddrIn;
Size: integer;
begin
Size := sizeof(SockAddrIn);
getsockname(FSocket, SockAddrIn, Size);
result := inet_ntoa(SockAddrIn.sin_addr);
end;
function TServerSocket.GetLocalPort: integer;
var
SockAddrIn: TSockAddrIn;
Size: integer;
begin
Size := sizeof(SockAddrIn);
getsockname(FSocket, SockAddrIn, Size);
result := ntohs(SockAddrIn.sin_port);
end;
procedure TServerSocket.Idle;
var
FDset: TFDSet;
begin
FD_ZERO(FDset);
FD_SET(FSocket, FDset);
select(0, @FDset, nil, nil, nil);
end;
function TServerSocket.Accept: TClientSocket;
var
Size: integer;
SockAddr: TSockAddr;
begin
result := TClientSocket.Create;
Size := sizeof(TSockAddr);
result.FSocket := Winsock.Accept(FSocket, @SockAddr, @Size);
if result.FSocket = INVALID_SOCKET then
begin
Disconnect;
result.FConnected := false;
end
else
begin
result.FConnected := true;
end;
end;
procedure TServerSocket.Disconnect;
begin
FListening := false;
closesocket(FSocket);
end;
destructor TServerSocket.Destroy;
begin
Disconnect;
inherited Destroy;
end;
initialization
WSAStartUp(257, WSAData);
finalization
WSACleanup;
end.