IdIPMCastServer.pas 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. Classes,
  46. IdComponent,
  47. IdGlobal,
  48. IdIPMCastBase,
  49. IdSocketHandle;
  50. const
  51. DEF_IMP_LOOPBACK = True;
  52. DEF_IMP_TTL = 1;
  53. type
  54. TIdIPMCastServer = class(TIdIPMCastBase)
  55. protected
  56. FBinding: TIdSocketHandle;
  57. FBoundIP: String;
  58. FBoundPort: TIdPort;
  59. FLoopback: Boolean;
  60. FTimeToLive: Byte;
  61. //
  62. procedure ApplyLoopback;
  63. procedure ApplyTimeToLive;
  64. procedure CloseBinding; override;
  65. function GetActive: Boolean; override;
  66. function GetBinding: TIdSocketHandle; override;
  67. procedure MulticastBuffer(const AHost: string; const APort: Integer; const ABuffer : TIdBytes);
  68. procedure SetLoopback(const AValue: Boolean); virtual;
  69. procedure SetTTL(const AValue: Byte); virtual;
  70. public
  71. constructor Create(AOwner: TComponent); override;
  72. destructor Destroy; override;
  73. //
  74. procedure Send(const AData: string; AByteEncoding: IIdTextEncoding = nil); overload;
  75. procedure Send(const ABuffer : TIdBytes); overload;
  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. constructor TIdIPMCastServer.Create(AOwner: TComponent);
  97. begin
  98. inherited Create(AOwner);
  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. begin
  151. MulticastBuffer(FMulticastGroup, FPort, ToBytes(AData, AByteEncoding));
  152. end;
  153. procedure TIdIPMCastServer.Send(const ABuffer : TIdBytes);
  154. begin
  155. MulticastBuffer(FMulticastGroup, FPort, ABuffer);
  156. end;
  157. procedure TIdIPMCastServer.SetLoopback(const AValue: Boolean);
  158. begin
  159. if FLoopback <> AValue then begin
  160. FLoopback := AValue;
  161. ApplyLoopback;
  162. end;
  163. end;
  164. procedure TIdIPMCastServer.SetTTL(const AValue: Byte);
  165. begin
  166. if FTimeToLive <> AValue then begin
  167. FTimeToLive := AValue;
  168. ApplyTimeToLive;
  169. end;
  170. end;
  171. procedure TIdIPMCastServer.ApplyLoopback;
  172. begin
  173. if Assigned(FBinding) and FBinding.HandleAllocated then begin
  174. FBinding.SetLoopBack(FLoopback);
  175. end;
  176. end;
  177. procedure TIdIPMCastServer.ApplyTimeToLive;
  178. begin
  179. if Assigned(FBinding) and FBinding.HandleAllocated then begin
  180. FBinding.SetMulticastTTL(FTimeToLive);
  181. end;
  182. end;
  183. end.