Queue.cpp 72 KB

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