Queue.cpp 70 KB

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