-
Notifications
You must be signed in to change notification settings - Fork 2
/
enet_socket.pas
894 lines (756 loc) · 24.4 KB
/
enet_socket.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
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
unit enet_socket;
(**
@file win32.c
@brief ENet Win32 system specific functions
for freepascal
1.3.12
win32 => ENET_CALLBACK = _cdecl
linux socket implementaion wasn't tested.
*)
{$ifdef APPLE}
{$define HAS_GETADDRINFO}
{$define HAS_GETNAMEINFO}
{$define HAS_INET_PTON}
{$define HAS_INET_NTOP}
{$define HAS_MSGHDR_FLAGS}
{$define HAS_FCNTL}
{$endif}
interface
uses enet_consts,
{$ifdef WINDOWS}
WinSock2
{$else}
Sockets,
BaseUnix,
Unix
{$endif}
;
type
pENetSocketSet = PFDSet;
ENetSocketSet = TFDSet;
procedure ENET_SOCKETSET_EMPTY(var sockset : TFDSet);
procedure ENET_SOCKETSET_ADD(var sockset:TFDSet; socket:TSocket);
procedure ENET_SOCKETSET_REMOVE(var sockset:TFDSet; socket:TSocket);
function ENET_SOCKETSET_CHECK(var sockset:TFDSet; socket:TSocket):boolean;
function ENET_HOST_TO_NET_16(value:word):word;
function ENET_HOST_TO_NET_32(value:longword):longword;
function ENET_NET_TO_HOST_16(value:word):word;
function ENET_NET_TO_HOST_32(value:longword):longword;
function enet_initialize:integer;
procedure enet_deinitialize;
function enet_time_get:enet_uint32;
procedure enet_time_set (newTimeBase : enet_uint32);
function enet_address_set_host (address : pENetAddress; name : pchar):integer;
function enet_address_get_host_ip (address : pENetAddress; name : pchar ; nameLength : enet_size_t):integer;
function enet_address_get_host (address : pENetAddress; name : pchar; nameLength : enet_size_t):integer;
function enet_socket_create (Socktype : integer):ENetSocket;
function enet_socket_bind (socket : ENetSocket; const address : pENetAddress):integer;
function enet_socket_listen (socket: ENetSocket; backlog:integer):integer;
function enet_socketset_select (maxSocket:ENetSocket; readSet, writeSet:pENetSocketSet; timeout:enet_uint32):integer;
function enet_socket_set_option (socket : ENetSocket; option : (*ENetSocketOption*)integer; value : integer):integer;
function enet_socket_connect ( socket : ENetSocket;address : pENetAddress):integer;
function enet_socket_accept (socket : ENetSocket; address : pENetAddress):ENetSocket;
function enet_socket_shutdown (socket:ENetSocket; how : Integer { ENetSocketShutdown } ):Integer;
procedure enet_socket_destroy (socket : ENetSocket);
function enet_socket_send (socket : ENetSocket;
address : pENetAddress;
buffers : pENetBuffer;
bufferCount : enet_size_t ):integer;
function enet_socket_receive (socket : ENetSocket;
address : pENetAddress;
buffers : pENetBuffer;
bufferCount : enet_size_t):integer;
function enet_socket_wait (socket : ENetSocket; condition : penet_uint32; timeout : enet_uint32):integer;
function enet_host_random_seed: enet_uint32;
function enet_socket_get_address (socket : ENetSocket; address : pENetAddress):integer;
function enet_socket_get_option (socket:ENetSocket; option: integer (* ENetSocketOption *); value:Integer):integer;
implementation
uses
sysutils
{$ifdef WINDOWS}
,mmsystem
{$else}
,netdb
,termio
{$endif}
;
{$ifndef WINDOWS}
const
EINTR = 4;
EWOULDBLOCK = 11;
{$endif}
var
// to do : cause this value, only one enet used in one executable.
timeBase : enet_uint32 =0;
versionRequested : WORD;
{$ifdef WINDOWS}
wsaData : TWSAData;
{$endif}
procedure ENET_SOCKETSET_EMPTY(var sockset:TFDSet);
begin
{$ifdef WINDOWS}FD_ZERO{$else}fpFD_ZERO{$endif} (sockset);
end;
procedure ENET_SOCKETSET_ADD(var sockset:TFDSet; socket:TSocket);
begin
{$ifdef WINDOWS}FD_SET{$else}fpFD_SET{$endif} (socket, sockset);
end;
procedure ENET_SOCKETSET_REMOVE(var sockset:TFDSet; socket:TSocket);
begin
{$ifdef WINDOWS}FD_CLR{$else}fpFD_CLR{$endif} (socket, sockset);
end;
function ENET_SOCKETSET_CHECK(var sockset:TFDSet; socket:TSocket):boolean;
begin
result := {$ifdef WINDOWS}FD_ISSET{$else}0<>fpFD_ISSET{$endif} (socket, sockset);
end;
function ENET_HOST_TO_NET_16(value:word):word;
begin
result := (htons (value));
end;
function ENET_HOST_TO_NET_32(value:longword):longword;
begin
result := (htonl (value));
end;
function ENET_NET_TO_HOST_16(value:word):word;
begin
result := (ntohs (value));
end;
function ENET_NET_TO_HOST_32(value:longword):longword;
begin
result := (ntohl (value));
end;
function enet_initialize:integer;
{$ifdef WINDOWS}
var
versionRequested : WORD;
wsaData : TWSAData;
{$endif}
begin
{$ifdef WINDOWS}
versionRequested := $0101;
if longbool(WSAStartup (versionRequested, wsaData)) then
begin result := -1; exit; end;
if (lo(wsaData.wVersion) <> 1) or
(hi(wsaData.wVersion) <> 1) then
begin
WSACleanup;
result := -1; exit;
end;
timeBeginPeriod (1);
{$endif}
result := 0;
end;
procedure enet_deinitialize;
begin
{$ifdef WINDOWS}
timeEndPeriod (1);
WSACleanup ();
{$endif}
end;
{$push}
{$R-}
function enet_time_get:enet_uint32;
{$ifndef WINDOWS}
var
tv : TTimeVal;
{$endif}
begin
{$ifdef WINDOWS}
Result := enet_uint32(timeGetTime - timeBase);
{$else}
fpgettimeofday(@tv,nil);
Result := tv.tv_sec * 1000 + tv.tv_usec div 1000 - timeBase;
{$endif}
end;
procedure enet_time_set (newTimeBase : enet_uint32);
{$ifndef WINDOWS}
var
tv : TTimeVal;
{$endif}
begin
{$ifdef WINDOWS}
timeBase := enet_uint32( timeGetTime - newTimeBase);
{$else}
fpgettimeofday(@tv,nil);
timeBase := tv.tv_sec * 1000 + tv.tv_usec div 1000 - newTimeBase;
{$endif}
end;
{$pop}
function strtolong(str, strend:pchar):longint;
var
buf: array[0..11] of char;
i: Integer;
begin
i:=0;
while str<strend do begin
buf[i]:=str^;
Inc(str);
Inc(i);
end;
buf[i]:=#0;
Result:=StrToInt(buf);
end;
function enet_address_set_host_ip (address: pENetAddress; name:pchar):Integer;
{$ifdef MSWINDOWS}
var
vals: array[0..3] of enet_uint8;
i: Integer;
next: pchar;
val: longint;
begin
fillchar(vals,sizeof(vals),0);
for i:=0 to 3 do
begin
next := name+1;
if name^ <> '0' then
begin
val := strtolong (name, next);
if (val < 0) or (val > 255) or (next = name) or (next - name > 3) then
begin
Result:=-1; exit;
end;
vals [i] := enet_uint8(val);
end;
if i < 3 then begin
if next^ <> '.' then
begin
Result:=-1; exit;
end;
end else begin
if next^ <> #0 then
begin
Result:=-1; exit;
end;
end;
name := next + 1;
end;
system.Move(vals,address ^. host,sizeof (enet_uint32));
{$else}
{$ifdef HAS_INET_PTON}
if 0=inet_pton (AF_INET, name, @ address ^. host)
{$else}
if 0=inet_aton (name, pin_addr(@ address ^. host)
{$endif}
then
return -1;
{$endif}
Result:=0;
end;
function enet_address_set_host (address : pENetAddress; name : pchar):integer;
var
hostEntry : {$ifdef WINDOWS}PHostEnt{$else}THostEntry{$endif};
host : longword;
begin
{$ifdef WINDOWS}
hostEntry :=gethostbyname (name);
if (hostEntry = nil) or
(hostEntry ^. h_addrtype <> AF_INET) then
begin
Result:=enet_address_set_host_ip (address, name);
exit;
end;
{$else}
if not gethostbyname (name, hostEntry) then
begin
address ^.host := enet_uint32(htonl(StrToHostAddr(name).s_addr));
if address ^.host<>0 then
begin
result:=0; exit;
end;
end;
{$endif}
{$ifdef WINDOWS}
address ^. host := (penet_uint32(hostEntry^.h_addr_list^))^;
result := 0;
{$else}
Result:=enet_address_set_host_ip (address, name);
{$endif}
end;
function enet_address_get_host_ip (address : pENetAddress; name : pchar ; nameLength : enet_size_t):integer;
var
addr : pchar;
addrLen : SizeInt;
begin
{$ifdef WINDOWS}
addr :=inet_ntoa (pInAddr(@address ^. host)^);
{$else}
addr := PAnsiChar(HostAddrToStr(in_addr(NToHl(address^.host))));
if addr='' then addr:=nil;
{$endif}
if (addr = nil) then
begin result := -1; exit; end
else begin
addrLen := strlen(addr);
if (addrLen >= nameLength) then
begin Result:=-1; exit; end;
system.Move(addr^,name^, addrLen+1);
end;
result := 0;
end;
function enet_address_get_host (address : pENetAddress; name : pchar; nameLength : enet_size_t):integer;
var
inadd : TInAddr;
hostEntry : {$ifdef WINDOWS}PHostEnt{$else}THostEntry{$endif};
hostLen : SizeInt;
begin
inadd.s_addr :={$ifndef WINDOWS}NToHl{$endif}(address ^. host);
{$ifdef WINDOWS}
hostEntry :=gethostbyaddr (@inadd, sizeof (TInAddr), AF_INET);
if (hostEntry <> nil) then
{$else}
if GetHostByAddr (inadd, hostEntry) then
{$endif}
begin
hostLen := {$ifdef WINDOWS}strlen (hostEntry ^. h_name){$else}Length(hostEntry.Name){$endif};
if (hostLen >= nameLength) then
begin Result:=-1; exit; end;
system.Move(hostEntry {$ifdef WINDOWS} ^. h_name^ {$else}.Name[1]{$endif}, name^, hostLen + 1);
Result:=0; exit;
end
else
result := enet_address_get_host_ip (address, name, nameLength);
end;
function enet_socket_bind (socket : ENetSocket; const address : pENetAddress):integer;
var
sin : SOCKADDR_IN;
retvalue : integer;
begin
fillchar(sin, sizeof(SOCKADDR_IN), 0);
sin.sin_family := AF_INET;
if (address <> nil) then
begin
sin.sin_port := ENET_HOST_TO_NET_16(address ^. port);
sin.sin_addr.s_addr := address ^. host;
end
else
begin
sin.sin_port := 0;
sin.sin_addr.s_addr := INADDR_ANY;
end;
retvalue := {$ifdef WINDOWS}bind{$else}fpbind{$endif} (socket,
@sin,
sizeof (SOCKADDR_IN));
if retvalue = {$ifdef WINDOWS}SOCKET_ERROR{$else}-1{$endif} then
result := -1
else result := 0;
end;
function enet_socket_get_address (socket : ENetSocket; address : pENetAddress):integer;
var
sin : sockaddr_in;
sinLength : {$ifdef WINDOWS}Integer{$else}TSocklen{$endif};
begin
sinLength := sizeof (sockaddr_in);
if ({$ifdef WINDOWS}getsockname{$else}fpgetsockname{$endif} (socket,{$ifndef WINDOWS}@{$endif}sin, {$ifndef WINDOWS}@{$endif} sinLength) = -1) then
begin Result:=-1; exit; end;
address ^. host := enet_uint32(sin.sin_addr.s_addr);
address ^. port := ENET_NET_TO_HOST_16 (sin.sin_port);
Result:=0;
end;
function enet_socket_listen (socket: ENetSocket; backlog:integer):integer;
var
backlogparam, retvalue : integer;
begin
if backlog < 0 then
backlogparam := SOMAXCONN
else backlogparam := backlog;
retvalue := {$ifdef WINDOWS}listen{$else}fplisten{$endif} (socket, backlogparam);
if retvalue = {$ifdef WINDOWS}SOCKET_ERROR{$else}-1{$endif} then
result := -1
else result := 0;
end;
function enet_socket_create (Socktype : integer):ENetSocket;
var
iSockType : integer;
begin
if Socktype = ENET_SOCKET_TYPE_DATAGRAM then
iSocktype := SOCK_DGRAM
else iSocktype := SOCK_STREAM;
result := {$ifdef WINDOWS}socket{$else}fpsocket{$endif} (PF_INET, iSocktype, 0);
end;
function enet_socket_set_option (socket : ENetSocket; option : (*ENetSocketOption*)integer; value : integer):integer;
var
iResult : integer;
nonBlocking : Longword;
{$ifndef WINDOWS}
timeval : TTimeval;
{$endif}
begin
iResult := -1;
case (option) of
ENET_SOCKOPT_NONBLOCK:
begin
nonBlocking := Longword(value);
{$ifdef WINDOWS}
iResult := ioctlsocket (socket, LongInt(FIONBIO), nonBlocking);
{$else}
{$ifdef HAS_FCNTL}
if nonBlocking<>0 then
iResult := fpfcntl (socket, F_SETFL, O_NONBLOCK or fpfcntl (socket, F_GETFL, 0))
else
iResult := fpfcntl (socket, F_SETFL, not(O_NONBLOCK) and fpfcntl (socket, F_GETFL, 0));
{$else}
iResult := fpioctl (socket, FIONBIO, @nonBlocking);
{$endif}
{$endif}
end;
ENET_SOCKOPT_BROADCAST:
iResult := {$ifdef WINDOWS}setsockopt{$else}fpsetsockopt{$endif} (socket, SOL_SOCKET, SO_BROADCAST, @ value, sizeof (integer));
ENET_SOCKOPT_REUSEADDR:
iResult := {$ifdef WINDOWS}setsockopt{$else}fpsetsockopt{$endif} (socket, SOL_SOCKET, SO_REUSEADDR, @ value, sizeof (integer));
ENET_SOCKOPT_RCVBUF:
iResult := {$ifdef WINDOWS}setsockopt{$else}fpsetsockopt{$endif} (socket, SOL_SOCKET, SO_RCVBUF, @ value, sizeof (integer));
ENET_SOCKOPT_SNDBUF:
iResult := {$ifdef WINDOWS}setsockopt{$else}fpsetsockopt{$endif} (socket, SOL_SOCKET, SO_SNDBUF, @ value, sizeof (integer));
ENET_SOCKOPT_RCVTIMEO:
begin
{$ifndef WINDOWS}
timeVal.tv_sec := value div 1000;
timeVal.tv_usec := (value mod 1000) * 1000;
{$endif}
iResult := {$ifdef WINDOWS}setsockopt{$else}fpsetsockopt{$endif} (socket, SOL_SOCKET, SO_RCVTIMEO,
@ {$ifndef WINDOWS} timeVal {$else} value {$endif} ,
sizeof( {$ifndef WINDOWS} TTimeVal {$else}integer{$endif} )
);
end;
ENET_SOCKOPT_SNDTIMEO:
begin
{$ifndef WINDOWS}
timeVal.tv_sec := value div 1000;
timeVal.tv_usec := (value mod 1000) * 1000;
{$endif}
iResult := {$ifdef WINDOWS}setsockopt{$else}fpsetsockopt{$endif} (socket, SOL_SOCKET, SO_SNDTIMEO,
@{$ifndef WINDOWS}timeval {$else}value {$endif} ,
sizeof ( {$ifndef WINDOWS} TTimeVal {$else} integer{$endif} )
);
end;
ENET_SOCKOPT_NODELAY:
iResult := {$ifdef WINDOWS}setsockopt{$else}fpsetsockopt{$endif} (socket, IPPROTO_TCP, TCP_NODELAY, @ value, sizeof (integer));
else ;
end;
if iResult = {$ifdef WINDOWS}SOCKET_ERROR{$else}-1{$endif} then
result := -1
else result := 0;
end;
function enet_socket_get_option (socket:ENetSocket; option: integer (* ENetSocketOption *); value:Integer):integer;
var
iResult : Integer;
len : {$ifdef WINDOWS}Integer{$else}TSocklen{$endif};
begin
iResult := {$ifdef WINDOWS}SOCKET_ERROR{$else}-1{$endif};
case option of
ENET_SOCKOPT_ERROR:
begin
len := sizeof(integer);
IResult := {$ifdef WINDOWS}getsockopt{$else}fpgetsockopt{$endif} (socket, SOL_SOCKET, SO_ERROR, {$ifndef WINDOWS}@{$endif}value,{$ifndef WINDOWS}@{$endif}len);
end;
else ;
end;
if iResult={$ifdef WINDOWS}SOCKET_ERROR{$else}-1{$endif} then
Result:=-1
else
Result:=0;
end;
function enet_socket_connect ( socket : ENetSocket;address : pENetAddress):integer;
{$ifndef WINDOWS}
const
EINPROGRESS = 36;
{$endif}
var
sin : sockaddr_in;
retvalue : integer;
begin
fillchar (sin, sizeof (sockaddr_in), 0);
sin.sin_family :=AF_INET;
sin.sin_port :=ENET_HOST_TO_NET_16 (address ^. port);
sin.sin_addr.s_addr := address ^. host;
retvalue := {$ifdef WINDOWS}connect{$else}fpconnect{$endif} (socket, @ sin, sizeof (sockaddr_in));
{$ifdef WINDOWS}
if (retvalue = SOCKET_ERROR) and (WSAGetLastError <> WSAEWOULDBLOCK) then
Result := -1
else
Result:=0;
{$else}
if (retvalue = -1) and (errno = EINPROGRESS) then
begin
Result:=0; exit;
end;
Result:=retvalue;
{$endif}
end;
function enet_socket_accept (socket : ENetSocket; address : pENetAddress):ENetSocket;
var
sresult : TSocket;
sin : sockaddr_in;
sinLength : integer;
p1 : PSockAddr;
p2 : {$ifdef WINDOWS}PInteger{$else}pSocklen{$endif};
begin
sinLength :=sizeof (sockaddr_in);
p1 := nil;
p2 := nil;
if address<>nil then begin
p1 := @sin;
p2 := @sinLength;
end;
sresult := {$ifdef WINDOWS}accept{$else}fpaccept{$endif} (socket,
p1,
p2);
{$ifdef WINDOWS}
if (sresult = INVALID_SOCKET) then
{$else}
if sresult = -1 then
{$endif}
begin result := ENET_SOCKET_NULL; exit; end;
if (address <> nil) then
begin
address ^. host :=enet_uint32(sin.sin_addr.s_addr);
address ^. port :=ENET_NET_TO_HOST_16 (sin.sin_port);
end;
result := sresult;
end;
function enet_socket_shutdown (socket:ENetSocket; how : Integer { ENetSocketShutdown } ):Integer;
begin
Result := {$ifdef WINDOWS}shutdown{$else}fpshutdown{$endif}(socket,how);
{$ifdef WINDOWS}
if Result = SOCKET_ERROR then
Result := -1
else Result := 0;
{$endif}
end;
procedure enet_socket_destroy (socket : ENetSocket);
begin
if TSocket(socket) <> INVALID_SOCKET then
closesocket(socket);
end;
function enet_socket_send (socket : ENetSocket;
address : pENetAddress;
buffers : pENetBuffer;
bufferCount : enet_size_t ):integer;
var
sin : sockaddr_in;
sentLength : longword;
{$ifdef WINDOWS}
ipto : PSockAddr;
iptolen : integer;
{$else}
iCount, iBufLen, iBufPos : Integer;
pbuf : pchar;
pnetbuf : pENetBuffer;
{$endif}
begin
if (address <> nil) then
begin
fillchar(sin,sizeof(sockaddr_in),0);
sin.sin_family :=AF_INET;
sin.sin_port :=ENET_HOST_TO_NET_16 (address ^. port);
sin.sin_addr.s_addr :=address ^. host;
end;
{$ifdef WINDOWS}
ipto := nil;
iptolen := 0;
if address <> nil then begin
ipto := @sin;
iptolen := sizeof(sockaddr_in);
end;
if (wsaSendTo (socket,
LPWSABUF(buffers),
longword(bufferCount),
sentLength,
0,
ipto,
iptolen,
nil,
nil) = SOCKET_ERROR) then
begin
if (WSAGetLastError () = WSAEWOULDBLOCK) then
begin result := 0; exit; end;
result := -1; exit;
end;
{$else}
// emulate sendmsg
// get buffer length
iBufLen:=0;
pnetbuf:=buffers;
iCount:=bufferCount;
while iCount>0 do begin
Inc(iBufLen,pnetbuf^.dataLength);
Inc(pnetbuf);
Dec(iCount);
end;
GetMem(pbuf,iBufLen);
try
// copy memory
pnetbuf:=buffers;
iCount:=bufferCount;
iBufPos:=0;
while iCount>0 do begin
system.Move(pnetbuf^.data^,(pbuf+iBufPos)^,pnetbuf^.dataLength);
Inc(iBufPos,pnetbuf^.dataLength);
Inc(pnetbuf);
Dec(iCount);
end;
sentLength:=fpsendto(socket,pbuf,iBufLen,MSG_NOSIGNAL,@sin,sizeof(sockaddr_in));
finally
Freemem(pbuf);
end;
if (integer(sentLength) = -1) then
begin
if (errno = EWOULDBLOCK) then
begin
Result:=0; exit;
end;
Result:=-1; exit;
end;
{$endif}
result := integer(sentLength);
end;
function enet_socket_receive (socket : ENetSocket;
address : pENetAddress;
buffers : pENetBuffer;
bufferCount : enet_size_t):integer;
var
recvLength : longword;
sin : SOCKADDR_IN;
{$ifdef WINDOWS}
sinLength : integer;
flags : Longword;
ipfrom : PSockAddr;
ipfromlen : pinteger;
{$else}
slen : TSocklen;
{$endif}
begin
{$ifdef WINDOWS}
sinLength :=sizeof (sockaddr_in);
flags :=0;
ipfrom := nil;
ipfromlen := nil;
if address <> nil then begin
ipfrom := @sin;
ipfromlen := @sinLength;
end;
if (WSARecvFrom (socket,
LPWSABUF(buffers),
longword(bufferCount),
recvLength,
flags,
ipfrom,
ipfromlen,
nil,
nil) = SOCKET_ERROR) then
begin
case (WSAGetLastError ()) of
WSAEWOULDBLOCK,
WSAECONNRESET:
begin result := 0; exit; end;
end;
result := -1; exit;
end;
if (flags and MSG_PARTIAL)<>0 then
begin result := -1; exit; end;
{$else}
slen := sizeof(sockaddr_in);
recvLength:=fprecvfrom(socket,buffers^.data,buffers^.dataLength,MSG_NOSIGNAL,@sin,@slen);
if (integer(recvLength) = -1) then
begin
if (errno = EWOULDBLOCK) then
begin
Result:=0; exit;
end;
Result:=-1; exit;
end;
{$ifdef _APPLE_}
if (_msgHdr.msg_flags and MSG_TRUNC <> 0) then
begin
Result:=-1; exit;
end;
{$endif}
{$endif}
if (address <> nil) then
begin
address ^. host :=enet_uint32(sin.sin_addr.s_addr);
address ^. port :=ENET_NET_TO_HOST_16 (sin.sin_port);
end;
result := integer(recvLength);
end;
function enet_socketset_select (maxSocket:ENetSocket; readSet, writeSet:pENetSocketSet; timeout:enet_uint32):integer;
var
timeVal : TTimeVal;
begin
timeVal.tv_sec := timeout div 1000;
timeVal.tv_usec := (timeout mod 1000) * 1000;
result := {$ifdef WINDOWS}select{$else}fpSelect{$endif} (maxSocket + 1, readSet, writeSet, nil, @ timeVal);
end;
function enet_socket_wait (socket : ENetSocket; condition : penet_uint32; timeout : enet_uint32):integer;
var
readSet, writeSet : TFDSet;
timeVal : TTimeVal;
selectCount : integer;
{$ifdef HAS_POLL}
pollCount : Integer;
pollSocket : tpollfd;
{$endif}
begin
{$ifdef HAS_POLL}
pollSocket.fd := socket;
pollSocket.events := 0;
if ( condition^ and ENET_SOCKET_WAIT_SEND<>0) then
pollSocket.events := pollSocket.events or POLLOUT;
if ( condition^ and ENET_SOCKET_WAIT_RECEIVE<>0) then
pollSocket.events := pollSocket.events or POLLIN;
pollCount := fppoll (@ pollSocket, 1, timeout);
if (pollCount < 0) then
begin
if ((errno = EINTR) and ( condition^ and ENET_SOCKET_WAIT_INTERRUPT<>0))
begin
condition^ := ENET_SOCKET_WAIT_INTERRUPT;
Result:=0; exit;
end;
Result:=-1; exit;
end;
condition^ := ENET_SOCKET_WAIT_NONE;
if (pollCount = 0) then
begin Result:=0; exit; end;
if (pollSocket.revents and POLLOUT<>0) then
condition^ := condition^ or ENET_SOCKET_WAIT_SEND;
if (pollSocket.revents and POLLIN<>0) then
condition^ := condition^ or ENET_SOCKET_WAIT_RECEIVE;
{$else}
timeVal.tv_sec :=timeout div 1000;
timeVal.tv_usec :=(timeout mod 1000) * 1000;
{$ifdef WINDOWS}FD_ZERO{$else}fpFD_ZERO{$endif} (readSet);
{$ifdef WINDOWS}FD_ZERO{$else}fpFD_ZERO{$endif} (writeSet);
if (condition^ and ENET_SOCKET_WAIT_SEND)<>0 then
{$ifdef WINDOWS}FD_SET{$else}fpFD_SET{$endif} (socket, writeSet);
if (condition^ and ENET_SOCKET_WAIT_RECEIVE)<>0 then
{$ifdef WINDOWS}FD_SET{$else}fpFD_SET{$endif} (socket, readSet);
selectCount := {$ifdef WINDOWS}select{$else}fpSelect{$endif} (socket + 1, @readSet, @writeSet, nil, @timeVal);
if (selectCount < 0) then
begin
{$ifndef WINDOWS}
if ((errno = EINTR) and ( condition^ and ENET_SOCKET_WAIT_INTERRUPT <>0)) then
begin
condition^ := ENET_SOCKET_WAIT_INTERRUPT;
Result:=0; exit;
end;
{$endif}
Result:=-1; exit;
end;
condition^ :=ENET_SOCKET_WAIT_NONE;
if (selectCount = 0) then
begin result := 0; exit; end;
if ({$ifdef WINDOWS}FD_ISSET{$else}0<>fpFD_ISSET{$endif} (socket, writeSet)) then
condition^ := condition^ or ENET_SOCKET_WAIT_SEND;
if ({$ifdef WINDOWS}FD_ISSET{$else}0<>fpFD_ISSET{$endif} (socket, readSet)) then
condition^ := condition^ or ENET_SOCKET_WAIT_RECEIVE;
{$endif}
result := 0;
end;
function enet_host_random_seed: enet_uint32;
begin
{$ifdef WINDOWS}
Result := timeGetTime;
{$else}
Result := GetTickCount;
{$endif}
end;
end.