8 IdException},KOLClasses
,
10 IdStack
, IdStackConsts
;
14 { TIdSocketHandle = object(TObj)
16 PIdSocketHandle
=^TIdSocketHandle
;
17 PIdSocketHandles
=^TIdSocketHandles
;
19 TIdSocketHandles
= object(TOwnedCollection
)
21 FDefaultPort
: integer;
23 function GetItem(Index
: Integer):PIdSocketHandle
; //TIdSocketHandle;
24 procedure SetItem(Index
: Integer; const Value
: {TIdSocketHandle}PIdSocketHandle
);
26 { constructor Create(AOwner: TComponent); }// reintroduce;
27 function Add
: PIdSocketHandle
{TIdSocketHandle};// reintroduce;
28 function BindingByHandle(const AHandle
: TIdStackSocketHandle
):
29 { TIdSocketHandle}PIdSocketHandle
;
30 property Items
[Index
: Integer]: PIdSocketHandle
{TIdSocketHandle} read GetItem write SetItem
;
33 property DefaultPort
: integer read FDefaultPort write FDefaultPort
;
35 //PdSocketHandle=^IdSocketHandle;
36 //function NewIdSocketHandles(AOwner: PControl):PIdSocketHandles;
39 TIdSocketHandle
= object(TCollectionItem
)
41 // FACollection:PCollection;
42 FHandle
: TIdStackSocketHandle
;
43 FHandleAllocated
: Boolean;
45 FPort
, FPeerPort
: integer;
47 procedure Init
; virtual;
48 procedure Accept(ASocket
: TIdStackSocketHandle
);
49 procedure AllocateSocket(const ASocketType
: Integer = Id_SOCK_STREAM
;
50 const AProtocol
: Integer = Id_IPPROTO_IP
);
52 procedure Assign(Source
: PObj
{TPersistent});// override;
54 procedure CloseSocket(const AResetLocal
: boolean = True); virtual;
55 function Connect(const AFamily
: Integer = Id_PF_INET
): Integer; virtual;
56 { constructor Create(ACollection: TCollection); override;
58 virtual; procedure Listen(const anQueueCount
: integer = 5);
59 function Readable(AMSec
: Integer = IdTimeoutDefault
): boolean;
60 function Recv(var ABuf
; ALen
, AFlags
: Integer): Integer;
61 function RecvFrom(var ABuffer
; const ALength
, AFlags
: Integer; var VIP
:
63 var VPort
: Integer): Integer; virtual;
64 procedure Reset(const AResetLocal
: boolean = True);
65 function Send(var Buf
; len
, flags
: Integer): Integer;
66 procedure SendTo(const AIP
: string; const APort
: Integer; var ABuffer
;
67 const ABufferSize
: Integer);
68 procedure SetPeer(const asIP
: string; anPort
: integer);
69 procedure SetSockOpt(level
, optname
: Integer; optval
: PChar
; optlen
:
71 procedure UpdateBindingLocal
;
72 procedure UpdateBindingPeer
;
74 property HandleAllocated
: Boolean read FHandleAllocated
;
75 property Handle
: TIdStackSocketHandle read FHandle
;
76 property PeerIP
: string read FPeerIP
;
77 property PeerPort
: integer read FPeerPort
;
79 property IP
: string read FIP write FIP
;
80 property Port
: integer read FPort write FPort
;
82 //PSocketHandle=^dSocketHandle;
83 function NewIdSocketHandles(AOwner
: PControl
):PIdSocketHandles
;
84 function NewIdSocketHandle(ACollection
: PCollection
):PIdSocketHandle
;
85 { type MyStupid86104=DWord;
86 EIdSocketHandleError = object(EIdException);
87 PocketHandle=^SocketHandle; type MyStupid20258=DWord;
88 EIdPackageSizeTooBig = object(EIdSocketHandleError);
89 PcketHandle=^ocketHandle; type MyStupid27292=DWord;
90 EIdNotAllBytesSent = object(EIdSocketHandleError);
91 PketHandle=^cketHandle; type MyStupid67165=DWord;
92 EIdCouldNotBindSocket = object(EIdSocketHandleError);
93 PetHandle=^ketHandle; type MyStupid31869=DWord;}
101 procedure TIdSocketHandle
.AllocateSocket(const ASocketType
: Integer =
103 const AProtocol
: Integer = Id_IPPROTO_IP
);
107 FHandle
:= GStack
.CreateSocketHandle(ASocketType
, AProtocol
);
108 FHandleAllocated
:= True;
111 procedure TIdSocketHandle
.CloseSocket(const AResetLocal
: boolean = True);
113 if HandleAllocated
then
115 FHandleAllocated
:= False;
116 GStack
.WSShutdown(Handle
, Id_SD_Send
);
117 GStack
.WSCloseSocket(Handle
);
122 function TIdSocketHandle
.Connect(const AFamily
: Integer = Id_PF_INET
): Integer;
124 result
:= GStack
.WSConnect(Handle
, AFamily
, PeerIP
, PeerPort
);
125 if result
<> Id_Socket_Error
then
132 destructor TIdSocketHandle
.Destroy
;
138 function TIdSocketHandle
.Recv(var ABuf
; ALen
, AFlags
: Integer): Integer;
140 result
:= GStack
.WSRecv(Handle
, ABuf
, ALen
, AFlags
);
143 function TIdSocketHandle
.Send(var Buf
; len
, flags
: Integer): Integer;
145 result
:= GStack
.WSSend(Handle
, Buf
, len
, flags
);
148 procedure TIdSocketHandle
.SetSockOpt(level
, optname
: Integer; optval
: PChar
;
151 GStack
.CheckForSocketError(GStack
.WSSetSockOpt(Handle
, level
, optname
, optval
,
155 procedure TIdSocketHandle
.SendTo(const AIP
: string; const APort
: Integer; var
157 const ABufferSize
: Integer);
161 BytesOut
:= GStack
.WSSendTo(Handle
, ABuffer
, ABufferSize
, 0, AIP
, APort
);
162 if BytesOut
= Id_SOCKET_ERROR
then
164 if GStack
.WSGetLastError() = Id_WSAEMSGSIZE
then
166 // raise EIdPackageSizeTooBig.Create(RSPackageSizeTooBig);
170 GStack
.CheckForSocketError
;
174 if BytesOut
<> ABufferSize
then
176 // raise EIdNotAllBytesSent.Create(RSNotAllBytesSent);
180 function TIdSocketHandle
.RecvFrom(var ABuffer
; const ALength
, AFlags
: Integer;
182 var VPort
: Integer): Integer;
184 result
:= GStack
.WSRecvFrom(Handle
, ABuffer
, ALength
, AFlags
, VIP
, VPort
);
187 procedure TIdSocketHandle
.Bind
;
189 if GStack
.CheckForSocketError(GStack
.WSBind(Handle
, Id_PF_INET
, IP
, Port
),
190 [Id_WSAEADDRINUSE
]) then
192 // raise EIdCouldNotBindSocket.Create(RSCouldNotBindSocket);
197 procedure TIdSocketHandle
.SetPeer(const asIP
: string; anPort
: integer);
203 procedure TIdSocketHandle
.Listen(const anQueueCount
: integer);
205 GStack
.CheckForSocketError(GStack
.WSListen(Handle
, anQueueCount
));
208 procedure TIdSocketHandle
.Accept(ASocket
: TIdStackSocketHandle
);
210 LAcceptedSocket
: TIdStackSocketHandle
;
212 LAcceptedSocket
:= GStack
.WSAccept(ASocket
, FIP
, FPort
);
213 GStack
.CheckForSocketError(LAcceptedSocket
);
214 FHandle
:= LAcceptedSocket
;
215 FHandleAllocated
:= True;
220 //constructor TIdSocketHandle.Create(ACollection: TCollection);
221 function NewIdSocketHandle(ACollection
: PCollection
):PIdSocketHandle
;
224 New( Result
, Create
);
226 FACollection
:=ACollection
;
231 if assigned(ACollection) then
233 // Port := TIdSocketHandles(ACollection).DefaultPort;
238 procedure TIdSocketHandle
.Init
;
244 if assigned(FACollection
) then
246 Port
:= PIdSocketHandles(FACollection
).DefaultPort
;
251 function TIdSocketHandle
.Readable(AMSec
: Integer = IdTimeoutDefault
): boolean;
253 ReadList
: PList
;//TList;
255 if not FHandleAllocated
then
257 // raise EIdConnClosedGracefully.Create(RSConnectionClosedGracefully);
260 if GAntiFreeze
<> nil then
262 if GAntiFreeze
.Active
then
264 if AMSec
= IdTimeoutInfinite
then
267 result
:= Readable(GAntiFreeze
.IdleTimeOut
);
272 if AMSec
> GAntiFreeze
.IdleTimeOut
then
274 result
:= Readable(AMSec
- GAntiFreeze
.IdleTimeOut
);
281 AMSec
:= GAntiFreeze
.IdleTimeOut
;
287 // ReadList := TList.Create;
289 ReadList
.Add(Pointer(Handle
));
290 Result
:= GStack
.WSSelect(ReadList
, nil, nil, AMSec
) = 1;
291 // TIdAntiFreezeBase.DoProcess(result = false);
292 finally ReadList
.free
;
296 procedure TIdSocketHandle
.Assign(Source
: PObj
);
298 LSource
: PIdSocketHandle
;//TIdSocketHandle;
300 { if ClassType <> Source.ClassType then
306 LSource
:= PIdSocketHandle(Source
);//TIdSocketHandle(Source);
308 Port
:= LSource
.Port
;
309 FPeerIP
:= LSource
.PeerIP
;
310 FPeerPort
:= LSource
.PeerPort
;
314 procedure TIdSocketHandle
.UpdateBindingLocal
;
318 GStack
.WSGetSockName(Handle
, LFamily
, FIP
, FPort
);
321 procedure TIdSocketHandle
.UpdateBindingPeer
;
325 GStack
.WSGetPeerName(Handle
, LFamily
, FPeerIP
, FPeerPort
);
328 procedure TIdSocketHandle
.Reset(const AResetLocal
: boolean = True);
330 FHandleAllocated
:= False;
331 FHandle
:= Id_INVALID_SOCKET
;
341 function TIdSocketHandles
.Add
: PIdSocketHandle
;
343 result
:= NewIdSocketHandle(@Self
);//PIdSocketHandle(inherited Add);// PIdSocketHandle(Add);// as //TIdSocketHandle;
344 result
.Port
:= DefaultPort
;
347 function TIdSocketHandles
.BindingByHandle(const AHandle
: TIdStackSocketHandle
):
354 while (i
>= 0) and (Items
[i
].Handle
<> AHandle
) do
364 //constructor TIdSocketHandles.Create(AOwner: TComponent);
365 function NewIdSocketHandles(AOwner
: PControl
):PIdSocketHandles
;
367 New( Result
, Create
);
369 /// inherited Create(AOwner, TIdSocketHandle);
372 function TIdSocketHandles
.GetItem(Index
: Integer): PIdSocketHandle
;
374 result
:= PIdSocketHandle
{TIdSocketHandle}(inherited Items
[index
]);
377 procedure TIdSocketHandles
.SetItem(Index
: Integer; const Value
:
380 inherited SetItem(Index
, Value
);