-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSimpleTCP.pas
More file actions
1061 lines (924 loc) · 33.9 KB
/
SimpleTCP.pas
File metadata and controls
1061 lines (924 loc) · 33.9 KB
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
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ==============================
// SimpleTCP
// MODIFIED BY 3d[Power]
// Adapted for Need For Kill game
// www.3dpower.org
// haz-3dpower@mail.ru
// ==============================
{*************************************************************}
{ SimpleTCP components for Delphi and C++ Builder }
{ Version: 2.0 }
{ E-Mail: info@utilmind.com }
{ WWW: http://www.utilmind.com }
{ Created: July 8, 2000 }
{ Modified: January 17, 2002 }
{ Legal: Copyright (c) 2000-2002, UtilMind Solutions }
{*************************************************************}
{ SimpleTCP is pack of two components (TSimpleTCPServer and }
{ TSimpleTCPClient) for working with Asynchronous TCP sockets.}
{*************************************************************}
{ Please see demo program for more information. }
{*************************************************************}
{ IMPORTANT NOTE: }
{ This software is provided 'as-is', without any express or }
{ implied warranty. In no event will the author be held }
{ liable for any damages arising from the use of this }
{ software. }
{ Permission is granted to anyone to use this software for }
{ any purpose, including commercial applications, and to }
{ alter it and redistribute it freely, subject to the }
{ following restrictions: }
{ 1. The origin of this software must not be misrepresented, }
{ you must not claim that you wrote the original software. }
{ If you use this software in a product, an acknowledgment }
{ in the product documentation would be appreciated but is }
{ not required. }
{ 2. Altered source versions must be plainly marked as such, }
{ and must not be misrepresented as being the original }
{ software. }
{ 3. This notice may not be removed or altered from any }
{ source distribution. }
{*************************************************************}
{$IFNDEF VER80} {Delphi 1}
{$IFNDEF VER90} {Delphi 2}
{$IFNDEF VER93} {BCB 1}
{$DEFINE D3} {* Delphi 3 or higher}
{$IFNDEF VER100} {Delphi 3}
{$IFNDEF VER110} {BCB 3}
{$DEFINE D4} {* Delphi 4 or higher}
{$ENDIF}
{$ENDIF}
{$ENDIF}
{$ENDIF}
{$ENDIF}
unit SimpleTCP;
interface
uses
Windows, Messages, Classes, WinSock, dialogs;
const
UM_TCPASYNCSELECT = WM_USER + 323;//$0001;
type
TSimpleTCPClient = class;
TSimpleTCPAcceptEvent = procedure(Sender: TObject; Client: TSimpleTCPClient; var Accept: Boolean) of object;
TSimpleTCPServerEvent = procedure(Sender: TObject; Client: TSimpleTCPClient) of object;
TSimpleTCPServerDataAvailEvent = procedure(Sender: TObject; Client: TSimpleTCPClient; DataSize: Integer) of object;
TSimpleTCPClientDataAvailEvent = procedure(Sender: TObject; DataSize: Integer) of object;
TSimpleTCPServerIOEvent = procedure(Sender: TObject; Client: TSimpleTCPClient; Stream: TStream) of object;
TSimpleTCPClientIOEvent = procedure(Sender: TObject; Stream: TStream) of object;
TSimpleTCPErrorEvent = procedure(Sender: TObject; Socket: TSocket; ErrorCode: Integer; ErrorMsg: String) of object;
TCustomSimpleSocket = class(TComponent)
private
FAllowChangeHostAndPortOnConnection: Boolean;
FHost: String;
FPort: Word;
FSocket: TSocket;
FOnError: TSimpleTCPErrorEvent;
// For internal use
FConnections: TList;
SockAddrIn: TSockAddrIn;
HostEnt: PHostEnt;
WSAData: TWSAData;
WindowHandle: hWnd;
procedure WndProc(var Message: TMessage); virtual;
procedure UMTCPSelect(var Msg: TMessage); message UM_TCPASYNCSELECT;
function SendEx_(Socket: TSocket; var Buffer; BufLength: Integer): Integer; // EX
function SendBufferTo(Socket: TSocket; Buffer: PChar; BufLength: Integer): Integer; // returns N of bytes sent
function SendStreamTo(Socket: TSocket; Stream: TStream): Integer; // returns N of bytes sent
function ReceiveFrom(Socket: TSocket; Buffer: PChar; BufLength: Integer; ReceiveCompletely: Boolean): Integer; // returns N of bytes read
function ReceiveStreamFrom(Socket: TSocket; Stream: TStream; DataSize: Integer; ReceiveCompletely: Boolean): Integer; // returns N of bytes read
protected
procedure SocketError(Socket: TSocket; ErrorCode: Integer); virtual;
procedure SetHost(Value: String); virtual; abstract;
procedure SetPort(Value: Word); virtual; abstract;
procedure DoAccept; virtual; abstract;
procedure DoConnect; virtual; abstract;
procedure DoClose(Socket: TSocket); virtual; abstract;
procedure DoDataAvailable(Client: TSimpleTCPClient; DataSize: Integer; var Handled: Boolean); virtual; abstract;
procedure DoRead(Client: TSimpleTCPClient; Stream: TStream); virtual; abstract;
public
constructor Create(aOwner: TComponent); override;
destructor Destroy; override;
property AllowChangeHostAndPortOnConnection: Boolean read FAllowChangeHostAndPortOnConnection write FAllowChangeHostAndPortOnConnection default False;
property Host: String read FHost write SetHost;
property Port: Word read FPort write SetPort default 0;
property Socket: TSocket read FSocket write FSocket;
property OnError: TSimpleTCPErrorEvent read FOnError write FOnError;
end;
{ TSimpleTCPServer }
TSimpleTCPServer = class(TCustomSimpleSocket)
private
FListen: Boolean;
FOnAccept: TSimpleTCPAcceptEvent;
FOnClientConnected: TSimpleTCPServerEvent;
FOnClientDisconnected: TSimpleTCPServerEvent;
FOnClientDataAvailable: TSimpleTCPServerDataAvailEvent;
FOnClientRead: TSimpleTCPServerIOEvent;
function GetLocalHostName: String;
function GetLocalIP: String;
procedure SetNoneStr(Value: String);
protected
procedure SocketError(Socket: TSocket; ErrorCode: Integer); override;
procedure SetListen(Value: Boolean); virtual;
procedure SetPort(Value: Word); override;
procedure DoAccept; override;
procedure DoClose(Socket: TSocket); override;
procedure DoDataAvailable(Client: TSimpleTCPClient; DataSize: Integer; var Handled: Boolean); override;
procedure DoRead(Client: TSimpleTCPClient; Stream: TStream); override;
public
constructor Create(aOwner: TComponent); override;
destructor Destroy; override;
function SendEx(Client: TSimpleTCPClient; var Buffer; BufLength: Integer): Integer; // EX
function Send(Client: TSimpleTCPClient; Buffer: PChar; BufLength: Integer): Integer; // returns N of bytes sent
function SendStream(Client: TSimpleTCPClient; Stream: TStream): Integer; // returns N of bytes sent
procedure Broadcast(Buffer: PChar; BufLength: Integer);
procedure BroadcastStream(Stream: TStream);
function Receive(Client: TSimpleTCPClient; Buffer: PChar; BufLength: Integer; ReceiveCompletely: Boolean): Integer;
function ReceiveStream(Client: TSimpleTCPClient; Stream: TStream; DataSize: Integer; ReceiveCompletely: Boolean): Integer;
property Connections: TList read FConnections;
published
property Listen: Boolean read FListen write SetListen stored False;
property LocalHostName: String read GetLocalHostName write SetNoneStr stored False;
property LocalIP: String read GetLocalIP write SetNoneStr stored False;
property OnAccept: TSimpleTCPAcceptEvent read FOnAccept write FOnAccept;
property OnClientConnected: TSimpleTCPServerEvent read FOnClientConnected write FOnClientConnected;
property OnClientDisconnected: TSimpleTCPServerEvent read FOnClientDisconnected write FOnClientDisconnected;
property OnClientDataAvailable: TSimpleTCPServerDataAvailEvent read FOnClientDataAvailable write FOnClientDataAvailable;
property OnClientRead: TSimpleTCPServerIOEvent read FOnClientRead write FOnClientRead;
property AllowChangeHostAndPortOnConnection;
property Port;
property OnError;
end;
TSimpleTCPClient = class(TCustomSimpleSocket)
private
FAutoTryReconnect: Boolean;
FConditionallyConnected, FConnected: Boolean;
FOnConnected: TNotifyEvent;
FOnDisconnected: TNotifyEvent;
FOnDataAvailable: TSimpleTCPClientDataAvailEvent;
FOnRead: TSimpleTCPClientIOEvent;
function GetIP: LongInt;
procedure SetIP(Value: LongInt);
protected
// procedure WndProc(var Message: TMessage); override;
procedure SocketError(Socket: TSocket; ErrorCode: Integer); override;
procedure SetConnected(Value: Boolean); virtual;
procedure SetHost(Value: String); override;
procedure SetPort(Value: Word); override;
procedure DoConnect; override;
procedure DoClose(Socket: TSocket); override;
procedure DoDataAvailable(Client: TSimpleTCPClient; DataSize: Integer; var Handled: Boolean); override;
procedure DoRead(Client: TSimpleTCPClient; Stream: TStream); override;
public
destructor Destroy; override;
function SendEx(var Buffer; BufLength: Integer): Integer; // EX
function Send(Buffer: PChar; BufLength: Integer): Integer; // returns N of bytes sent
function SendStream(Stream: TStream): Integer; // returns N of bytes sent
function Receive(Buffer: PChar; BufLength: Integer; ReceiveCompletely: Boolean): Integer;
function ReceiveStream(Stream: TStream; DataSize: Integer; ReceiveCompletely: Boolean): Integer;
property IP: LongInt read GetIP write SetIP;
published
property AutoTryReconnect: Boolean read FAutoTryReconnect write FAutoTryReconnect default False;
property Connected: Boolean read FConnected write SetConnected stored False;
property OnConnected: TNotifyEvent read FOnConnected write FOnConnected;
property OnDisconnected: TNotifyEvent read FOnDisconnected write FOnDisconnected;
property OnDataAvailable: TSimpleTCPClientDataAvailEvent read FOnDataAvailable write FOnDataAvailable;
property OnRead: TSimpleTCPClientIOEvent read FOnRead write FOnRead;
property AllowChangeHostAndPortOnConnection;
property Host;
property Port;
property OnError;
end;
procedure Register;
implementation
uses SysUtils, Forms;
const
PROTO_TCP = 'tcp';
{$IFNDEF D4}
type
SunB = packed record
s_b1, s_b2, s_b3, s_b4: Char;
end;
SunW = packed record
s_w1, s_w2: Word;
end;
in_addr = record
case Integer of
0: (S_un_b: SunB);
1: (S_un_w: SunW);
2: (S_addr: LongInt);
end;
{$ENDIF}
{ Internal utilities }
function IPToStr(IP: Integer): String;
var
Addr: in_addr;
begin
Addr.S_addr := IP;
Result := IntToStr(Byte(Addr.S_un_b.s_b1)) + '.' +
IntToStr(Byte(Addr.S_un_b.s_b2)) + '.' +
IntToStr(Byte(Addr.S_un_b.s_b3)) + '.' +
IntToStr(Byte(Addr.S_un_b.s_b4));
end;
procedure DisableDelay(FSocket : Integer);
var Opt : BOOL;
len : integer;
begin
len :=0;
Opt := true;
showmessage(inttostr(
getsockopt(FSocket, IPPROTO_TCP, TCP_NODELAY, @Opt, len)
));
showmessagE('len:'+inttostr(len));
setsockopt(FSocket, IPPROTO_TCP, TCP_NODELAY, @Opt, 0);
showmessagE('len:'+inttostr(len));
showmessage(inttostr(
getsockopt(FSocket, IPPROTO_TCP, TCP_NODELAY, @Opt, len)
));
// showmessage('hi');
// getsockopt
// SetSockOpt(FSocket, IPPROTO_TCP1, TCP_NODELAY, char(#01), sizeof(char) );
end;
function StrToIP(Host: String): LongInt;
begin
Result := inet_addr(PChar(Host))
end;
{ TCustomSimpleSocket }
constructor TCustomSimpleSocket.Create(aOwner: TComponent);
begin
inherited Create(aOwner);
FSocket := INVALID_SOCKET;
WindowHandle := AllocateHWnd(WndProc);
if WSAStartup($0101, WSAData) <> 0 then
raise Exception.Create('Can not start socket session');
end;
destructor TCustomSimpleSocket.Destroy;
begin
if WSACleanup <> 0 then
raise Exception.Create('Can not clean socket session');
DeallocateHWnd(WindowHandle);
inherited Destroy;
end;
procedure TCustomSimpleSocket.WndProc(var Message: TMessage);
begin
with Message do
try
if Msg = WM_QUERYENDSESSION then
Result := 1 // Correct shutdown
else
Dispatch(Msg);
except
Application.HandleException(Self);
end;
end;
procedure TCustomSimpleSocket.UMTCPSelect(var Msg: TMessage);
var
tmpSocket: TSocket;
tmpTCPClient: TSimpleTCPClient;
SelectEvent, I: Integer;
MS: TMemoryStream;
Handled: Boolean;
DataAvail: LongInt;
begin
I := WSAGetSelectError(Msg.LParam);
if I > WSABASEERR then
SocketError(Msg.wParam, I)
else
begin
SelectEvent := WSAGetSelectEvent(Msg.lParam);
case SelectEvent of
FD_READ: begin
tmpSocket := Msg.wParam;
{ if this is the server }
tmpTCPClient := nil;
if Assigned(FConnections) then
begin
I := FConnections.Count;
if I <> 0 then
for I := 0 to I - 1 do
begin
tmpTCPClient := FConnections[I];
if tmpTCPClient.FSocket = tmpSocket then Break;
end;
end;
MS := TMemoryStream.Create;
with MS do
try
while True do
begin
{ check whether data available }
if IoctlSocket(tmpSocket, FIONREAD, DataAvail) = SOCKET_ERROR then
begin
SocketError(tmpSocket, WSAGetLastError);
Exit;
end;
if DataAvail = 0 then Break;
Handled := False;
DoDataAvailable(tmpTCPClient, DataAvail, Handled);
if not Handled then
ReceiveStreamFrom(tmpSocket, MS, DataAvail, False);
end;
if not Handled and (MS.Size <> 0) then
begin
Seek(0, soFromBeginning); { to beginning of stream }
DoRead(tmpTCPClient, MS);
end;
finally
Free;
end;
end;
FD_CLOSE: DoClose(Msg.wParam);
FD_ACCEPT: DoAccept;
FD_CONNECT: DoConnect;
end;
end;
end;
procedure TCustomSimpleSocket.SocketError(Socket: TSocket; ErrorCode: Integer);
var
ErrorMsg: String;
begin
case ErrorCode of
WSAEINTR: ErrorMsg := 'Interrupted system call';
WSAEBADF: ErrorMsg := 'Bad file number';
WSAEACCES: ErrorMsg := 'Permission denied';
WSAEFAULT: ErrorMsg := 'Bad address';
WSAEINVAL: ErrorMsg := 'Invalid argument';
WSAEMFILE: ErrorMsg := 'Too many open files';
WSAEWOULDBLOCK: ErrorMsg := 'Operation would block';
WSAEINPROGRESS: ErrorMsg := 'Operation now in progress';
WSAEALREADY: ErrorMsg := 'Operation already in progress';
WSAENOTSOCK: ErrorMsg := 'Socket operation on non-socket';
WSAEDESTADDRREQ: ErrorMsg := 'Destination address required';
WSAEMSGSIZE: ErrorMsg := 'Message too long';
WSAEPROTOTYPE: ErrorMsg := 'Protocol wrong type for socket';
WSAENOPROTOOPT: ErrorMsg := 'Protocol not available';
WSAEPROTONOSUPPORT: ErrorMsg := 'Protocol not supported';
WSAESOCKTNOSUPPORT: ErrorMsg := 'Socket type not supported';
WSAEOPNOTSUPP: ErrorMsg := 'Operation not supported on socket';
WSAEPFNOSUPPORT: ErrorMsg := 'Protocol family not supported';
WSAEAFNOSUPPORT: ErrorMsg := 'Address family not supported by protocol family';
WSAEADDRINUSE: ErrorMsg := 'Address already in use';
WSAEADDRNOTAVAIL: ErrorMsg := 'Can''t assign requested address';
WSAENETDOWN: ErrorMsg := 'Network is down';
WSAENETUNREACH: ErrorMsg := 'Network is unreachable';
WSAENETRESET: ErrorMsg := 'Network dropped connection on reset';
WSAECONNABORTED: ErrorMsg := 'Software caused connection abort';
WSAECONNRESET: ErrorMsg := 'Connection reset by peer';
WSAENOBUFS: ErrorMsg := 'No buffer space available';
WSAEISCONN: ErrorMsg := 'Socket is already connected';
WSAENOTCONN: ErrorMsg := 'Socket is not connected';
WSAESHUTDOWN: ErrorMsg := 'Can''t send after socket shutdown';
WSAETOOMANYREFS: ErrorMsg := 'Too many references: can''t splice';
WSAETIMEDOUT: ErrorMsg := 'Connection timed out';
WSAECONNREFUSED: ErrorMsg := 'Connection refused';
WSAELOOP: ErrorMsg := 'Too many levels of symbolic links';
WSAENAMETOOLONG: ErrorMsg := 'File name too long';
WSAEHOSTDOWN: ErrorMsg := 'Host is down';
WSAEHOSTUNREACH: ErrorMsg := 'No route to host';
WSAENOTEMPTY: ErrorMsg := 'Directory not empty';
WSAEPROCLIM: ErrorMsg := 'Too many processes';
WSAEUSERS: ErrorMsg := 'Too many users';
WSAEDQUOT: ErrorMsg := 'Disk quota exceeded';
WSAESTALE: ErrorMsg := 'Stale NFS file handle';
WSAEREMOTE: ErrorMsg := 'Too many levels of remote in path';
WSASYSNOTREADY: ErrorMsg := 'Network sub-system is unusable';
WSAVERNOTSUPPORTED: ErrorMsg := 'WinSock DLL cannot support this application';
WSANOTINITIALISED: ErrorMsg := 'WinSock not initialized';
WSAHOST_NOT_FOUND: ErrorMsg := 'Host not found';
WSATRY_AGAIN: ErrorMsg := 'Non-authoritative host not found';
WSANO_RECOVERY: ErrorMsg := 'Non-recoverable error';
WSANO_DATA: ErrorMsg := 'No Data';
else ErrorMsg := 'Not a WinSock error ?';
end;
if Assigned(FOnError) then
FOnError(Self, Socket, ErrorCode, ErrorMsg)
else
raise Exception.Create(ErrorMsg);
end;
function TCustomSimpleSocket.SendBufferTo(Socket: TSocket; Buffer: PChar; BufLength: Integer): Integer; // bytes sent
begin
Result := 0;
if (Socket <> INVALID_SOCKET) and (BufLength <> 0) then
begin
Result := WinSock.Send(Socket, Buffer^, BufLength, 0);
if Result = SOCKET_ERROR then
SocketError(Socket, WSAGetLastError);
end;
end;
// 3d[Power]: sending based on pointers
function TCustomSimpleSocket.SendEx_(Socket: TSocket; var Buffer; BufLength: Integer): Integer; // bytes sent
begin
Result := 0;
if (Socket <> INVALID_SOCKET) and (BufLength <> 0) then
begin
Result := WinSock.Send(Socket, Buffer, BufLength, 0);
if Result = SOCKET_ERROR then
SocketError(Socket, WSAGetLastError);
end;
end;
function TCustomSimpleSocket.SendStreamTo(Socket: TSocket; Stream: TStream): Integer; // returns N of bytes sent
var
Buffer: Pointer;
SavePosition: LongInt;
begin
Result := 0;
if (Socket <> INVALID_SOCKET) and (Stream <> nil) then
begin
{ save position in stream and go to beginning }
SavePosition := Stream.Position;
Stream.Seek(0, soFromBeginning);
try
{ allocate memory for swap buffer }
GetMem(Buffer, Stream.Size);
try
{ filling the buffer from stream }
Stream.Read(Buffer^, Stream.Size);
{ SENDING! }
Result := WinSock.Send(Socket, Buffer^, Stream.Size, 0);
if Result = SOCKET_ERROR then { process the error if occurs }
SocketError(Socket, WSAGetLastError);
finally
{ release memory taken for buffer }
FreeMem(Buffer);
end;
finally
{ restore position in stream }
Stream.Seek(SavePosition, soFromBeginning);
end;
end;
end;
function TCustomSimpleSocket.ReceiveFrom(Socket: TSocket; Buffer: PChar; BufLength: Integer; ReceiveCompletely: Boolean): Integer;
var
p: Pointer;
DataAvail: LongInt;
begin
Result := recv(Socket, Buffer^, BufLength, 0);
if Result = SOCKET_ERROR then
begin
SocketError(Socket, WSAGetLastError);
Exit;
end;
if ReceiveCompletely then
while Result < BufLength do
begin
if IoctlSocket(Socket, FIONREAD, DataAvail) = SOCKET_ERROR then
begin
SocketError(Socket, WSAGetLastError);
Exit;
end;
if DataAvail = 0 then Continue;
p := Pointer(Integer(Buffer) + Result);
DataAvail := recv(Socket, p^, BufLength - Result, 0);
if DataAvail = SOCKET_ERROR then
begin
SocketError(Socket, WSAGetLastError);
Exit;
end;
inc(Result, DataAvail);
end;
end;
function TCustomSimpleSocket.ReceiveStreamFrom(Socket: TSocket; Stream: TStream; DataSize: Integer; ReceiveCompletely: Boolean): Integer;
var
Buf: Pointer;
begin
Result := 0;
if DataSize <= 0 then Exit;
GetMem(Buf, DataSize);
try
Result := ReceiveFrom(Socket, Buf, DataSize, ReceiveCompletely);
if Result <> 0 then
Stream.Write(Buf^, Result);
finally
FreeMem(Buf);
end;
end;
{------------------------------------------------------------}
{ TSimpleTCPServer }
constructor TSimpleTCPServer.Create(aOwner: TComponent);
begin
inherited Create(aOwner);
FConnections := TList.Create;
end;
destructor TSimpleTCPServer.Destroy;
begin
Listen := False; // cancel listening
FConnections.Free;
inherited Destroy;
end;
procedure TSimpleTCPServer.SocketError(Socket: TSocket; ErrorCode: Integer);
begin
Listen := False; // cancel listening
inherited;
end;
procedure TSimpleTCPServer.DoAccept;
var
Tmp: Integer;
tmpSocket: TSocket;
tmpTCPClient: TSimpleTCPClient;
IsAccept: Boolean;
begin
Tmp := SizeOf(SockAddrIn);
{$IFNDEF D3}
tmpSocket := WinSock.Accept(FSocket, SockAddrIn, Tmp);
{$ELSE}
tmpSocket := WinSock.Accept(FSocket, @SockAddrIn, @Tmp);
{$ENDIF}
if tmpSocket = SOCKET_ERROR then
SocketError(tmpSocket, WSAGetLastError);
{$WARNINGS OFF}
tmpTCPClient := TSimpleTCPClient.Create(nil);
{$WARNINGS ON}
tmpTCPClient.FSocket := tmpSocket;
tmpTCPClient.FHost := inet_ntoa(SockAddrIn.SIn_Addr);
tmpTCPClient.FPort := FPort;
tmpTCPClient.FConnected := True;
if Assigned(FOnAccept) then
begin
IsAccept := True;
FOnAccept(Self, tmpTCPClient, IsAccept);
if IsAccept then
Connections.Add(tmpTCPClient)
else
tmpTCPClient.Free;
end
else
Connections.Add(tmpTCPClient);
if Assigned(FOnClientConnected) then
FOnClientConnected(Self, tmpTCPClient);
end;
procedure TSimpleTCPServer.DoClose(Socket: TSocket);
var
I: Integer;
tmpTCPClient: TSimpleTCPClient;
begin
tmpTCPClient := nil;
I := FConnections.Count;
if I <> 0 then
for I := 0 to I - 1 do
begin
tmpTCPClient := FConnections[I];
if tmpTCPClient.FSocket = Socket then
begin
FConnections.Delete(I);
Break;
end;
end;
if Assigned(tmpTCPClient) then
begin
if Assigned(FOnClientDisconnected) and not (csDestroying in ComponentState) then
FOnClientDisconnected(Self, tmpTCPClient);
tmpTCPClient.Free;
end;
end;
procedure TSimpleTCPServer.DoDataAvailable(Client: TSimpleTCPClient; DataSize: Integer; var Handled: Boolean);
begin
Handled := Assigned(FOnClientDataAvailable);
if Handled then
FOnClientDataAvailable(Self, Client, DataSize);
end;
procedure TSimpleTCPServer.DoRead(Client: TSimpleTCPClient; Stream: TStream);
begin
if Assigned(FOnClientRead) then
FOnClientRead(Self, Client, Stream);
end;
function TSimpleTCPServer.Send(Client: TSimpleTCPClient; Buffer: PChar; BufLength: Integer): Integer; // bytes sent
begin
Result := SendBufferTo(Client.FSocket, Buffer, BufLength);
end;
// 3d[Power]: sending based on pointers
function TSimpleTCPServer.SendEx(Client: TSimpleTCPClient; var Buffer; BufLength: Integer): Integer; // bytes sent
begin
Result := SendEx_(Client.FSocket, Buffer, BufLength);
end;
function TSimpleTCPServer.SendStream(Client: TSimpleTCPClient; Stream: TStream): Integer; // returns N of bytes sent
begin
Result := SendStreamTo(Client.FSocket, Stream);
end;
procedure TSimpleTCPServer.Broadcast(Buffer: PChar; BufLength: Integer);
var
I: Integer;
begin
I := FConnections.Count;
if I <> 0 then
for I := 0 to I - 1 do
with TSimpleTCPClient(FConnections[I]) do
SendBufferTo(FSocket, Buffer, BufLength);
end;
procedure TSimpleTCPServer.BroadcastStream(Stream: TStream);
var
I: Integer;
begin
I := FConnections.Count;
if I <> 0 then
for I := 0 to I - 1 do
with TSimpleTCPClient(FConnections[I]) do
SendStreamTo(FSocket, Stream);
end;
function TSimpleTCPServer.Receive(Client: TSimpleTCPClient; Buffer: PChar; BufLength: Integer; ReceiveCompletely: Boolean): Integer;
begin
Result := ReceiveFrom(Client.FSocket, Buffer, BufLength, ReceiveCompletely);
end;
function TSimpleTCPServer.ReceiveStream(Client: TSimpleTCPClient; Stream: TStream; DataSize: Integer; ReceiveCompletely: Boolean): Integer;
begin
Result := ReceiveStreamFrom(Client.FSocket, Stream, DataSize, ReceiveCompletely);
end;
procedure TSimpleTCPServer.SetPort(Value: Word);
begin
if not (csDesigning in ComponentState) then
if FPort <> Value then
if FListen then
if FAllowChangeHostAndPortOnConnection then
begin
Listen := False;
FPort := Value;
Listen := True;
end
else
raise Exception.Create('Can not change Port while listening')
else FPort := Value
else
else FPort := Value;
end;
procedure TSimpleTCPServer.SetListen(Value: Boolean);
var
I: Integer;
tmpTCPClient: TSimpleTCPClient;
begin
if not (csDesigning in ComponentState) then
if FListen <> Value then
begin
if Value then
begin
FSocket := WinSock.Socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if FSocket = SOCKET_ERROR then
begin
SocketError(INVALID_SOCKET, WSAGetLastError);
Exit;
end;
SockAddrIn.sin_family := AF_INET;
SockAddrIn.sin_addr.s_addr := INADDR_ANY;
SockAddrIn.sin_port := htons(FPort);
if Bind(FSocket, SockAddrIn, SizeOf(SockAddrIn)) <> 0 then
begin
SocketError(FSocket, WSAGetLastError);
Exit;
end;
if WinSock.Listen(FSocket, SOMAXCONN) <> 0 then
begin
SocketError(FSocket, WSAGetLastError);
Exit;
end;
DisableDelay(FSocket);
if WSAAsyncSelect(FSocket, WindowHandle, UM_TCPASYNCSELECT,
FD_READ or FD_ACCEPT or FD_CLOSE) <> 0 then
begin
SocketError(FSocket, WSAGetLastError);
Exit;
end;
end
else
begin
// Closing all connections first
I := FConnections.Count;
if I <> 0 then
for I := I - 1 downto 0 do
begin
tmpTCPClient := FConnections[I];
tmpTCPClient.Connected := False;
FConnections.Delete(I);
end;
// Cancel listening
WSAASyncSelect(FSocket, WindowHandle, UM_TCPASYNCSELECT, 0);
Shutdown(FSocket, 2);
if CloseSocket(FSocket) <> 0 then
begin
SocketError(FSocket, WSAGetLastError);
Exit;
end;
FSocket := INVALID_SOCKET;
end;
FListen := Value;
end
else
else
FListen := Value;
end;
function TSimpleTCPServer.GetLocalHostName: String;
var
HostName: Array[0..MAX_PATH] of Char;
begin
if GetHostName(HostName, MAX_PATH) = 0 then
Result := HostName
else
SocketError(FSocket, WSAGetLastError);
end;
function TSimpleTCPServer.GetLocalIP: String;
var
SockAddrIn: TSockAddrIn;
HostEnt: PHostEnt;
HostName: Array[0..MAX_PATH] of Char;
begin
if GetHostName(HostName, MAX_PATH) = 0 then
begin
HostEnt:= GetHostByName(HostName);
if HostEnt = nil then
Result := ''
else
begin
SockAddrIn.sin_addr.S_addr := LongInt(PLongInt(HostEnt^.h_addr_list^)^);
Result := inet_ntoa(SockAddrIn.sin_addr);
end;
end
else
SocketError(FSocket, WSAGetLastError);
end;
procedure TSimpleTCPServer.SetNoneStr(Value: String); begin end;
{------------------------------------------------------------}
{ TSimpleTCPClient }
destructor TSimpleTCPClient.Destroy;
begin
Connected := False;
inherited Destroy;
end;
{procedure TSimpleTCPClient.WndProc(var Message: TMessage);
begin
inherited WndProc(Message);
end;}
procedure TSimpleTCPClient.SocketError(Socket: TSocket; ErrorCode: Integer);
begin
Connected := False; // broke connection
inherited;
end;
procedure TSimpleTCPClient.DoConnect;
begin
FConnected := True; { definitely connected! }
if Assigned(FOnConnected) then
FOnConnected(Self);
end;
procedure TSimpleTCPClient.DoClose(Socket: TSocket);
begin
Connected := False;
if not (csDestroying in ComponentState) then
begin
if Assigned(FOnDisconnected) then
FOnDisconnected(Self);
if FAutoTryReconnect then
Connected := True;
end;
end;
procedure TSimpleTCPClient.DoDataAvailable(Client: TSimpleTCPClient; DataSize: Integer; var Handled: Boolean);
begin
Handled := Assigned(FOnDataAvailable);
if Handled then
FOnDataAvailable(Self, DataSize);
end;
procedure TSimpleTCPClient.DoRead(Client: TSimpleTCPClient; Stream: TStream);
begin
if Assigned(FOnRead) then
FOnRead(Self, Stream);
end;
function TSimpleTCPClient.Send(Buffer: PChar; BufLength: Integer): Integer; // bytes sent
begin
Result := SendBufferTo(FSocket, Buffer, BufLength);
end;
// 3d[Power]: sending based on pointers
function TSimpleTCPClient.SendEx(var Buffer; BufLength: Integer): Integer; // EX
begin
Result := SendEx_(FSocket, Buffer, BufLength);
end;
function TSimpleTCPClient.SendStream(Stream: TStream): Integer; // returns N of bytes sent
begin
Result := SendStreamTo(FSocket, Stream);
end;
function TSimpleTCPClient.Receive(Buffer: PChar; BufLength: Integer; ReceiveCompletely: Boolean): Integer;
begin
Result := ReceiveFrom(FSocket, Buffer, BufLength, ReceiveCompletely);
end;
function TSimpleTCPClient.ReceiveStream(Stream: TStream; DataSize: Integer; ReceiveCompletely: Boolean): Integer;
begin
Result := ReceiveStreamFrom(FSocket, Stream, DataSize, ReceiveCompletely);
end;
procedure TSimpleTCPClient.SetConnected(Value: Boolean);
var
lin: TLinger;
linx: Array[0..3] of Char absolute lin;
ErrorCode: Integer;
begin
if not (csDesigning in ComponentState) then
if FConditionallyConnected <> Value then
begin
FConditionallyConnected := Value;
if Value then
begin
SockAddrIn.sin_family := AF_INET;
SockAddrIn.sin_port := htons(FPort);
SockAddrIn.sin_addr.s_addr := inet_addr(PChar(Host));
if SockAddrIn.sin_addr.s_addr = -1 then
begin
HostEnt := GetHostByName(PChar(Host));
if HostEnt = nil then
begin
SocketError(INVALID_SOCKET, WSAEFAULT);
Exit;
end;
SockAddrIn.sin_addr.S_addr := LongInt(PLongInt(HostEnt^.h_addr_list^)^);
end;
FSocket := WinSock.Socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if FSocket = SOCKET_ERROR then
begin
SocketError(INVALID_SOCKET, WSAGetLastError);
Exit;
end;
DisableDelay(FSocket);
ErrorCode := WSAASyncSelect(FSocket, WindowHandle, UM_TCPASYNCSELECT,
FD_READ or FD_CONNECT or FD_CLOSE);
if ErrorCode <> 0 then
begin
SocketError(FSocket, WSAGetLastError);
Exit;
end;
ErrorCode := WinSock.Connect(FSocket, SockAddrIn, SizeOf(SockAddrIn));
if ErrorCode <> 0 then
begin
ErrorCode := WSAGetLastError;
if ErrorCode <> WSAEWOULDBLOCK then
begin
SocketError(FSocket, WSAGetLastError);
Exit;
end;
end;
end
else
begin
DisableDelay(FSocket);
WSAASyncSelect(FSocket, WindowHandle, UM_TCPASYNCSELECT, 0);
Shutdown(FSocket, 2);
lin.l_onoff := 1;
lin.l_linger := 0;
SetSockOpt(FSocket, SOL_SOCKET, SO_LINGER, linx, SizeOf(Lin));
ErrorCode := CloseSocket(FSocket);
if ErrorCode <> 0 then
begin
SocketError(FSocket, WSAGetLastError);
Exit;
end;
FSocket := INVALID_SOCKET;
FConnected := False;
if Assigned(FOnDisconnected) and not (csDestroying in ComponentState) then
FOnDisconnected(Self);
end;
end