Queue.cpp 80 KB

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