Queue.cpp 80 KB

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