8 IdComponent
{, IdException}, IdGlobal
, IdSocketHandle
;
11 ID_UDP_BUFFERSIZE
= 8192;
14 TIdUDPBase
= object(TIdComponent
)
16 FBinding
: TIdSocketHandle
;
21 FReceiveTimeout
: Integer;
23 FBroadcastEnabled
: Boolean;
24 procedure BroadcastEnabledChanged
;// dynamic;
25 procedure CloseBinding
; virtual;
26 function GetActive
: Boolean; virtual;
27 procedure SetActive(const Value
: Boolean);
28 procedure SetBroadcastFlag(const Enabled
: Boolean; ABinding
: TIdSocketHandle
30 procedure SetBroadcastEnabled(const Value
: Boolean);
31 function GetBinding
: TIdSocketHandle
; virtual;
32 procedure Loaded
; virtual;//override;
34 { constructor Create(AOwner: TComponent); override;
37 property Binding
: TIdSocketHandle read GetBinding
;
38 procedure Broadcast(const AData
: string; const APort
: integer);
39 function ReceiveBuffer(var ABuffer
; const ABufferSize
: Integer;
40 const AMSec
: Integer = IdTimeoutDefault
): integer; overload
;
41 function ReceiveBuffer(var ABuffer
; const ABufferSize
: Integer; var VPeerIP
:
43 var VPeerPort
: integer; AMSec
: Integer = IdTimeoutDefault
): integer;
45 function ReceiveString(const AMSec
: Integer = IdTimeoutDefault
): string;
47 function ReceiveString(var VPeerIP
: string; var VPeerPort
: integer;
48 const AMSec
: Integer = IdTimeoutDefault
): string; overload
;
49 procedure Send(AHost
: string; const APort
: Integer; const AData
: string);
50 procedure SendBuffer(AHost
: string; const APort
: Integer; var ABuffer
;
51 const AByteCount
: integer);
53 property ReceiveTimeout
: Integer read FReceiveTimeout write FReceiveTimeout
54 default IdTimeoutInfinite
;
56 property Active
: Boolean read GetActive write SetActive default
False;
57 property BufferSize
: Integer read FBufferSize write FBufferSize default
59 property BroadcastEnabled
: Boolean read FBroadcastEnabled write
60 SetBroadcastEnabled default
False;
62 PIdUDPBase
=^TIdUDPBase
;
63 function NewIdUDPBase(AOwner
: PControl
):PIdUDPBase
;{ type MyStupid0=DWord;
64 EIdUDPException = object(EIdException);
65 PdUDPBase=^IdUDPBase; type MyStupid3137=DWord;
66 EIdUDPReceiveErrorZeroBytes = object(EIdUDPException);
67 PUDPBase=^dUDPBase; type MyStupid86104=DWord; }
72 IdResourceStrings
, IdStack
, IdStackConsts
, SysUtils
;
74 procedure TIdUDPBase
.Broadcast(const AData
: string; const APort
: integer);
76 // SetBroadcastFlag(True);
77 Send('255.255.255.255', APort
, AData
);
78 BroadcastEnabledChanged
;
81 procedure TIdUDPBase
.BroadcastEnabledChanged
;
83 // SetBroadcastFlag(BroadcastEnabled);
86 procedure TIdUDPBase
.CloseBinding
;
91 //constructor TIdUDPBase.Create(AOwner: TComponent);
92 function NewIdUDPBase(AOwner
: PControl
):PIdUDPBase
;
95 New( Result
, Create
);
98 BufferSize
:= ID_UDP_BUFFERSIZE
;
99 FReceiveTimeout
:= IdTimeoutInfinite
;
103 destructor TIdUDPBase
.Destroy
;
109 function TIdUDPBase
.GetActive
: Boolean;
111 // Result := FDsgnActive or (Assigned(FBinding) and FBinding.HandleAllocated);
114 function TIdUDPBase
.GetBinding
: TIdSocketHandle
;
116 // if not Assigned(FBinding) then
118 // FBinding := TIdSocketHandle.Create(nil);
120 if not FBinding
.HandleAllocated
then
122 FBinding
.AllocateSocket(Id_SOCK_DGRAM
);
123 BroadcastEnabledChanged
;
128 procedure TIdUDPBase
.Loaded
;
134 FDsgnActive
:= False;
138 function TIdUDPBase
.ReceiveBuffer(var ABuffer
; const ABufferSize
: Integer;
139 const AMSec
: Integer): Integer;
144 Result
:= ReceiveBuffer(ABuffer
, ABufferSize
, VoidIP
, VoidPort
, AMSec
);
147 function TIdUDPBase
.ReceiveBuffer(var ABuffer
; const ABufferSize
: Integer;
148 var VPeerIP
: string; var VPeerPort
: integer;
149 AMSec
: Integer = IdTimeoutDefault
): integer;
151 if AMSec
= IdTimeoutDefault
then
153 AMSec
:= ReceiveTimeOut
;
155 if not Binding
.Readable(AMSec
) then
162 Result
:= Binding
.RecvFrom(ABuffer
, ABufferSize
, 0, VPeerIP
, VPeerPort
);
163 GStack
.CheckForSocketError(Result
);
166 // raise EIdUDPReceiveErrorZeroBytes.Create(RSUDPReceiveError0);
170 function TIdUDPBase
.ReceiveString(var VPeerIP
: string; var VPeerPort
: integer;
171 const AMSec
: Integer = IdTimeoutDefault
): string;
175 SetLength(Result
, BufferSize
);
176 i
:= ReceiveBuffer(Result
[1], Length(Result
), VPeerIP
, VPeerPort
, AMSec
);
177 SetLength(Result
, i
);
180 function TIdUDPBase
.ReceiveString(const AMSec
: Integer): string;
185 result
:= ReceiveString(VoidIP
, VoidPort
, AMSec
);
188 procedure TIdUDPBase
.Send(AHost
: string; const APort
: Integer; const AData
:
191 SendBuffer(AHost
, APort
, PChar(AData
)^, Length(AData
));
194 procedure TIdUDPBase
.SendBuffer(AHost
: string; const APort
: Integer; var
196 const AByteCount
: integer);
198 AHost
:= GStack
.ResolveHost(AHost
);
199 Binding
.SendTo(AHost
, APort
, ABuffer
, AByteCount
);
202 procedure TIdUDPBase
.SetActive(const Value
: Boolean);
204 if (Active
<> Value
) then
206 // if not ((csDesigning in ComponentState) or (csLoading in ComponentState))
220 FDsgnActive := Value;
225 procedure TIdUDPBase
.SetBroadcastEnabled(const Value
: Boolean);
227 if FBroadCastEnabled
<> Value
then
229 FBroadcastEnabled
:= Value
;
232 BroadcastEnabledChanged
;
237 procedure TIdUDPBase
.SetBroadcastFlag(const Enabled
: Boolean; ABinding
:
238 TIdSocketHandle
{ = nil});
240 BroadCastData
: LongBool
;
242 BroadCastData
:= Enabled
;
243 // if ABinding = nil then
247 ABinding
.SetSockOpt(Id_SOL_SOCKET
, Id_SO_BROADCAST
, PChar(@BroadCastData
),
248 SizeOf(BroadCastData
));