IdIPMCastServer.pas 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. {
  2. $Project$
  3. $Workfile$
  4. $Revision$
  5. $DateUTC$
  6. $Id$
  7. This file is part of the Indy (Internet Direct) project, and is offered
  8. under the dual-licensing agreement described on the Indy website.
  9. (http://www.indyproject.org/)
  10. Copyright:
  11. (c) 1993-2005, Chad Z. Hower and the Indy Pit Crew. All rights reserved.
  12. }
  13. {
  14. $Log$
  15. }
  16. {
  17. Rev 1.7 14/06/2004 21:38:42 CCostelloe
  18. Converted StringToTIn4Addr call
  19. Rev 1.6 09/06/2004 10:00:50 CCostelloe
  20. Kylix 3 patch
  21. Rev 1.5 2004.02.03 5:43:52 PM czhower
  22. Name changes
  23. Rev 1.4 1/21/2004 3:11:10 PM JPMugaas
  24. InitComponent
  25. Rev 1.3 10/26/2003 09:11:54 AM JPMugaas
  26. Should now work in NET.
  27. Rev 1.2 2003.10.24 10:38:28 AM czhower
  28. UDP Server todos
  29. Rev 1.1 2003.10.12 4:03:58 PM czhower
  30. compile todos
  31. Rev 1.0 11/13/2002 07:55:26 AM JPMugaas
  32. 2001-10-16 DSiders
  33. Modified TIdIPMCastServer.MulticastBuffer to
  34. validate the AHost argument to the method instead
  35. of the MulticastGroup property.
  36. }
  37. unit IdIPMCastServer;
  38. {
  39. Dr. Harley J. Mackenzie, Initial revision.
  40. }
  41. interface
  42. {$I IdCompilerDefines.inc}
  43. //Put FPC into Delphi mode
  44. uses
  45. IdComponent,
  46. IdGlobal,
  47. IdIPMCastBase,
  48. IdSocketHandle;
  49. const
  50. DEF_IMP_LOOPBACK = True;
  51. DEF_IMP_TTL = 1;
  52. type
  53. TIdIPMCastServer = class(TIdIPMCastBase)
  54. protected
  55. FBinding: TIdSocketHandle;
  56. FBoundIP: String;
  57. FBoundPort: TIdPort;
  58. FLoopback: Boolean;
  59. FTimeToLive: Byte;
  60. //
  61. procedure ApplyLoopback;
  62. procedure ApplyTimeToLive;
  63. procedure CloseBinding; override;
  64. function GetActive: Boolean; override;
  65. function GetBinding: TIdSocketHandle; override;
  66. procedure MulticastBuffer(const AHost: string; const APort: Integer; const ABuffer : TIdBytes);
  67. procedure SetLoopback(const AValue: Boolean); virtual;
  68. procedure SetTTL(const AValue: Byte); virtual;
  69. procedure InitComponent; override;
  70. public
  71. procedure Send(const AData: string; AByteEncoding: IIdTextEncoding = nil
  72. {$IFDEF STRING_IS_ANSI}; ASrcEncoding: IIdTextEncoding = nil{$ENDIF}
  73. ); overload;
  74. procedure Send(const ABuffer : TIdBytes); overload;
  75. destructor Destroy; override;
  76. //
  77. property Binding: TIdSocketHandle read GetBinding;
  78. published
  79. property Active;
  80. property BoundIP: String read FBoundIP write FBoundIP;
  81. property BoundPort: TIdPort read FBoundPort write FBoundPort;
  82. property Loopback: Boolean read FLoopback write SetLoopback default DEF_IMP_LOOPBACK;
  83. property MulticastGroup;
  84. property IPVersion default ID_DEFAULT_IP_VERSION;
  85. property Port;
  86. property ReuseSocket;
  87. property TimeToLive: Byte read FTimeToLive write SetTTL default DEF_IMP_TTL;
  88. end;
  89. implementation
  90. { TIdIPMCastServer }
  91. uses
  92. IdResourceStringsCore,
  93. IdStack,
  94. IdStackConsts,
  95. SysUtils;
  96. procedure TIdIPMCastServer.InitComponent;
  97. begin
  98. inherited InitComponent;
  99. FLoopback := DEF_IMP_LOOPBACK;
  100. FTimeToLive := DEF_IMP_TTL;
  101. end;
  102. destructor TIdIPMCastServer.Destroy;
  103. begin
  104. Active := False;
  105. inherited Destroy;
  106. end;
  107. procedure TIdIPMCastServer.CloseBinding;
  108. begin
  109. FreeAndNil(FBinding);
  110. end;
  111. function TIdIPMCastServer.GetActive: Boolean;
  112. begin
  113. if IsDesignTime then begin
  114. // inherited GetActive keeps track of design-time Active property
  115. Result := inherited GetActive;
  116. end else begin
  117. Result := Assigned(FBinding);
  118. if Result then begin
  119. Result := FBinding.HandleAllocated;
  120. end;
  121. end;
  122. end;
  123. function TIdIPMCastServer.GetBinding: TIdSocketHandle;
  124. begin
  125. if not Assigned(FBinding) then begin
  126. FBinding := TIdSocketHandle.Create(nil);
  127. end;
  128. if not FBinding.HandleAllocated then begin
  129. FBinding.IPVersion := FIPVersion;
  130. FBinding.AllocateSocket(Id_SOCK_DGRAM);
  131. FBinding.IP := FBoundIP;
  132. FBinding.Port := FBoundPort;
  133. FBinding.ReuseSocket := FReuseSocket;
  134. FBinding.Bind;
  135. //TODO: FBinding.EnableMulticastInterface;
  136. ApplyTimeToLive;
  137. ApplyLoopback;
  138. end;
  139. Result := FBinding;
  140. end;
  141. procedure TIdIPMCastServer.MulticastBuffer(const AHost: string; const APort: Integer; const ABuffer : TIdBytes);
  142. begin
  143. // DS - if not IsValidMulticastGroup(FMulticastGroup) then
  144. if not IsValidMulticastGroup(AHost) then begin
  145. raise EIdMCastNotValidAddress.Create(RSIPMCastInvalidMulticastAddress);
  146. end;
  147. Binding.SendTo(AHost, APort, ABuffer, Binding.IPVersion);
  148. end;
  149. procedure TIdIPMCastServer.Send(const AData: string; AByteEncoding: IIdTextEncoding = nil
  150. {$IFDEF STRING_IS_ANSI}; ASrcEncoding: IIdTextEncoding = nil{$ENDIF}
  151. );
  152. begin
  153. MulticastBuffer(FMulticastGroup, FPort, ToBytes(AData, AByteEncoding{$IFDEF STRING_IS_ANSI}, ASrcEncoding{$ENDIF}));
  154. end;
  155. procedure TIdIPMCastServer.Send(const ABuffer : TIdBytes);
  156. begin
  157. MulticastBuffer(FMulticastGroup, FPort, ABuffer);
  158. end;
  159. procedure TIdIPMCastServer.SetLoopback(const AValue: Boolean);
  160. begin
  161. if FLoopback <> AValue then begin
  162. FLoopback := AValue;
  163. ApplyLoopback;
  164. end;
  165. end;
  166. procedure TIdIPMCastServer.SetTTL(const AValue: Byte);
  167. begin
  168. if FTimeToLive <> AValue then begin
  169. FTimeToLive := AValue;
  170. ApplyTimeToLive;
  171. end;
  172. end;
  173. procedure TIdIPMCastServer.ApplyLoopback;
  174. begin
  175. if Assigned(FBinding) and FBinding.HandleAllocated then begin
  176. FBinding.SetLoopBack(FLoopback);
  177. end;
  178. end;
  179. procedure TIdIPMCastServer.ApplyTimeToLive;
  180. begin
  181. if Assigned(FBinding) and FBinding.HandleAllocated then begin
  182. FBinding.SetMulticastTTL(FTimeToLive);
  183. end;
  184. end;
  185. end.