IdCustomTCPServer.pas 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179
  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.1 1/15/05 2:23:00 PM RLebeau
  18. Comment added to SetScheduler()
  19. Rev 1.0 12/2/2004 3:26:32 PM JPMugaas
  20. Moved most of TIdTCPServer here so we can use TIdTCPServer as an end point
  21. which requires an OnExecute event.
  22. Rev 1.68 11/29/04 11:50:26 PM RLebeau
  23. Updated ContextDisconected() to call DoDisconnect()
  24. Rev 1.67 11/27/04 3:28:36 AM RLebeau
  25. Updated to automatically set up the client IOHandler before calling
  26. DoConnect(), and to tear the IOHandler down before calling OnDisconnect().
  27. Rev 1.66 10/8/2004 10:11:02 PM BGooijen
  28. uncommented intercept code
  29. Rev 1.65 2004.08.13 10:55:38 czhower
  30. Removed IFDEF
  31. Rev 1.64 08.08.2004 10:43:10 OMonien
  32. temporary Thread.priority fix for Kylix
  33. Rev 1.63 6/11/2004 12:41:52 PM JPMugaas
  34. Reuse Address now reenabled.
  35. Rev 1.62 6/1/2004 1:22:28 PM DSiders
  36. Added TODO for TerminateWaitTimeout.
  37. Rev 1.61 28/04/2004 15:54:40 HHariri
  38. Changed thread priority for scheduler
  39. Rev 1.60 2004.04.22 11:44:48 PM czhower
  40. Boosted thread priority of listener thread.
  41. Rev 1.59 2004.03.06 10:40:34 PM czhower
  42. Changed IOHandler management to fix bug in server shutdowns.
  43. Rev 1.58 2004.03.01 5:12:40 PM czhower
  44. -Bug fix for shutdown of servers when connections still existed (AV)
  45. -Implicit HELP support in CMDserver
  46. -Several command handler bugs
  47. -Additional command handler functionality.
  48. Rev 1.57 2004.02.03 4:16:56 PM czhower
  49. For unit name changes.
  50. Rev 1.56 2004.01.20 10:03:36 PM czhower
  51. InitComponent
  52. Rev 1.55 1/3/2004 11:49:30 PM BGooijen
  53. the server creates a default binding for IPv6 now too, if IPv6 is supported
  54. Rev 1.54 2003.12.28 8:04:54 PM czhower
  55. Shutdown fix for .net.
  56. Rev 1.53 2003.11.29 6:03:46 PM czhower
  57. Active = True now works when set at design time.
  58. Rev 1.52 2003.10.21 12:19:02 AM czhower
  59. TIdTask support and fiber bug fixes.
  60. Rev 1.51 2003.10.18 9:33:30 PM czhower
  61. Boatload of bug fixes to command handlers.
  62. Rev 1.50 2003.10.18 8:04:28 PM czhower
  63. Fixed bug with setting active at design time.
  64. Rev 1.49 10/15/2003 11:10:00 PM DSiders
  65. Added localization comments.
  66. Added resource srting for exception raised in TIdTCPServer.SetScheduler.
  67. Rev 1.48 2003.10.15 4:34:38 PM czhower
  68. Bug fix for shutdown.
  69. Rev 1.47 2003.10.14 11:18:12 PM czhower
  70. Fix for AV on shutdown and other bugs
  71. Rev 1.46 2003.10.11 5:51:38 PM czhower
  72. -VCL fixes for servers
  73. -Chain suport for servers (Super core)
  74. -Scheduler upgrades
  75. -Full yarn support
  76. Rev 1.45 10/5/2003 9:55:26 PM BGooijen
  77. TIdTCPServer works on D7 and DotNet now
  78. Rev 1.44 10/5/2003 03:07:48 AM JPMugaas
  79. Should compile.
  80. Rev 1.43 2003.10.01 9:11:28 PM czhower
  81. .Net
  82. Rev 1.42 2003.09.30 1:23:08 PM czhower
  83. Stack split for DotNet
  84. Rev 1.41 2003.09.19 10:11:22 PM czhower
  85. Next stage of fiber support in servers.
  86. Rev 1.40 2003.09.19 11:54:34 AM czhower
  87. -Completed more features necessary for servers
  88. -Fixed some bugs
  89. Rev 1.39 2003.09.18 4:43:18 PM czhower
  90. -Removed IdBaseThread
  91. -Threads now have default names
  92. Rev 1.37 7/6/2003 8:04:10 PM BGooijen
  93. Renamed IdScheduler* to IdSchedulerOf*
  94. Rev 1.36 2003.06.30 9:41:06 PM czhower
  95. Fix for AV during server shut down.
  96. Rev 1.35 6/25/2003 3:57:58 PM BGooijen
  97. Disconnecting the context is now inside try...except
  98. Rev 1.34 6/8/2003 2:13:02 PM BGooijen
  99. Made ContextClass public
  100. Rev 1.33 6/5/2003 12:43:26 PM BGooijen
  101. changed short circuit fix code
  102. Rev 1.32 2003.06.04 10:14:08 AM czhower
  103. Removed short circuit dependency and fixed some older irrelevant code.
  104. Rev 1.31 6/3/2003 11:49:38 PM BGooijen
  105. removed AV in TIdTCPServer.DoExecute (hopefully)
  106. Rev 1.30 5/26/2003 04:29:58 PM JPMugaas
  107. Removed GenerateReply and ParseReply. Those are now obsolete duplicate
  108. functions in the new design.
  109. Rev 1.29 2003.05.26 10:35:26 PM czhower
  110. Fixed spelling typo.
  111. Rev 1.28 5/26/2003 12:20:00 PM JPMugaas
  112. Rev 1.27 2003.05.26 11:38:22 AM czhower
  113. Rev 1.26 5/25/2003 03:38:04 AM JPMugaas
  114. Rev 1.25 5/25/2003 03:26:38 AM JPMugaas
  115. Rev 1.24 5/20/2003 12:43:52 AM BGooijen
  116. changeable reply types
  117. Rev 1.23 5/13/2003 2:56:40 PM BGooijen
  118. changed GetGreating to SendGreeting
  119. Rev 1.21 4/4/2003 8:09:46 PM BGooijen
  120. moved some consts tidcmdtcpserver, changed DoExecute to return
  121. .connection.connected
  122. Rev 1.20 3/25/2003 9:04:06 PM BGooijen
  123. Scheduler in IOHandler is now updated when the scheduler is removed
  124. Rev 1.19 3/23/2003 11:33:34 PM BGooijen
  125. Updates the scheduler in the iohandler when scheduler/iohandler is changed
  126. Rev 1.18 3/22/2003 11:44:08 PM BGooijen
  127. ServerIntercept now logs connects/disconnects
  128. Rev 1.17 3/22/2003 1:46:02 PM BGooijen
  129. Better handling of exceptions in TIdListenerThread.Run (could cause mem leaks
  130. first (in non-paged-memory))
  131. Rev 1.16 3/21/2003 5:55:54 PM BGooijen
  132. Added code for serverIntercept
  133. Rev 1.15 3/21/2003 11:44:00 AM JPMugaas
  134. Updated with a OnBeforeConnect event for the TIdMappedPort components.
  135. Rev 1.14 3/20/2003 12:18:32 PM BGooijen
  136. Moved ReplyExceptionCode from TIdTCPServer to TIdCmdTCPServer
  137. Rev 1.13 3/13/2003 10:18:26 AM BGooijen
  138. Server side fibers, bug fixes
  139. Rev 1.12 2003.02.18 5:52:16 PM czhower
  140. Fix for warnings and logic error.
  141. Rev 1.11 1/23/2003 8:33:16 PM BGooijen
  142. Rev 1.10 1/23/2003 11:05:48 AM BGooijen
  143. Rev 1.9 1/20/2003 12:50:44 PM BGooijen
  144. Added a Contexts propperty, which contains all contexts for that server
  145. Moved the commandhandlers to TIdCmdTCPServer
  146. Rev 1.8 1-18-2003 0:00:30 BGooijen
  147. Removed TIdContext.OnCreate
  148. Added ContextCreated
  149. Rev 1.7 1-17-2003 23:44:32 BGooijen
  150. added support code for TIdContext.OnCreate
  151. Rev 1.6 1-17-2003 22:22:10 BGooijen
  152. new design
  153. Rev 1.5 1-10-2003 23:59:22 BGooijen
  154. Connection is now freed in destructor of TIdContext
  155. Rev 1.4 1-10-2003 19:46:22 BGooijen
  156. The context was not freed, now it is
  157. Rev 1.3 1-9-2003 11:52:28 BGooijen
  158. changed construction of TIdContext to Create(AServer: TIdTCPServer)
  159. added TIdContext property .Server
  160. Rev 1.2 1-3-2003 19:05:56 BGooijen
  161. added FContextClass:TIdContextClass to TIdTcpServer
  162. added Data:TObject to TIdContext
  163. Rev 1.1 1-1-2003 16:42:10 BGooijen
  164. Changed TIdThread to TIdYarn
  165. Added TIdContext
  166. Rev 1.0 11/13/2002 09:00:42 AM JPMugaas
  167. 2002-01-01 - Andrew P.Rybin
  168. - bug fix (MaxConnections, SetActive(FALSE)), TerminateListenerThreads, DoExecute
  169. 2002-04-17 - Andrew P.Rybin
  170. - bug fix: if exception raised in OnConnect, Threads.Remove and ThreadMgr.ReleaseThread are not called
  171. }
  172. unit IdCustomTCPServer;
  173. {
  174. Original Author and Maintainer:
  175. - Chad Z. Hower a.k.a Kudzu
  176. }
  177. interface
  178. {$I IdCompilerDefines.inc}
  179. //here to flip FPC into Delphi mode
  180. uses
  181. Classes,
  182. {$IFDEF HAS_UNIT_Generics_Collections}
  183. System.Generics.Collections,
  184. {$ENDIF}
  185. IdBaseComponent,
  186. IdComponent,IdContext, IdGlobal, IdException,
  187. IdIntercept, IdIOHandler, IdIOHandlerStack,
  188. IdReply, IdScheduler, IdSchedulerOfThread, IdServerIOHandler,
  189. IdServerIOHandlerStack, IdSocketHandle, IdStackConsts, IdTCPConnection,
  190. IdThread, IdYarn, SysUtils;
  191. const
  192. IdListenQueueDefault = 15;
  193. type
  194. TIdCustomTCPServer = class;
  195. // This is the thread that listens for incoming connections and spawns
  196. // new ones to handle each one
  197. TIdListenerThread = class(TIdThread)
  198. protected
  199. FBinding: TIdSocketHandle;
  200. FServer: TIdCustomTCPServer;
  201. FOnBeforeRun: TIdNotifyThreadEvent;
  202. //
  203. procedure AfterRun; override;
  204. procedure BeforeRun; override;
  205. procedure Run; override;
  206. public
  207. constructor Create(AServer: TIdCustomTCPServer; ABinding: TIdSocketHandle); reintroduce;
  208. //
  209. property Binding: TIdSocketHandle read FBinding;
  210. property Server: TIdCustomTCPServer read FServer;
  211. property OnBeforeRun: TIdNotifyThreadEvent read FOnBeforeRun write FOnBeforeRun;
  212. End;
  213. {$IFDEF HAS_GENERICS_TThreadList}
  214. TIdListenerThreadList = TThreadList<TIdListenerThread>;
  215. TIdListenerList = TList<TIdListenerThread>;
  216. {$ELSE}
  217. // TODO: flesh out to match TThreadList<TIdListenerThread> and TList<TIdListenerThread> for non-Generics compilers
  218. TIdListenerThreadList = TThreadList;
  219. TIdListenerList = TList;
  220. {$ENDIF}
  221. TIdListenExceptionEvent = procedure(AThread: TIdListenerThread; AException: Exception) of object;
  222. TIdServerThreadExceptionEvent = procedure(AContext: TIdContext; AException: Exception) of object;
  223. TIdServerThreadEvent = procedure(AContext: TIdContext) of object;
  224. TIdServerContext = class(TIdContext)
  225. protected
  226. FServer: TIdCustomTCPServer;
  227. public
  228. property Server: TIdCustomTCPServer read FServer;
  229. end;
  230. TIdServerContextClass = class of TIdServerContext;
  231. TIdCustomTCPServer = class(TIdComponent)
  232. protected
  233. FActive: Boolean;
  234. {$IFDEF USE_OBJECT_ARC}[Weak]{$ENDIF} FScheduler: TIdScheduler;
  235. FBindings: TIdSocketHandles;
  236. FContextClass: TIdServerContextClass;
  237. FImplicitScheduler: Boolean;
  238. FImplicitIOHandler: Boolean;
  239. {$IFDEF USE_OBJECT_ARC}[Weak]{$ENDIF} FIntercept: TIdServerIntercept;
  240. {$IFDEF USE_OBJECT_ARC}[Weak]{$ENDIF} FIOHandler: TIdServerIOHandler;
  241. FListenerThreads: TIdListenerThreadList;
  242. FListenQueue: integer;
  243. FMaxConnections: Integer;
  244. FReuseSocket: TIdReuseSocket;
  245. FTerminateWaitTime: Integer;
  246. FContexts: TIdContextThreadList;
  247. FOnContextCreated: TIdServerThreadEvent;
  248. FOnConnect: TIdServerThreadEvent;
  249. FOnDisconnect: TIdServerThreadEvent;
  250. FOnException: TIdServerThreadExceptionEvent;
  251. FOnExecute: TIdServerThreadEvent;
  252. FOnListenException: TIdListenExceptionEvent;
  253. FOnBeforeBind: TIdSocketHandleEvent;
  254. FOnAfterBind: TNotifyEvent;
  255. FOnBeforeListenerRun: TIdNotifyThreadEvent;
  256. FUseNagle : Boolean;
  257. //
  258. procedure CheckActive;
  259. procedure CheckOkToBeActive; virtual;
  260. procedure ContextCreated(AContext: TIdContext); virtual;
  261. procedure ContextConnected(AContext: TIdContext); virtual;
  262. procedure ContextDisconnected(AContext: TIdContext); virtual;
  263. function CreateConnection: TIdTCPConnection; virtual;
  264. procedure DoBeforeBind(AHandle: TIdSocketHandle); virtual;
  265. procedure DoAfterBind; virtual;
  266. procedure DoBeforeListenerRun(AThread: TIdThread); virtual;
  267. procedure DoConnect(AContext: TIdContext); virtual;
  268. procedure DoDisconnect(AContext: TIdContext); virtual;
  269. procedure DoException(AContext: TIdContext; AException: Exception); virtual;
  270. function DoExecute(AContext: TIdContext): Boolean; virtual;
  271. procedure DoListenException(AThread: TIdListenerThread; AException: Exception); virtual;
  272. procedure DoMaxConnectionsExceeded(AIOHandler: TIdIOHandler); virtual;
  273. procedure DoTerminateContext(AContext: TIdContext); virtual;
  274. function GetDefaultPort: TIdPort;
  275. procedure InitComponent; override;
  276. procedure Loaded; override;
  277. procedure Notification(AComponent: TComponent; Operation: TOperation); override;
  278. // This is needed for POP3's APOP authentication. For that,
  279. // you send a unique challenge to the client dynamically.
  280. procedure SendGreeting(AContext: TIdContext; AGreeting: TIdReply); virtual;
  281. procedure SetActive(AValue: Boolean); virtual;
  282. procedure SetBindings(const AValue: TIdSocketHandles); virtual;
  283. procedure SetDefaultPort(const AValue: TIdPort); virtual;
  284. procedure SetIntercept(const AValue: TIdServerIntercept); virtual;
  285. procedure SetIOHandler(const AValue: TIdServerIOHandler); virtual;
  286. procedure SetScheduler(const AValue: TIdScheduler); virtual;
  287. procedure Startup; virtual;
  288. procedure Shutdown; virtual;
  289. procedure TerminateAllThreads;
  290. // Occurs in the context of the peer thread
  291. property OnExecute: TIdServerThreadEvent read FOnExecute write FOnExecute;
  292. public
  293. destructor Destroy; override;
  294. //
  295. procedure StartListening;
  296. procedure StopListening;
  297. //
  298. property Contexts: TIdContextThreadList read FContexts;
  299. property ContextClass: TIdServerContextClass read FContextClass write FContextClass;
  300. property ImplicitIOHandler: Boolean read FImplicitIOHandler;
  301. property ImplicitScheduler: Boolean read FImplicitScheduler;
  302. published
  303. property Active: Boolean read FActive write SetActive default False;
  304. property Bindings: TIdSocketHandles read FBindings write SetBindings;
  305. property DefaultPort: TIdPort read GetDefaultPort write SetDefaultPort;
  306. property Intercept: TIdServerIntercept read FIntercept write SetIntercept;
  307. property IOHandler: TIdServerIOHandler read FIOHandler write SetIOHandler;
  308. property ListenQueue: integer read FListenQueue write FListenQueue default IdListenQueueDefault;
  309. property MaxConnections: Integer read FMaxConnections write FMaxConnections default 0;
  310. // right before/after binding sockets
  311. property OnBeforeBind: TIdSocketHandleEvent read FOnBeforeBind write FOnBeforeBind;
  312. property OnAfterBind: TNotifyEvent read FOnAfterBind write FOnAfterBind;
  313. property OnBeforeListenerRun: TIdNotifyThreadEvent read FOnBeforeListenerRun write FOnBeforeListenerRun;
  314. property OnContextCreated: TIdServerThreadEvent read FOnContextCreated write FOnContextCreated;
  315. // Occurs in the context of the peer thread
  316. property OnConnect: TIdServerThreadEvent read FOnConnect write FOnConnect;
  317. // Occurs in the context of the peer thread
  318. property OnDisconnect: TIdServerThreadEvent read FOnDisconnect write FOnDisconnect;
  319. // Occurs in the context of the peer thread
  320. property OnException: TIdServerThreadExceptionEvent read FOnException write FOnException;
  321. property OnListenException: TIdListenExceptionEvent read FOnListenException write FOnListenException;
  322. property ReuseSocket: TIdReuseSocket read FReuseSocket write FReuseSocket default rsOSDependent; // {$IFDEF HAS_DEPRECATED}deprecated{$IFDEF HAS_DEPRECATED_MSG} 'Use TIdSocketHandle.ReuseSocket'{$ENDIF};{$ENDIF}
  323. //UseNagle should be set to true in most cases.
  324. //See: http://tangentsoft.net/wskfaq/intermediate.html#disable-nagle and
  325. // http://tangentsoft.net/wskfaq/articles/lame-list.html#item19
  326. //The Nagle algorithm reduces the amount of needless traffic. Disabling Nagle
  327. //program’s throughput to degrade.
  328. property UseNagle: boolean read FUseNagle write FUseNagle default true;
  329. property TerminateWaitTime: Integer read FTerminateWaitTime write FTerminateWaitTime default 5000;
  330. property Scheduler: TIdScheduler read FScheduler write SetScheduler;
  331. end;
  332. EIdTCPServerError = class(EIdException);
  333. EIdNoExecuteSpecified = class(EIdTCPServerError);
  334. EIdTerminateThreadTimeout = class(EIdTCPServerError);
  335. implementation
  336. uses
  337. {$IFDEF VCL_2010_OR_ABOVE}
  338. {$IFDEF WINDOWS}
  339. Windows,
  340. {$ENDIF}
  341. {$ENDIF}
  342. IdGlobalCore,
  343. IdResourceStringsCore, IdReplyRFC,
  344. IdSchedulerOfThreadDefault, IdStack,
  345. IdThreadSafe;
  346. { TIdCustomTCPServer }
  347. procedure TIdCustomTCPServer.CheckActive;
  348. begin
  349. if Active and not (IsDesignTime or IsLoading) then begin
  350. raise EIdTCPServerError.Create(RSCannotPerformTaskWhileServerIsActive);
  351. end;
  352. end;
  353. procedure TIdCustomTCPServer.ContextCreated(AContext: TIdContext);
  354. begin
  355. if Assigned(FOnContextCreated) then begin
  356. FOnContextCreated(AContext);
  357. end;
  358. end;
  359. destructor TIdCustomTCPServer.Destroy;
  360. begin
  361. Active := False;
  362. SetIOHandler(nil);
  363. // Destroy bindings first
  364. FreeAndNil(FBindings);
  365. //
  366. FreeAndNil(FContexts);
  367. FreeAndNil(FListenerThreads);
  368. //
  369. inherited Destroy;
  370. end;
  371. procedure TIdCustomTCPServer.DoBeforeBind(AHandle: TIdSocketHandle);
  372. begin
  373. if Assigned(FOnBeforeBind) then begin
  374. FOnBeforeBind(AHandle);
  375. end;
  376. end;
  377. procedure TIdCustomTCPServer.DoAfterBind;
  378. begin
  379. if Assigned(FOnAfterBind) then begin
  380. FOnAfterBind(Self);
  381. end;
  382. end;
  383. procedure TIdCustomTCPServer.SendGreeting(AContext: TIdContext; AGreeting: TIdReply);
  384. begin
  385. AContext.Connection.IOHandler.Write(AGreeting.FormattedReply);
  386. end;
  387. procedure TIdCustomTCPServer.ContextConnected(AContext: TIdContext);
  388. var
  389. // under ARC, convert weak references to strong references before working with them
  390. LServerIntercept: TIdServerIntercept;
  391. LConnIntercept: TIdConnectionIntercept;
  392. begin
  393. LServerIntercept := Intercept;
  394. if Assigned(LServerIntercept) then begin
  395. LConnIntercept := LServerIntercept.Accept(AContext.Connection);
  396. AContext.Connection.IOHandler.Intercept := LConnIntercept;
  397. if Assigned(LConnIntercept) then begin
  398. LConnIntercept.Connect(AContext.Connection);
  399. end;
  400. end;
  401. DoConnect(AContext);
  402. end;
  403. procedure TIdCustomTCPServer.ContextDisconnected(AContext: TIdContext);
  404. var
  405. // under ARC, convert weak references to strong references before working with them
  406. LIOHandler: TIdIOHandler;
  407. LIntercept: TIdConnectionIntercept;
  408. begin
  409. DoDisconnect(AContext);
  410. LIOHandler := AContext.Connection.IOHandler;
  411. if Assigned(LIOHandler) then begin
  412. LIntercept := LIOHandler.Intercept;
  413. if Assigned(LIntercept) then begin
  414. LIntercept.Disconnect;
  415. LIOHandler.Intercept := nil;
  416. FreeAndNil(LIntercept);
  417. end;
  418. end;
  419. end;
  420. function TIdCustomTCPServer.CreateConnection: TIdTCPConnection;
  421. begin
  422. Result := TIdTCPConnection.Create(nil);
  423. end;
  424. procedure TIdCustomTCPServer.DoConnect(AContext: TIdContext);
  425. begin
  426. if Assigned(OnConnect) then begin
  427. OnConnect(AContext);
  428. end;
  429. end;
  430. procedure TIdCustomTCPServer.DoDisconnect(AContext: TIdContext);
  431. begin
  432. if Assigned(OnDisconnect) then begin
  433. OnDisconnect(AContext);
  434. end;
  435. end;
  436. procedure TIdCustomTCPServer.DoException(AContext: TIdContext; AException: Exception);
  437. begin
  438. if Assigned(OnException) then begin
  439. OnException(AContext, AException);
  440. end;
  441. end;
  442. function TIdCustomTCPServer.DoExecute(AContext: TIdContext): Boolean;
  443. var
  444. // under ARC, convert a weak reference to a strong reference before working with it
  445. LConn: TIdTCPConnection;
  446. begin
  447. if Assigned(OnExecute) then begin
  448. OnExecute(AContext);
  449. end;
  450. Result := False;
  451. if AContext <> nil then begin
  452. LConn := AContext.Connection;
  453. if LConn <> nil then begin
  454. Result := LConn.Connected;
  455. end;
  456. end;
  457. end;
  458. procedure TIdCustomTCPServer.DoListenException(AThread: TIdListenerThread; AException: Exception);
  459. begin
  460. if Assigned(FOnListenException) then begin
  461. FOnListenException(AThread, AException);
  462. end;
  463. end;
  464. function TIdCustomTCPServer.GetDefaultPort: TIdPort;
  465. begin
  466. Result := FBindings.DefaultPort;
  467. end;
  468. procedure TIdCustomTCPServer.Loaded;
  469. begin
  470. inherited Loaded;
  471. // Active = True must not be performed before all other props are loaded
  472. if Active then begin
  473. FActive := False;
  474. Active := True;
  475. end;
  476. end;
  477. // under ARC, all weak references to a freed object get nil'ed automatically
  478. // so this is mostly redundant
  479. procedure TIdCustomTCPServer.Notification(AComponent: TComponent; Operation: TOperation);
  480. begin
  481. // Remove the reference to the linked components if they are deleted
  482. if (Operation = opRemove) then begin
  483. if (AComponent = FScheduler) then begin
  484. FScheduler := nil;
  485. FImplicitScheduler := False;
  486. end
  487. else if (AComponent = FIntercept) then begin
  488. FIntercept := nil;
  489. end
  490. else if (AComponent = FIOHandler) then begin
  491. FIOHandler := nil;
  492. FImplicitIOHandler := False;
  493. end;
  494. end;
  495. inherited Notification(AComponent, Operation);
  496. end;
  497. procedure TIdCustomTCPServer.SetActive(AValue: Boolean);
  498. begin
  499. // At design time we just set the value and save it for run time.
  500. // During loading we ignore it till all other properties are set.
  501. // Loaded will recall it to toggle it
  502. if IsDesignTime or IsLoading then begin
  503. FActive := AValue;
  504. end
  505. else if FActive <> AValue then begin
  506. if AValue then begin
  507. CheckOkToBeActive;
  508. try
  509. Startup;
  510. except
  511. FActive := True;
  512. SetActive(False); // allow descendants to clean up
  513. raise;
  514. end;
  515. FActive := True;
  516. end else begin
  517. // Must set to False here. Shutdown() implementations call property setters that check this
  518. FActive := False;
  519. Shutdown;
  520. end;
  521. end;
  522. end;
  523. procedure TIdCustomTCPServer.SetBindings(const AValue: TIdSocketHandles);
  524. begin
  525. FBindings.Assign(AValue);
  526. end;
  527. procedure TIdCustomTCPServer.SetDefaultPort(const AValue: TIdPort);
  528. begin
  529. FBindings.DefaultPort := AValue;
  530. end;
  531. // RLebeau: not IFDEF'ing the entire method since it is virtual and could be
  532. // overridden in user code...
  533. procedure TIdCustomTCPServer.SetIntercept(const AValue: TIdServerIntercept);
  534. begin
  535. {$IFDEF USE_OBJECT_ARC}
  536. // under ARC, all weak references to a freed object get nil'ed automatically
  537. FIntercept := AValue;
  538. {$ELSE}
  539. if FIntercept <> AValue then
  540. begin
  541. // Remove self from the intercept's notification list
  542. if Assigned(FIntercept) then begin
  543. FIntercept.RemoveFreeNotification(Self);
  544. end;
  545. FIntercept := AValue;
  546. // Add self to the intercept's notification list
  547. if Assigned(FIntercept) then begin
  548. FIntercept.FreeNotification(Self);
  549. end;
  550. end;
  551. {$ENDIF}
  552. end;
  553. procedure TIdCustomTCPServer.SetScheduler(const AValue: TIdScheduler);
  554. var
  555. // under ARC, convert weak references to strong references before working with them
  556. LScheduler: TIdScheduler;
  557. LIOHandler: TIdServerIOHandler;
  558. begin
  559. LScheduler := FScheduler;
  560. if LScheduler <> AValue then
  561. begin
  562. // RLebeau - is this really needed? What should happen if this
  563. // gets called by Notification() if the Scheduler is freed while
  564. // the server is still Active?
  565. if Active then begin
  566. raise EIdException.Create(RSTCPServerSchedulerAlreadyActive); // TODO: create a new Exception class for this
  567. end;
  568. // under ARC, all weak references to a freed object get nil'ed automatically
  569. // If implicit one already exists free it
  570. // Free the default Thread manager
  571. if FImplicitScheduler then begin
  572. // Under D8 notification gets called after .Free of FreeAndNil, but before
  573. // its set to nil with a side effect of IDisposable. To counteract this we
  574. // set it to nil first.
  575. // -Kudzu
  576. FScheduler := nil;
  577. FImplicitScheduler := False;
  578. IdDisposeAndNil(LScheduler);
  579. end;
  580. {$IFNDEF USE_OBJECT_ARC}
  581. // Ensure we will no longer be notified when the component is freed
  582. if LScheduler <> nil then begin
  583. LScheduler.RemoveFreeNotification(Self);
  584. end;
  585. {$ENDIF}
  586. FScheduler := AValue;
  587. {$IFNDEF USE_OBJECT_ARC}
  588. // Ensure we will be notified when the component is freed, even is it's on
  589. // another form
  590. if AValue <> nil then begin
  591. AValue.FreeNotification(Self);
  592. end;
  593. {$ENDIF}
  594. LIOHandler := FIOHandler;
  595. if LIOHandler <> nil then begin
  596. LIOHandler.SetScheduler(AValue);
  597. end;
  598. end;
  599. end;
  600. procedure TIdCustomTCPServer.SetIOHandler(const AValue: TIdServerIOHandler);
  601. var
  602. // under ARC, convert a weak reference to a strong reference before working with it
  603. LIOHandler: TIdServerIOHandler;
  604. begin
  605. LIOHandler := FIOHandler;
  606. if LIOHandler <> AValue then begin
  607. // RLebeau - is this needed? SetScheduler() does it, so should SetIOHandler()
  608. // also do it? What should happen if this gets called by Notification() if the
  609. // IOHandler is freed while the server is still Active?
  610. {
  611. if Active then begin
  612. raise EIdException.Create(RSTCPServerIOHandlerAlreadyActive); // TODO: create a new Exception class for this
  613. end;
  614. }
  615. if FImplicitIOHandler then begin
  616. FIOHandler := nil;
  617. FImplicitIOHandler := False;
  618. IdDisposeAndNil(LIOHandler);
  619. end;
  620. {$IFNDEF USE_OBJECT_ARC}
  621. // Ensure we will no longer be notified when the component is freed
  622. if Assigned(LIOHandler) then begin
  623. LIOHandler.RemoveFreeNotification(Self);
  624. // TODO: do we need this?
  625. // LIOHandler.SetScheduler(nil);
  626. end;
  627. {$ENDIF}
  628. FIOHandler := AValue;
  629. if AValue <> nil then begin
  630. {$IFNDEF USE_OBJECT_ARC}
  631. // Ensure we will be notified when the component is freed, even is it's on
  632. // another form
  633. AValue.FreeNotification(Self);
  634. {$ENDIF}
  635. AValue.SetScheduler(FScheduler);
  636. end;
  637. end;
  638. end;
  639. procedure TIdCustomTCPServer.StartListening;
  640. var
  641. LListenerThreads: TIdListenerList;
  642. LListenerThread: TIdListenerThread;
  643. I: Integer;
  644. LBinding: TIdSocketHandle;
  645. LName: string;
  646. begin
  647. LListenerThreads := FListenerThreads.LockList;
  648. try
  649. // Set up any sockets that are not already listening
  650. I := LListenerThreads.Count;
  651. try
  652. while I < Bindings.Count do begin
  653. LBinding := Bindings[I];
  654. LBinding.AllocateSocket;
  655. // do not overwrite if the default. This allows ReuseSocket to be set per binding
  656. if FReuseSocket <> rsOSDependent then begin
  657. LBinding.ReuseSocket := FReuseSocket;
  658. end;
  659. DoBeforeBind(LBinding);
  660. LBinding.Bind;
  661. LBinding.UseNagle := FUseNagle;
  662. Inc(I);
  663. end;
  664. except
  665. Dec(I); // the one that failed doesn't need to be closed
  666. while I >= 0 do begin
  667. Bindings[I].CloseSocket;
  668. Dec(I);
  669. end;
  670. raise;
  671. end;
  672. if I > LListenerThreads.Count then begin
  673. DoAfterBind;
  674. end;
  675. // Set up any threads that are not already running
  676. LName := Name;
  677. if LName = '' then begin
  678. LName := 'IdCustomTCPServer'; {do not localize}
  679. end;
  680. for I := LListenerThreads.Count to Bindings.Count - 1 do
  681. begin
  682. LBinding := Bindings[I];
  683. LBinding.Listen(FListenQueue);
  684. LListenerThread := TIdListenerThread.Create(Self, LBinding);
  685. try
  686. LListenerThread.Name := LName + ' Listener #' + IntToStr(I + 1); {do not localize}
  687. LListenerThread.OnBeforeRun := DoBeforeListenerRun;
  688. //Todo: Implement proper priority handling for Linux
  689. //http://www.midnightbeach.com/jon/pubs/2002/BorCon.London/Sidebar.3.html
  690. LListenerThread.Priority := tpListener;
  691. LListenerThreads.Add(LListenerThread);
  692. except
  693. LBinding.CloseSocket;
  694. FreeAndNil(LListenerThread);
  695. raise;
  696. end;
  697. LListenerThread.Start;
  698. end;
  699. finally
  700. FListenerThreads.UnlockList;
  701. end;
  702. end;
  703. //APR-011207: for safe-close Ex: SQL Server ShutDown 1) stop listen 2) wait until all clients go out
  704. procedure TIdCustomTCPServer.StopListening;
  705. var
  706. LListenerThreads: TIdListenerList;
  707. LListener: TIdListenerThread;
  708. begin
  709. LListenerThreads := FListenerThreads.LockList;
  710. try
  711. // TODO: use two loops - one to close all of the sockets and signal all
  712. // of the threads to terminate, then another to free the threads.
  713. // This will be faster than doing everything one thread at a time...
  714. while LListenerThreads.Count > 0 do begin
  715. LListener := {$IFDEF HAS_GENERICS_TThreadList}LListenerThreads[0]{$ELSE}TIdListenerThread(LListenerThreads[0]){$ENDIF};
  716. // Stop listening
  717. LListener.Terminate;
  718. LListener.Binding.CloseSocket;
  719. // Tear down Listener thread
  720. LListener.WaitFor;
  721. LListener.Free;
  722. LListenerThreads.Delete(0); // RLebeau 2/17/2006
  723. end;
  724. finally
  725. FListenerThreads.UnlockList;
  726. end;
  727. end;
  728. {$IFDEF STRING_IS_UNICODE}
  729. //This is an ugly hack that's required because a ShortString does not seem
  730. //to be acceptable to D2009's Assert function.
  731. procedure AssertClassName(const ABool : Boolean; const AString : String); inline;
  732. begin
  733. Assert(ABool, AString);
  734. end;
  735. {$ENDIF}
  736. procedure TIdCustomTCPServer.TerminateAllThreads;
  737. var
  738. i: Integer;
  739. LContext: TIdContext;
  740. LList: TIdContextList;
  741. // under ARC, convert a weak reference to a strong reference before working with it
  742. LScheduler: TIdScheduler;
  743. begin
  744. // TODO: reimplement support for TerminateWaitTimeout
  745. //BGO: find out why TerminateAllThreads is sometimes called multiple times
  746. //Kudzu: Its because of notifications. It calls shutdown when the Scheduler is
  747. // set to nil and then again on destroy.
  748. if Contexts <> nil then begin
  749. LList := Contexts.LockList;
  750. try
  751. for i := 0 to LList.Count - 1 do begin
  752. LContext := {$IFDEF HAS_GENERICS_TList}LList.Items[i]{$ELSE}TIdContext(LList.Items[i]){$ENDIF};
  753. Assert(LContext<>nil);
  754. {$IFDEF STRING_IS_UNICODE}
  755. AssertClassName(LContext.Connection<>nil, LContext.ClassName);
  756. {$ELSE}
  757. Assert(LContext.Connection<>nil, LContext.ClassName);
  758. {$ENDIF}
  759. // RLebeau: allow descendants to perform their own cleanups before
  760. // closing the connection. FTP, for example, needs to abort an
  761. // active data transfer on a separate asociated connection
  762. DoTerminateContext(LContext);
  763. end;
  764. finally
  765. Contexts.UnLockList;
  766. end;
  767. end;
  768. // Scheduler may be nil during destroy which calls TerminateAllThreads
  769. // This happens with explicit schedulers
  770. LScheduler := FScheduler;
  771. if Assigned(LScheduler) then begin
  772. LScheduler.TerminateAllYarns;
  773. end;
  774. end;
  775. procedure TIdCustomTCPServer.DoBeforeListenerRun(AThread: TIdThread);
  776. begin
  777. if Assigned(OnBeforeListenerRun) then begin
  778. OnBeforeListenerRun(AThread);
  779. end;
  780. end;
  781. procedure TIdCustomTCPServer.DoMaxConnectionsExceeded(AIOHandler: TIdIOHandler);
  782. begin
  783. //
  784. end;
  785. procedure TIdCustomTCPServer.DoTerminateContext(AContext: TIdContext);
  786. begin
  787. // Dont call disconnect with true. Otherwise it frees the IOHandler and the thread
  788. // is still running which often causes AVs and other.
  789. //AContext.Connection.Disconnect(False);
  790. // RLebeau 9/10/2021: not calling disconnect here anymore. Just close the socket without
  791. // closing the IOHandler itself. Doing so can cause AVs and other, such as in
  792. // TIdSSLIOHandlerSocketOpenSSL, when Disconnect() calls IOHandler.Close() which
  793. // frees internal objects that may still be in use...
  794. AContext.Binding.CloseSocket;
  795. // TODO: since we are in the mist of a server shutdown, should the socket's SO_LINGER
  796. // option to enabled and set to 0 seconds to force an abortive (RSET) closure?
  797. end;
  798. procedure TIdCustomTCPServer.InitComponent;
  799. begin
  800. inherited InitComponent;
  801. FBindings := TIdSocketHandles.Create(Self);
  802. FContexts := TIdContextThreadList.Create;
  803. FContextClass := TIdServerContext;
  804. //
  805. FTerminateWaitTime := 5000;
  806. FListenQueue := IdListenQueueDefault;
  807. FListenerThreads := TIdListenerThreadList.Create;
  808. //TODO: When reestablished, use a sleeping thread instead
  809. // fSessionTimer := TTimer.Create(self);
  810. FUseNagle := true; // default
  811. end;
  812. procedure TIdCustomTCPServer.Shutdown;
  813. var
  814. // under ARC, convert the weak reference to a strong reference before working with it
  815. LIOHandler: TIdServerIOHandler;
  816. begin
  817. // tear down listening threads
  818. StopListening;
  819. // Tear down ThreadMgr
  820. try
  821. TerminateAllThreads;
  822. finally
  823. {//bgo TODO: fix this: and Threads.IsCountLessThan(1)}
  824. // DONE -oAPR: BUG! Threads still live, Mgr dead ;-(
  825. if ImplicitScheduler then begin
  826. SetScheduler(nil);
  827. end;
  828. end;
  829. LIOHandler := IOHandler;
  830. if LIOHandler <> nil then begin
  831. LIOHandler.Shutdown;
  832. end;
  833. end;
  834. // Linux/Unix does not allow an IPv4 socket and an IPv6 socket
  835. // to listen on the same port at the same time! Windows does not
  836. // have that problem...
  837. {$DEFINE CanCreateTwoBindings}
  838. {$IFDEF LINUX} // should this be UNIX instead?
  839. {$UNDEF CanCreateTwoBindings}
  840. {$ENDIF}
  841. {$IFDEF ANDROID}
  842. {$UNDEF CanCreateTwoBindings}
  843. {$ENDIF}
  844. // TODO: Would this be solved by enabling the SO_REUSEPORT option on
  845. // platforms that support it?
  846. procedure TIdCustomTCPServer.Startup;
  847. var
  848. LScheduler: TIdScheduler;
  849. LIOHandler: TIdServerIOHandler;
  850. {$IFDEF CanCreateTwoBindings}
  851. LBinding: TIdSocketHandle;
  852. {$ENDIF}
  853. begin
  854. // Set up bindings
  855. if Bindings.Count = 0 then begin
  856. // TODO: on systems that support dual-stack sockets, create a single
  857. // Binding object that supports both IPv4 and IPv6 on the same socket...
  858. {$IFDEF CanCreateTwoBindings}LBinding := {$ENDIF}Bindings.Add; // IPv4 or IPv6 by default
  859. {$IFDEF CanCreateTwoBindings}
  860. // TODO: maybe add a property so the developer can switch this behavior on/off
  861. case LBinding.IPVersion of
  862. Id_IPv4: begin
  863. if GStack.SupportsIPv6 then begin
  864. Bindings.Add.IPVersion := Id_IPv6;
  865. end;
  866. end;
  867. Id_IPv6: begin
  868. if GStack.SupportsIPv4 then begin
  869. Bindings.Add.IPVersion := Id_IPv4;
  870. end;
  871. end;
  872. end;
  873. {$ENDIF}
  874. end;
  875. // Setup IOHandler
  876. LIOHandler := FIOHandler;
  877. if not Assigned(LIOHandler) then begin
  878. LIOHandler := TIdServerIOHandlerStack.Create(Self);
  879. SetIOHandler(LIOHandler);
  880. FImplicitIOHandler := True;
  881. end;
  882. LIOHandler.Init;
  883. // Set up scheduler
  884. LScheduler := FScheduler;
  885. if not Assigned(FScheduler) then begin
  886. LScheduler := TIdSchedulerOfThreadDefault.Create(Self);
  887. SetScheduler(LScheduler);
  888. FImplicitScheduler := True;
  889. // Useful in debugging and for thread names
  890. LScheduler.Name := Name + 'Scheduler'; {do not localize}
  891. end;
  892. LScheduler.Init;
  893. StartListening;
  894. end;
  895. procedure TIdCustomTCPServer.CheckOkToBeActive;
  896. begin
  897. //nothing here. Override in a descendant for an end-point
  898. end;
  899. { TIdListenerThread }
  900. procedure TIdListenerThread.AfterRun;
  901. begin
  902. inherited AfterRun;
  903. // Close just own binding. The rest will be closed from their coresponding
  904. // threads
  905. FBinding.CloseSocket;
  906. end;
  907. procedure TIdListenerThread.BeforeRun;
  908. begin
  909. inherited BeforeRun;
  910. if Assigned(FOnBeforeRun) then begin
  911. FOnBeforeRun(Self);
  912. end;
  913. end;
  914. constructor TIdListenerThread.Create(AServer: TIdCustomTCPServer; ABinding: TIdSocketHandle);
  915. begin
  916. inherited Create;
  917. FBinding := ABinding;
  918. FServer := AServer;
  919. end;
  920. type
  921. TIdServerContextAccess = class(TIdServerContext)
  922. end;
  923. procedure TIdListenerThread.Run;
  924. var
  925. LContext: TIdServerContext;
  926. LIOHandler: TIdIOHandler;
  927. LPeer: TIdTCPConnection;
  928. LYarn: TIdYarn;
  929. begin
  930. Assert(Server<>nil);
  931. Assert(Server.IOHandler<>nil);
  932. LContext := nil;
  933. LPeer := nil;
  934. LYarn := nil;
  935. try
  936. // GetYarn can raise exceptions
  937. LYarn := Server.Scheduler.AcquireYarn;
  938. // TODO: under Windows at least, use SO_CONDITIONAL_ACCEPT to allow
  939. // the user to reject connections before they are accepted. Somehow
  940. // expose an event here for the user to decide with...
  941. LIOHandler := Server.IOHandler.Accept(Binding, Self, LYarn);
  942. if LIOHandler = nil then begin
  943. // Listening has finished
  944. Stop;
  945. Abort;
  946. end else begin
  947. // We have accepted the connection and need to handle it
  948. LPeer := TIdTCPConnection.Create(nil);
  949. {$IFDEF USE_OBJECT_ARC}
  950. // under ARC, the TIdTCPConnection.IOHandler property is a weak reference.
  951. // TIdServerIOHandler.Accept() returns an IOHandler with no Owner assigned,
  952. // so lets make the TIdTCPConnection become the Owner in order to keep the
  953. // IOHandler alive when this method exits.
  954. //
  955. // TODO: should we assign Ownership unconditionally on all platforms?
  956. //
  957. LPeer.InsertComponent(LIOHandler);
  958. {$ENDIF}
  959. LPeer.IOHandler := LIOHandler;
  960. LPeer.ManagedIOHandler := True;
  961. end;
  962. // LastRcvTimeStamp := Now; // Added for session timeout support
  963. // ProcessingTimeout := False;
  964. // Check MaxConnections
  965. if (Server.MaxConnections > 0) and (not Server.Contexts.IsCountLessThan(Server.MaxConnections)) then begin
  966. FServer.DoMaxConnectionsExceeded(LIOHandler);
  967. LPeer.Disconnect;
  968. Abort;
  969. end;
  970. // Create and init context
  971. LContext := Server.FContextClass.Create(LPeer, LYarn, Server.Contexts);
  972. LContext.FServer := Server;
  973. // We set these instead of having the context call them directly
  974. // because they are protected methods. Also its good to keep
  975. // Context indepent of the server as well.
  976. LContext.OnBeforeRun := Server.ContextConnected;
  977. LContext.OnRun := Server.DoExecute;
  978. LContext.OnAfterRun := Server.ContextDisconnected;
  979. LContext.OnException := Server.DoException;
  980. //
  981. Server.ContextCreated(LContext);
  982. //
  983. // If all ok, lets start the yarn
  984. Server.Scheduler.StartYarn(LYarn, LContext);
  985. except
  986. on E: Exception do begin
  987. // RLebeau 1/11/07: TIdContext owns the Peer by default so
  988. // take away ownership here so the Peer is not freed twice
  989. if LContext <> nil then begin
  990. TIdServerContextAccess(LContext).FOwnsConnection := False;
  991. end;
  992. FreeAndNil(LContext);
  993. FreeAndNil(LPeer);
  994. // Must terminate - likely has not started yet
  995. if LYarn <> nil then begin
  996. Server.Scheduler.TerminateYarn(LYarn);
  997. end;
  998. // EAbort is used to kick out above and destroy yarns and other, but
  999. // we dont want to show the user
  1000. // TODO: should we include EIdSilentException here, too?
  1001. // To ignore EIdConnClosedGracefully, for instance...
  1002. if not (E is EAbort) then begin
  1003. Server.DoListenException(Self, E);
  1004. end;
  1005. end;
  1006. end;
  1007. end;
  1008. end.