Queue.cpp 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791
  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. FActivePrimaryCount = -1;
  1720. FActiveAndPendingPrimaryCount = -1;
  1721. }
  1722. //---------------------------------------------------------------------------
  1723. void __fastcall TTerminalQueueStatus::SetDoneCount(int Value)
  1724. {
  1725. FDoneCount = Value;
  1726. ResetStats();
  1727. }
  1728. //---------------------------------------------------------------------------
  1729. void __fastcall TTerminalQueueStatus::NeedStats()
  1730. {
  1731. if (FActiveCount < 0)
  1732. {
  1733. FActiveCount = 0;
  1734. FActivePrimaryCount = 0;
  1735. FActiveAndPendingPrimaryCount = 0;
  1736. for (int Index = DoneCount; Index < Count; Index++)
  1737. {
  1738. bool Primary = GetItem(Index)->Info->Primary;
  1739. if (GetItem(Index)->Status != TQueueItem::qsPending)
  1740. {
  1741. FActiveCount++;
  1742. if (Primary)
  1743. {
  1744. FActivePrimaryCount++;
  1745. }
  1746. }
  1747. if (Primary)
  1748. {
  1749. FActiveAndPendingPrimaryCount++;
  1750. }
  1751. }
  1752. }
  1753. }
  1754. //---------------------------------------------------------------------------
  1755. int __fastcall TTerminalQueueStatus::GetActiveCount()
  1756. {
  1757. NeedStats();
  1758. return FActiveCount;
  1759. }
  1760. //---------------------------------------------------------------------------
  1761. int __fastcall TTerminalQueueStatus::GetDoneAndActiveCount()
  1762. {
  1763. return DoneCount + ActiveCount;
  1764. }
  1765. //---------------------------------------------------------------------------
  1766. int __fastcall TTerminalQueueStatus::GetActivePrimaryCount()
  1767. {
  1768. NeedStats();
  1769. return FActivePrimaryCount;
  1770. }
  1771. //---------------------------------------------------------------------------
  1772. bool __fastcall TTerminalQueueStatus::IsOnlyOneActiveAndNoPending()
  1773. {
  1774. return (ActivePrimaryCount == 1) && (ActiveAndPendingPrimaryCount == 1);
  1775. }
  1776. //---------------------------------------------------------------------------
  1777. int __fastcall TTerminalQueueStatus::GetActiveAndPendingPrimaryCount()
  1778. {
  1779. NeedStats();
  1780. return FActiveAndPendingPrimaryCount;
  1781. }
  1782. //---------------------------------------------------------------------------
  1783. void __fastcall TTerminalQueueStatus::Add(TQueueItemProxy * ItemProxy)
  1784. {
  1785. ItemProxy->FQueueStatus = this;
  1786. int Index = FList->Count;
  1787. if (!ItemProxy->Info->Primary)
  1788. {
  1789. for (int I = 0; I < FList->Count; I++)
  1790. {
  1791. if (Items[I]->Info->GroupToken == ItemProxy->Info->GroupToken)
  1792. {
  1793. Index = I + 1;
  1794. }
  1795. }
  1796. DebugAssert(Index >= 0);
  1797. }
  1798. FList->Insert(Index, ItemProxy);
  1799. ResetStats();
  1800. }
  1801. //---------------------------------------------------------------------------
  1802. void __fastcall TTerminalQueueStatus::Delete(TQueueItemProxy * ItemProxy)
  1803. {
  1804. FList->Extract(ItemProxy);
  1805. ItemProxy->FQueueStatus = NULL;
  1806. ResetStats();
  1807. }
  1808. //---------------------------------------------------------------------------
  1809. int __fastcall TTerminalQueueStatus::GetCount()
  1810. {
  1811. return FList->Count;
  1812. }
  1813. //---------------------------------------------------------------------------
  1814. TQueueItemProxy * __fastcall TTerminalQueueStatus::GetItem(int Index)
  1815. {
  1816. return reinterpret_cast<TQueueItemProxy *>(FList->Items[Index]);
  1817. }
  1818. //---------------------------------------------------------------------------
  1819. TQueueItemProxy * __fastcall TTerminalQueueStatus::FindByQueueItem(
  1820. TQueueItem * QueueItem)
  1821. {
  1822. TQueueItemProxy * Item;
  1823. for (int Index = 0; Index < FList->Count; Index++)
  1824. {
  1825. Item = GetItem(Index);
  1826. if (Item->FQueueItem == QueueItem)
  1827. {
  1828. return Item;
  1829. }
  1830. }
  1831. return NULL;
  1832. }
  1833. //---------------------------------------------------------------------------
  1834. // TLocatedQueueItem
  1835. //---------------------------------------------------------------------------
  1836. __fastcall TLocatedQueueItem::TLocatedQueueItem(TTerminal * Terminal) :
  1837. TQueueItem()
  1838. {
  1839. DebugAssert(Terminal != NULL);
  1840. FCurrentDir = Terminal->CurrentDirectory;
  1841. }
  1842. //---------------------------------------------------------------------------
  1843. __fastcall TLocatedQueueItem::TLocatedQueueItem(const TLocatedQueueItem & Source) :
  1844. TQueueItem()
  1845. {
  1846. FCurrentDir = Source.FCurrentDir;
  1847. }
  1848. //---------------------------------------------------------------------------
  1849. UnicodeString __fastcall TLocatedQueueItem::StartupDirectory() const
  1850. {
  1851. return FCurrentDir;
  1852. }
  1853. //---------------------------------------------------------------------------
  1854. void __fastcall TLocatedQueueItem::DoExecute(TTerminal * Terminal)
  1855. {
  1856. DebugAssert(Terminal != NULL);
  1857. Terminal->CurrentDirectory = FCurrentDir;
  1858. }
  1859. //---------------------------------------------------------------------------
  1860. // TTransferQueueItem
  1861. //---------------------------------------------------------------------------
  1862. __fastcall TTransferQueueItem::TTransferQueueItem(TTerminal * Terminal,
  1863. TStrings * FilesToCopy, const UnicodeString & TargetDir,
  1864. const TCopyParamType * CopyParam, int Params, TOperationSide Side,
  1865. bool SingleFile, bool Parallel) :
  1866. TLocatedQueueItem(Terminal), FFilesToCopy(NULL), FCopyParam(NULL)
  1867. {
  1868. FInfo->Operation = (Params & cpDelete ? foMove : foCopy);
  1869. FInfo->Side = Side;
  1870. FInfo->SingleFile = SingleFile;
  1871. DebugAssert(FilesToCopy != NULL);
  1872. FFilesToCopy = new TStringList();
  1873. for (int Index = 0; Index < FilesToCopy->Count; Index++)
  1874. {
  1875. FFilesToCopy->AddObject(FilesToCopy->Strings[Index],
  1876. ((FilesToCopy->Objects[Index] == NULL) || (Side == osLocal)) ? NULL :
  1877. dynamic_cast<TRemoteFile*>(FilesToCopy->Objects[Index])->Duplicate());
  1878. }
  1879. FTargetDir = TargetDir;
  1880. DebugAssert(CopyParam != NULL);
  1881. FCopyParam = new TCopyParamType(*CopyParam);
  1882. FParams = Params;
  1883. FParallel = Parallel;
  1884. FLastParallelOperationAdded = GetTickCount();
  1885. }
  1886. //---------------------------------------------------------------------------
  1887. __fastcall TTransferQueueItem::~TTransferQueueItem()
  1888. {
  1889. for (int Index = 0; Index < FFilesToCopy->Count; Index++)
  1890. {
  1891. delete FFilesToCopy->Objects[Index];
  1892. }
  1893. delete FFilesToCopy;
  1894. delete FCopyParam;
  1895. }
  1896. //---------------------------------------------------------------------------
  1897. unsigned long __fastcall TTransferQueueItem::DefaultCPSLimit()
  1898. {
  1899. return FCopyParam->CPSLimit;
  1900. }
  1901. //---------------------------------------------------------------------------
  1902. void __fastcall TTransferQueueItem::DoExecute(TTerminal * Terminal)
  1903. {
  1904. TLocatedQueueItem::DoExecute(Terminal);
  1905. DebugAssert(Terminal != NULL);
  1906. TParallelOperation ParallelOperation(FInfo->Side);
  1907. FParallelOperation = &ParallelOperation;
  1908. try
  1909. {
  1910. DoTransferExecute(Terminal, FParallelOperation);
  1911. }
  1912. __finally
  1913. {
  1914. FParallelOperation = NULL;
  1915. }
  1916. }
  1917. //---------------------------------------------------------------------------
  1918. void __fastcall TTransferQueueItem::ProgressUpdated()
  1919. {
  1920. TLocatedQueueItem::ProgressUpdated();
  1921. if (FParallel)
  1922. {
  1923. bool Add = false;
  1924. bool Force = false;
  1925. DWORD LastParallelOperationAddedPrev = 0;
  1926. {
  1927. TGuard Guard(FSection);
  1928. // Move this to TTransferQueueItem once implemented for downloads too
  1929. DebugAssert(FParallelOperation != NULL);
  1930. DebugAssert((FProgressData->Operation == foCopy) || (FProgressData->Operation == foCalculateSize));
  1931. if (FProgressData->Operation == foCopy)
  1932. {
  1933. Add = FParallelOperation->ShouldAddClient();
  1934. if (Add)
  1935. {
  1936. DWORD Now = GetTickCount();
  1937. Force =
  1938. (Now - FLastParallelOperationAdded >= 5*1000) &&
  1939. (TimeToSeconds(FProgressData->TotalTimeLeft()) >= 20);
  1940. LastParallelOperationAddedPrev = FLastParallelOperationAdded;
  1941. // update now already to prevent race condition, but we will have to rollback it back,
  1942. // if we actually do not add the parallel operation
  1943. FLastParallelOperationAdded = Now;
  1944. }
  1945. }
  1946. }
  1947. if (Add)
  1948. {
  1949. if (!FQueue->TryAddParallelOperation(this, Force))
  1950. {
  1951. TGuard Guard(FSection);
  1952. FLastParallelOperationAdded = LastParallelOperationAddedPrev;
  1953. }
  1954. }
  1955. }
  1956. }
  1957. //---------------------------------------------------------------------------
  1958. TQueueItem * __fastcall TTransferQueueItem::CreateParallelOperation()
  1959. {
  1960. DebugAssert(FParallelOperation != NULL);
  1961. FParallelOperation->AddClient();
  1962. return new TParallelTransferQueueItem(this, FParallelOperation);
  1963. }
  1964. //---------------------------------------------------------------------------
  1965. // TUploadQueueItem
  1966. //---------------------------------------------------------------------------
  1967. __fastcall TUploadQueueItem::TUploadQueueItem(TTerminal * Terminal,
  1968. TStrings * FilesToCopy, const UnicodeString & TargetDir,
  1969. const TCopyParamType * CopyParam, int Params, bool SingleFile, bool Parallel) :
  1970. TTransferQueueItem(Terminal, FilesToCopy, TargetDir, CopyParam, Params, osLocal, SingleFile, Parallel)
  1971. {
  1972. if (FilesToCopy->Count > 1)
  1973. {
  1974. if (FLAGSET(Params, cpTemporary))
  1975. {
  1976. FInfo->Source = L"";
  1977. FInfo->ModifiedLocal = L"";
  1978. }
  1979. else
  1980. {
  1981. ExtractCommonPath(FilesToCopy, FInfo->Source);
  1982. // this way the trailing backslash is preserved for root directories like D:\\
  1983. FInfo->Source = ExtractFileDir(IncludeTrailingBackslash(FInfo->Source));
  1984. FInfo->ModifiedLocal = FLAGCLEAR(Params, cpDelete) ? UnicodeString() :
  1985. IncludeTrailingBackslash(FInfo->Source);
  1986. }
  1987. }
  1988. else
  1989. {
  1990. if (FLAGSET(Params, cpTemporary))
  1991. {
  1992. FInfo->Source = ExtractFileName(FilesToCopy->Strings[0]);
  1993. FInfo->ModifiedLocal = L"";
  1994. }
  1995. else
  1996. {
  1997. DebugAssert(FilesToCopy->Count > 0);
  1998. FInfo->Source = FilesToCopy->Strings[0];
  1999. FInfo->ModifiedLocal = FLAGCLEAR(Params, cpDelete) ? UnicodeString() :
  2000. IncludeTrailingBackslash(ExtractFilePath(FInfo->Source));
  2001. }
  2002. }
  2003. FInfo->Destination =
  2004. UnixIncludeTrailingBackslash(TargetDir) + CopyParam->FileMask;
  2005. FInfo->ModifiedRemote = UnixIncludeTrailingBackslash(TargetDir);
  2006. }
  2007. //---------------------------------------------------------------------------
  2008. void __fastcall TUploadQueueItem::DoTransferExecute(TTerminal * Terminal, TParallelOperation * ParallelOperation)
  2009. {
  2010. Terminal->CopyToRemote(FFilesToCopy, FTargetDir, FCopyParam, FParams, ParallelOperation);
  2011. }
  2012. //---------------------------------------------------------------------------
  2013. // TParallelTransferQueueItem
  2014. //---------------------------------------------------------------------------
  2015. __fastcall TParallelTransferQueueItem::TParallelTransferQueueItem(
  2016. const TLocatedQueueItem * ParentItem, TParallelOperation * ParallelOperation) :
  2017. TLocatedQueueItem(*ParentItem),
  2018. FParallelOperation(ParallelOperation)
  2019. {
  2020. // deliberately not copying the ModifiedLocal and ModifiedRemote, not to trigger panel refresh, when sub-item completes
  2021. FInfo->Operation = ParentItem->FInfo->Operation;
  2022. FInfo->Side = ParentItem->FInfo->Side;
  2023. FInfo->Source = ParentItem->FInfo->Source;
  2024. FInfo->Destination = ParentItem->FInfo->Destination;
  2025. FInfo->SingleFile = DebugAlwaysFalse(ParentItem->FInfo->SingleFile);
  2026. FInfo->Primary = false;
  2027. FInfo->GroupToken = ParentItem->FInfo->GroupToken;
  2028. }
  2029. //---------------------------------------------------------------------------
  2030. void __fastcall TParallelTransferQueueItem::DoExecute(TTerminal * Terminal)
  2031. {
  2032. TLocatedQueueItem::DoExecute(Terminal);
  2033. TFileOperationProgressType OperationProgress(Terminal->OnProgress, Terminal->OnFinished, FParallelOperation->MainOperationProgress);
  2034. TFileOperation Operation = (FLAGSET(FParallelOperation->Params, cpDelete) ? foMove : foCopy);
  2035. bool Temp = FLAGSET(FParallelOperation->Params, cpTemporary);
  2036. OperationProgress.Start(
  2037. // CPS limit inherited from parent OperationProgress.
  2038. // Count not known and won't be needed as we will always have TotalSize as we always transfer a single file at a time.
  2039. Operation, FParallelOperation->Side, -1, Temp, FParallelOperation->TargetDir, 0);
  2040. try
  2041. {
  2042. bool Continue = true;
  2043. do
  2044. {
  2045. int GotNext = Terminal->CopyToParallel(FParallelOperation, &OperationProgress);
  2046. if (GotNext < 0)
  2047. {
  2048. Continue = false;
  2049. }
  2050. else if (!FQueue->ContinueParallelOperation())
  2051. {
  2052. Continue = false;
  2053. }
  2054. }
  2055. while (Continue);
  2056. }
  2057. __finally
  2058. {
  2059. OperationProgress.Stop();
  2060. FParallelOperation->RemoveClient();
  2061. }
  2062. }
  2063. //---------------------------------------------------------------------------
  2064. // TDownloadQueueItem
  2065. //---------------------------------------------------------------------------
  2066. __fastcall TDownloadQueueItem::TDownloadQueueItem(TTerminal * Terminal,
  2067. TStrings * FilesToCopy, const UnicodeString & TargetDir,
  2068. const TCopyParamType * CopyParam, int Params, bool SingleFile, bool Parallel) :
  2069. TTransferQueueItem(Terminal, FilesToCopy, TargetDir, CopyParam, Params, osRemote, SingleFile, Parallel)
  2070. {
  2071. if (FilesToCopy->Count > 1)
  2072. {
  2073. if (!UnixExtractCommonPath(FilesToCopy, FInfo->Source))
  2074. {
  2075. FInfo->Source = Terminal->CurrentDirectory;
  2076. }
  2077. FInfo->Source = UnixExcludeTrailingBackslash(FInfo->Source);
  2078. FInfo->ModifiedRemote = FLAGCLEAR(Params, cpDelete) ? UnicodeString() :
  2079. UnixIncludeTrailingBackslash(FInfo->Source);
  2080. }
  2081. else
  2082. {
  2083. DebugAssert(FilesToCopy->Count > 0);
  2084. FInfo->Source = FilesToCopy->Strings[0];
  2085. if (UnixExtractFilePath(FInfo->Source).IsEmpty())
  2086. {
  2087. FInfo->Source = UnixIncludeTrailingBackslash(Terminal->CurrentDirectory) +
  2088. FInfo->Source;
  2089. FInfo->ModifiedRemote = FLAGCLEAR(Params, cpDelete) ? UnicodeString() :
  2090. UnixIncludeTrailingBackslash(Terminal->CurrentDirectory);
  2091. }
  2092. else
  2093. {
  2094. FInfo->ModifiedRemote = FLAGCLEAR(Params, cpDelete) ? UnicodeString() :
  2095. UnixExtractFilePath(FInfo->Source);
  2096. }
  2097. }
  2098. if (FLAGSET(Params, cpTemporary))
  2099. {
  2100. FInfo->Destination = L"";
  2101. }
  2102. else
  2103. {
  2104. FInfo->Destination =
  2105. IncludeTrailingBackslash(TargetDir) + CopyParam->FileMask;
  2106. }
  2107. FInfo->ModifiedLocal = IncludeTrailingBackslash(TargetDir);
  2108. }
  2109. //---------------------------------------------------------------------------
  2110. void __fastcall TDownloadQueueItem::DoTransferExecute(TTerminal * Terminal, TParallelOperation * ParallelOperation)
  2111. {
  2112. Terminal->CopyToLocal(FFilesToCopy, FTargetDir, FCopyParam, FParams, ParallelOperation);
  2113. }
  2114. //---------------------------------------------------------------------------
  2115. // TTerminalThread
  2116. //---------------------------------------------------------------------------
  2117. __fastcall TTerminalThread::TTerminalThread(TTerminal * Terminal) :
  2118. TSignalThread(false), FTerminal(Terminal)
  2119. {
  2120. FAction = NULL;
  2121. FActionEvent = CreateEvent(NULL, false, false, NULL);
  2122. FException = NULL;
  2123. FIdleException = NULL;
  2124. FOnIdle = NULL;
  2125. FUserAction = NULL;
  2126. FCancel = false;
  2127. FMainThread = GetCurrentThreadId();
  2128. FSection = new TCriticalSection();
  2129. FOnInformation = FTerminal->OnInformation;
  2130. FOnQueryUser = FTerminal->OnQueryUser;
  2131. FOnPromptUser = FTerminal->OnPromptUser;
  2132. FOnShowExtendedException = FTerminal->OnShowExtendedException;
  2133. FOnDisplayBanner = FTerminal->OnDisplayBanner;
  2134. FOnChangeDirectory = FTerminal->OnChangeDirectory;
  2135. FOnReadDirectory = FTerminal->OnReadDirectory;
  2136. FOnStartReadDirectory = FTerminal->OnStartReadDirectory;
  2137. FOnReadDirectoryProgress = FTerminal->OnReadDirectoryProgress;
  2138. FOnInitializeLog = FTerminal->OnInitializeLog;
  2139. FTerminal->OnInformation = TerminalInformation;
  2140. FTerminal->OnQueryUser = TerminalQueryUser;
  2141. FTerminal->OnPromptUser = TerminalPromptUser;
  2142. FTerminal->OnShowExtendedException = TerminalShowExtendedException;
  2143. FTerminal->OnDisplayBanner = TerminalDisplayBanner;
  2144. FTerminal->OnChangeDirectory = TerminalChangeDirectory;
  2145. FTerminal->OnReadDirectory = TerminalReadDirectory;
  2146. FTerminal->OnStartReadDirectory = TerminalStartReadDirectory;
  2147. FTerminal->OnReadDirectoryProgress = TerminalReadDirectoryProgress;
  2148. FTerminal->OnInitializeLog = TerminalInitializeLog;
  2149. Start();
  2150. }
  2151. //---------------------------------------------------------------------------
  2152. __fastcall TTerminalThread::~TTerminalThread()
  2153. {
  2154. Close();
  2155. CloseHandle(FActionEvent);
  2156. DebugAssert(FTerminal->OnInformation == TerminalInformation);
  2157. DebugAssert(FTerminal->OnQueryUser == TerminalQueryUser);
  2158. DebugAssert(FTerminal->OnPromptUser == TerminalPromptUser);
  2159. DebugAssert(FTerminal->OnShowExtendedException == TerminalShowExtendedException);
  2160. DebugAssert(FTerminal->OnDisplayBanner == TerminalDisplayBanner);
  2161. DebugAssert(FTerminal->OnChangeDirectory == TerminalChangeDirectory);
  2162. DebugAssert(FTerminal->OnReadDirectory == TerminalReadDirectory);
  2163. DebugAssert(FTerminal->OnStartReadDirectory == TerminalStartReadDirectory);
  2164. DebugAssert(FTerminal->OnReadDirectoryProgress == TerminalReadDirectoryProgress);
  2165. DebugAssert(FTerminal->OnInitializeLog == TerminalInitializeLog);
  2166. FTerminal->OnInformation = FOnInformation;
  2167. FTerminal->OnQueryUser = FOnQueryUser;
  2168. FTerminal->OnPromptUser = FOnPromptUser;
  2169. FTerminal->OnShowExtendedException = FOnShowExtendedException;
  2170. FTerminal->OnDisplayBanner = FOnDisplayBanner;
  2171. FTerminal->OnChangeDirectory = FOnChangeDirectory;
  2172. FTerminal->OnReadDirectory = FOnReadDirectory;
  2173. FTerminal->OnStartReadDirectory = FOnStartReadDirectory;
  2174. FTerminal->OnReadDirectoryProgress = FOnReadDirectoryProgress;
  2175. FTerminal->OnInitializeLog = FOnInitializeLog;
  2176. delete FSection;
  2177. }
  2178. //---------------------------------------------------------------------------
  2179. void __fastcall TTerminalThread::Cancel()
  2180. {
  2181. FCancel = true;
  2182. }
  2183. //---------------------------------------------------------------------------
  2184. void __fastcall TTerminalThread::Idle()
  2185. {
  2186. TGuard Guard(FSection);
  2187. // only when running user action already,
  2188. // so that the exception is caught, saved and actually
  2189. // passed back into the terminal thread, saved again
  2190. // and passed back to us
  2191. if ((FUserAction != NULL) && (FIdleException != NULL))
  2192. {
  2193. Rethrow(FIdleException);
  2194. }
  2195. FPendingIdle = true;
  2196. }
  2197. //---------------------------------------------------------------------------
  2198. void __fastcall TTerminalThread::TerminalOpen()
  2199. {
  2200. RunAction(TerminalOpenEvent);
  2201. }
  2202. //---------------------------------------------------------------------------
  2203. void __fastcall TTerminalThread::TerminalReopen()
  2204. {
  2205. RunAction(TerminalReopenEvent);
  2206. }
  2207. //---------------------------------------------------------------------------
  2208. void __fastcall TTerminalThread::RunAction(TNotifyEvent Action)
  2209. {
  2210. DebugAssert(FAction == NULL);
  2211. DebugAssert(FException == NULL);
  2212. DebugAssert(FIdleException == NULL);
  2213. DebugAssert(FOnIdle != NULL);
  2214. FCancelled = false;
  2215. FAction = Action;
  2216. try
  2217. {
  2218. try
  2219. {
  2220. TriggerEvent();
  2221. bool Done = false;
  2222. const unsigned int MaxWait = 50;
  2223. unsigned int Wait = MaxWait;
  2224. do
  2225. {
  2226. switch (WaitForSingleObject(FActionEvent, Wait))
  2227. {
  2228. case WAIT_OBJECT_0:
  2229. Done = true;
  2230. break;
  2231. case WAIT_TIMEOUT:
  2232. if (FUserAction != NULL)
  2233. {
  2234. try
  2235. {
  2236. FUserAction->Execute(NULL);
  2237. }
  2238. catch (Exception & E)
  2239. {
  2240. SaveException(E, FException);
  2241. }
  2242. FUserAction = NULL;
  2243. TriggerEvent();
  2244. Wait = 0;
  2245. }
  2246. else
  2247. {
  2248. if (FOnIdle != NULL)
  2249. {
  2250. FOnIdle(NULL);
  2251. }
  2252. Wait = std::min(Wait + 10, MaxWait);
  2253. }
  2254. break;
  2255. default:
  2256. throw Exception(L"Error waiting for background session task to complete");
  2257. }
  2258. }
  2259. while (!Done);
  2260. Rethrow(FException);
  2261. }
  2262. __finally
  2263. {
  2264. FAction = NULL;
  2265. SAFE_DESTROY(FException);
  2266. }
  2267. }
  2268. catch(...)
  2269. {
  2270. if (FCancelled)
  2271. {
  2272. // even if the abort thrown as result of Cancel() was wrapped into
  2273. // some higher-level exception, normalize back to message-less fatal
  2274. // exception here
  2275. FatalAbort();
  2276. }
  2277. else
  2278. {
  2279. throw;
  2280. }
  2281. }
  2282. }
  2283. //---------------------------------------------------------------------------
  2284. void __fastcall TTerminalThread::TerminalOpenEvent(TObject * /*Sender*/)
  2285. {
  2286. FTerminal->Open();
  2287. }
  2288. //---------------------------------------------------------------------------
  2289. void __fastcall TTerminalThread::TerminalReopenEvent(TObject * /*Sender*/)
  2290. {
  2291. FTerminal->Reopen(0);
  2292. }
  2293. //---------------------------------------------------------------------------
  2294. void __fastcall TTerminalThread::ProcessEvent()
  2295. {
  2296. DebugAssert(FEvent != NULL);
  2297. DebugAssert(FException == NULL);
  2298. try
  2299. {
  2300. FAction(NULL);
  2301. }
  2302. catch(Exception & E)
  2303. {
  2304. SaveException(E, FException);
  2305. }
  2306. SetEvent(FActionEvent);
  2307. }
  2308. //---------------------------------------------------------------------------
  2309. void __fastcall TTerminalThread::Rethrow(Exception *& Exception)
  2310. {
  2311. if (Exception != NULL)
  2312. {
  2313. try
  2314. {
  2315. RethrowException(Exception);
  2316. }
  2317. __finally
  2318. {
  2319. SAFE_DESTROY(Exception);
  2320. }
  2321. }
  2322. }
  2323. //---------------------------------------------------------------------------
  2324. void __fastcall TTerminalThread::SaveException(Exception & E, Exception *& Exception)
  2325. {
  2326. DebugAssert(Exception == NULL);
  2327. Exception = CloneException(&E);
  2328. }
  2329. //---------------------------------------------------------------------------
  2330. void __fastcall TTerminalThread::FatalAbort()
  2331. {
  2332. FTerminal->FatalAbort();
  2333. }
  2334. //---------------------------------------------------------------------------
  2335. void __fastcall TTerminalThread::CheckCancel()
  2336. {
  2337. if (FCancel && !FCancelled)
  2338. {
  2339. FCancelled = true;
  2340. FatalAbort();
  2341. }
  2342. }
  2343. //---------------------------------------------------------------------------
  2344. void __fastcall TTerminalThread::WaitForUserAction(TUserAction * UserAction)
  2345. {
  2346. DWORD Thread = GetCurrentThreadId();
  2347. // we can get called from the main thread from within Idle,
  2348. // should be only to call HandleExtendedException
  2349. if (Thread == FMainThread)
  2350. {
  2351. if (UserAction != NULL)
  2352. {
  2353. UserAction->Execute(NULL);
  2354. }
  2355. }
  2356. else
  2357. {
  2358. // we should be called from our thread only,
  2359. // with exception noted above
  2360. DebugAssert(Thread == FThreadId);
  2361. bool DoCheckCancel =
  2362. DebugAlwaysFalse(UserAction == NULL) || !UserAction->Force();
  2363. if (DoCheckCancel)
  2364. {
  2365. CheckCancel();
  2366. }
  2367. // have to save it as we can go recursive via TQueryParams::TimerEvent,
  2368. // see TTerminalThread::TerminalQueryUser
  2369. TUserAction * PrevUserAction = FUserAction;
  2370. try
  2371. {
  2372. FUserAction = UserAction;
  2373. while (true)
  2374. {
  2375. {
  2376. TGuard Guard(FSection);
  2377. // If idle exception is already set, we are only waiting
  2378. // for the main thread to pick it up
  2379. // (or at least to finish handling the user action, so
  2380. // that we rethrow the idle exception below)
  2381. // Also if idle exception is set, it is probable that terminal
  2382. // is not active anyway.
  2383. if (FTerminal->Active && FPendingIdle && (FIdleException == NULL))
  2384. {
  2385. FPendingIdle = false;
  2386. try
  2387. {
  2388. FTerminal->Idle();
  2389. }
  2390. catch (Exception & E)
  2391. {
  2392. SaveException(E, FIdleException);
  2393. }
  2394. }
  2395. }
  2396. int WaitResult = WaitForEvent(1000);
  2397. if (WaitResult == 0)
  2398. {
  2399. SAFE_DESTROY(FIdleException);
  2400. FatalAbort();
  2401. }
  2402. else if (WaitResult > 0)
  2403. {
  2404. break;
  2405. }
  2406. }
  2407. Rethrow(FException);
  2408. if (FIdleException != NULL)
  2409. {
  2410. // idle exception was not used to cancel the user action
  2411. // (if it where it would be already cloned into the FException above
  2412. // and rethrown)
  2413. Rethrow(FIdleException);
  2414. }
  2415. }
  2416. __finally
  2417. {
  2418. FUserAction = PrevUserAction;
  2419. SAFE_DESTROY(FException);
  2420. }
  2421. // Contrary to a call before, this is unconditional,
  2422. // otherwise cancelling authentication won't work,
  2423. // if it is tried only after the last user action
  2424. // (what is common, when cancelling while waiting for
  2425. // resolving of unresolvable host name, where the last user action is
  2426. // "resolving hostname" information action)
  2427. CheckCancel();
  2428. }
  2429. }
  2430. //---------------------------------------------------------------------------
  2431. void __fastcall TTerminalThread::TerminalInformation(
  2432. TTerminal * Terminal, const UnicodeString & Str, bool Status, int Phase)
  2433. {
  2434. TInformationUserAction Action(FOnInformation);
  2435. Action.Terminal = Terminal;
  2436. Action.Str = Str;
  2437. Action.Status = Status;
  2438. Action.Phase = Phase;
  2439. WaitForUserAction(&Action);
  2440. }
  2441. //---------------------------------------------------------------------------
  2442. void __fastcall TTerminalThread::TerminalQueryUser(TObject * Sender,
  2443. const UnicodeString Query, TStrings * MoreMessages, unsigned int Answers,
  2444. const TQueryParams * Params, unsigned int & Answer, TQueryType Type, void * Arg)
  2445. {
  2446. DebugUsedParam(Arg);
  2447. DebugAssert(Arg == NULL);
  2448. // note about TQueryParams::TimerEvent
  2449. // So far there is only one use for this, the TSecureShell::SendBuffer,
  2450. // which should be thread-safe, as long as the terminal thread,
  2451. // is stopped waiting for OnQueryUser to finish.
  2452. // note about TQueryButtonAlias::OnClick
  2453. // So far there is only one use for this, the TClipboardHandler,
  2454. // which is thread-safe.
  2455. TQueryUserAction Action(FOnQueryUser);
  2456. Action.Sender = Sender;
  2457. Action.Query = Query;
  2458. Action.MoreMessages = MoreMessages;
  2459. Action.Answers = Answers;
  2460. Action.Params = Params;
  2461. Action.Answer = Answer;
  2462. Action.Type = Type;
  2463. WaitForUserAction(&Action);
  2464. Answer = Action.Answer;
  2465. }
  2466. //---------------------------------------------------------------------------
  2467. void __fastcall TTerminalThread::TerminalInitializeLog(TObject * Sender)
  2468. {
  2469. if (FOnInitializeLog != NULL)
  2470. {
  2471. // never used, so not tested either
  2472. DebugFail();
  2473. TNotifyAction Action(FOnInitializeLog);
  2474. Action.Sender = Sender;
  2475. WaitForUserAction(&Action);
  2476. }
  2477. }
  2478. //---------------------------------------------------------------------------
  2479. void __fastcall TTerminalThread::TerminalPromptUser(TTerminal * Terminal,
  2480. TPromptKind Kind, UnicodeString Name, UnicodeString Instructions, TStrings * Prompts,
  2481. TStrings * Results, bool & Result, void * Arg)
  2482. {
  2483. DebugUsedParam(Arg);
  2484. DebugAssert(Arg == NULL);
  2485. TPromptUserAction Action(FOnPromptUser);
  2486. Action.Terminal = Terminal;
  2487. Action.Kind = Kind;
  2488. Action.Name = Name;
  2489. Action.Instructions = Instructions;
  2490. Action.Prompts = Prompts;
  2491. Action.Results->AddStrings(Results);
  2492. WaitForUserAction(&Action);
  2493. Results->Clear();
  2494. Results->AddStrings(Action.Results);
  2495. Result = Action.Result;
  2496. }
  2497. //---------------------------------------------------------------------------
  2498. void __fastcall TTerminalThread::TerminalShowExtendedException(
  2499. TTerminal * Terminal, Exception * E, void * Arg)
  2500. {
  2501. DebugUsedParam(Arg);
  2502. DebugAssert(Arg == NULL);
  2503. TShowExtendedExceptionAction Action(FOnShowExtendedException);
  2504. Action.Terminal = Terminal;
  2505. Action.E = E;
  2506. WaitForUserAction(&Action);
  2507. }
  2508. //---------------------------------------------------------------------------
  2509. void __fastcall TTerminalThread::TerminalDisplayBanner(TTerminal * Terminal,
  2510. UnicodeString SessionName, const UnicodeString & Banner,
  2511. bool & NeverShowAgain, int Options)
  2512. {
  2513. TDisplayBannerAction Action(FOnDisplayBanner);
  2514. Action.Terminal = Terminal;
  2515. Action.SessionName = SessionName;
  2516. Action.Banner = Banner;
  2517. Action.NeverShowAgain = NeverShowAgain;
  2518. Action.Options = Options;
  2519. WaitForUserAction(&Action);
  2520. NeverShowAgain = Action.NeverShowAgain;
  2521. }
  2522. //---------------------------------------------------------------------------
  2523. void __fastcall TTerminalThread::TerminalChangeDirectory(TObject * Sender)
  2524. {
  2525. TNotifyAction Action(FOnChangeDirectory);
  2526. Action.Sender = Sender;
  2527. WaitForUserAction(&Action);
  2528. }
  2529. //---------------------------------------------------------------------------
  2530. void __fastcall TTerminalThread::TerminalReadDirectory(TObject * Sender, Boolean ReloadOnly)
  2531. {
  2532. TReadDirectoryAction Action(FOnReadDirectory);
  2533. Action.Sender = Sender;
  2534. Action.ReloadOnly = ReloadOnly;
  2535. WaitForUserAction(&Action);
  2536. }
  2537. //---------------------------------------------------------------------------
  2538. void __fastcall TTerminalThread::TerminalStartReadDirectory(TObject * Sender)
  2539. {
  2540. TNotifyAction Action(FOnStartReadDirectory);
  2541. Action.Sender = Sender;
  2542. WaitForUserAction(&Action);
  2543. }
  2544. //---------------------------------------------------------------------------
  2545. void __fastcall TTerminalThread::TerminalReadDirectoryProgress(
  2546. TObject * Sender, int Progress, int ResolvedLinks, bool & Cancel)
  2547. {
  2548. TReadDirectoryProgressAction Action(FOnReadDirectoryProgress);
  2549. Action.Sender = Sender;
  2550. Action.Progress = Progress;
  2551. Action.ResolvedLinks = ResolvedLinks;
  2552. Action.Cancel = Cancel;
  2553. WaitForUserAction(&Action);
  2554. Cancel = Action.Cancel;
  2555. }
  2556. //---------------------------------------------------------------------------