Queue.cpp 92 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237
  1. //---------------------------------------------------------------------------
  2. #include <CorePCH.h>
  3. #pragma hdrstop
  4. #include "Terminal.h"
  5. #include "Queue.h"
  6. //---------------------------------------------------------------------------
  7. #pragma package(smart_init)
  8. //---------------------------------------------------------------------------
  9. class TBackgroundTerminal;
  10. //---------------------------------------------------------------------------
  11. class TParallelTransferQueueItem : public TLocatedQueueItem
  12. {
  13. public:
  14. __fastcall TParallelTransferQueueItem(const TLocatedQueueItem * ParentItem, TParallelOperation * ParallelOperation);
  15. protected:
  16. virtual void __fastcall DoExecute(TTerminal * Terminal);
  17. private:
  18. TParallelOperation * FParallelOperation;
  19. };
  20. //---------------------------------------------------------------------------
  21. class TUserAction
  22. {
  23. public:
  24. virtual __fastcall ~TUserAction() {}
  25. virtual void __fastcall Execute(void * Arg) = 0;
  26. virtual bool __fastcall Force() { return false; }
  27. };
  28. //---------------------------------------------------------------------------
  29. class TNotifyAction : public TUserAction
  30. {
  31. public:
  32. TNotifyAction(TNotifyEvent AOnNotify) :
  33. OnNotify(AOnNotify)
  34. {
  35. }
  36. virtual void __fastcall Execute(void *)
  37. {
  38. if (OnNotify != NULL)
  39. {
  40. OnNotify(Sender);
  41. }
  42. }
  43. TNotifyEvent OnNotify;
  44. TObject * Sender;
  45. };
  46. //---------------------------------------------------------------------------
  47. class TInformationUserAction : public TUserAction
  48. {
  49. public:
  50. TInformationUserAction(TInformationEvent AOnInformation) :
  51. OnInformation(AOnInformation)
  52. {
  53. }
  54. virtual void __fastcall Execute(void *)
  55. {
  56. if (OnInformation != NULL)
  57. {
  58. OnInformation(Terminal, Str, Phase, Additional);
  59. }
  60. }
  61. virtual bool __fastcall Force()
  62. {
  63. // we need to propagate mainly the end-phase event even, when user cancels
  64. // the connection, so that authentication window is closed
  65. return TUserAction::Force() || (Phase >= 0);
  66. }
  67. TInformationEvent OnInformation;
  68. TTerminal * Terminal;
  69. UnicodeString Str;
  70. int Phase;
  71. UnicodeString Additional;
  72. };
  73. //---------------------------------------------------------------------------
  74. class TQueryUserAction : public TUserAction
  75. {
  76. public:
  77. TQueryUserAction(TQueryUserEvent AOnQueryUser) :
  78. OnQueryUser(AOnQueryUser)
  79. {
  80. }
  81. virtual void __fastcall Execute(void * Arg)
  82. {
  83. if (OnQueryUser != NULL)
  84. {
  85. OnQueryUser(Sender, Query, MoreMessages, Answers, Params, Answer, Type, Arg);
  86. }
  87. }
  88. TQueryUserEvent OnQueryUser;
  89. TObject * Sender;
  90. UnicodeString Query;
  91. TStrings * MoreMessages;
  92. unsigned int Answers;
  93. const TQueryParams * Params;
  94. unsigned int Answer;
  95. TQueryType Type;
  96. };
  97. //---------------------------------------------------------------------------
  98. class TPromptUserAction : public TUserAction
  99. {
  100. public:
  101. __fastcall TPromptUserAction(TPromptUserEvent AOnPromptUser) :
  102. OnPromptUser(AOnPromptUser), Results(new TStringList())
  103. {
  104. }
  105. virtual __fastcall ~TPromptUserAction()
  106. {
  107. delete Results;
  108. }
  109. virtual void __fastcall Execute(void * Arg)
  110. {
  111. if (OnPromptUser != NULL)
  112. {
  113. OnPromptUser(Terminal, Kind, Name, Instructions, Prompts, Results, Result, Arg);
  114. }
  115. }
  116. TPromptUserEvent OnPromptUser;
  117. TTerminal * Terminal;
  118. TPromptKind Kind;
  119. UnicodeString Name;
  120. UnicodeString Instructions;
  121. TStrings * Prompts;
  122. TStrings * Results;
  123. bool Result;
  124. };
  125. //---------------------------------------------------------------------------
  126. class TShowExtendedExceptionAction : public TUserAction
  127. {
  128. public:
  129. __fastcall TShowExtendedExceptionAction(TExtendedExceptionEvent AOnShowExtendedException) :
  130. OnShowExtendedException(AOnShowExtendedException)
  131. {
  132. }
  133. virtual void __fastcall Execute(void * Arg)
  134. {
  135. if (OnShowExtendedException != NULL)
  136. {
  137. OnShowExtendedException(Terminal, E, Arg);
  138. }
  139. }
  140. TExtendedExceptionEvent OnShowExtendedException;
  141. TTerminal * Terminal;
  142. Exception * E;
  143. };
  144. //---------------------------------------------------------------------------
  145. class TDisplayBannerAction : public TUserAction
  146. {
  147. public:
  148. __fastcall TDisplayBannerAction (TDisplayBannerEvent AOnDisplayBanner) :
  149. OnDisplayBanner(AOnDisplayBanner)
  150. {
  151. }
  152. virtual void __fastcall Execute(void *)
  153. {
  154. if (OnDisplayBanner != NULL)
  155. {
  156. OnDisplayBanner(Terminal, SessionName, Banner, NeverShowAgain, Options, Params);
  157. }
  158. }
  159. TDisplayBannerEvent OnDisplayBanner;
  160. TTerminal * Terminal;
  161. UnicodeString SessionName;
  162. UnicodeString Banner;
  163. bool NeverShowAgain;
  164. int Options;
  165. unsigned int Params;
  166. };
  167. //---------------------------------------------------------------------------
  168. class TReadDirectoryAction : public TUserAction
  169. {
  170. public:
  171. TReadDirectoryAction(TReadDirectoryEvent AOnReadDirectory) :
  172. OnReadDirectory(AOnReadDirectory)
  173. {
  174. }
  175. virtual void __fastcall Execute(void *)
  176. {
  177. if (OnReadDirectory != NULL)
  178. {
  179. OnReadDirectory(Sender, ReloadOnly);
  180. }
  181. }
  182. TReadDirectoryEvent OnReadDirectory;
  183. TObject * Sender;
  184. bool ReloadOnly;
  185. };
  186. //---------------------------------------------------------------------------
  187. class TReadDirectoryProgressAction : public TUserAction
  188. {
  189. public:
  190. TReadDirectoryProgressAction(TReadDirectoryProgressEvent AOnReadDirectoryProgress) :
  191. OnReadDirectoryProgress(AOnReadDirectoryProgress)
  192. {
  193. }
  194. virtual void __fastcall Execute(void *)
  195. {
  196. if (OnReadDirectoryProgress != NULL)
  197. {
  198. OnReadDirectoryProgress(Sender, Progress, ResolvedLinks, Cancel);
  199. }
  200. }
  201. TReadDirectoryProgressEvent OnReadDirectoryProgress;
  202. TObject * Sender;
  203. int Progress;
  204. int ResolvedLinks;
  205. bool Cancel;
  206. };
  207. //---------------------------------------------------------------------------
  208. class TTerminalItem : public TSignalThread
  209. {
  210. friend class TQueueItem;
  211. friend class TBackgroundTerminal;
  212. public:
  213. __fastcall TTerminalItem(TTerminalQueue * Queue, int Index);
  214. virtual __fastcall ~TTerminalItem();
  215. void __fastcall Process(TQueueItem * Item);
  216. bool __fastcall ProcessUserAction(void * Arg);
  217. void __fastcall Cancel();
  218. void __fastcall Idle();
  219. bool __fastcall Pause();
  220. bool __fastcall Resume();
  221. bool IsCancelled();
  222. protected:
  223. TTerminalQueue * FQueue;
  224. TBackgroundTerminal * FTerminal;
  225. TQueueItem * FItem;
  226. TCriticalSection * FCriticalSection;
  227. TUserAction * FUserAction;
  228. bool FCancel;
  229. bool FPause;
  230. virtual void __fastcall ProcessEvent();
  231. virtual bool __fastcall Finished();
  232. bool __fastcall WaitForUserAction(TQueueItem::TStatus ItemStatus, TUserAction * UserAction);
  233. bool __fastcall OverrideItemStatus(TQueueItem::TStatus & ItemStatus);
  234. void __fastcall TerminalQueryUser(TObject * Sender,
  235. const UnicodeString Query, TStrings * MoreMessages, unsigned int Answers,
  236. const TQueryParams * Params, unsigned int & Answer, TQueryType Type, void * Arg);
  237. void __fastcall TerminalPromptUser(TTerminal * Terminal, TPromptKind Kind,
  238. UnicodeString Name, UnicodeString Instructions,
  239. TStrings * Prompts, TStrings * Results, bool & Result, void * Arg);
  240. void __fastcall TerminalShowExtendedException(TTerminal * Terminal,
  241. Exception * E, void * Arg);
  242. void __fastcall OperationFinished(TFileOperation Operation, TOperationSide Side,
  243. bool Temp, const UnicodeString & FileName, bool Success, bool NotCancelled,
  244. TOnceDoneOperation & OnceDoneOperation);
  245. void __fastcall OperationProgress(TFileOperationProgressType & ProgressData);
  246. };
  247. //---------------------------------------------------------------------------
  248. // TSignalThread
  249. //---------------------------------------------------------------------------
  250. int __fastcall TSimpleThread::ThreadProc(void * Thread)
  251. {
  252. TSimpleThread * SimpleThread = reinterpret_cast<TSimpleThread*>(Thread);
  253. DebugAssert(SimpleThread != NULL);
  254. try
  255. {
  256. SimpleThread->Execute();
  257. }
  258. catch(...)
  259. {
  260. // we do not expect thread to be terminated with exception
  261. DebugFail();
  262. }
  263. SimpleThread->FFinished = true;
  264. if (SimpleThread->Finished())
  265. {
  266. delete SimpleThread;
  267. }
  268. return 0;
  269. }
  270. //---------------------------------------------------------------------------
  271. __fastcall TSimpleThread::TSimpleThread() :
  272. FThread(NULL), FFinished(true)
  273. {
  274. FThread = reinterpret_cast<HANDLE>(
  275. StartThread(NULL, 0, ThreadProc, this, CREATE_SUSPENDED, FThreadId));
  276. }
  277. //---------------------------------------------------------------------------
  278. __fastcall TSimpleThread::~TSimpleThread()
  279. {
  280. // This is turn calls pure virtual Terminate, what does not work as intended, do not rely on it and remove the call eventually
  281. Close();
  282. if (FThread != NULL)
  283. {
  284. CloseHandle(FThread);
  285. }
  286. }
  287. //---------------------------------------------------------------------------
  288. bool __fastcall TSimpleThread::IsFinished()
  289. {
  290. return FFinished;
  291. }
  292. //---------------------------------------------------------------------------
  293. void __fastcall TSimpleThread::Start()
  294. {
  295. if (ResumeThread(FThread) == 1)
  296. {
  297. FFinished = false;
  298. }
  299. }
  300. //---------------------------------------------------------------------------
  301. bool __fastcall TSimpleThread::Finished()
  302. {
  303. return false;
  304. }
  305. //---------------------------------------------------------------------------
  306. void __fastcall TSimpleThread::Close()
  307. {
  308. if (!FFinished)
  309. {
  310. Terminate();
  311. WaitFor();
  312. }
  313. }
  314. //---------------------------------------------------------------------------
  315. void __fastcall TSimpleThread::WaitFor(unsigned int Milliseconds)
  316. {
  317. WaitForSingleObject(FThread, Milliseconds);
  318. }
  319. //---------------------------------------------------------------------------
  320. // TSignalThread
  321. //---------------------------------------------------------------------------
  322. __fastcall TSignalThread::TSignalThread(bool LowPriority, HANDLE Event) :
  323. TSimpleThread(),
  324. FEvent(NULL), FTerminated(true)
  325. {
  326. if (Event == NULL)
  327. {
  328. FEvent = CreateEvent(NULL, false, false, NULL);
  329. }
  330. else
  331. {
  332. FEvent = Event;
  333. }
  334. DebugAssert(FEvent != NULL);
  335. if (LowPriority)
  336. {
  337. ::SetThreadPriority(FThread, THREAD_PRIORITY_BELOW_NORMAL);
  338. }
  339. }
  340. //---------------------------------------------------------------------------
  341. __fastcall TSignalThread::~TSignalThread()
  342. {
  343. // cannot leave closing to TSimpleThread as we need to close it before
  344. // destroying the event
  345. Close();
  346. if (FEvent)
  347. {
  348. CloseHandle(FEvent);
  349. }
  350. }
  351. //---------------------------------------------------------------------------
  352. void __fastcall TSignalThread::Start()
  353. {
  354. FTerminated = false;
  355. TSimpleThread::Start();
  356. }
  357. //---------------------------------------------------------------------------
  358. void __fastcall TSignalThread::TriggerEvent()
  359. {
  360. SetEvent(FEvent);
  361. }
  362. //---------------------------------------------------------------------------
  363. bool __fastcall TSignalThread::WaitForEvent()
  364. {
  365. // should never return -1, so it is only about 0 or 1
  366. return (WaitForEvent(INFINITE) > 0);
  367. }
  368. //---------------------------------------------------------------------------
  369. int __fastcall TSignalThread::WaitForEvent(unsigned int Timeout)
  370. {
  371. unsigned int Result = WaitForSingleObject(FEvent, Timeout);
  372. int Return;
  373. if ((Result == WAIT_TIMEOUT) && !FTerminated)
  374. {
  375. Return = -1;
  376. }
  377. else
  378. {
  379. Return = ((Result == WAIT_OBJECT_0) && !FTerminated) ? 1 : 0;
  380. }
  381. return Return;
  382. }
  383. //---------------------------------------------------------------------------
  384. void __fastcall TSignalThread::Execute()
  385. {
  386. while (!FTerminated)
  387. {
  388. if (WaitForEvent())
  389. {
  390. ProcessEvent();
  391. }
  392. }
  393. }
  394. //---------------------------------------------------------------------------
  395. void __fastcall TSignalThread::Terminate()
  396. {
  397. FTerminated = true;
  398. TriggerEvent();
  399. }
  400. //---------------------------------------------------------------------------
  401. // TTerminalQueue
  402. //---------------------------------------------------------------------------
  403. __fastcall TTerminalQueue::TTerminalQueue(TTerminal * Terminal,
  404. TConfiguration * Configuration) :
  405. TSignalThread(true), FTerminal(Terminal), FConfiguration(Configuration), FSessionData(NULL), FItems(NULL), FDoneItems(NULL),
  406. FItemsInProcess(0), FItemsSection(NULL), FFreeTerminals(0), FTerminals(NULL), FTemporaryTerminals(0),
  407. FOverallTerminals(0), FTransfersLimit(2), FKeepDoneItemsFor(0), FEnabled(true)
  408. {
  409. FOnQueryUser = NULL;
  410. FOnPromptUser = NULL;
  411. FOnShowExtendedException = NULL;
  412. FOnQueueItemUpdate = NULL;
  413. FOnListUpdate = NULL;
  414. FOnEvent = NULL;
  415. FLastIdle = Now();
  416. FIdleInterval = EncodeTimeVerbose(0, 0, 2, 0);
  417. DebugAssert(Terminal != NULL);
  418. FSessionData = new TSessionData(L"");
  419. FSessionData->Assign(Terminal->SessionData);
  420. FItems = new TList();
  421. FDoneItems = new TList();
  422. FTerminals = new TList();
  423. FForcedItems = new TList();
  424. FItemsSection = new TCriticalSection();
  425. Start();
  426. }
  427. //---------------------------------------------------------------------------
  428. __fastcall TTerminalQueue::~TTerminalQueue()
  429. {
  430. Close();
  431. {
  432. TGuard Guard(FItemsSection);
  433. TTerminalItem * TerminalItem;
  434. while (FTerminals->Count > 0)
  435. {
  436. TerminalItem = reinterpret_cast<TTerminalItem*>(FTerminals->Items[0]);
  437. FTerminals->Delete(0);
  438. TerminalItem->Terminate();
  439. TerminalItem->WaitFor();
  440. delete TerminalItem;
  441. }
  442. delete FTerminals;
  443. delete FForcedItems;
  444. FreeItemsList(FItems);
  445. FreeItemsList(FDoneItems);
  446. }
  447. delete FItemsSection;
  448. delete FSessionData;
  449. }
  450. //---------------------------------------------------------------------------
  451. void __fastcall TTerminalQueue::FreeItemsList(TList * List)
  452. {
  453. for (int Index = 0; Index < List->Count; Index++)
  454. {
  455. delete GetItem(List, Index);
  456. }
  457. delete List;
  458. }
  459. //---------------------------------------------------------------------------
  460. void __fastcall TTerminalQueue::TerminalFinished(TTerminalItem * TerminalItem)
  461. {
  462. if (!FTerminated)
  463. {
  464. {
  465. TGuard Guard(FItemsSection);
  466. int Index = FTerminals->IndexOf(TerminalItem);
  467. DebugAssert(Index >= 0);
  468. if (Index < FFreeTerminals)
  469. {
  470. FFreeTerminals--;
  471. }
  472. // Index may be >= FTransfersLimit also when the transfer limit was
  473. // recently decreased, then
  474. // FTemporaryTerminals < FTerminals->Count - FTransfersLimit
  475. if ((FTransfersLimit >= 0) && (Index >= FTransfersLimit) && (FTemporaryTerminals > 0))
  476. {
  477. FTemporaryTerminals--;
  478. }
  479. FTerminals->Extract(TerminalItem);
  480. delete TerminalItem;
  481. }
  482. TriggerEvent();
  483. }
  484. }
  485. //---------------------------------------------------------------------------
  486. bool __fastcall TTerminalQueue::TerminalFree(TTerminalItem * TerminalItem)
  487. {
  488. bool Result = true;
  489. if (!FTerminated)
  490. {
  491. {
  492. TGuard Guard(FItemsSection);
  493. int Index = FTerminals->IndexOf(TerminalItem);
  494. DebugAssert(Index >= 0);
  495. DebugAssert(Index >= FFreeTerminals);
  496. Result = (FTransfersLimit < 0) || (Index < FTransfersLimit);
  497. if (Result)
  498. {
  499. FTerminals->Move(Index, 0);
  500. FFreeTerminals++;
  501. }
  502. }
  503. TriggerEvent();
  504. }
  505. return Result;
  506. }
  507. //---------------------------------------------------------------------------
  508. int __fastcall TTerminalQueue::GetParallelDurationThreshold()
  509. {
  510. return FConfiguration->ParallelDurationThreshold;
  511. }
  512. //---------------------------------------------------------------------------
  513. void __fastcall TTerminalQueue::AddItem(TQueueItem * Item)
  514. {
  515. DebugAssert(!FTerminated);
  516. Item->SetStatus(TQueueItem::qsPending);
  517. {
  518. TGuard Guard(FItemsSection);
  519. FItems->Add(Item);
  520. Item->FQueue = this;
  521. }
  522. DoListUpdate();
  523. TriggerEvent();
  524. }
  525. //---------------------------------------------------------------------------
  526. void __fastcall TTerminalQueue::RetryItem(TQueueItem * Item)
  527. {
  528. if (!FTerminated)
  529. {
  530. {
  531. TGuard Guard(FItemsSection);
  532. int Index = FItems->Remove(Item);
  533. DebugAssert(Index < FItemsInProcess);
  534. DebugUsedParam(Index);
  535. FItemsInProcess--;
  536. FItems->Add(Item);
  537. }
  538. DoListUpdate();
  539. TriggerEvent();
  540. }
  541. }
  542. //---------------------------------------------------------------------------
  543. void __fastcall TTerminalQueue::DeleteItem(TQueueItem * Item, bool CanKeep)
  544. {
  545. if (!FTerminated)
  546. {
  547. bool Empty;
  548. bool EmptyButMonitored;
  549. bool Monitored;
  550. {
  551. TGuard Guard(FItemsSection);
  552. // does this need to be within guard?
  553. Monitored = (Item->CompleteEvent != INVALID_HANDLE_VALUE);
  554. int Index = FItems->Remove(Item);
  555. DebugAssert(Index < FItemsInProcess);
  556. DebugUsedParam(Index);
  557. FItemsInProcess--;
  558. FForcedItems->Remove(Item);
  559. // =0 do not keep
  560. // <0 infinity
  561. if ((FKeepDoneItemsFor != 0) && CanKeep && Item->Complete())
  562. {
  563. DebugAssert(Item->Status == TQueueItem::qsDone);
  564. FDoneItems->Add(Item);
  565. }
  566. else
  567. {
  568. delete Item;
  569. }
  570. EmptyButMonitored = true;
  571. Index = 0;
  572. while (EmptyButMonitored && (Index < FItems->Count))
  573. {
  574. EmptyButMonitored = (GetItem(FItems, Index)->CompleteEvent != INVALID_HANDLE_VALUE);
  575. Index++;
  576. }
  577. Empty = (FItems->Count == 0);
  578. }
  579. DoListUpdate();
  580. // report empty but/except for monitored, if queue is empty or only monitored items are pending.
  581. // do not report if current item was the last, but was monitored.
  582. if (!Monitored && EmptyButMonitored)
  583. {
  584. DoEvent(qeEmptyButMonitored);
  585. }
  586. if (Empty)
  587. {
  588. DoEvent(qeEmpty);
  589. }
  590. }
  591. }
  592. //---------------------------------------------------------------------------
  593. TQueueItem * __fastcall TTerminalQueue::GetItem(TList * List, int Index)
  594. {
  595. return reinterpret_cast<TQueueItem*>(List->Items[Index]);
  596. }
  597. //---------------------------------------------------------------------------
  598. TQueueItem * __fastcall TTerminalQueue::GetItem(int Index)
  599. {
  600. return GetItem(FItems, Index);
  601. }
  602. //---------------------------------------------------------------------------
  603. void __fastcall TTerminalQueue::UpdateStatusForList(
  604. TTerminalQueueStatus * Status, TList * List, TTerminalQueueStatus * Current)
  605. {
  606. TQueueItem * Item;
  607. TQueueItemProxy * ItemProxy;
  608. for (int Index = 0; Index < List->Count; Index++)
  609. {
  610. Item = GetItem(List, Index);
  611. if (Current != NULL)
  612. {
  613. ItemProxy = Current->FindByQueueItem(Item);
  614. }
  615. else
  616. {
  617. ItemProxy = NULL;
  618. }
  619. if (ItemProxy != NULL)
  620. {
  621. Current->Delete(ItemProxy);
  622. Status->Add(ItemProxy);
  623. ItemProxy->Update();
  624. }
  625. else
  626. {
  627. Status->Add(new TQueueItemProxy(this, Item));
  628. }
  629. }
  630. }
  631. //---------------------------------------------------------------------------
  632. TTerminalQueueStatus * __fastcall TTerminalQueue::CreateStatus(TTerminalQueueStatus * Current)
  633. {
  634. TTerminalQueueStatus * Status = new TTerminalQueueStatus();
  635. try
  636. {
  637. try
  638. {
  639. TGuard Guard(FItemsSection);
  640. UpdateStatusForList(Status, FDoneItems, Current);
  641. Status->SetDoneCount(Status->Count);
  642. UpdateStatusForList(Status, FItems, Current);
  643. }
  644. __finally
  645. {
  646. if (Current != NULL)
  647. {
  648. delete Current;
  649. }
  650. }
  651. }
  652. catch(...)
  653. {
  654. delete Status;
  655. throw;
  656. }
  657. return Status;
  658. }
  659. //---------------------------------------------------------------------------
  660. bool __fastcall TTerminalQueue::ItemGetData(TQueueItem * Item, TQueueItemProxy * Proxy, TQueueFileList * FileList)
  661. {
  662. // to prevent deadlocks when closing queue from other thread
  663. bool Result = !FFinished;
  664. if (Result)
  665. {
  666. TGuard Guard(FItemsSection);
  667. Result = (FDoneItems->IndexOf(Item) >= 0) || (FItems->IndexOf(Item) >= 0);
  668. if (Result)
  669. {
  670. if (FileList != NULL)
  671. {
  672. Result = Item->UpdateFileList(FileList);
  673. }
  674. else
  675. {
  676. Item->GetData(Proxy);
  677. }
  678. }
  679. }
  680. return Result;
  681. }
  682. //---------------------------------------------------------------------------
  683. bool __fastcall TTerminalQueue::ItemProcessUserAction(TQueueItem * Item, void * Arg)
  684. {
  685. // to prevent deadlocks when closing queue from other thread
  686. bool Result = !FFinished;
  687. if (Result)
  688. {
  689. TTerminalItem * TerminalItem = NULL; // shut up
  690. {
  691. TGuard Guard(FItemsSection);
  692. Result = (FItems->IndexOf(Item) >= 0) &&
  693. TQueueItem::IsUserActionStatus(Item->Status);
  694. if (Result)
  695. {
  696. TerminalItem = Item->FTerminalItem;
  697. }
  698. }
  699. if (Result)
  700. {
  701. Result = TerminalItem->ProcessUserAction(Arg);
  702. }
  703. }
  704. return Result;
  705. }
  706. //---------------------------------------------------------------------------
  707. bool __fastcall TTerminalQueue::ItemMove(TQueueItem * Item, TQueueItem * BeforeItem)
  708. {
  709. // to prevent deadlocks when closing queue from other thread
  710. bool Result = !FFinished;
  711. if (Result)
  712. {
  713. {
  714. TGuard Guard(FItemsSection);
  715. int Index = FItems->IndexOf(Item);
  716. int IndexDest = FItems->IndexOf(BeforeItem);
  717. Result = (Index >= 0) && (IndexDest >= 0) &&
  718. (Item->GetStatus() == TQueueItem::qsPending) &&
  719. (BeforeItem->GetStatus() == TQueueItem::qsPending);
  720. if (Result)
  721. {
  722. FItems->Move(Index, IndexDest);
  723. }
  724. }
  725. if (Result)
  726. {
  727. DoListUpdate();
  728. TriggerEvent();
  729. }
  730. }
  731. return Result;
  732. }
  733. //---------------------------------------------------------------------------
  734. bool __fastcall TTerminalQueue::ItemExecuteNow(TQueueItem * Item)
  735. {
  736. // to prevent deadlocks when closing queue from other thread
  737. bool Result = !FFinished;
  738. if (Result)
  739. {
  740. {
  741. TGuard Guard(FItemsSection);
  742. int Index = FItems->IndexOf(Item);
  743. Result = (Index >= 0) && (Item->GetStatus() == TQueueItem::qsPending) &&
  744. // prevent double-initiation when "execute" is clicked twice too fast
  745. (Index >= FItemsInProcess);
  746. if (Result)
  747. {
  748. if (Index > FItemsInProcess)
  749. {
  750. FItems->Move(Index, FItemsInProcess);
  751. }
  752. if ((FTransfersLimit >= 0) && (FTerminals->Count >= FTransfersLimit) &&
  753. // when queue is disabled, we may have idle terminals,
  754. // even when there are pending queue items
  755. (FFreeTerminals == 0))
  756. {
  757. FTemporaryTerminals++;
  758. }
  759. FForcedItems->Add(Item);
  760. }
  761. }
  762. if (Result)
  763. {
  764. DoListUpdate();
  765. TriggerEvent();
  766. }
  767. }
  768. return Result;
  769. }
  770. //---------------------------------------------------------------------------
  771. bool __fastcall TTerminalQueue::ItemDelete(TQueueItem * Item)
  772. {
  773. // to prevent deadlocks when closing queue from other thread
  774. bool Result = !FFinished;
  775. if (Result)
  776. {
  777. bool UpdateList = false;
  778. {
  779. TGuard Guard(FItemsSection);
  780. int Index = FItems->IndexOf(Item);
  781. Result = (Index >= 0);
  782. if (Result)
  783. {
  784. if (Item->Status == TQueueItem::qsPending)
  785. {
  786. FItems->Delete(Index);
  787. FForcedItems->Remove(Item);
  788. delete Item;
  789. UpdateList = true;
  790. }
  791. else
  792. {
  793. Item->FTerminalItem->Cancel();
  794. }
  795. }
  796. else
  797. {
  798. Index = FDoneItems->IndexOf(Item);
  799. Result = (Index >= 0);
  800. if (Result)
  801. {
  802. FDoneItems->Delete(Index);
  803. delete Item;
  804. UpdateList = true;
  805. }
  806. }
  807. }
  808. if (UpdateList)
  809. {
  810. DoListUpdate();
  811. TriggerEvent();
  812. }
  813. }
  814. return Result;
  815. }
  816. //---------------------------------------------------------------------------
  817. bool __fastcall TTerminalQueue::ItemPause(TQueueItem * Item, bool Pause)
  818. {
  819. // to prevent deadlocks when closing queue from other thread
  820. bool Result = !FFinished;
  821. if (Result)
  822. {
  823. TTerminalItem * TerminalItem = NULL; // shut up
  824. {
  825. TGuard Guard(FItemsSection);
  826. Result = (FItems->IndexOf(Item) >= 0) &&
  827. ((Pause && (Item->Status == TQueueItem::qsProcessing)) ||
  828. (!Pause && (Item->Status == TQueueItem::qsPaused)));
  829. if (Result)
  830. {
  831. TerminalItem = Item->FTerminalItem;
  832. }
  833. }
  834. if (Result)
  835. {
  836. if (Pause)
  837. {
  838. Result = TerminalItem->Pause();
  839. }
  840. else
  841. {
  842. Result = TerminalItem->Resume();
  843. }
  844. }
  845. }
  846. return Result;
  847. }
  848. //---------------------------------------------------------------------------
  849. bool __fastcall TTerminalQueue::ItemSetCPSLimit(TQueueItem * Item, unsigned long CPSLimit)
  850. {
  851. // to prevent deadlocks when closing queue from other thread
  852. bool Result = !FFinished;
  853. if (Result)
  854. {
  855. TGuard Guard(FItemsSection);
  856. Result = (FItems->IndexOf(Item) >= 0);
  857. if (Result)
  858. {
  859. Item->SetCPSLimit(CPSLimit);
  860. }
  861. }
  862. return Result;
  863. }
  864. //---------------------------------------------------------------------------
  865. bool __fastcall TTerminalQueue::ItemGetCPSLimit(TQueueItem * Item, unsigned long & CPSLimit)
  866. {
  867. CPSLimit = 0;
  868. // to prevent deadlocks when closing queue from other thread
  869. bool Result = !FFinished;
  870. if (Result)
  871. {
  872. TGuard Guard(FItemsSection);
  873. Result = (FItems->IndexOf(Item) >= 0);
  874. if (Result)
  875. {
  876. CPSLimit = Item->GetCPSLimit();
  877. }
  878. }
  879. return Result;
  880. }
  881. //---------------------------------------------------------------------------
  882. void __fastcall TTerminalQueue::Idle()
  883. {
  884. TDateTime N = Now();
  885. if (N - FLastIdle > FIdleInterval)
  886. {
  887. FLastIdle = N;
  888. TTerminalItem * TerminalItem = NULL;
  889. if (FFreeTerminals > 0)
  890. {
  891. TGuard Guard(FItemsSection);
  892. if (FFreeTerminals > 0)
  893. {
  894. // take the last free terminal, because TerminalFree() puts it to the
  895. // front, this ensures we cycle thru all free terminals
  896. TerminalItem = reinterpret_cast<TTerminalItem*>(FTerminals->Items[FFreeTerminals - 1]);
  897. FTerminals->Move(FFreeTerminals - 1, FTerminals->Count - 1);
  898. FFreeTerminals--;
  899. }
  900. }
  901. if (TerminalItem != NULL)
  902. {
  903. TerminalItem->Idle();
  904. }
  905. }
  906. }
  907. //---------------------------------------------------------------------------
  908. bool __fastcall TTerminalQueue::WaitForEvent()
  909. {
  910. // terminate loop regularly, so that we can check for expired done items
  911. bool Result = (TSignalThread::WaitForEvent(1000) != 0);
  912. return Result;
  913. }
  914. //---------------------------------------------------------------------------
  915. void __fastcall TTerminalQueue::ProcessEvent()
  916. {
  917. TTerminalItem * TerminalItem;
  918. TQueueItem * Item;
  919. do
  920. {
  921. TerminalItem = NULL;
  922. Item = NULL;
  923. {
  924. TGuard Guard(FItemsSection);
  925. // =0 do not keep
  926. // <0 infinity
  927. if (FKeepDoneItemsFor >= 0)
  928. {
  929. TDateTime RemoveDoneItemsBefore = Now();
  930. if (FKeepDoneItemsFor > 0)
  931. {
  932. RemoveDoneItemsBefore = IncSecond(RemoveDoneItemsBefore, -FKeepDoneItemsFor);
  933. }
  934. for (int Index = 0; Index < FDoneItems->Count; Index++)
  935. {
  936. TQueueItem * Item = GetItem(FDoneItems, Index);
  937. if (Item->FDoneAt <= RemoveDoneItemsBefore)
  938. {
  939. FDoneItems->Delete(Index);
  940. delete Item;
  941. Index--;
  942. DoListUpdate();
  943. }
  944. }
  945. }
  946. if (FItems->Count > FItemsInProcess)
  947. {
  948. Item = GetItem(FItemsInProcess);
  949. int ForcedIndex = FForcedItems->IndexOf(Item);
  950. if (FEnabled || (ForcedIndex >= 0))
  951. {
  952. if ((FFreeTerminals == 0) &&
  953. ((FTransfersLimit < 0) ||
  954. (FTerminals->Count < FTransfersLimit + FTemporaryTerminals)))
  955. {
  956. FOverallTerminals++;
  957. TerminalItem = new TTerminalItem(this, FOverallTerminals);
  958. FTerminals->Add(TerminalItem);
  959. }
  960. else if (FFreeTerminals > 0)
  961. {
  962. TerminalItem = reinterpret_cast<TTerminalItem*>(FTerminals->Items[0]);
  963. FTerminals->Move(0, FTerminals->Count - 1);
  964. FFreeTerminals--;
  965. }
  966. if (TerminalItem != NULL)
  967. {
  968. if (ForcedIndex >= 0)
  969. {
  970. FForcedItems->Delete(ForcedIndex);
  971. }
  972. FItemsInProcess++;
  973. }
  974. }
  975. }
  976. }
  977. if (TerminalItem != NULL)
  978. {
  979. TerminalItem->Process(Item);
  980. }
  981. }
  982. while (!FTerminated && (TerminalItem != NULL));
  983. }
  984. //---------------------------------------------------------------------------
  985. void __fastcall TTerminalQueue::DoQueueItemUpdate(TQueueItem * Item)
  986. {
  987. if (OnQueueItemUpdate != NULL)
  988. {
  989. OnQueueItemUpdate(this, Item);
  990. }
  991. }
  992. //---------------------------------------------------------------------------
  993. void __fastcall TTerminalQueue::DoListUpdate()
  994. {
  995. if (OnListUpdate != NULL)
  996. {
  997. OnListUpdate(this);
  998. }
  999. }
  1000. //---------------------------------------------------------------------------
  1001. void __fastcall TTerminalQueue::DoEvent(TQueueEvent Event)
  1002. {
  1003. if (OnEvent != NULL)
  1004. {
  1005. OnEvent(this, Event);
  1006. }
  1007. }
  1008. //---------------------------------------------------------------------------
  1009. void __fastcall TTerminalQueue::SetTransfersLimit(int value)
  1010. {
  1011. if (FTransfersLimit != value)
  1012. {
  1013. {
  1014. TGuard Guard(FItemsSection);
  1015. if ((value >= 0) && (value < FItemsInProcess))
  1016. {
  1017. FTemporaryTerminals = (FItemsInProcess - value);
  1018. }
  1019. else
  1020. {
  1021. FTemporaryTerminals = 0;
  1022. }
  1023. FTransfersLimit = value;
  1024. }
  1025. TriggerEvent();
  1026. }
  1027. }
  1028. //---------------------------------------------------------------------------
  1029. void __fastcall TTerminalQueue::SetKeepDoneItemsFor(int value)
  1030. {
  1031. if (FKeepDoneItemsFor != value)
  1032. {
  1033. {
  1034. TGuard Guard(FItemsSection);
  1035. FKeepDoneItemsFor = value;
  1036. }
  1037. }
  1038. }
  1039. //---------------------------------------------------------------------------
  1040. void __fastcall TTerminalQueue::SetEnabled(bool value)
  1041. {
  1042. if (FEnabled != value)
  1043. {
  1044. {
  1045. TGuard Guard(FItemsSection);
  1046. FEnabled = value;
  1047. }
  1048. TriggerEvent();
  1049. }
  1050. }
  1051. //---------------------------------------------------------------------------
  1052. bool __fastcall TTerminalQueue::GetIsEmpty()
  1053. {
  1054. TGuard Guard(FItemsSection);
  1055. return (FItems->Count == 0);
  1056. }
  1057. //---------------------------------------------------------------------------
  1058. bool __fastcall TTerminalQueue::TryAddParallelOperation(TQueueItem * Item, bool Force)
  1059. {
  1060. TGuard Guard(FItemsSection);
  1061. bool Result =
  1062. (FFreeTerminals > 0) ||
  1063. (Force && (FItemsInProcess < FTransfersLimit));
  1064. if (Result)
  1065. {
  1066. TQueueItem * ParallelItem = DebugNotNull(Item->CreateParallelOperation());
  1067. if (!FEnabled)
  1068. {
  1069. FForcedItems->Add(ParallelItem);
  1070. }
  1071. AddItem(ParallelItem);
  1072. }
  1073. return Result;
  1074. }
  1075. //---------------------------------------------------------------------------
  1076. bool __fastcall TTerminalQueue::ContinueParallelOperation()
  1077. {
  1078. TGuard Guard(FItemsSection);
  1079. return
  1080. (FItems->Count <= FItemsInProcess) ||
  1081. // When queue auto processing is not enabled, keep using all connections for the transfers that were manually started
  1082. !FEnabled;
  1083. }
  1084. //---------------------------------------------------------------------------
  1085. // TBackgroundItem
  1086. //---------------------------------------------------------------------------
  1087. class TBackgroundTerminal : public TSecondaryTerminal
  1088. {
  1089. friend class TTerminalItem;
  1090. public:
  1091. __fastcall TBackgroundTerminal(TTerminal * MainTerminal,
  1092. TSessionData * SessionData, TConfiguration * Configuration,
  1093. TTerminalItem * Item, const UnicodeString & Name);
  1094. protected:
  1095. virtual bool __fastcall DoQueryReopen(Exception * E);
  1096. private:
  1097. TTerminalItem * FItem;
  1098. };
  1099. //---------------------------------------------------------------------------
  1100. __fastcall TBackgroundTerminal::TBackgroundTerminal(TTerminal * MainTerminal,
  1101. TSessionData * SessionData, TConfiguration * Configuration, TTerminalItem * Item,
  1102. const UnicodeString & Name) :
  1103. TSecondaryTerminal(MainTerminal, SessionData, Configuration, Name, NULL), FItem(Item)
  1104. {
  1105. ActionLog->Enabled = false;
  1106. }
  1107. //---------------------------------------------------------------------------
  1108. bool __fastcall TBackgroundTerminal::DoQueryReopen(Exception * /*E*/)
  1109. {
  1110. bool Result;
  1111. if (FItem->IsCancelled())
  1112. {
  1113. // avoid reconnection if we are closing
  1114. Result = false;
  1115. }
  1116. else
  1117. {
  1118. Sleep(Configuration->SessionReopenBackground);
  1119. Result = true;
  1120. }
  1121. return Result;
  1122. }
  1123. //---------------------------------------------------------------------------
  1124. // TTerminalItem
  1125. //---------------------------------------------------------------------------
  1126. __fastcall TTerminalItem::TTerminalItem(TTerminalQueue * Queue, int Index) :
  1127. TSignalThread(true), FQueue(Queue), FTerminal(NULL), FItem(NULL),
  1128. FCriticalSection(NULL), FUserAction(NULL)
  1129. {
  1130. FCriticalSection = new TCriticalSection();
  1131. FTerminal = new TBackgroundTerminal(FQueue->FTerminal, Queue->FSessionData,
  1132. FQueue->FConfiguration, this, FORMAT(L"Background %d", (Index)));
  1133. try
  1134. {
  1135. FTerminal->UseBusyCursor = false;
  1136. FTerminal->OnQueryUser = TerminalQueryUser;
  1137. FTerminal->OnPromptUser = TerminalPromptUser;
  1138. FTerminal->OnShowExtendedException = TerminalShowExtendedException;
  1139. FTerminal->OnProgress = OperationProgress;
  1140. FTerminal->OnFinished = OperationFinished;
  1141. }
  1142. catch(...)
  1143. {
  1144. delete FTerminal;
  1145. throw;
  1146. }
  1147. Start();
  1148. }
  1149. //---------------------------------------------------------------------------
  1150. __fastcall TTerminalItem::~TTerminalItem()
  1151. {
  1152. Close();
  1153. DebugAssert(FItem == NULL);
  1154. delete FTerminal;
  1155. delete FCriticalSection;
  1156. }
  1157. //---------------------------------------------------------------------------
  1158. void __fastcall TTerminalItem::Process(TQueueItem * Item)
  1159. {
  1160. {
  1161. TGuard Guard(FCriticalSection);
  1162. DebugAssert(FItem == NULL);
  1163. FItem = Item;
  1164. }
  1165. TriggerEvent();
  1166. }
  1167. //---------------------------------------------------------------------------
  1168. void __fastcall TTerminalItem::ProcessEvent()
  1169. {
  1170. TGuard Guard(FCriticalSection);
  1171. bool Retry = true;
  1172. FCancel = false;
  1173. FPause = false;
  1174. FItem->FTerminalItem = this;
  1175. try
  1176. {
  1177. DebugAssert(FItem != NULL);
  1178. if (!FTerminal->Active)
  1179. {
  1180. FItem->SetStatus(TQueueItem::qsConnecting);
  1181. FTerminal->SessionData->RemoteDirectory = FItem->StartupDirectory();
  1182. FTerminal->Open();
  1183. }
  1184. Retry = false;
  1185. if (!FCancel)
  1186. {
  1187. FTerminal->UpdateFromMain();
  1188. FItem->SetStatus(TQueueItem::qsProcessing);
  1189. FItem->Execute();
  1190. }
  1191. }
  1192. catch(Exception & E)
  1193. {
  1194. UnicodeString Message;
  1195. if (ExceptionMessageFormatted(&E, Message))
  1196. {
  1197. // do not show error messages, if task was canceled anyway
  1198. // (for example if transfer is canceled during reconnection attempts)
  1199. if (!FCancel &&
  1200. (FTerminal->QueryUserException(L"", &E, qaOK | qaCancel, NULL, qtError) == qaCancel))
  1201. {
  1202. FCancel = true;
  1203. }
  1204. }
  1205. }
  1206. FItem->SetStatus(TQueueItem::qsDone);
  1207. FItem->FTerminalItem = NULL;
  1208. TQueueItem * Item = FItem;
  1209. FItem = NULL;
  1210. if (Retry && !FCancel)
  1211. {
  1212. FQueue->RetryItem(Item);
  1213. }
  1214. else
  1215. {
  1216. FQueue->DeleteItem(Item, !FCancel);
  1217. }
  1218. if (!FTerminal->Active ||
  1219. !FQueue->TerminalFree(this))
  1220. {
  1221. Terminate();
  1222. }
  1223. }
  1224. //---------------------------------------------------------------------------
  1225. void __fastcall TTerminalItem::Idle()
  1226. {
  1227. TGuard Guard(FCriticalSection);
  1228. DebugAssert(FTerminal->Active);
  1229. try
  1230. {
  1231. FTerminal->Idle();
  1232. }
  1233. catch(...)
  1234. {
  1235. }
  1236. if (!FTerminal->Active ||
  1237. !FQueue->TerminalFree(this))
  1238. {
  1239. Terminate();
  1240. }
  1241. }
  1242. //---------------------------------------------------------------------------
  1243. void __fastcall TTerminalItem::Cancel()
  1244. {
  1245. FCancel = true;
  1246. if ((FItem->GetStatus() == TQueueItem::qsPaused) ||
  1247. TQueueItem::IsUserActionStatus(FItem->GetStatus()))
  1248. {
  1249. TriggerEvent();
  1250. }
  1251. }
  1252. //---------------------------------------------------------------------------
  1253. bool __fastcall TTerminalItem::Pause()
  1254. {
  1255. DebugAssert(FItem != NULL);
  1256. bool Result = (FItem->GetStatus() == TQueueItem::qsProcessing) && !FPause;
  1257. if (Result)
  1258. {
  1259. FPause = true;
  1260. }
  1261. return Result;
  1262. }
  1263. //---------------------------------------------------------------------------
  1264. bool __fastcall TTerminalItem::Resume()
  1265. {
  1266. DebugAssert(FItem != NULL);
  1267. bool Result = (FItem->GetStatus() == TQueueItem::qsPaused);
  1268. if (Result)
  1269. {
  1270. TriggerEvent();
  1271. }
  1272. return Result;
  1273. }
  1274. //---------------------------------------------------------------------------
  1275. bool __fastcall TTerminalItem::ProcessUserAction(void * Arg)
  1276. {
  1277. // When status is changed twice quickly, the controller when responding
  1278. // to the first change (non-user-action) can be so slow to check only after
  1279. // the second (user-action) change occurs. Thus it responds it.
  1280. // Then as reaction to the second (user-action) change there will not be
  1281. // any outstanding user-action.
  1282. bool Result = (FUserAction != NULL);
  1283. if (Result)
  1284. {
  1285. DebugAssert(FItem != NULL);
  1286. FUserAction->Execute(Arg);
  1287. FUserAction = NULL;
  1288. TriggerEvent();
  1289. }
  1290. return Result;
  1291. }
  1292. //---------------------------------------------------------------------------
  1293. bool __fastcall TTerminalItem::WaitForUserAction(
  1294. TQueueItem::TStatus ItemStatus, TUserAction * UserAction)
  1295. {
  1296. DebugAssert(FItem != NULL);
  1297. DebugAssert((FItem->GetStatus() == TQueueItem::qsProcessing) ||
  1298. (FItem->GetStatus() == TQueueItem::qsConnecting));
  1299. bool Result;
  1300. TQueueItem::TStatus PrevStatus = FItem->GetStatus();
  1301. try
  1302. {
  1303. FUserAction = UserAction;
  1304. FItem->SetStatus(ItemStatus);
  1305. FQueue->DoEvent(qePendingUserAction);
  1306. Result = !FTerminated && WaitForEvent() && !FCancel;
  1307. }
  1308. __finally
  1309. {
  1310. FUserAction = NULL;
  1311. FItem->SetStatus(PrevStatus);
  1312. }
  1313. return Result;
  1314. }
  1315. //---------------------------------------------------------------------------
  1316. bool __fastcall TTerminalItem::Finished()
  1317. {
  1318. bool Result = TSignalThread::Finished();
  1319. FQueue->TerminalFinished(this);
  1320. return Result;
  1321. }
  1322. //---------------------------------------------------------------------------
  1323. void __fastcall TTerminalItem::TerminalQueryUser(TObject * Sender,
  1324. const UnicodeString Query, TStrings * MoreMessages, unsigned int Answers,
  1325. const TQueryParams * Params, unsigned int & Answer, TQueryType Type, void * Arg)
  1326. {
  1327. // so far query without queue item can occur only for key confirmation
  1328. // on re-key with non-cached host key. make it fail.
  1329. if (FItem != NULL)
  1330. {
  1331. DebugUsedParam(Arg);
  1332. DebugAssert(Arg == NULL);
  1333. TQueryUserAction Action(FQueue->OnQueryUser);
  1334. Action.Sender = Sender;
  1335. Action.Query = Query;
  1336. Action.MoreMessages = MoreMessages;
  1337. Action.Answers = Answers;
  1338. Action.Params = Params;
  1339. Action.Answer = Answer;
  1340. Action.Type = Type;
  1341. // if the query is "error", present it as an "error" state in UI,
  1342. // however it is still handled as query by the action.
  1343. TQueueItem::TStatus ItemStatus =
  1344. (Action.Type == qtError ? TQueueItem::qsError : TQueueItem::qsQuery);
  1345. if (WaitForUserAction(ItemStatus, &Action))
  1346. {
  1347. Answer = Action.Answer;
  1348. }
  1349. }
  1350. }
  1351. //---------------------------------------------------------------------------
  1352. void __fastcall TTerminalItem::TerminalPromptUser(TTerminal * Terminal,
  1353. TPromptKind Kind, UnicodeString Name, UnicodeString Instructions, TStrings * Prompts,
  1354. TStrings * Results, bool & Result, void * Arg)
  1355. {
  1356. if (FItem == NULL)
  1357. {
  1358. // sanity, should not occur
  1359. DebugFail();
  1360. Result = false;
  1361. }
  1362. else
  1363. {
  1364. DebugUsedParam(Arg);
  1365. DebugAssert(Arg == NULL);
  1366. TPromptUserAction Action(FQueue->OnPromptUser);
  1367. Action.Terminal = Terminal;
  1368. Action.Kind = Kind;
  1369. Action.Name = Name;
  1370. Action.Instructions = Instructions;
  1371. Action.Prompts = Prompts;
  1372. Action.Results->AddStrings(Results);
  1373. if (WaitForUserAction(TQueueItem::qsPrompt, &Action))
  1374. {
  1375. Results->Clear();
  1376. Results->AddStrings(Action.Results);
  1377. Result = Action.Result;
  1378. }
  1379. }
  1380. }
  1381. //---------------------------------------------------------------------------
  1382. void __fastcall TTerminalItem::TerminalShowExtendedException(
  1383. TTerminal * Terminal, Exception * E, void * Arg)
  1384. {
  1385. DebugUsedParam(Arg);
  1386. DebugAssert(Arg == NULL);
  1387. if ((FItem != NULL) &&
  1388. ShouldDisplayException(E))
  1389. {
  1390. TShowExtendedExceptionAction Action(FQueue->OnShowExtendedException);
  1391. Action.Terminal = Terminal;
  1392. Action.E = E;
  1393. WaitForUserAction(TQueueItem::qsError, &Action);
  1394. }
  1395. }
  1396. //---------------------------------------------------------------------------
  1397. void __fastcall TTerminalItem::OperationFinished(TFileOperation /*Operation*/,
  1398. TOperationSide /*Side*/, bool /*Temp*/, const UnicodeString & /*FileName*/,
  1399. bool /*Success*/, bool /*NotCancelled*/, TOnceDoneOperation & /*OnceDoneOperation*/)
  1400. {
  1401. // nothing
  1402. }
  1403. //---------------------------------------------------------------------------
  1404. void __fastcall TTerminalItem::OperationProgress(
  1405. TFileOperationProgressType & ProgressData)
  1406. {
  1407. if (FPause && !IsCancelled())
  1408. {
  1409. DebugAssert(FItem != NULL);
  1410. TQueueItem::TStatus PrevStatus = FItem->GetStatus();
  1411. DebugAssert(PrevStatus == TQueueItem::qsProcessing);
  1412. // must be set before TFileOperationProgressType::Suspend(), because
  1413. // it invokes this method back
  1414. FPause = false;
  1415. ProgressData.Suspend();
  1416. try
  1417. {
  1418. FItem->SetStatus(TQueueItem::qsPaused);
  1419. WaitForEvent();
  1420. }
  1421. __finally
  1422. {
  1423. FItem->SetStatus(PrevStatus);
  1424. ProgressData.Resume();
  1425. }
  1426. }
  1427. if (IsCancelled())
  1428. {
  1429. if (ProgressData.TransferringFile)
  1430. {
  1431. ProgressData.SetCancel(csCancelTransfer);
  1432. }
  1433. else
  1434. {
  1435. ProgressData.SetCancel(csCancel);
  1436. }
  1437. }
  1438. DebugAssert(FItem != NULL);
  1439. FItem->SetProgress(ProgressData);
  1440. }
  1441. //---------------------------------------------------------------------------
  1442. bool __fastcall TTerminalItem::OverrideItemStatus(TQueueItem::TStatus & ItemStatus)
  1443. {
  1444. DebugAssert(FTerminal != NULL);
  1445. bool Result = (FTerminal->Status < ssOpened) && (ItemStatus == TQueueItem::qsProcessing);
  1446. if (Result)
  1447. {
  1448. ItemStatus = TQueueItem::qsConnecting;
  1449. }
  1450. return Result;
  1451. }
  1452. //---------------------------------------------------------------------------
  1453. bool TTerminalItem::IsCancelled()
  1454. {
  1455. return FTerminated || FCancel;
  1456. }
  1457. //---------------------------------------------------------------------------
  1458. // TQueueItem
  1459. //---------------------------------------------------------------------------
  1460. __fastcall TQueueItem::TQueueItem() :
  1461. FStatus(qsPending), FSection(NULL), FTerminalItem(NULL), FProgressData(NULL),
  1462. FInfo(NULL), FQueue(NULL), FCompleteEvent(INVALID_HANDLE_VALUE),
  1463. FCPSLimit(-1)
  1464. {
  1465. FSection = new TCriticalSection();
  1466. FInfo = new TInfo();
  1467. FInfo->SingleFile = false;
  1468. FInfo->Primary = true;
  1469. FInfo->GroupToken = this;
  1470. }
  1471. //---------------------------------------------------------------------------
  1472. __fastcall TQueueItem::~TQueueItem()
  1473. {
  1474. // we need to keep the total transfer size even after transfer completes
  1475. delete FProgressData;
  1476. Complete();
  1477. delete FSection;
  1478. delete FInfo;
  1479. }
  1480. //---------------------------------------------------------------------------
  1481. bool __fastcall TQueueItem::Complete()
  1482. {
  1483. TGuard Guard(FSection);
  1484. if (FCompleteEvent != INVALID_HANDLE_VALUE)
  1485. {
  1486. SetEvent(FCompleteEvent);
  1487. FCompleteEvent = INVALID_HANDLE_VALUE;
  1488. }
  1489. return FInfo->Primary;
  1490. }
  1491. //---------------------------------------------------------------------------
  1492. bool __fastcall TQueueItem::IsUserActionStatus(TStatus Status)
  1493. {
  1494. return (Status == qsQuery) || (Status == qsError) || (Status == qsPrompt);
  1495. }
  1496. //---------------------------------------------------------------------------
  1497. TQueueItem::TStatus __fastcall TQueueItem::GetStatus()
  1498. {
  1499. TGuard Guard(FSection);
  1500. return FStatus;
  1501. }
  1502. //---------------------------------------------------------------------------
  1503. void __fastcall TQueueItem::SetStatus(TStatus Status)
  1504. {
  1505. {
  1506. TGuard Guard(FSection);
  1507. FStatus = Status;
  1508. if (FStatus == qsDone)
  1509. {
  1510. FDoneAt = Now();
  1511. }
  1512. }
  1513. DebugAssert((FQueue != NULL) || (Status == qsPending));
  1514. if (FQueue != NULL)
  1515. {
  1516. FQueue->DoQueueItemUpdate(this);
  1517. }
  1518. }
  1519. //---------------------------------------------------------------------------
  1520. void __fastcall TQueueItem::ProgressUpdated()
  1521. {
  1522. // noop
  1523. }
  1524. //---------------------------------------------------------------------------
  1525. void __fastcall TQueueItem::SetProgress(
  1526. TFileOperationProgressType & ProgressData)
  1527. {
  1528. {
  1529. TGuard Guard(FSection);
  1530. // do not lose CPS limit override on "calculate size" operation,
  1531. // wait until the real transfer operation starts
  1532. if ((FCPSLimit >= 0) && ProgressData.IsTransfer())
  1533. {
  1534. ProgressData.SetCPSLimit(static_cast<unsigned long>(FCPSLimit));
  1535. FCPSLimit = -1;
  1536. }
  1537. DebugAssert(FProgressData != NULL);
  1538. FProgressData->Assign(ProgressData);
  1539. FProgressData->Reset();
  1540. }
  1541. ProgressUpdated();
  1542. FQueue->DoQueueItemUpdate(this);
  1543. }
  1544. //---------------------------------------------------------------------------
  1545. void __fastcall TQueueItem::GetData(TQueueItemProxy * Proxy)
  1546. {
  1547. TGuard Guard(FSection);
  1548. DebugAssert(Proxy->FProgressData != NULL);
  1549. if (FProgressData != NULL)
  1550. {
  1551. Proxy->FProgressData->Assign(*FProgressData);
  1552. }
  1553. else
  1554. {
  1555. Proxy->FProgressData->Clear();
  1556. }
  1557. *Proxy->FInfo = *FInfo;
  1558. Proxy->FStatus = FStatus;
  1559. if (FTerminalItem != NULL)
  1560. {
  1561. FTerminalItem->OverrideItemStatus(Proxy->FStatus);
  1562. }
  1563. }
  1564. //---------------------------------------------------------------------------
  1565. bool __fastcall TQueueItem::UpdateFileList(TQueueFileList *)
  1566. {
  1567. // noop - implemented in TTransferQueueItem
  1568. return false;
  1569. }
  1570. //---------------------------------------------------------------------------
  1571. bool TQueueItem::IsExecutionCancelled()
  1572. {
  1573. return DebugAlwaysTrue(FTerminalItem != NULL) ? FTerminalItem->IsCancelled() : true;
  1574. }
  1575. //---------------------------------------------------------------------------
  1576. void __fastcall TQueueItem::Execute()
  1577. {
  1578. {
  1579. DebugAssert(FProgressData == NULL);
  1580. TGuard Guard(FSection);
  1581. FProgressData = new TFileOperationProgressType();
  1582. }
  1583. DoExecute(FTerminalItem->FTerminal);
  1584. }
  1585. //---------------------------------------------------------------------------
  1586. void __fastcall TQueueItem::SetCPSLimit(unsigned long CPSLimit)
  1587. {
  1588. FCPSLimit = static_cast<long>(CPSLimit);
  1589. }
  1590. //---------------------------------------------------------------------------
  1591. unsigned long __fastcall TQueueItem::DefaultCPSLimit()
  1592. {
  1593. return 0;
  1594. }
  1595. //---------------------------------------------------------------------------
  1596. unsigned long __fastcall TQueueItem::GetCPSLimit()
  1597. {
  1598. unsigned long Result;
  1599. if (FCPSLimit >= 0)
  1600. {
  1601. Result = FCPSLimit;
  1602. }
  1603. else if (FProgressData != NULL)
  1604. {
  1605. Result = FProgressData->CPSLimit;
  1606. }
  1607. else
  1608. {
  1609. Result = DefaultCPSLimit();
  1610. }
  1611. return Result;
  1612. }
  1613. //---------------------------------------------------------------------------
  1614. TQueueItem * __fastcall TQueueItem::CreateParallelOperation()
  1615. {
  1616. return NULL;
  1617. }
  1618. //---------------------------------------------------------------------------
  1619. UnicodeString __fastcall TQueueItem::StartupDirectory() const
  1620. {
  1621. return EmptyStr;
  1622. }
  1623. //---------------------------------------------------------------------------
  1624. // TQueueItemProxy
  1625. //---------------------------------------------------------------------------
  1626. __fastcall TQueueItemProxy::TQueueItemProxy(TTerminalQueue * Queue,
  1627. TQueueItem * QueueItem) :
  1628. UserData(NULL), FProgressData(NULL), FQueue(Queue), FQueueItem(QueueItem),
  1629. FQueueStatus(NULL), FInfo(NULL), FProcessingUserAction(false)
  1630. {
  1631. FProgressData = new TFileOperationProgressType();
  1632. FInfo = new TQueueItem::TInfo();
  1633. Update();
  1634. }
  1635. //---------------------------------------------------------------------------
  1636. __fastcall TQueueItemProxy::~TQueueItemProxy()
  1637. {
  1638. delete FProgressData;
  1639. delete FInfo;
  1640. }
  1641. //---------------------------------------------------------------------------
  1642. TFileOperationProgressType * __fastcall TQueueItemProxy::GetProgressData()
  1643. {
  1644. return (FProgressData->Operation == foNone) ? NULL : FProgressData;
  1645. }
  1646. //---------------------------------------------------------------------------
  1647. __int64 __fastcall TQueueItemProxy::GetTotalTransferred()
  1648. {
  1649. // want to show total transferred also for "completed" items,
  1650. // for which GetProgressData() is NULL
  1651. return
  1652. (FProgressData->Operation == Info->Operation) || (Status == TQueueItem::qsDone) ?
  1653. FProgressData->TotalTransferred : -1;
  1654. }
  1655. //---------------------------------------------------------------------------
  1656. bool __fastcall TQueueItemProxy::Update()
  1657. {
  1658. DebugAssert(FQueueItem != NULL);
  1659. TQueueItem::TStatus PrevStatus = Status;
  1660. bool Result = FQueue->ItemGetData(FQueueItem, this, NULL);
  1661. if ((FQueueStatus != NULL) && (PrevStatus != Status))
  1662. {
  1663. FQueueStatus->ResetStats();
  1664. }
  1665. return Result;
  1666. }
  1667. //---------------------------------------------------------------------------
  1668. bool __fastcall TQueueItemProxy::UpdateFileList(TQueueFileList * FileList)
  1669. {
  1670. return FQueue->ItemGetData(FQueueItem, NULL, FileList);
  1671. }
  1672. //---------------------------------------------------------------------------
  1673. bool __fastcall TQueueItemProxy::ExecuteNow()
  1674. {
  1675. return FQueue->ItemExecuteNow(FQueueItem);
  1676. }
  1677. //---------------------------------------------------------------------------
  1678. bool __fastcall TQueueItemProxy::Move(bool Sooner)
  1679. {
  1680. bool Result = false;
  1681. int I = Index;
  1682. if (Sooner)
  1683. {
  1684. if (I > 0)
  1685. {
  1686. Result = Move(FQueueStatus->Items[I - 1]);
  1687. }
  1688. }
  1689. else
  1690. {
  1691. if (I < FQueueStatus->Count - 1)
  1692. {
  1693. Result = FQueueStatus->Items[I + 1]->Move(this);
  1694. }
  1695. }
  1696. return Result;
  1697. }
  1698. //---------------------------------------------------------------------------
  1699. bool __fastcall TQueueItemProxy::Move(TQueueItemProxy * BeforeItem)
  1700. {
  1701. return FQueue->ItemMove(FQueueItem, BeforeItem->FQueueItem);
  1702. }
  1703. //---------------------------------------------------------------------------
  1704. bool __fastcall TQueueItemProxy::Delete()
  1705. {
  1706. return FQueue->ItemDelete(FQueueItem);
  1707. }
  1708. //---------------------------------------------------------------------------
  1709. bool __fastcall TQueueItemProxy::Pause()
  1710. {
  1711. return FQueue->ItemPause(FQueueItem, true);
  1712. }
  1713. //---------------------------------------------------------------------------
  1714. bool __fastcall TQueueItemProxy::Resume()
  1715. {
  1716. return FQueue->ItemPause(FQueueItem, false);
  1717. }
  1718. //---------------------------------------------------------------------------
  1719. bool __fastcall TQueueItemProxy::ProcessUserAction()
  1720. {
  1721. DebugAssert(FQueueItem != NULL);
  1722. bool Result;
  1723. FProcessingUserAction = true;
  1724. try
  1725. {
  1726. Result = FQueue->ItemProcessUserAction(FQueueItem, NULL);
  1727. }
  1728. __finally
  1729. {
  1730. FProcessingUserAction = false;
  1731. }
  1732. return Result;
  1733. }
  1734. //---------------------------------------------------------------------------
  1735. bool __fastcall TQueueItemProxy::GetCPSLimit(unsigned long & CPSLimit)
  1736. {
  1737. return FQueue->ItemGetCPSLimit(FQueueItem, CPSLimit);
  1738. }
  1739. //---------------------------------------------------------------------------
  1740. bool __fastcall TQueueItemProxy::SetCPSLimit(unsigned long CPSLimit)
  1741. {
  1742. return FQueue->ItemSetCPSLimit(FQueueItem, CPSLimit);
  1743. }
  1744. //---------------------------------------------------------------------------
  1745. int __fastcall TQueueItemProxy::GetIndex()
  1746. {
  1747. DebugAssert(FQueueStatus != NULL);
  1748. int Index = FQueueStatus->FList->IndexOf(this);
  1749. DebugAssert(Index >= 0);
  1750. return Index;
  1751. }
  1752. //---------------------------------------------------------------------------
  1753. // TTerminalQueueStatus
  1754. //---------------------------------------------------------------------------
  1755. __fastcall TTerminalQueueStatus::TTerminalQueueStatus() :
  1756. FList(NULL)
  1757. {
  1758. FList = new TList();
  1759. ResetStats();
  1760. }
  1761. //---------------------------------------------------------------------------
  1762. __fastcall TTerminalQueueStatus::~TTerminalQueueStatus()
  1763. {
  1764. for (int Index = 0; Index < FList->Count; Index++)
  1765. {
  1766. delete GetItem(Index);
  1767. }
  1768. delete FList;
  1769. FList = NULL;
  1770. }
  1771. //---------------------------------------------------------------------------
  1772. void __fastcall TTerminalQueueStatus::ResetStats()
  1773. {
  1774. FActiveCount = -1;
  1775. FActivePrimaryCount = -1;
  1776. FActiveAndPendingPrimaryCount = -1;
  1777. }
  1778. //---------------------------------------------------------------------------
  1779. void __fastcall TTerminalQueueStatus::SetDoneCount(int Value)
  1780. {
  1781. FDoneCount = Value;
  1782. ResetStats();
  1783. }
  1784. //---------------------------------------------------------------------------
  1785. void __fastcall TTerminalQueueStatus::NeedStats()
  1786. {
  1787. if (FActiveCount < 0)
  1788. {
  1789. FActiveCount = 0;
  1790. FActivePrimaryCount = 0;
  1791. FActiveAndPendingPrimaryCount = 0;
  1792. for (int Index = DoneCount; Index < Count; Index++)
  1793. {
  1794. bool Primary = GetItem(Index)->Info->Primary;
  1795. if (GetItem(Index)->Status != TQueueItem::qsPending)
  1796. {
  1797. FActiveCount++;
  1798. if (Primary)
  1799. {
  1800. FActivePrimaryCount++;
  1801. }
  1802. }
  1803. if (Primary)
  1804. {
  1805. FActiveAndPendingPrimaryCount++;
  1806. }
  1807. }
  1808. }
  1809. }
  1810. //---------------------------------------------------------------------------
  1811. int __fastcall TTerminalQueueStatus::GetActiveCount()
  1812. {
  1813. NeedStats();
  1814. return FActiveCount;
  1815. }
  1816. //---------------------------------------------------------------------------
  1817. int __fastcall TTerminalQueueStatus::GetDoneAndActiveCount()
  1818. {
  1819. return DoneCount + ActiveCount;
  1820. }
  1821. //---------------------------------------------------------------------------
  1822. int __fastcall TTerminalQueueStatus::GetActivePrimaryCount()
  1823. {
  1824. NeedStats();
  1825. return FActivePrimaryCount;
  1826. }
  1827. //---------------------------------------------------------------------------
  1828. bool __fastcall TTerminalQueueStatus::IsOnlyOneActiveAndNoPending()
  1829. {
  1830. return (ActivePrimaryCount == 1) && (ActiveAndPendingPrimaryCount == 1);
  1831. }
  1832. //---------------------------------------------------------------------------
  1833. int __fastcall TTerminalQueueStatus::GetActiveAndPendingPrimaryCount()
  1834. {
  1835. NeedStats();
  1836. return FActiveAndPendingPrimaryCount;
  1837. }
  1838. //---------------------------------------------------------------------------
  1839. void __fastcall TTerminalQueueStatus::Add(TQueueItemProxy * ItemProxy)
  1840. {
  1841. ItemProxy->FQueueStatus = this;
  1842. int Index = FList->Count;
  1843. if (!ItemProxy->Info->Primary)
  1844. {
  1845. for (int I = 0; I < FList->Count; I++)
  1846. {
  1847. if (Items[I]->Info->GroupToken == ItemProxy->Info->GroupToken)
  1848. {
  1849. Index = I + 1;
  1850. }
  1851. }
  1852. DebugAssert(Index >= 0);
  1853. }
  1854. FList->Insert(Index, ItemProxy);
  1855. ResetStats();
  1856. }
  1857. //---------------------------------------------------------------------------
  1858. void __fastcall TTerminalQueueStatus::Delete(TQueueItemProxy * ItemProxy)
  1859. {
  1860. FList->Extract(ItemProxy);
  1861. ItemProxy->FQueueStatus = NULL;
  1862. ResetStats();
  1863. }
  1864. //---------------------------------------------------------------------------
  1865. int __fastcall TTerminalQueueStatus::GetCount()
  1866. {
  1867. return FList->Count;
  1868. }
  1869. //---------------------------------------------------------------------------
  1870. TQueueItemProxy * __fastcall TTerminalQueueStatus::GetItem(int Index)
  1871. {
  1872. return reinterpret_cast<TQueueItemProxy *>(FList->Items[Index]);
  1873. }
  1874. //---------------------------------------------------------------------------
  1875. TQueueItemProxy * __fastcall TTerminalQueueStatus::FindByQueueItem(
  1876. TQueueItem * QueueItem)
  1877. {
  1878. TQueueItemProxy * Item;
  1879. for (int Index = 0; Index < FList->Count; Index++)
  1880. {
  1881. Item = GetItem(Index);
  1882. if (Item->FQueueItem == QueueItem)
  1883. {
  1884. return Item;
  1885. }
  1886. }
  1887. return NULL;
  1888. }
  1889. //---------------------------------------------------------------------------
  1890. bool __fastcall TTerminalQueueStatus::UpdateFileList(TQueueItemProxy * ItemProxy, TQueueFileList * FileList)
  1891. {
  1892. bool Result;
  1893. if (ItemProxy != NULL)
  1894. {
  1895. Result = ItemProxy->UpdateFileList(FileList);
  1896. }
  1897. else
  1898. {
  1899. Result = (FileList->GetCount() > 0);
  1900. if (Result)
  1901. {
  1902. FileList->Clear();
  1903. }
  1904. }
  1905. return Result;
  1906. }
  1907. //---------------------------------------------------------------------------
  1908. // TBootstrapQueueItem
  1909. //---------------------------------------------------------------------------
  1910. __fastcall TBootstrapQueueItem::TBootstrapQueueItem()
  1911. {
  1912. FInfo->SingleFile = true;
  1913. }
  1914. //---------------------------------------------------------------------------
  1915. void __fastcall TBootstrapQueueItem::DoExecute(TTerminal * DebugUsedArg(Terminal))
  1916. {
  1917. // noop
  1918. }
  1919. //---------------------------------------------------------------------------
  1920. bool __fastcall TBootstrapQueueItem::Complete()
  1921. {
  1922. TQueueItem::Complete();
  1923. // To hide the item, even if "keep done items" is on
  1924. return false;
  1925. }
  1926. //---------------------------------------------------------------------------
  1927. // TLocatedQueueItem
  1928. //---------------------------------------------------------------------------
  1929. __fastcall TLocatedQueueItem::TLocatedQueueItem(TTerminal * Terminal) :
  1930. TQueueItem()
  1931. {
  1932. DebugAssert(Terminal != NULL);
  1933. FCurrentDir = Terminal->CurrentDirectory;
  1934. }
  1935. //---------------------------------------------------------------------------
  1936. __fastcall TLocatedQueueItem::TLocatedQueueItem(const TLocatedQueueItem & Source) :
  1937. TQueueItem()
  1938. {
  1939. FCurrentDir = Source.FCurrentDir;
  1940. }
  1941. //---------------------------------------------------------------------------
  1942. UnicodeString __fastcall TLocatedQueueItem::StartupDirectory() const
  1943. {
  1944. return FCurrentDir;
  1945. }
  1946. //---------------------------------------------------------------------------
  1947. void __fastcall TLocatedQueueItem::DoExecute(TTerminal * Terminal)
  1948. {
  1949. DebugAssert(Terminal != NULL);
  1950. Terminal->CurrentDirectory = FCurrentDir;
  1951. }
  1952. //---------------------------------------------------------------------------
  1953. // TTransferQueueItem
  1954. //---------------------------------------------------------------------------
  1955. __fastcall TTransferQueueItem::TTransferQueueItem(TTerminal * Terminal,
  1956. TStrings * FilesToCopy, const UnicodeString & TargetDir,
  1957. const TCopyParamType * CopyParam, int Params, TOperationSide Side,
  1958. bool SingleFile, bool Parallel) :
  1959. TLocatedQueueItem(Terminal), FFilesToCopy(NULL), FCopyParam(NULL)
  1960. {
  1961. FInfo->Operation = (Params & cpDelete ? foMove : foCopy);
  1962. FInfo->Side = Side;
  1963. FInfo->SingleFile = SingleFile;
  1964. if (FilesToCopy != NULL)
  1965. {
  1966. FFilesToCopy = new TStringList();
  1967. for (int Index = 0; Index < FilesToCopy->Count; Index++)
  1968. {
  1969. UnicodeString FileName = FilesToCopy->Strings[Index];
  1970. TRemoteFile * File = dynamic_cast<TRemoteFile *>(FilesToCopy->Objects[Index]);
  1971. FFilesToCopy->AddObject(FileName, ((File == NULL) || (Side == osLocal)) ? NULL : File->Duplicate());
  1972. }
  1973. }
  1974. FTargetDir = TargetDir;
  1975. DebugAssert(CopyParam != NULL);
  1976. FCopyParam = new TCopyParamType(*CopyParam);
  1977. FParams = Params;
  1978. FParallel = Parallel;
  1979. FLastParallelOperationAdded = GetTickCount();
  1980. }
  1981. //---------------------------------------------------------------------------
  1982. __fastcall TTransferQueueItem::~TTransferQueueItem()
  1983. {
  1984. if (FFilesToCopy != NULL)
  1985. {
  1986. for (int Index = 0; Index < FFilesToCopy->Count; Index++)
  1987. {
  1988. delete FFilesToCopy->Objects[Index];
  1989. }
  1990. delete FFilesToCopy;
  1991. }
  1992. delete FCopyParam;
  1993. }
  1994. //---------------------------------------------------------------------------
  1995. unsigned long __fastcall TTransferQueueItem::DefaultCPSLimit()
  1996. {
  1997. return FCopyParam->CPSLimit;
  1998. }
  1999. //---------------------------------------------------------------------------
  2000. void __fastcall TTransferQueueItem::DoExecute(TTerminal * Terminal)
  2001. {
  2002. TLocatedQueueItem::DoExecute(Terminal);
  2003. DebugAssert(Terminal != NULL);
  2004. FParallelOperation.reset(new TParallelOperation(FInfo->Side));
  2005. try
  2006. {
  2007. DoTransferExecute(Terminal, FParallelOperation.get());
  2008. }
  2009. __finally
  2010. {
  2011. FParallelOperation->WaitFor();
  2012. }
  2013. }
  2014. //---------------------------------------------------------------------------
  2015. void __fastcall TTransferQueueItem::ProgressUpdated()
  2016. {
  2017. TLocatedQueueItem::ProgressUpdated();
  2018. if (FParallel)
  2019. {
  2020. bool Add = false;
  2021. bool Force = false;
  2022. DWORD LastParallelOperationAddedPrev = 0;
  2023. {
  2024. TGuard Guard(FSection);
  2025. DebugAssert(FParallelOperation.get() != NULL);
  2026. // Won't be initialized, if the operation is not eligible for parallel transfers (like cpDelete).
  2027. // We can probably move the check outside of the guard.
  2028. if (FParallelOperation->IsInitialized())
  2029. {
  2030. DebugAssert(FProgressData->IsTransfer());
  2031. if (FProgressData->Operation == foCopy)
  2032. {
  2033. Add = FParallelOperation->ShouldAddClient();
  2034. if (Add)
  2035. {
  2036. DWORD Now = GetTickCount();
  2037. Force =
  2038. (Now - FLastParallelOperationAdded >= 5*1000) &&
  2039. (TimeToSeconds(FProgressData->TotalTimeLeft()) >= FQueue->ParallelDurationThreshold);
  2040. LastParallelOperationAddedPrev = FLastParallelOperationAdded;
  2041. // update now already to prevent race condition, but we will have to rollback it back,
  2042. // if we actually do not add the parallel operation
  2043. FLastParallelOperationAdded = Now;
  2044. }
  2045. }
  2046. }
  2047. }
  2048. if (Add)
  2049. {
  2050. if (!FQueue->TryAddParallelOperation(this, Force))
  2051. {
  2052. TGuard Guard(FSection);
  2053. FLastParallelOperationAdded = LastParallelOperationAddedPrev;
  2054. }
  2055. }
  2056. }
  2057. }
  2058. //---------------------------------------------------------------------------
  2059. TQueueItem * __fastcall TTransferQueueItem::CreateParallelOperation()
  2060. {
  2061. DebugAssert(FParallelOperation.get() != NULL);
  2062. FParallelOperation->AddClient();
  2063. DebugAssert(!FInfo->SingleFile || FParallelOperation->IsParallelFileTransfer);
  2064. FInfo->SingleFile = false;
  2065. return new TParallelTransferQueueItem(this, FParallelOperation.get());
  2066. }
  2067. //---------------------------------------------------------------------------
  2068. bool __fastcall TTransferQueueItem::UpdateFileList(TQueueFileList * FileList)
  2069. {
  2070. TGuard Guard(FSection);
  2071. bool Result;
  2072. if ((FParallelOperation.get() != NULL) && FParallelOperation->IsInitialized())
  2073. {
  2074. Result = FParallelOperation->UpdateFileList(FileList);
  2075. }
  2076. else
  2077. {
  2078. Result = false;
  2079. }
  2080. return Result;
  2081. }
  2082. //---------------------------------------------------------------------------
  2083. // TUploadQueueItem
  2084. //---------------------------------------------------------------------------
  2085. static void ExtractLocalSourcePath(TStrings * Files, UnicodeString & Path)
  2086. {
  2087. ExtractCommonPath(Files, Path);
  2088. // this way the trailing backslash is preserved for root directories like "D:\"
  2089. Path = ExtractFileDir(IncludeTrailingBackslash(Path));
  2090. }
  2091. //---------------------------------------------------------------------------
  2092. static bool IsSingleFileUpload(TStrings * FilesToCopy)
  2093. {
  2094. return
  2095. (FilesToCopy->Count == 1) &&
  2096. FileExists(ApiPath(FilesToCopy->Strings[0]));
  2097. }
  2098. //---------------------------------------------------------------------------
  2099. __fastcall TUploadQueueItem::TUploadQueueItem(
  2100. TTerminal * Terminal, TStrings * FilesToCopy, const UnicodeString & TargetDir,
  2101. const TCopyParamType * CopyParam, int Params, bool Parallel) :
  2102. TTransferQueueItem(
  2103. Terminal, FilesToCopy, TargetDir, CopyParam, Params, osLocal, IsSingleFileUpload(FilesToCopy), Parallel)
  2104. {
  2105. if (FilesToCopy->Count > 1)
  2106. {
  2107. if (FLAGSET(Params, cpTemporary))
  2108. {
  2109. FInfo->Source = L"";
  2110. FInfo->ModifiedLocal = L"";
  2111. }
  2112. else
  2113. {
  2114. ExtractLocalSourcePath(FilesToCopy, FInfo->Source);
  2115. FInfo->ModifiedLocal = FLAGCLEAR(Params, cpDelete) ? UnicodeString() :
  2116. IncludeTrailingBackslash(FInfo->Source);
  2117. }
  2118. }
  2119. else
  2120. {
  2121. if (FLAGSET(Params, cpTemporary))
  2122. {
  2123. FInfo->Source = ExtractFileName(FilesToCopy->Strings[0]);
  2124. FInfo->ModifiedLocal = L"";
  2125. }
  2126. else
  2127. {
  2128. DebugAssert(FilesToCopy->Count > 0);
  2129. FInfo->Source = FilesToCopy->Strings[0];
  2130. FInfo->ModifiedLocal = FLAGCLEAR(Params, cpDelete) ? UnicodeString() :
  2131. IncludeTrailingBackslash(ExtractFilePath(FInfo->Source));
  2132. }
  2133. }
  2134. FInfo->Destination =
  2135. UnixIncludeTrailingBackslash(TargetDir) + CopyParam->FileMask;
  2136. FInfo->ModifiedRemote = UnixIncludeTrailingBackslash(TargetDir);
  2137. }
  2138. //---------------------------------------------------------------------------
  2139. void __fastcall TUploadQueueItem::DoTransferExecute(TTerminal * Terminal, TParallelOperation * ParallelOperation)
  2140. {
  2141. Terminal->CopyToRemote(FFilesToCopy, FTargetDir, FCopyParam, FParams, ParallelOperation);
  2142. }
  2143. //---------------------------------------------------------------------------
  2144. // TParallelTransferQueueItem
  2145. //---------------------------------------------------------------------------
  2146. __fastcall TParallelTransferQueueItem::TParallelTransferQueueItem(
  2147. const TLocatedQueueItem * ParentItem, TParallelOperation * ParallelOperation) :
  2148. TLocatedQueueItem(*ParentItem),
  2149. FParallelOperation(ParallelOperation)
  2150. {
  2151. // deliberately not copying the ModifiedLocal and ModifiedRemote, not to trigger panel refresh, when sub-item completes
  2152. FInfo->Operation = ParentItem->FInfo->Operation;
  2153. FInfo->Side = ParentItem->FInfo->Side;
  2154. FInfo->Source = ParentItem->FInfo->Source;
  2155. FInfo->Destination = ParentItem->FInfo->Destination;
  2156. FInfo->SingleFile = DebugAlwaysFalse(ParentItem->FInfo->SingleFile);
  2157. FInfo->Primary = false;
  2158. FInfo->GroupToken = ParentItem->FInfo->GroupToken;
  2159. }
  2160. //---------------------------------------------------------------------------
  2161. void __fastcall TParallelTransferQueueItem::DoExecute(TTerminal * Terminal)
  2162. {
  2163. TLocatedQueueItem::DoExecute(Terminal);
  2164. Terminal->LogParallelTransfer(FParallelOperation);
  2165. TFileOperationProgressType OperationProgress(Terminal->OnProgress, Terminal->OnFinished, FParallelOperation->MainOperationProgress);
  2166. TFileOperation Operation = (FLAGSET(FParallelOperation->Params, cpDelete) ? foMove : foCopy);
  2167. bool Temp = FLAGSET(FParallelOperation->Params, cpTemporary);
  2168. OperationProgress.Start(
  2169. // CPS limit inherited from parent OperationProgress.
  2170. // Count not known and won't be needed as we will always have TotalSize as we always transfer a single file at a time.
  2171. Operation, FParallelOperation->Side, -1, Temp, FParallelOperation->TargetDir, 0, odoIdle);
  2172. try
  2173. {
  2174. bool Continue = true;
  2175. do
  2176. {
  2177. int GotNext = Terminal->CopyToParallel(FParallelOperation, &OperationProgress);
  2178. if (GotNext < 0)
  2179. {
  2180. Continue = false;
  2181. }
  2182. else if (!FQueue->ContinueParallelOperation())
  2183. {
  2184. Continue = false;
  2185. }
  2186. }
  2187. while (Continue);
  2188. }
  2189. __finally
  2190. {
  2191. OperationProgress.Stop();
  2192. FParallelOperation->RemoveClient();
  2193. }
  2194. }
  2195. //---------------------------------------------------------------------------
  2196. // TDownloadQueueItem
  2197. //---------------------------------------------------------------------------
  2198. static void ExtractRemoteSourcePath(TTerminal * Terminal, TStrings * Files, UnicodeString & Path)
  2199. {
  2200. if (!UnixExtractCommonPath(Files, Path))
  2201. {
  2202. Path = Terminal->CurrentDirectory;
  2203. }
  2204. Path = UnixExcludeTrailingBackslash(Path);
  2205. }
  2206. //---------------------------------------------------------------------------
  2207. static bool IsSingleFileDownload(TStrings * FilesToCopy)
  2208. {
  2209. return
  2210. (FilesToCopy->Count == 1) &&
  2211. !static_cast<TRemoteFile *>(FilesToCopy->Objects[0])->IsDirectory;
  2212. }
  2213. //---------------------------------------------------------------------------
  2214. __fastcall TDownloadQueueItem::TDownloadQueueItem(TTerminal * Terminal,
  2215. TStrings * FilesToCopy, const UnicodeString & TargetDir,
  2216. const TCopyParamType * CopyParam, int Params, bool Parallel) :
  2217. TTransferQueueItem(
  2218. Terminal, FilesToCopy, TargetDir, CopyParam, Params, osRemote, IsSingleFileDownload(FilesToCopy), Parallel)
  2219. {
  2220. if (FilesToCopy->Count > 1)
  2221. {
  2222. ExtractRemoteSourcePath(Terminal, FilesToCopy, FInfo->Source);
  2223. FInfo->ModifiedRemote = FLAGCLEAR(Params, cpDelete) ? UnicodeString() :
  2224. UnixIncludeTrailingBackslash(FInfo->Source);
  2225. }
  2226. else
  2227. {
  2228. DebugAssert(FilesToCopy->Count > 0);
  2229. FInfo->Source = FilesToCopy->Strings[0];
  2230. if (UnixExtractFilePath(FInfo->Source).IsEmpty())
  2231. {
  2232. FInfo->Source = UnixIncludeTrailingBackslash(Terminal->CurrentDirectory) +
  2233. FInfo->Source;
  2234. FInfo->ModifiedRemote = FLAGCLEAR(Params, cpDelete) ? UnicodeString() :
  2235. UnixIncludeTrailingBackslash(Terminal->CurrentDirectory);
  2236. }
  2237. else
  2238. {
  2239. FInfo->ModifiedRemote = FLAGCLEAR(Params, cpDelete) ? UnicodeString() :
  2240. UnixExtractFilePath(FInfo->Source);
  2241. }
  2242. }
  2243. if (FLAGSET(Params, cpTemporary))
  2244. {
  2245. FInfo->Destination = L"";
  2246. }
  2247. else
  2248. {
  2249. FInfo->Destination =
  2250. IncludeTrailingBackslash(TargetDir) + CopyParam->FileMask;
  2251. }
  2252. FInfo->ModifiedLocal = IncludeTrailingBackslash(TargetDir);
  2253. }
  2254. //---------------------------------------------------------------------------
  2255. void __fastcall TDownloadQueueItem::DoTransferExecute(TTerminal * Terminal, TParallelOperation * ParallelOperation)
  2256. {
  2257. Terminal->CopyToLocal(FFilesToCopy, FTargetDir, FCopyParam, FParams, ParallelOperation);
  2258. }
  2259. //---------------------------------------------------------------------------
  2260. //---------------------------------------------------------------------------
  2261. TRemoteDeleteQueueItem::TRemoteDeleteQueueItem(TTerminal * Terminal, TStrings * FilesToDelete, int Params) :
  2262. TLocatedQueueItem(Terminal)
  2263. {
  2264. FInfo->Operation = foDelete;
  2265. FInfo->Side = osRemote;
  2266. DebugAssert(FilesToDelete != NULL);
  2267. FFilesToDelete.reset(TRemoteFileList::CloneStrings(FilesToDelete));
  2268. ExtractRemoteSourcePath(Terminal, FilesToDelete, FInfo->Source);
  2269. FInfo->ModifiedRemote = FInfo->Source;
  2270. FParams = Params;
  2271. }
  2272. //---------------------------------------------------------------------------
  2273. void __fastcall TRemoteDeleteQueueItem::DoExecute(TTerminal * Terminal)
  2274. {
  2275. TLocatedQueueItem::DoExecute(Terminal);
  2276. DebugAssert(Terminal != NULL);
  2277. Terminal->DeleteFiles(FFilesToDelete.get(), FParams);
  2278. }
  2279. //---------------------------------------------------------------------------
  2280. //---------------------------------------------------------------------------
  2281. TLocalDeleteQueueItem::TLocalDeleteQueueItem(TStrings * FilesToDelete, int Params) :
  2282. TQueueItem()
  2283. {
  2284. FInfo->Operation = foDelete;
  2285. FInfo->Side = osLocal;
  2286. DebugAssert(FilesToDelete != NULL);
  2287. FFilesToDelete.reset(CloneStrings(FilesToDelete));
  2288. ExtractLocalSourcePath(FilesToDelete, FInfo->Source);
  2289. FInfo->ModifiedLocal = FInfo->Source;
  2290. FParams = Params;
  2291. }
  2292. //---------------------------------------------------------------------------
  2293. void __fastcall TLocalDeleteQueueItem::DoExecute(TTerminal * Terminal)
  2294. {
  2295. DebugAssert(Terminal != NULL);
  2296. Terminal->DeleteLocalFiles(FFilesToDelete.get(), FParams);
  2297. }
  2298. //---------------------------------------------------------------------------
  2299. // TTerminalThread
  2300. //---------------------------------------------------------------------------
  2301. __fastcall TTerminalThread::TTerminalThread(TTerminal * Terminal) :
  2302. TSignalThread(false), FTerminal(Terminal)
  2303. {
  2304. FAction = NULL;
  2305. FActionEvent = CreateEvent(NULL, false, false, NULL);
  2306. FException = NULL;
  2307. FIdleException = NULL;
  2308. FOnIdle = NULL;
  2309. FUserAction = NULL;
  2310. FCancel = false;
  2311. FAbandoned = false;
  2312. FAllowAbandon = false;
  2313. FMainThread = GetCurrentThreadId();
  2314. FSection = new TCriticalSection();
  2315. FOnInformation = FTerminal->OnInformation;
  2316. FOnQueryUser = FTerminal->OnQueryUser;
  2317. FOnPromptUser = FTerminal->OnPromptUser;
  2318. FOnShowExtendedException = FTerminal->OnShowExtendedException;
  2319. FOnDisplayBanner = FTerminal->OnDisplayBanner;
  2320. FOnChangeDirectory = FTerminal->OnChangeDirectory;
  2321. FOnReadDirectory = FTerminal->OnReadDirectory;
  2322. FOnStartReadDirectory = FTerminal->OnStartReadDirectory;
  2323. FOnReadDirectoryProgress = FTerminal->OnReadDirectoryProgress;
  2324. FOnInitializeLog = FTerminal->OnInitializeLog;
  2325. FTerminal->OnInformation = TerminalInformation;
  2326. FTerminal->OnQueryUser = TerminalQueryUser;
  2327. FTerminal->OnPromptUser = TerminalPromptUser;
  2328. FTerminal->OnShowExtendedException = TerminalShowExtendedException;
  2329. FTerminal->OnDisplayBanner = TerminalDisplayBanner;
  2330. FTerminal->OnChangeDirectory = TerminalChangeDirectory;
  2331. FTerminal->OnReadDirectory = TerminalReadDirectory;
  2332. FTerminal->OnStartReadDirectory = TerminalStartReadDirectory;
  2333. FTerminal->OnReadDirectoryProgress = TerminalReadDirectoryProgress;
  2334. FTerminal->OnInitializeLog = TerminalInitializeLog;
  2335. Start();
  2336. }
  2337. //---------------------------------------------------------------------------
  2338. __fastcall TTerminalThread::~TTerminalThread()
  2339. {
  2340. Close();
  2341. CloseHandle(FActionEvent);
  2342. DebugAssert(FTerminal->OnInformation == TerminalInformation);
  2343. DebugAssert(FTerminal->OnQueryUser == TerminalQueryUser);
  2344. DebugAssert(FTerminal->OnPromptUser == TerminalPromptUser);
  2345. DebugAssert(FTerminal->OnShowExtendedException == TerminalShowExtendedException);
  2346. DebugAssert(FTerminal->OnDisplayBanner == TerminalDisplayBanner);
  2347. DebugAssert(FTerminal->OnChangeDirectory == TerminalChangeDirectory);
  2348. DebugAssert(FTerminal->OnReadDirectory == TerminalReadDirectory);
  2349. DebugAssert(FTerminal->OnStartReadDirectory == TerminalStartReadDirectory);
  2350. DebugAssert(FTerminal->OnReadDirectoryProgress == TerminalReadDirectoryProgress);
  2351. DebugAssert(FTerminal->OnInitializeLog == TerminalInitializeLog);
  2352. FTerminal->OnInformation = FOnInformation;
  2353. FTerminal->OnQueryUser = FOnQueryUser;
  2354. FTerminal->OnPromptUser = FOnPromptUser;
  2355. FTerminal->OnShowExtendedException = FOnShowExtendedException;
  2356. FTerminal->OnDisplayBanner = FOnDisplayBanner;
  2357. FTerminal->OnChangeDirectory = FOnChangeDirectory;
  2358. FTerminal->OnReadDirectory = FOnReadDirectory;
  2359. FTerminal->OnStartReadDirectory = FOnStartReadDirectory;
  2360. FTerminal->OnReadDirectoryProgress = FOnReadDirectoryProgress;
  2361. FTerminal->OnInitializeLog = FOnInitializeLog;
  2362. while (!FNonInteractiveInformation.empty())
  2363. {
  2364. delete FNonInteractiveInformation.front();
  2365. FNonInteractiveInformation.pop_front();
  2366. }
  2367. delete FSection;
  2368. if (FAbandoned)
  2369. {
  2370. delete FTerminal;
  2371. }
  2372. }
  2373. //---------------------------------------------------------------------------
  2374. void __fastcall TTerminalThread::Cancel()
  2375. {
  2376. FCancel = true;
  2377. FCancelAfter = IncMilliSecond(Now(), 1000);
  2378. }
  2379. //---------------------------------------------------------------------------
  2380. void __fastcall TTerminalThread::Idle()
  2381. {
  2382. // If user action is needed during Idle() call from TTerminalThread::WaitForUserAction
  2383. // (e.g. when disconnect is detected and session get reconnected)
  2384. // unconditional Enter() here would deadlock.
  2385. if (FSection->TryEnter())
  2386. {
  2387. try
  2388. {
  2389. // only when running user action already,
  2390. // so that the exception is caught, saved and actually
  2391. // passed back into the terminal thread, saved again
  2392. // and passed back to us
  2393. if ((FUserAction != NULL) && (FIdleException != NULL))
  2394. {
  2395. Rethrow(FIdleException);
  2396. }
  2397. FPendingIdle = true;
  2398. }
  2399. __finally
  2400. {
  2401. FSection->Release();
  2402. }
  2403. }
  2404. }
  2405. //---------------------------------------------------------------------------
  2406. void TTerminalThread::StartTerminalReopenNonInteractive()
  2407. {
  2408. FNonInteractive = true;
  2409. StartAction(TerminalReopenEvent);
  2410. }
  2411. //---------------------------------------------------------------------------
  2412. TTerminalReopenResult TTerminalThread::IsTerminalReopenComplete()
  2413. {
  2414. DebugAssert(!FAbandoned);
  2415. TTerminalReopenResult Result;
  2416. if (WaitForActionEvent(0))
  2417. {
  2418. if (FNeedsInteraction)
  2419. {
  2420. Result = trrNeedsInteraction;
  2421. }
  2422. else if (FException != NULL)
  2423. {
  2424. Result = trrFailed;
  2425. }
  2426. else
  2427. {
  2428. Result = trrSucceeded;
  2429. }
  2430. }
  2431. else
  2432. {
  2433. Result = trrPending;
  2434. }
  2435. if (Result != trrPending)
  2436. {
  2437. DiscardException();
  2438. }
  2439. DebugAssert(FUserAction == NULL);
  2440. DebugAssert((!FCancel && !FCancelled) || (Result == trrNeedsInteraction));
  2441. return Result;
  2442. }
  2443. //---------------------------------------------------------------------------
  2444. void TTerminalThread::ContinueTerminalReopenInteractive()
  2445. {
  2446. FNonInteractive = false;
  2447. while (!FNonInteractiveInformation.empty())
  2448. {
  2449. std::unique_ptr<TInformationUserAction> Action(FNonInteractiveInformation.front());
  2450. FNonInteractiveInformation.pop_front();
  2451. Action->Execute(NULL);
  2452. }
  2453. CompleteAction();
  2454. }
  2455. //---------------------------------------------------------------------------
  2456. void __fastcall TTerminalThread::TerminalOpen()
  2457. {
  2458. RunAction(TerminalOpenEvent);
  2459. }
  2460. //---------------------------------------------------------------------------
  2461. void __fastcall TTerminalThread::TerminalReopen()
  2462. {
  2463. RunAction(TerminalReopenEvent);
  2464. }
  2465. //---------------------------------------------------------------------------
  2466. void TTerminalThread::DiscardException()
  2467. {
  2468. SAFE_DESTROY(FException);
  2469. }
  2470. //---------------------------------------------------------------------------
  2471. void TTerminalThread::StartAction(TNotifyEvent Action)
  2472. {
  2473. DebugAssert(FAction == NULL);
  2474. DebugAssert(FException == NULL);
  2475. DebugAssert(FIdleException == NULL);
  2476. FCancelled = false;
  2477. FAction = Action;
  2478. FNeedsInteraction = false;
  2479. TriggerEvent();
  2480. }
  2481. //---------------------------------------------------------------------------
  2482. bool TTerminalThread::WaitForActionEvent(unsigned int Wait)
  2483. {
  2484. switch (WaitForSingleObject(FActionEvent, Wait))
  2485. {
  2486. case WAIT_OBJECT_0:
  2487. return true;
  2488. case WAIT_TIMEOUT:
  2489. return false;
  2490. default:
  2491. throw Exception(L"Error waiting for background session task to complete");
  2492. }
  2493. }
  2494. //---------------------------------------------------------------------------
  2495. bool TTerminalThread::Abandoned()
  2496. {
  2497. bool Result = false;
  2498. if (AllowAbandon && FCancel && (Now() >= FCancelAfter))
  2499. {
  2500. TGuard Guard(FSection);
  2501. if (WaitForSingleObject(FActionEvent, 0) != WAIT_OBJECT_0)
  2502. {
  2503. FAbandoned = true;
  2504. FCancelled = true;
  2505. Result = true;
  2506. }
  2507. }
  2508. return Result;
  2509. }
  2510. //---------------------------------------------------------------------------
  2511. void TTerminalThread::RunAction(TNotifyEvent Action)
  2512. {
  2513. FNonInteractive = false;
  2514. StartAction(Action);
  2515. CompleteAction();
  2516. }
  2517. //---------------------------------------------------------------------------
  2518. void TTerminalThread::CompleteAction()
  2519. {
  2520. DebugAssert(FOnIdle != NULL);
  2521. try
  2522. {
  2523. try
  2524. {
  2525. bool Done = false;
  2526. const unsigned int MaxWait = 50;
  2527. unsigned int Wait = MaxWait;
  2528. do
  2529. {
  2530. if (WaitForActionEvent(Wait))
  2531. {
  2532. Done = true;
  2533. }
  2534. else
  2535. {
  2536. if (FUserAction != NULL)
  2537. {
  2538. try
  2539. {
  2540. FUserAction->Execute(NULL);
  2541. }
  2542. catch (Exception & E)
  2543. {
  2544. SaveException(E, FException);
  2545. }
  2546. FUserAction = NULL;
  2547. TriggerEvent();
  2548. Wait = 0;
  2549. }
  2550. else
  2551. {
  2552. if (FOnIdle != NULL)
  2553. {
  2554. FOnIdle(NULL);
  2555. }
  2556. Wait = std::min(Wait + 10, MaxWait);
  2557. }
  2558. }
  2559. if (!Done && Abandoned())
  2560. {
  2561. FatalAbort();
  2562. }
  2563. }
  2564. while (!Done);
  2565. if (Done)
  2566. {
  2567. Rethrow(FException);
  2568. }
  2569. }
  2570. __finally
  2571. {
  2572. FAction = NULL;
  2573. DiscardException();
  2574. }
  2575. }
  2576. catch(...)
  2577. {
  2578. if (FCancelled)
  2579. {
  2580. // even if the abort thrown as result of Cancel() was wrapped into
  2581. // some higher-level exception, normalize back to message-less fatal
  2582. // exception here
  2583. FatalAbort();
  2584. }
  2585. else
  2586. {
  2587. throw;
  2588. }
  2589. }
  2590. }
  2591. //---------------------------------------------------------------------------
  2592. void __fastcall TTerminalThread::TerminalOpenEvent(TObject * /*Sender*/)
  2593. {
  2594. FTerminal->Open();
  2595. }
  2596. //---------------------------------------------------------------------------
  2597. void __fastcall TTerminalThread::TerminalReopenEvent(TObject * /*Sender*/)
  2598. {
  2599. FTerminal->Reopen(0);
  2600. }
  2601. //---------------------------------------------------------------------------
  2602. void __fastcall TTerminalThread::ProcessEvent()
  2603. {
  2604. DebugAssert(FEvent != NULL);
  2605. DebugAssert(FException == NULL);
  2606. // Needed at least for TXMLDocument use in TS3FileSystem
  2607. CoInitialize(NULL);
  2608. try
  2609. {
  2610. FAction(NULL);
  2611. }
  2612. catch(Exception & E)
  2613. {
  2614. SaveException(E, FException);
  2615. }
  2616. {
  2617. TGuard Guard(FSection);
  2618. if (!FAbandoned)
  2619. {
  2620. SetEvent(FActionEvent);
  2621. }
  2622. }
  2623. }
  2624. //---------------------------------------------------------------------------
  2625. void __fastcall TTerminalThread::Rethrow(Exception *& Exception)
  2626. {
  2627. if (Exception != NULL)
  2628. {
  2629. try
  2630. {
  2631. RethrowException(Exception);
  2632. }
  2633. __finally
  2634. {
  2635. SAFE_DESTROY(Exception);
  2636. }
  2637. }
  2638. }
  2639. //---------------------------------------------------------------------------
  2640. void __fastcall TTerminalThread::SaveException(Exception & E, Exception *& Exception)
  2641. {
  2642. DebugAssert(Exception == NULL);
  2643. Exception = CloneException(&E);
  2644. }
  2645. //---------------------------------------------------------------------------
  2646. void __fastcall TTerminalThread::FatalAbort()
  2647. {
  2648. if (FAbandoned)
  2649. {
  2650. // We cannot use TTerminal::FatalError as the terminal still runs on a background thread,
  2651. // may have its TCallbackGuard armed right now.
  2652. throw ESshFatal(NULL, L"");
  2653. }
  2654. else
  2655. {
  2656. FTerminal->FatalAbort();
  2657. }
  2658. }
  2659. //---------------------------------------------------------------------------
  2660. void __fastcall TTerminalThread::CheckCancel()
  2661. {
  2662. // Should we really fatally abort when e.g. just cancelling password prompt?
  2663. if (FCancel && !FCancelled)
  2664. {
  2665. FCancelled = true;
  2666. FatalAbort();
  2667. }
  2668. }
  2669. //---------------------------------------------------------------------------
  2670. void __fastcall TTerminalThread::WaitForUserAction(TUserAction * UserAction)
  2671. {
  2672. DebugAssert(!FNonInteractive);
  2673. DWORD Thread = GetCurrentThreadId();
  2674. // we can get called from the main thread from within Idle,
  2675. // should be only to call HandleExtendedException
  2676. if (Thread == FMainThread)
  2677. {
  2678. if (UserAction != NULL)
  2679. {
  2680. UserAction->Execute(NULL);
  2681. }
  2682. }
  2683. else
  2684. {
  2685. // we should be called from our thread only,
  2686. // with exception noted above
  2687. DebugAssert(Thread == FThreadId);
  2688. bool DoCheckCancel =
  2689. DebugAlwaysFalse(UserAction == NULL) || !UserAction->Force() || FAbandoned;
  2690. if (DoCheckCancel)
  2691. {
  2692. CheckCancel();
  2693. }
  2694. // have to save it as we can go recursive via TQueryParams::TimerEvent,
  2695. // see TTerminalThread::TerminalQueryUser
  2696. TUserAction * PrevUserAction = FUserAction;
  2697. try
  2698. {
  2699. FUserAction = UserAction;
  2700. while (true)
  2701. {
  2702. {
  2703. TGuard Guard(FSection);
  2704. // If idle exception is already set, we are only waiting
  2705. // for the main thread to pick it up
  2706. // (or at least to finish handling the user action, so
  2707. // that we rethrow the idle exception below)
  2708. // Also if idle exception is set, it is probable that terminal
  2709. // is not active anyway.
  2710. if (FTerminal->Active && FPendingIdle && (FIdleException == NULL))
  2711. {
  2712. FPendingIdle = false;
  2713. try
  2714. {
  2715. FTerminal->Idle();
  2716. }
  2717. catch (Exception & E)
  2718. {
  2719. SaveException(E, FIdleException);
  2720. }
  2721. }
  2722. }
  2723. int WaitResult = WaitForEvent(1000);
  2724. if (WaitResult == 0)
  2725. {
  2726. SAFE_DESTROY(FIdleException);
  2727. FatalAbort();
  2728. }
  2729. else if (WaitResult > 0)
  2730. {
  2731. break;
  2732. }
  2733. }
  2734. Rethrow(FException);
  2735. if (FIdleException != NULL)
  2736. {
  2737. // idle exception was not used to cancel the user action
  2738. // (if it where it would be already cloned into the FException above
  2739. // and rethrown)
  2740. Rethrow(FIdleException);
  2741. }
  2742. }
  2743. __finally
  2744. {
  2745. FUserAction = PrevUserAction;
  2746. DiscardException();
  2747. }
  2748. // Contrary to a call before, this is unconditional,
  2749. // otherwise cancelling authentication won't work,
  2750. // if it is tried only after the last user action
  2751. // (what is common, when cancelling while waiting for
  2752. // resolving of unresolvable host name, where the last user action is
  2753. // "resolving hostname" information action)
  2754. CheckCancel();
  2755. }
  2756. }
  2757. //---------------------------------------------------------------------------
  2758. void __fastcall TTerminalThread::TerminalInformation(
  2759. TTerminal * Terminal, const UnicodeString & Str, int Phase, const UnicodeString & Additional)
  2760. {
  2761. std::unique_ptr<TInformationUserAction> Action(new TInformationUserAction(FOnInformation));
  2762. Action->Terminal = Terminal;
  2763. Action->Str = Str;
  2764. Action->Phase = Phase;
  2765. Action->Additional = Additional;
  2766. if (!FNonInteractive)
  2767. {
  2768. WaitForUserAction(Action.get());
  2769. }
  2770. else
  2771. {
  2772. FNonInteractiveInformation.push_back(Action.release());
  2773. }
  2774. }
  2775. //---------------------------------------------------------------------------
  2776. void TTerminalThread::NeedsInteraction()
  2777. {
  2778. FNeedsInteraction = true;
  2779. Cancel();
  2780. }
  2781. //---------------------------------------------------------------------------
  2782. void __fastcall TTerminalThread::TerminalQueryUser(TObject * Sender,
  2783. const UnicodeString Query, TStrings * MoreMessages, unsigned int Answers,
  2784. const TQueryParams * Params, unsigned int & Answer, TQueryType Type, void * Arg)
  2785. {
  2786. DebugUsedParam(Arg);
  2787. DebugAssert(Arg == NULL);
  2788. if (FNonInteractive)
  2789. {
  2790. NeedsInteraction();
  2791. Answer = AbortAnswer(Answers);
  2792. }
  2793. else
  2794. {
  2795. // note about TQueryParams::TimerEvent
  2796. // So far there is only one use for this, the TSecureShell::SendBuffer,
  2797. // which should be thread-safe, as long as the terminal thread,
  2798. // is stopped waiting for OnQueryUser to finish.
  2799. // note about TQueryButtonAlias::OnClick
  2800. // So far there is only one use for this, the TClipboardHandler,
  2801. // which is thread-safe.
  2802. TQueryUserAction Action(FOnQueryUser);
  2803. Action.Sender = Sender;
  2804. Action.Query = Query;
  2805. Action.MoreMessages = MoreMessages;
  2806. Action.Answers = Answers;
  2807. Action.Params = Params;
  2808. Action.Answer = Answer;
  2809. Action.Type = Type;
  2810. WaitForUserAction(&Action);
  2811. Answer = Action.Answer;
  2812. }
  2813. }
  2814. //---------------------------------------------------------------------------
  2815. void __fastcall TTerminalThread::TerminalInitializeLog(TObject * Sender)
  2816. {
  2817. // never used, so not tested either
  2818. if (DebugAlwaysFalse(FOnInitializeLog != NULL) && DebugAlwaysTrue(!FNonInteractive))
  2819. {
  2820. TNotifyAction Action(FOnInitializeLog);
  2821. Action.Sender = Sender;
  2822. WaitForUserAction(&Action);
  2823. }
  2824. }
  2825. //---------------------------------------------------------------------------
  2826. void __fastcall TTerminalThread::TerminalPromptUser(TTerminal * Terminal,
  2827. TPromptKind Kind, UnicodeString Name, UnicodeString Instructions, TStrings * Prompts,
  2828. TStrings * Results, bool & Result, void * Arg)
  2829. {
  2830. DebugUsedParam(Arg);
  2831. if (FNonInteractive)
  2832. {
  2833. NeedsInteraction();
  2834. Result = false;
  2835. }
  2836. else
  2837. {
  2838. DebugAssert(Arg == NULL);
  2839. TPromptUserAction Action(FOnPromptUser);
  2840. Action.Terminal = Terminal;
  2841. Action.Kind = Kind;
  2842. Action.Name = Name;
  2843. Action.Instructions = Instructions;
  2844. Action.Prompts = Prompts;
  2845. Action.Results->AddStrings(Results);
  2846. WaitForUserAction(&Action);
  2847. Results->Clear();
  2848. Results->AddStrings(Action.Results);
  2849. Result = Action.Result;
  2850. }
  2851. }
  2852. //---------------------------------------------------------------------------
  2853. void __fastcall TTerminalThread::TerminalShowExtendedException(
  2854. TTerminal * Terminal, Exception * E, void * Arg)
  2855. {
  2856. DebugUsedParam(Arg);
  2857. if (FNonInteractive)
  2858. {
  2859. NeedsInteraction();
  2860. }
  2861. else
  2862. {
  2863. DebugAssert(Arg == NULL);
  2864. TShowExtendedExceptionAction Action(FOnShowExtendedException);
  2865. Action.Terminal = Terminal;
  2866. Action.E = E;
  2867. WaitForUserAction(&Action);
  2868. }
  2869. }
  2870. //---------------------------------------------------------------------------
  2871. void __fastcall TTerminalThread::TerminalDisplayBanner(TTerminal * Terminal,
  2872. UnicodeString SessionName, const UnicodeString & Banner,
  2873. bool & NeverShowAgain, int Options, unsigned int & Params)
  2874. {
  2875. if (!FNonInteractive)
  2876. {
  2877. TDisplayBannerAction Action(FOnDisplayBanner);
  2878. Action.Terminal = Terminal;
  2879. Action.SessionName = SessionName;
  2880. Action.Banner = Banner;
  2881. Action.NeverShowAgain = NeverShowAgain;
  2882. Action.Options = Options;
  2883. Action.Params = Params;
  2884. WaitForUserAction(&Action);
  2885. NeverShowAgain = Action.NeverShowAgain;
  2886. Params = Action.Params;
  2887. }
  2888. }
  2889. //---------------------------------------------------------------------------
  2890. void __fastcall TTerminalThread::TerminalChangeDirectory(TObject * Sender)
  2891. {
  2892. if (!FNonInteractive)
  2893. {
  2894. TNotifyAction Action(FOnChangeDirectory);
  2895. Action.Sender = Sender;
  2896. WaitForUserAction(&Action);
  2897. }
  2898. }
  2899. //---------------------------------------------------------------------------
  2900. void __fastcall TTerminalThread::TerminalReadDirectory(TObject * Sender, Boolean ReloadOnly)
  2901. {
  2902. if (!FNonInteractive)
  2903. {
  2904. TReadDirectoryAction Action(FOnReadDirectory);
  2905. Action.Sender = Sender;
  2906. Action.ReloadOnly = ReloadOnly;
  2907. WaitForUserAction(&Action);
  2908. }
  2909. }
  2910. //---------------------------------------------------------------------------
  2911. void __fastcall TTerminalThread::TerminalStartReadDirectory(TObject * Sender)
  2912. {
  2913. if (!FNonInteractive)
  2914. {
  2915. TNotifyAction Action(FOnStartReadDirectory);
  2916. Action.Sender = Sender;
  2917. WaitForUserAction(&Action);
  2918. }
  2919. }
  2920. //---------------------------------------------------------------------------
  2921. void __fastcall TTerminalThread::TerminalReadDirectoryProgress(
  2922. TObject * Sender, int Progress, int ResolvedLinks, bool & Cancel)
  2923. {
  2924. if (!FNonInteractive)
  2925. {
  2926. TReadDirectoryProgressAction Action(FOnReadDirectoryProgress);
  2927. Action.Sender = Sender;
  2928. Action.Progress = Progress;
  2929. Action.ResolvedLinks = ResolvedLinks;
  2930. Action.Cancel = Cancel;
  2931. WaitForUserAction(&Action);
  2932. Cancel = Action.Cancel;
  2933. }
  2934. }
  2935. //---------------------------------------------------------------------------
  2936. bool __fastcall TTerminalThread::Release()
  2937. {
  2938. bool Result = !FAbandoned;
  2939. if (Result)
  2940. {
  2941. delete this;
  2942. }
  2943. return Result;
  2944. }
  2945. //---------------------------------------------------------------------------
  2946. bool __fastcall TTerminalThread::Finished()
  2947. {
  2948. return TSimpleThread::Finished() || FAbandoned;
  2949. }
  2950. //---------------------------------------------------------------------------
  2951. //---------------------------------------------------------------------------
  2952. TQueueFileList::TQueueFileList() :
  2953. FList(new TStringList())
  2954. {
  2955. Clear();
  2956. }
  2957. //---------------------------------------------------------------------------
  2958. void TQueueFileList::Clear()
  2959. {
  2960. FList->Clear();
  2961. FLastParallelOperation = NULL;
  2962. FLastParallelOperationVersion = -1;
  2963. }
  2964. //---------------------------------------------------------------------------
  2965. void TQueueFileList::Add(const UnicodeString & FileName, int State)
  2966. {
  2967. FList->AddObject(FileName, reinterpret_cast<TObject *>(State));
  2968. }
  2969. //---------------------------------------------------------------------------
  2970. UnicodeString TQueueFileList::GetFileName(int Index) const
  2971. {
  2972. return FList->Strings[Index];
  2973. }
  2974. //---------------------------------------------------------------------------
  2975. int TQueueFileList::GetState(int Index) const
  2976. {
  2977. return reinterpret_cast<int>(FList->Objects[Index]);
  2978. }
  2979. //---------------------------------------------------------------------------
  2980. void TQueueFileList::SetState(int Index, int State)
  2981. {
  2982. FList->Objects[Index] = reinterpret_cast<TObject *>(State);
  2983. }
  2984. //---------------------------------------------------------------------------
  2985. int TQueueFileList::GetCount() const
  2986. {
  2987. return FList->Count;
  2988. }