Queue.cpp 78 KB

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