IdIPMCastClient.pas 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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.6 14/06/2004 21:38:28 CCostelloe
  18. Converted StringToTIn4Addr call
  19. Rev 1.5 09/06/2004 10:00:34 CCostelloe
  20. Kylix 3 patch
  21. Rev 1.4 2004.02.03 5:43:52 PM czhower
  22. Name changes
  23. Rev 1.3 1/21/2004 3:11:08 PM JPMugaas
  24. InitComponent
  25. Rev 1.2 10/26/2003 09:11:52 AM JPMugaas
  26. Should now work in NET.
  27. Rev 1.1 2003.10.12 4:03:56 PM czhower
  28. compile todos
  29. Rev 1.0 11/13/2002 07:55:22 AM JPMugaas
  30. }
  31. unit IdIPMCastClient;
  32. interface
  33. {$I IdCompilerDefines.inc}
  34. //Put FPC into Delphi mode
  35. uses
  36. Classes,
  37. IdException,
  38. IdGlobal,
  39. IdIPMCastBase,
  40. IdUDPBase,
  41. IdComponent,
  42. IdSocketHandle,
  43. IdThread;
  44. const
  45. DEF_IMP_THREADEDEVENT = False;
  46. type
  47. TIPMCastReadEvent = procedure(Sender: TObject; const AData: TIdBytes; ABinding: TIdSocketHandle) of object;
  48. TIdIPMCastClient = class;
  49. TIdIPMCastListenerThread = class(TIdThread)
  50. protected
  51. IncomingData: TIdSocketHandle;
  52. FAcceptWait: integer;
  53. FBuffer: TIdBytes;
  54. FBufferSize: integer;
  55. procedure Run; override;
  56. public
  57. FServer: TIdIPMCastClient;
  58. //
  59. constructor Create(AOwner: TIdIPMCastClient); reintroduce;
  60. destructor Destroy; override;
  61. procedure IPMCastRead;
  62. //
  63. property AcceptWait: integer read FAcceptWait write FAcceptWait;
  64. end;
  65. TIdIPMCastClient = class(TIdIPMCastBase)
  66. protected
  67. FBindings: TIdSocketHandles;
  68. FBufferSize: Integer;
  69. FCurrentBinding: TIdSocketHandle;
  70. FListenerThread: TIdIPMCastListenerThread;
  71. FOnBeforeBind: TIdSocketHandleEvent;
  72. FOnAfterBind: TNotifyEvent;
  73. FOnIPMCastRead: TIPMCastReadEvent;
  74. FThreadedEvent: boolean;
  75. //
  76. procedure CloseBinding; override;
  77. procedure DoBeforeBind(AHandle: TIdSocketHandle); virtual;
  78. procedure DoAfterBind; virtual;
  79. procedure DoIPMCastRead(const AData: TIdBytes; ABinding: TIdSocketHandle);virtual;
  80. function GetActive: Boolean; override;
  81. function GetBinding: TIdSocketHandle; override;
  82. function GetDefaultPort: integer;
  83. procedure PacketReceived(const AData: TIdBytes; ABinding: TIdSocketHandle);
  84. procedure SetBindings(const Value: TIdSocketHandles);
  85. procedure SetDefaultPort(const AValue: integer);
  86. procedure InitComponent; override;
  87. public
  88. destructor Destroy; override;
  89. //
  90. published
  91. property IPVersion default ID_DEFAULT_IP_VERSION;
  92. property Active;
  93. property Bindings: TIdSocketHandles read FBindings write SetBindings;
  94. property BufferSize: Integer read FBufferSize write FBufferSize default ID_UDP_BUFFERSIZE;
  95. property DefaultPort: integer read GetDefaultPort write SetDefaultPort;
  96. property MulticastGroup;
  97. property ReuseSocket;
  98. property ThreadedEvent: boolean read FThreadedEvent write FThreadedEvent default DEF_IMP_THREADEDEVENT;
  99. property OnBeforeBind: TIdSocketHandleEvent read FOnBeforeBind write FOnBeforeBind;
  100. property OnAfterBind: TNotifyEvent read FOnAfterBind write FOnAfterBind;
  101. property OnIPMCastRead: TIPMCastReadEvent read FOnIPMCastRead write FOnIPMCastRead;
  102. end;
  103. implementation
  104. uses
  105. IdResourceStringsCore,
  106. IdStack,
  107. IdStackConsts,
  108. SysUtils;
  109. { TIdIPMCastClient }
  110. procedure TIdIPMCastClient.InitComponent;
  111. begin
  112. inherited InitComponent;
  113. BufferSize := ID_UDP_BUFFERSIZE;
  114. FThreadedEvent := DEF_IMP_THREADEDEVENT;
  115. FBindings := TIdSocketHandles.Create(Self);
  116. end;
  117. procedure TIdIPMCastClient.CloseBinding;
  118. var
  119. i: integer;
  120. begin
  121. if Assigned(FCurrentBinding) then begin
  122. // Necessary here - cancels the recvfrom in the listener thread
  123. FListenerThread.Stop;
  124. try
  125. for i := 0 to Bindings.Count - 1 do begin
  126. if Bindings[i].HandleAllocated then begin
  127. // RLebeau: DropMulticastMembership() can raise an exception if
  128. // the network cable has been pulled out...
  129. // TODO: update DropMulticastMembership() to not raise an exception...
  130. try
  131. Bindings[i].DropMulticastMembership(FMulticastGroup);
  132. except
  133. end;
  134. end;
  135. Bindings[i].CloseSocket;
  136. end;
  137. finally
  138. FListenerThread.WaitFor;
  139. FreeAndNil(FListenerThread);
  140. FCurrentBinding := nil;
  141. end;
  142. end;
  143. end;
  144. procedure TIdIPMCastClient.DoIPMCastRead(const AData: TIdBytes; ABinding: TIdSocketHandle);
  145. begin
  146. if Assigned(OnIPMCastRead) then begin
  147. OnIPMCastRead(Self, AData, ABinding);
  148. end;
  149. end;
  150. procedure TIdIPMCastClient.DoBeforeBind(AHandle: TIdSocketHandle);
  151. begin
  152. if Assigned(FOnBeforeBind) then begin
  153. FOnBeforeBind(AHandle);
  154. end;
  155. end;
  156. procedure TIdIPMCastClient.DoAfterBind;
  157. begin
  158. if Assigned(FOnAfterBind) then begin
  159. FOnAfterBind(Self);
  160. end;
  161. end;
  162. function TIdIPMCastClient.GetActive: Boolean;
  163. begin
  164. if IsDesignTime then begin
  165. // inherited GetActive keeps track of design-time Active property
  166. Result := inherited GetActive;
  167. end else begin
  168. Result := Assigned(FCurrentBinding);
  169. if Result then begin
  170. Result := FCurrentBinding.HandleAllocated;
  171. end;
  172. end;
  173. end;
  174. function TIdIPMCastClient.GetBinding: TIdSocketHandle;
  175. var
  176. i: integer;
  177. LBinding: TIdSocketHandle;
  178. begin
  179. if not Assigned(FCurrentBinding) then
  180. begin
  181. if Bindings.Count < 1 then begin
  182. if DefaultPort > 0 then begin
  183. Bindings.Add.IPVersion := FIPVersion;
  184. end else begin
  185. raise EIdMCastNoBindings.Create(RSNoBindingsSpecified);
  186. end;
  187. end;
  188. for i := 0 to Bindings.Count - 1 do begin
  189. try
  190. LBinding := Bindings[i];
  191. LBinding.AllocateSocket(Id_SOCK_DGRAM);
  192. // do not overwrite if the default. This allows ReuseSocket to be set per binding
  193. if FReuseSocket <> rsOSDependent then begin
  194. LBinding.ReuseSocket := FReuseSocket;
  195. end;
  196. DoBeforeBind(LBinding);
  197. LBinding.Bind;
  198. LBinding.AddMulticastMembership(FMulticastGroup);
  199. if not Assigned(FCurrentBinding) then begin
  200. FCurrentBinding := LBinding;
  201. end;
  202. except
  203. end;
  204. end;
  205. DoAfterBind;
  206. // RLebeau: why only one listener thread total, instead of one per Binding,
  207. // like TIdUDPServer uses?
  208. FListenerThread := TIdIPMCastListenerThread.Create(Self);
  209. end;
  210. Result := FCurrentBinding;
  211. end;
  212. function TIdIPMCastClient.GetDefaultPort: integer;
  213. begin
  214. Result := FBindings.DefaultPort;
  215. end;
  216. procedure TIdIPMCastClient.PacketReceived(const AData: TIdBytes; ABinding: TIdSocketHandle);
  217. begin
  218. FCurrentBinding := ABinding;
  219. DoIPMCastRead(AData, ABinding);
  220. end;
  221. procedure TIdIPMCastClient.SetBindings(const Value: TIdSocketHandles);
  222. begin
  223. FBindings.Assign(Value);
  224. end;
  225. procedure TIdIPMCastClient.SetDefaultPort(const AValue: integer);
  226. begin
  227. if (FBindings.DefaultPort <> AValue) then begin
  228. FBindings.DefaultPort := AValue;
  229. FPort := AValue;
  230. end;
  231. end;
  232. destructor TIdIPMCastClient.Destroy;
  233. begin
  234. Active := False;
  235. FreeAndNil(FBindings);
  236. inherited Destroy;
  237. end;
  238. { TIdIPMCastListenerThread }
  239. constructor TIdIPMCastListenerThread.Create(AOwner: TIdIPMCastClient);
  240. var
  241. LName: string;
  242. begin
  243. FAcceptWait := 1000;
  244. FBufferSize := AOwner.BufferSize;
  245. FBuffer := nil;
  246. FServer := AOwner;
  247. LName := AOwner.Name;
  248. if LName = '' then begin
  249. LName := 'IdIPMCastClient'; {do not localize}
  250. end;
  251. inherited Create(False, False, LName + ' Listener'); {do not localize}
  252. end;
  253. destructor TIdIPMCastListenerThread.Destroy;
  254. begin
  255. inherited Destroy;
  256. end;
  257. procedure TIdIPMCastListenerThread.Run;
  258. var
  259. PeerIP: string;
  260. PeerPort: TIdPort;
  261. PeerIPVersion: TIdIPVersion;
  262. ByteCount: Integer;
  263. LSocketList, LReadList: TIdSocketList;
  264. i: Integer;
  265. LBuffer : TIdBytes;
  266. begin
  267. SetLength(LBuffer, FBufferSize);
  268. // create a socket list to select for read
  269. LSocketList := TIdSocketList.CreateSocketList;
  270. try
  271. // fill list of socket handles for reading
  272. for i := 0 to FServer.Bindings.Count - 1 do
  273. begin
  274. LSocketList.Add(FServer.Bindings[i].Handle);
  275. end;
  276. LReadList := TIdSocketList.CreateSocketList;
  277. try
  278. while not Stopped do
  279. begin
  280. // select the handles for reading
  281. LReadList.Clear;
  282. if LSocketList.SelectReadList(LReadList, AcceptWait) then
  283. begin
  284. for i := 0 to LReadList.Count - 1 do
  285. begin
  286. // Doublecheck to see if we've been stopped
  287. // Depending on timing - may not reach here
  288. // if stopped the run method of the ancestor
  289. if Stopped then begin
  290. Exit;
  291. end;
  292. IncomingData := FServer.Bindings.BindingByHandle(LReadList[i]);
  293. if IncomingData <> nil then
  294. begin
  295. ByteCount := IncomingData.RecvFrom(LBuffer, PeerIP, PeerPort, PeerIPVersion);
  296. // RLebeau: some protocols make use of 0-length messages, so don't discard
  297. // them here. This is not connection-oriented, so recvfrom() only returns
  298. // 0 if a 0-length packet was actually received...
  299. if ByteCount >= 0 then
  300. begin
  301. SetLength(FBuffer, ByteCount);
  302. CopyTIdBytes(LBuffer, 0, FBuffer, 0, ByteCount);
  303. IncomingData.SetPeer(PeerIP, PeerPort, PeerIPVersion);
  304. if FServer.ThreadedEvent then begin
  305. IPMCastRead;
  306. end else begin
  307. Synchronize(IPMCastRead);
  308. end;
  309. end;
  310. end;
  311. end;
  312. end;
  313. end;
  314. finally
  315. LReadList.Free;
  316. end;
  317. finally
  318. LSocketList.Free;
  319. end;
  320. end;
  321. procedure TIdIPMCastListenerThread.IPMCastRead;
  322. begin
  323. FServer.PacketReceived(FBuffer, IncomingData);
  324. end;
  325. end.