Queue.cpp 72 KB

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