-
Notifications
You must be signed in to change notification settings - Fork 3
/
simplesockets.pas
599 lines (512 loc) · 20.1 KB
/
simplesockets.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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
unit SimpleSockets;
{$mode ObjFPC}{$H+}
{$TypedAddress on}
interface
uses
SysUtils, Sockets;
type
{ Basic Socket Types }
TSocketFD = Tsocket;
TSocketType = (stIPv4, stIPv6, stDualStack);
TSocket = record
FD: TSocketFD;
SocketType: TSocketType;
end;
TSocketArray = Array of TSocket;
TAddressType = (atIN4, atIN6);
TNetworkAddress = record
Address: String;
AddressType: TAddressType;
end;
TSocketConnection = record
ClientAddress: TNetworkAddress;
ClientPort: Word;
Socket: TSocket;
end;
{ ReceiveFrom Return Types }
TReceiveFromResult = record
FromAddr: TNetworkAddress;
FromPort: Word;
DataSize: SizeInt;
end;
generic TReceiveFromMessage<T> = record
FromAddr: TNetworkAddress;
FromPort: Word;
Data: T;
end;
TReceiveFromStringMessage = specialize TReceiveFromMessage<String>;
{ Exceptions }
EDualStackNotSupported = class(Exception);
EUnsupportedAddress = class(Exception);
{ ESocketError }
ESocketError = class(Exception)
private
FCode: Integer;
public
constructor Create(ACode: Integer; const FunName: String);
property Code: Integer read FCode;
end;
EConnectionClosedException = class(Exception);
EUDPFragmentationException = class(Exception);
const
MaxUDPPackageSize = 512;
{ Address Management }
function IN4Address(const Address: String): TNetworkAddress; inline;
function IN6Address(const Address: String): TNetworkAddress; inline;
function IN4MappedIN6Address(const In4Address: String): TNetworkAddress; inline;
function INAddr(const Address: String): TNetworkAddress; inline;
function IsIPv4Mapped(const IPv6Addr: TNetworkAddress): Boolean; inline;
function ExtractIPv4Address(const IPv6Addr: TNetworkAddress): TNetworkAddress; inline;
function IN6Equal(const A, B: String): Boolean;
operator =(const A, B: TNetworkAddress): Boolean; inline;
operator :=(const AStr: String): TNetworkAddress; inline;
{ Socket Functions }
function TCPSocket(AType: TSocketType): TSocket; inline;
function UDPSocket(AType: TSocketType): TSocket; inline;
procedure CloseSocket(const ASocket: TSocket); inline;
procedure Bind(const ASocket: TSocket; const AAddress: TNetworkAddress; APort: Word; ReuseAddr: Boolean = True);
procedure Listen(const ASocket: TSocket; Backlog: Integer); inline;
function AcceptConnection(const ASocket: TSocket): TSocketConnection; inline;
procedure Connect(const ASocket: TSocket; const AAddress: TNetworkAddress; APort: Word); inline;
function Receive(const ASocket: TSocket; ABuffer: Pointer; MaxSize: SizeInt; AFlags: Integer = 0): SizeInt; inline;
function ReceiveFrom(const ASocket: TSocket; ABuffer: Pointer; MaxSize: SizeInt; AFlags: Integer = 0): TReceiveFromResult;
function Send(const ASocket: TSocket; ABuffer: Pointer; ASize: SizeInt; AFlags: Integer = 0): SizeInt; inline;
function SendTo(const ASocket: TSocket; const ReceiverAddr: TNetworkAddress;
ReceiverPort: Word; ABuffer: Pointer; ASize: SizeInt; AFlags: Integer = 0): SizeInt; inline;
function ReceiveStr(const ASocket: TSocket; MaxLength: SizeInt = -1; AFlags: Integer = 0): String; inline;
function ReceiveStrFrom(const ASocket: TSocket; MaxLength: SizeInt = MaxUDPPackageSize; AFlags: Integer = 0): TReceiveFromStringMessage; inline;
function SendStr(const ASocket: TSocket; const AData: String; AFlags: Integer = 0): SizeInt; inline;
function SendStrTo(const ASocket: TSocket; const ReceiverAddr: TNetworkAddress; ReceiverPort: Word; const AData: String; AFlags: Integer = 0): SizeInt; inline;
generic function Receive<T>(const ASocket: TSocket; AFlags: Integer = 0): T; inline;
generic function ReceiveFrom<T>(const ASocket: TSocket; AFlags: Integer = 0): specialize TReceiveFromMessage<T>; inline;
generic function Send<T>(const ASocket: TSocket; constref AData: T; AFlags: Integer = 0): SizeInt; inline;
generic function SendTo<T>(const ASocket: TSocket; constref ReceiverAddr: TNetworkAddress; ReceiverPort: Word; const AData: T; AFlags: Integer = 0): SizeInt; inline;
generic function ReceiveArray<T>(const ASocket: TSocket; MaxCount: SizeInt = -1; AFlags: Integer = 0): specialize TArray<T>; inline;
generic function ReceiveArrayFrom<T>(const ASocket: TSocket; MaxCount: SizeInt = -1; AFlags: Integer = 0): specialize TReceiveFromMessage<specialize TArray<T>>; inline;
generic function SendArray<T>(const ASocket: TSocket; const AData: specialize TArray<T>; AFlags: Integer = 0): SizeInt; inline;
generic function SendArrayTo<T>(const ASocket: TSocket; const ReceiverAddr: TNetworkAddress; ReceiverPort: Word; const AData: specialize TArray<T>; AFlags: Integer = 0): SizeInt; inline;
// Timeout in MS
function DataAvailable(const SocketArray: TSocketArray; TimeOut: Integer = 0): TSocketArray; overload;
function DataAvailable(const ASocket: TSocket; TimeOut: Integer = 0): Boolean; overload; //inline;
function DataAvailable(const SocketArray: array of TSocket; TimeOut: Integer = 0): TSocketArray; overload; inline;
function BytesAvailable(const ASocket: TSocket): SizeInt;
{ Helper }
// Must be in interface because of generic functions
type
_PAddressUnion = ^_TAddressUnion;
_TAddressUnion = record
case Boolean of
True: (In4Addr: sockaddr_in);
False: (In6Addr: sockaddr_in6);
end;
function CreateAddr(AAddress: TNetworkAddress; APort: Word; DualStack: Boolean): _TAddressUnion; inline;
procedure ReadAddr(constref Addr: _TAddressUnion; DualStack: Boolean; out AAddress: TNetworkAddress; out APort: Word);
implementation
uses
{$IfDef WINDOWS}WinSock2{$Else}BaseUnix{$EndIf}, Math;
const
IPPROTO_IPV6 = {$IfDef WINDOWS}41{$Else}41{$EndIf};
IPV6_V6ONLY = {$IfDef WINDOWS}27{$Else}26{$EndIf};
function CreateAddr(AAddress: TNetworkAddress; APort: Word; DualStack: Boolean): _TAddressUnion;
begin
if (AAddress.AddressType = atIN4) and DualStack then
AAddress := IN4MappedIN6Address(AAddress.Address);
if AAddress.AddressType = atIN4 then
begin
Result.In4Addr.sin_family := AF_INET;
Result.In4Addr.sin_port := HToNS(APort);
Result.In4Addr.sin_addr.s_addr := LongWord(StrToNetAddr(AAddress.Address));
end
else if AAddress.AddressType = atIN6 then
begin
Result.In6Addr.sin6_family := AF_INET6;
Result.In6Addr.sin6_port := HToNS(APort);
Result.In6Addr.sin6_addr := StrToHostAddr6(AAddress.Address);
Result.In6Addr.sin6_flowinfo := 0;
Result.In6Addr.sin6_scope_id := 0;
end
else
raise EUnsupportedAddress.Create('Address type ' + ord(AAddress.AddressType).ToString + ' not supported');
end;
procedure ReadAddr(constref Addr: _TAddressUnion; DualStack: Boolean; out
AAddress: TNetworkAddress; out APort: Word);
begin
if Addr.In4Addr.sin_family = AF_INET then
begin
AAddress := IN4Address(NetAddrToStr(Addr.In4Addr.sin_addr));
APort := NToHs(Addr.In4Addr.sin_port);
end
else if Addr.In6Addr.sin6_family = AF_INET6 then
begin
AAddress := IN6Address(HostAddrToStr6(Addr.In6Addr.sin6_addr));
if DualStack and IsIPv4Mapped(AAddress.Address) then
AAddress := ExtractIPv4Address(AAddress);
APort := NToHs(Addr.In6Addr.sin6_port);
end
else
raise EUnsupportedAddress.Create('Address Family ' + Addr.In4Addr.sin_family.ToString + ' not supported');
end;
function SocketInvalid(ASocket: TSocketFD): Boolean; inline;
begin
{$IfDef Windows}
Result := ASocket = TSocketFD(INVALID_SOCKET);
{$Else}
Result := ASocket < 0;
{$EndIf}
end;
function CreateSocketFD(ADomain: TSocketType; AType: Integer; AProto: Integer): TSocketFD;
var
AFam, v6Only: Integer;
begin
if ADomain = stIPv4 then
AFam := AF_INET
else
AFam := AF_INET6;
Result := fpsocket(AFam, AType, AProto);
if SocketInvalid(Result) then
raise ESocketError.Create(socketerror, 'socket');
if ADomain = stDualStack then
begin
v6Only := 0;
if fpsetsockopt(Result, IPPROTO_IPV6, IPV6_V6ONLY, @v6Only, SizeOf(v6Only)) <> 0 then
raise EDualStackNotSupported.Create('Dualstack option not supported on this system: ' + socketerror.ToString);
end;
end;
function IN4Address(const Address: String): TNetworkAddress;
begin
Result := Default(TNetworkAddress);
Result.Address := Address;
Result.AddressType := atIN4;
end;
function IN6Address(const Address: String): TNetworkAddress;
begin
Result := Default(TNetworkAddress);
Result.Address := Address;
Result.AddressType := atIN6;
end;
function IN4MappedIN6Address(const In4Address: String): TNetworkAddress;
var
InAddr: TIn_addr;
begin
InAddr := StrToNetAddr(In4Address);
Result := IN6Address('::FFFF:%x:%x'.Format([(InAddr.s_bytes[1] shl 8) or InAddr.s_bytes[2],
(InAddr.s_bytes[3] shl 8) or InAddr.s_bytes[4]]));
end;
function INAddr(const Address: String): TNetworkAddress;
begin
Result := Default(TNetworkAddress);
if Pos(':', Address) = 0 then
Result.AddressType := atIN4
else
Result.AddressType := atIN6;
Result.Address := Address;
end;
function IsIPv4Mapped(const IPv6Addr: TNetworkAddress): Boolean;
var
In6Addr: Sockets.TIn6Addr;
begin
if IPv6Addr.AddressType = atIN4 then
Exit(True);
if IPv6Addr.AddressType <> atIN6 then
raise EUnsupportedAddress.Create('Can only check IPv4 mapping for IPv6 addresses');
IN6Addr := StrToHostAddr6(IPv6Addr.Address);
Result := (IN6Addr.u6_addr16[0] = 0) and
(IN6Addr.u6_addr16[1] = 0) and
(IN6Addr.u6_addr16[2] = 0) and
(IN6Addr.u6_addr16[3] = 0) and
(IN6Addr.u6_addr16[4] = 0) and
(IN6Addr.u6_addr16[5] = $FFFF);
end;
function ExtractIPv4Address(const IPv6Addr: TNetworkAddress): TNetworkAddress;
var
In6Addr: Sockets.TIn6Addr;
begin
if IPv6Addr.AddressType = atIN4 then
Exit(IPv6Addr);
if IPv6Addr.AddressType <> atIN6 then
raise EUnsupportedAddress.Create('Can only extract IPv4 mapping from IPv6 addresses');
IN6Addr := StrToHostAddr6(IPv6Addr.Address);
Result := IN4Address('%d.%d.%d.%d'.Format([IN6Addr.s6_addr8[12],
IN6Addr.s6_addr8[13],
IN6Addr.s6_addr8[14],
IN6Addr.s6_addr8[15]]));
end;
function IN6Equal(const A, B: String): Boolean;
var
AAddr, BAddr: Sockets.Tin6_addr;
begin
AAddr := StrToHostAddr6(A);
BAddr := StrToHostAddr6(B);
Result := (AAddr.s6_addr32[0] = BAddr.s6_addr32[0]) and
(AAddr.s6_addr32[1] = BAddr.s6_addr32[1]) and
(AAddr.s6_addr32[2] = BAddr.s6_addr32[2]) and
(AAddr.s6_addr32[3] = BAddr.s6_addr32[3]);
end;
operator=(const A, B: TNetworkAddress): Boolean;
begin
Result := (A.AddressType = B.AddressType) and (
((A.AddressType = atIN4) and (A.Address = B.Address)) or // IPv4: simple string equality
((A.AddressType = atIN6) and IN6Equal(A.Address, B.Address)) // IPv6 check binary equality
);
end;
operator:=(const AStr: String): TNetworkAddress;
begin
Result := INAddr(AStr);
end;
function TCPSocket(AType: TSocketType): TSocket;
begin
Result.SocketType := AType;
Result.FD := CreateSocketFD(AType, SOCK_STREAM, 0);
end;
function UDPSocket(AType: TSocketType): TSocket;
begin
Result.SocketType := AType;
Result.FD := CreateSocketFD(AType, SOCK_DGRAM, 0);
end;
procedure CloseSocket(const ASocket: TSocket);
begin
Sockets.CloseSocket(ASocket.FD);
end;
procedure Bind(const ASocket: TSocket; const AAddress: TNetworkAddress;
APort: Word; ReuseAddr: Boolean);
var
enableReuse: Integer = 1;
addr: _TAddressUnion;
begin
if ReuseAddr then
fpsetsockopt(ASocket.FD, SOL_SOCKET, SO_REUSEADDR, @enableReuse, SizeOf(enableReuse));
addr := CreateAddr(AAddress, APort, ASocket.SocketType = stDualStack);
if fpbind(ASocket.FD, Sockets.PSockAddr(@addr), SizeOf(addr)) <> 0 then raise
ESocketError.Create(socketerror, 'bind (%s:%d)'.Format([AAddress.Address, APort]));
end;
procedure Listen(const ASocket: TSocket; Backlog: Integer);
begin
if fplisten(ASocket.FD, Backlog) <> 0 then raise
ESocketError.Create(socketerror, 'listen');
end;
function AcceptConnection(const ASocket: TSocket): TSocketConnection;
var
addr: _TAddressUnion;
addrLen: TSocklen = SizeOf(addr);
begin
Result.Socket.FD := fpaccept(ASocket.FD, Sockets.psockaddr(@addr), @addrLen);
if SocketInvalid(Result.Socket.FD) then
raise ESocketError.Create(socketerror, 'accept');
Result.Socket.SocketType := ASocket.SocketType;
ReadAddr(addr, ASocket.SocketType = stDualStack, Result.ClientAddress, Result.ClientPort);
end;
procedure Connect(const ASocket: TSocket;
const AAddress: TNetworkAddress; APort: Word);
var
addr: _TAddressUnion;
begin
addr := CreateAddr(AAddress, APort, ASocket.SocketType = stDualStack);
if fpconnect(ASocket.FD, Sockets.psockaddr(@addr), SizeOf(addr)) <> 0 then
raise ESocketError.Create(socketerror, 'connect');
end;
function Receive(const ASocket: TSocket; ABuffer: Pointer; MaxSize: SizeInt;
AFlags: Integer): SizeInt;
begin
Result := fprecv(ASocket.FD, ABuffer, MaxSize, AFlags);
if Result = 0 then
raise EConnectionClosedException.Create('The connection closed')
else if Result < 0 then
raise ESocketError.Create(socketerror, 'recv');
end;
function ReceiveFrom(const ASocket: TSocket; ABuffer: Pointer; MaxSize: SizeInt;
AFlags: Integer): TReceiveFromResult;
var
addr: _TAddressUnion;
addrLen: TSocklen;
begin
addrLen := SizeOf(_TAddressUnion);
Result.DataSize := fprecvfrom(ASocket.FD, ABuffer, MaxSize, AFlags, Sockets.PSockAddr(@addr), @addrLen);
if Result.DataSize < 0 then
raise ESocketError.Create(socketerror, 'recvfrom');
ReadAddr(addr, ASocket.SocketType = stDualStack, Result.FromAddr, Result.FromPort);
end;
function Send(const ASocket: TSocket; ABuffer: Pointer; ASize: SizeInt;
AFlags: Integer): SizeInt;
begin
Result := fpsend(ASocket.FD, ABuffer, ASize, AFlags);
if Result < 0 then
raise ESocketError.Create(socketerror, 'send');
end;
function SendTo(const ASocket: TSocket; const ReceiverAddr: TNetworkAddress;
ReceiverPort: Word; ABuffer: Pointer; ASize: SizeInt; AFlags: Integer
): SizeInt;
var
addr: _TAddressUnion;
begin
addr := CreateAddr(ReceiverAddr, ReceiverPort, ASocket.SocketType = stDualStack);
Result := fpsendto(ASocket.FD, ABuffer, ASize, AFlags, Sockets.psockaddr(@addr), SizeOf(addr));
if Result < 0 then
raise ESocketError.Create(socketerror, 'sendto');
end;
function ReceiveStr(const ASocket: TSocket; MaxLength: SizeInt;
AFlags: Integer): String;
var
Len: SizeInt;
begin
if MaxLength < 0 then
MaxLength := BytesAvailable(ASocket);
Result := '';
SetLength(Result, MaxLength);
Len := Receive(ASocket, @Result[1], MaxLength, AFlags);
SetLength(Result, Len);
end;
function ReceiveStrFrom(const ASocket: TSocket; MaxLength: SizeInt;
AFlags: Integer): TReceiveFromStringMessage;
var
UdpMessage: TReceiveFromResult;
begin
Result := Default(specialize TReceiveFromMessage<String>);
SetLength(Result.Data, MaxLength);
UdpMessage := ReceiveFrom(ASocket, @Result.Data[1], MaxLength, AFlags);
SetLength(Result.Data, UdpMessage.DataSize);
Result.FromAddr := UdpMessage.FromAddr;
Result.FromPort := UdpMessage.FromPort;
end;
function SendStr(const ASocket: TSocket; const AData: String; AFlags: Integer
): SizeInt;
begin
if Length(AData) = 0 then Exit(0);
Result := Send(ASocket, @AData[1], Length(AData), AFlags);
end;
function SendStrTo(const ASocket: TSocket;
const ReceiverAddr: TNetworkAddress; ReceiverPort: Word; const AData: String; AFlags: Integer
): SizeInt;
begin
if Length(AData) = 0 then Exit(0);
Result := SendTo(ASocket, ReceiverAddr, ReceiverPort, @AData[1], Length(AData), AFlags);
end;
generic function Receive<T>(const ASocket: TSocket; AFlags: Integer = 0): T;
var
Len: SizeInt;
begin
Result := Default(T);
Len := 0;
while Len < SizeOf(Result) do
Len += Receive(ASocket, @PByte(@Result)[Len], SizeOf(Result) - Len, AFlags);
end;
generic function ReceiveFrom<T>(const ASocket: TSocket; AFlags: Integer = 0): specialize TReceiveFromMessage<T>;
var
UdpMessage: TReceiveFromResult;
begin
Result := Default(T);
UdpMessage := ReceiveFrom(ASocket, @Result, SizeOf(Result), AFlags);
if UdpMessage.DataSize < SizeOf(T) then
raise EUDPFragmentationException.Create('Receiving of fragmented data is not supported by typed receive');
Result.FromAddr := UdpMessage.FromAddr;
Result.FromPort := UdpMessage.FromPort;
end;
generic function Send<T>(const ASocket: TSocket; constref AData: T; AFlags: Integer = 0): SizeInt;
begin
Result := Send(ASocket, @AData, SizeOf(T), AFlags);
end;
generic function SendTo<T>(const ASocket: TSocket; const ReceiverAddr: TNetworkAddress; ReceiverPort: Word; constref AData: T; AFlags: Integer = 0): SizeInt;
begin
Result := SendTo(ASocket, ReceiverAddr, ReceiverPort, @AData, SizeOf(T), AFlags);
end;
generic function ReceiveArray<T>(const ASocket: TSocket; MaxCount: SizeInt; AFlags: Integer = 0): specialize TArray<T>;
var
Len: SizeInt;
begin
if MaxCount < 0 then
MaxCount := BytesAvailable(ASocket) div SizeOf(T);
Result := nil;
SetLength(Result, MaxCount);
Len := 0;
repeat
Len += Receive(ASocket, @PByte(@Result)[Len], MaxCount * SizeOf(T) - Len, AFlags);
until (Len mod SizeOf(T)) = 0;
SetLength(Result, Len div SizeOf(T));
end;
generic function ReceiveArrayFrom<T>(const ASocket: TSocket; MaxCount: SizeInt; AFlags: Integer = 0): specialize TReceiveFromMessage<specialize TArray<T>>;
var
UdpMessage: TReceiveFromResult;
begin
if MaxCount < 0 then
MaxCount := MaxUDPPackageSize div SizeOf(T);
Result.Data := nil;
SetLength(Result.Data, MaxCount);
UdpMessage := ReceiveFrom(ASocket, @Result.Data[0], MaxCount * SizeOf(T), AFlags);
if UdpMessage.DataSize mod SizeOf(T) > 0 then
raise EUDPFragmentationException.Create('Receiving of fragmented data is not supported by typed receive');
SetLength(Result.Data, UdpMessage.DataSize div SizeOf(T));
Result.FromAddr := UdpMessage.FromAddr;
Result.FromPort := UdpMessage.FromPort;
end;
generic function SendArray<T>(const ASocket: TSocket; const AData: specialize TArray<T>; AFlags: Integer = 0): SizeInt;
begin
if Length(AData) = 0 then Exit(0);
Result := Send(ASocket, @AData[0], Length(AData) * SizeOf(T), AFlags);
end;
generic function SendArrayTo<T>(const ASocket: TSocket; const ReceiverAddr: TNetworkAddress; ReceiverPort: Word; const AData: specialize TArray<T>; AFlags: Integer = 0): SizeInt;
begin
if Length(AData) = 0 then Exit(0);
Result := SendTo(ASocket, ReceiverAddr, ReceiverPort, @AData[0], Length(AData) * SizeOf(T), AFlags);
end;
function DataAvailable(const SocketArray: TSocketArray; TimeOut: Integer
): TSocketArray;
var
FDSet: TFDSet;
MaxSock: TSocketFD;
timeval: TTimeVal;
Ret: LongInt;
i, WriteHead: Integer;
begin
Result := nil;
MaxSock := 0;
{$IfDef UNIX}fpFD_ZERO{$else}FD_ZERO{$endif}(FDSet);
for i:=0 to Length(SocketArray) - 1 do
begin
MaxSock := Max(MaxSock, SocketArray[i].FD);
{$IfDef UNIX}fpFD_SET{$else}FD_SET{$endif}(SocketArray[i].FD, FDSet);
end;
timeval.tv_sec := TimeOut div 1000;
timeval.tv_usec := (TimeOut mod 1000) * 1000;
Ret := {$IfDef UNIX}fpselect{$else}select{$endif}(MaxSock + 1, @FDSet, nil, nil, @timeval);
if Ret < 0 then
raise ESocketError.Create(socketerror, 'select');
SetLength(Result, Ret);
WriteHead := 0;
for i:=0 to Length(SocketArray) - 1 do
if {$IfDef UNIX}fpFD_ISSET{$else}FD_ISSET{$endif}(SocketArray[i].FD, FDSet) {$Ifdef Unix}> 0{$Endif} then
begin
Result[WriteHead] := SocketArray[i];
Inc(WriteHead);
end;
end;
function DataAvailable(const ASocket: TSocket; TimeOut: Integer): Boolean;
var
Arr: TSocketArray;
begin
Arr := [ASocket];
Result := Length(DataAvailable(Arr, TimeOut)) > 0;
end;
function DataAvailable(const SocketArray: array of TSocket; TimeOut: Integer
): TSocketArray;
var
Arr: TSocketArray;
begin
if Length(SocketArray) = 0 then Exit(nil);
SetLength(Arr, Length(SocketArray));
Move(SocketArray[0], Arr[0], Length(SocketArray) * SizeOf(SocketArray[0]));
Result := DataAvailable(arr, TimeOut);
end;
function BytesAvailable(const ASocket: TSocket): SizeInt;
var
count: DWord;
begin
Result := -1;
if ioctlsocket(ASocket.FD, FIONREAD, @count) = 0 then
Result := Count;
end;
{ ESocketError }
constructor ESocketError.Create(ACode: Integer; const FunName: String);
begin
inherited CreateFmt('[Socket Error: %d] %s call failed', [ACode, FunName]);
FCode := ACode;
end;
end.