Queue.cpp 79 KB

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