Queue.cpp 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560
  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. bool __fastcall TTerminalQueue::ItemGetCPSLimit(TQueueItem * Item, unsigned long & CPSLimit)
  830. {
  831. CPSLimit = 0;
  832. // to prevent deadlocks when closing queue from other thread
  833. bool Result = !FFinished;
  834. if (Result)
  835. {
  836. TGuard Guard(FItemsSection);
  837. Result = (FItems->IndexOf(Item) >= 0);
  838. if (Result)
  839. {
  840. CPSLimit = Item->GetCPSLimit();
  841. }
  842. }
  843. return Result;
  844. }
  845. //---------------------------------------------------------------------------
  846. void __fastcall TTerminalQueue::Idle()
  847. {
  848. TDateTime N = Now();
  849. if (N - FLastIdle > FIdleInterval)
  850. {
  851. FLastIdle = N;
  852. TTerminalItem * TerminalItem = NULL;
  853. if (FFreeTerminals > 0)
  854. {
  855. TGuard Guard(FItemsSection);
  856. if (FFreeTerminals > 0)
  857. {
  858. // take the last free terminal, because TerminalFree() puts it to the
  859. // front, this ensures we cycle thru all free terminals
  860. TerminalItem = reinterpret_cast<TTerminalItem*>(FTerminals->Items[FFreeTerminals - 1]);
  861. FTerminals->Move(FFreeTerminals - 1, FTerminals->Count - 1);
  862. FFreeTerminals--;
  863. }
  864. }
  865. if (TerminalItem != NULL)
  866. {
  867. TerminalItem->Idle();
  868. }
  869. }
  870. }
  871. //---------------------------------------------------------------------------
  872. bool __fastcall TTerminalQueue::WaitForEvent()
  873. {
  874. // terminate loop regularly, so that we can check for expired done items
  875. bool Result = (TSignalThread::WaitForEvent(1000) != 0);
  876. return Result;
  877. }
  878. //---------------------------------------------------------------------------
  879. void __fastcall TTerminalQueue::ProcessEvent()
  880. {
  881. TTerminalItem * TerminalItem;
  882. TQueueItem * Item;
  883. do
  884. {
  885. TerminalItem = NULL;
  886. Item = NULL;
  887. {
  888. TGuard Guard(FItemsSection);
  889. // =0 do not keep
  890. // <0 infinity
  891. if (FKeepDoneItemsFor >= 0)
  892. {
  893. TDateTime RemoveDoneItemsBefore = Now();
  894. if (FKeepDoneItemsFor > 0)
  895. {
  896. RemoveDoneItemsBefore = IncSecond(RemoveDoneItemsBefore, -FKeepDoneItemsFor);
  897. }
  898. for (int Index = 0; Index < FDoneItems->Count; Index++)
  899. {
  900. TQueueItem * Item = GetItem(FDoneItems, Index);
  901. if (Item->FDoneAt <= RemoveDoneItemsBefore)
  902. {
  903. FDoneItems->Delete(Index);
  904. delete Item;
  905. Index--;
  906. DoListUpdate();
  907. }
  908. }
  909. }
  910. if (FItems->Count > FItemsInProcess)
  911. {
  912. Item = GetItem(FItemsInProcess);
  913. int ForcedIndex = FForcedItems->IndexOf(Item);
  914. if (FEnabled || (ForcedIndex >= 0))
  915. {
  916. if ((FFreeTerminals == 0) &&
  917. ((FTransfersLimit < 0) ||
  918. (FTerminals->Count < FTransfersLimit + FTemporaryTerminals)))
  919. {
  920. FOverallTerminals++;
  921. TerminalItem = new TTerminalItem(this, FOverallTerminals);
  922. FTerminals->Add(TerminalItem);
  923. }
  924. else if (FFreeTerminals > 0)
  925. {
  926. TerminalItem = reinterpret_cast<TTerminalItem*>(FTerminals->Items[0]);
  927. FTerminals->Move(0, FTerminals->Count - 1);
  928. FFreeTerminals--;
  929. }
  930. if (TerminalItem != NULL)
  931. {
  932. if (ForcedIndex >= 0)
  933. {
  934. FForcedItems->Delete(ForcedIndex);
  935. }
  936. FItemsInProcess++;
  937. }
  938. }
  939. }
  940. }
  941. if (TerminalItem != NULL)
  942. {
  943. TerminalItem->Process(Item);
  944. }
  945. }
  946. while (!FTerminated && (TerminalItem != NULL));
  947. }
  948. //---------------------------------------------------------------------------
  949. void __fastcall TTerminalQueue::DoQueueItemUpdate(TQueueItem * Item)
  950. {
  951. if (OnQueueItemUpdate != NULL)
  952. {
  953. OnQueueItemUpdate(this, Item);
  954. }
  955. }
  956. //---------------------------------------------------------------------------
  957. void __fastcall TTerminalQueue::DoListUpdate()
  958. {
  959. if (OnListUpdate != NULL)
  960. {
  961. OnListUpdate(this);
  962. }
  963. }
  964. //---------------------------------------------------------------------------
  965. void __fastcall TTerminalQueue::DoEvent(TQueueEvent Event)
  966. {
  967. if (OnEvent != NULL)
  968. {
  969. OnEvent(this, Event);
  970. }
  971. }
  972. //---------------------------------------------------------------------------
  973. void __fastcall TTerminalQueue::SetTransfersLimit(int value)
  974. {
  975. if (FTransfersLimit != value)
  976. {
  977. {
  978. TGuard Guard(FItemsSection);
  979. if ((value >= 0) && (value < FItemsInProcess))
  980. {
  981. FTemporaryTerminals = (FItemsInProcess - value);
  982. }
  983. else
  984. {
  985. FTemporaryTerminals = 0;
  986. }
  987. FTransfersLimit = value;
  988. }
  989. TriggerEvent();
  990. }
  991. }
  992. //---------------------------------------------------------------------------
  993. void __fastcall TTerminalQueue::SetKeepDoneItemsFor(int value)
  994. {
  995. if (FKeepDoneItemsFor != value)
  996. {
  997. {
  998. TGuard Guard(FItemsSection);
  999. FKeepDoneItemsFor = value;
  1000. }
  1001. }
  1002. }
  1003. //---------------------------------------------------------------------------
  1004. void __fastcall TTerminalQueue::SetEnabled(bool value)
  1005. {
  1006. if (FEnabled != value)
  1007. {
  1008. {
  1009. TGuard Guard(FItemsSection);
  1010. FEnabled = value;
  1011. }
  1012. TriggerEvent();
  1013. }
  1014. }
  1015. //---------------------------------------------------------------------------
  1016. bool __fastcall TTerminalQueue::GetIsEmpty()
  1017. {
  1018. TGuard Guard(FItemsSection);
  1019. return (FItems->Count == 0);
  1020. }
  1021. //---------------------------------------------------------------------------
  1022. // TBackgroundItem
  1023. //---------------------------------------------------------------------------
  1024. class TBackgroundTerminal : public TSecondaryTerminal
  1025. {
  1026. friend class TTerminalItem;
  1027. public:
  1028. __fastcall TBackgroundTerminal(TTerminal * MainTerminal,
  1029. TSessionData * SessionData, TConfiguration * Configuration,
  1030. TTerminalItem * Item, const UnicodeString & Name);
  1031. protected:
  1032. virtual bool __fastcall DoQueryReopen(Exception * E);
  1033. private:
  1034. TTerminalItem * FItem;
  1035. };
  1036. //---------------------------------------------------------------------------
  1037. __fastcall TBackgroundTerminal::TBackgroundTerminal(TTerminal * MainTerminal,
  1038. TSessionData * SessionData, TConfiguration * Configuration, TTerminalItem * Item,
  1039. const UnicodeString & Name) :
  1040. TSecondaryTerminal(MainTerminal, SessionData, Configuration, Name), FItem(Item)
  1041. {
  1042. }
  1043. //---------------------------------------------------------------------------
  1044. bool __fastcall TBackgroundTerminal::DoQueryReopen(Exception * /*E*/)
  1045. {
  1046. bool Result;
  1047. if (FItem->FTerminated || FItem->FCancel)
  1048. {
  1049. // avoid reconnection if we are closing
  1050. Result = false;
  1051. }
  1052. else
  1053. {
  1054. Sleep(Configuration->SessionReopenBackground);
  1055. Result = true;
  1056. }
  1057. return Result;
  1058. }
  1059. //---------------------------------------------------------------------------
  1060. // TTerminalItem
  1061. //---------------------------------------------------------------------------
  1062. __fastcall TTerminalItem::TTerminalItem(TTerminalQueue * Queue, int Index) :
  1063. TSignalThread(true), FQueue(Queue), FTerminal(NULL), FItem(NULL),
  1064. FCriticalSection(NULL), FUserAction(NULL)
  1065. {
  1066. FCriticalSection = new TCriticalSection();
  1067. FTerminal = new TBackgroundTerminal(FQueue->FTerminal, Queue->FSessionData,
  1068. FQueue->FConfiguration, this, FORMAT(L"Background %d", (Index)));
  1069. try
  1070. {
  1071. FTerminal->UseBusyCursor = false;
  1072. FTerminal->OnQueryUser = TerminalQueryUser;
  1073. FTerminal->OnPromptUser = TerminalPromptUser;
  1074. FTerminal->OnShowExtendedException = TerminalShowExtendedException;
  1075. FTerminal->OnProgress = OperationProgress;
  1076. FTerminal->OnFinished = OperationFinished;
  1077. }
  1078. catch(...)
  1079. {
  1080. delete FTerminal;
  1081. throw;
  1082. }
  1083. Start();
  1084. }
  1085. //---------------------------------------------------------------------------
  1086. __fastcall TTerminalItem::~TTerminalItem()
  1087. {
  1088. Close();
  1089. assert(FItem == NULL);
  1090. delete FTerminal;
  1091. delete FCriticalSection;
  1092. }
  1093. //---------------------------------------------------------------------------
  1094. void __fastcall TTerminalItem::Process(TQueueItem * Item)
  1095. {
  1096. {
  1097. TGuard Guard(FCriticalSection);
  1098. assert(FItem == NULL);
  1099. FItem = Item;
  1100. }
  1101. TriggerEvent();
  1102. }
  1103. //---------------------------------------------------------------------------
  1104. void __fastcall TTerminalItem::ProcessEvent()
  1105. {
  1106. TGuard Guard(FCriticalSection);
  1107. bool Retry = true;
  1108. FCancel = false;
  1109. FPause = false;
  1110. FItem->FTerminalItem = this;
  1111. try
  1112. {
  1113. assert(FItem != NULL);
  1114. if (!FTerminal->Active)
  1115. {
  1116. FItem->SetStatus(TQueueItem::qsConnecting);
  1117. FTerminal->SessionData->RemoteDirectory = FItem->StartupDirectory();
  1118. FTerminal->Open();
  1119. }
  1120. Retry = false;
  1121. if (!FCancel)
  1122. {
  1123. FItem->SetStatus(TQueueItem::qsProcessing);
  1124. FItem->Execute(this);
  1125. }
  1126. }
  1127. catch(Exception & E)
  1128. {
  1129. UnicodeString Message;
  1130. if (ExceptionMessageFormatted(&E, Message))
  1131. {
  1132. // do not show error messages, if task was cancelled anyway
  1133. // (for example if transfer is cancelled during reconnection attempts)
  1134. if (!FCancel &&
  1135. (FTerminal->QueryUserException(L"", &E, qaOK | qaCancel, NULL, qtError) == qaCancel))
  1136. {
  1137. FCancel = true;
  1138. }
  1139. }
  1140. }
  1141. FItem->SetStatus(TQueueItem::qsDone);
  1142. FItem->FTerminalItem = NULL;
  1143. TQueueItem * Item = FItem;
  1144. FItem = NULL;
  1145. if (Retry && !FCancel)
  1146. {
  1147. FQueue->RetryItem(Item);
  1148. }
  1149. else
  1150. {
  1151. FQueue->DeleteItem(Item, !FCancel);
  1152. }
  1153. if (!FTerminal->Active ||
  1154. !FQueue->TerminalFree(this))
  1155. {
  1156. Terminate();
  1157. }
  1158. }
  1159. //---------------------------------------------------------------------------
  1160. void __fastcall TTerminalItem::Idle()
  1161. {
  1162. TGuard Guard(FCriticalSection);
  1163. assert(FTerminal->Active);
  1164. try
  1165. {
  1166. FTerminal->Idle();
  1167. }
  1168. catch(...)
  1169. {
  1170. }
  1171. if (!FTerminal->Active ||
  1172. !FQueue->TerminalFree(this))
  1173. {
  1174. Terminate();
  1175. }
  1176. }
  1177. //---------------------------------------------------------------------------
  1178. void __fastcall TTerminalItem::Cancel()
  1179. {
  1180. FCancel = true;
  1181. if ((FItem->GetStatus() == TQueueItem::qsPaused) ||
  1182. TQueueItem::IsUserActionStatus(FItem->GetStatus()))
  1183. {
  1184. TriggerEvent();
  1185. }
  1186. }
  1187. //---------------------------------------------------------------------------
  1188. bool __fastcall TTerminalItem::Pause()
  1189. {
  1190. assert(FItem != NULL);
  1191. bool Result = (FItem->GetStatus() == TQueueItem::qsProcessing) && !FPause;
  1192. if (Result)
  1193. {
  1194. FPause = true;
  1195. }
  1196. return Result;
  1197. }
  1198. //---------------------------------------------------------------------------
  1199. bool __fastcall TTerminalItem::Resume()
  1200. {
  1201. assert(FItem != NULL);
  1202. bool Result = (FItem->GetStatus() == TQueueItem::qsPaused);
  1203. if (Result)
  1204. {
  1205. TriggerEvent();
  1206. }
  1207. return Result;
  1208. }
  1209. //---------------------------------------------------------------------------
  1210. bool __fastcall TTerminalItem::ProcessUserAction(void * Arg)
  1211. {
  1212. // When status is changed twice quickly, the controller when responding
  1213. // to the first change (non-user-action) can be so slow to check only after
  1214. // the second (user-action) change occurs. Thus it responds it.
  1215. // Then as reaction to the second (user-action) change there will not be
  1216. // any outstanding user-action.
  1217. bool Result = (FUserAction != NULL);
  1218. if (Result)
  1219. {
  1220. assert(FItem != NULL);
  1221. FUserAction->Execute(Arg);
  1222. FUserAction = NULL;
  1223. TriggerEvent();
  1224. }
  1225. return Result;
  1226. }
  1227. //---------------------------------------------------------------------------
  1228. bool __fastcall TTerminalItem::WaitForUserAction(
  1229. TQueueItem::TStatus ItemStatus, TUserAction * UserAction)
  1230. {
  1231. assert(FItem != NULL);
  1232. assert((FItem->GetStatus() == TQueueItem::qsProcessing) ||
  1233. (FItem->GetStatus() == TQueueItem::qsConnecting));
  1234. bool Result;
  1235. TQueueItem::TStatus PrevStatus = FItem->GetStatus();
  1236. try
  1237. {
  1238. FUserAction = UserAction;
  1239. FItem->SetStatus(ItemStatus);
  1240. FQueue->DoEvent(qePendingUserAction);
  1241. Result = !FTerminated && WaitForEvent() && !FCancel;
  1242. }
  1243. __finally
  1244. {
  1245. FUserAction = NULL;
  1246. FItem->SetStatus(PrevStatus);
  1247. }
  1248. return Result;
  1249. }
  1250. //---------------------------------------------------------------------------
  1251. void __fastcall TTerminalItem::Finished()
  1252. {
  1253. TSignalThread::Finished();
  1254. FQueue->TerminalFinished(this);
  1255. }
  1256. //---------------------------------------------------------------------------
  1257. void __fastcall TTerminalItem::TerminalQueryUser(TObject * Sender,
  1258. const UnicodeString Query, TStrings * MoreMessages, unsigned int Answers,
  1259. const TQueryParams * Params, unsigned int & Answer, TQueryType Type, void * Arg)
  1260. {
  1261. // so far query without queue item can occur only for key confirmation
  1262. // on re-key with non-cached host key. make it fail.
  1263. if (FItem != NULL)
  1264. {
  1265. USEDPARAM(Arg);
  1266. assert(Arg == NULL);
  1267. TQueryUserAction Action(FQueue->OnQueryUser);
  1268. Action.Sender = Sender;
  1269. Action.Query = Query;
  1270. Action.MoreMessages = MoreMessages;
  1271. Action.Answers = Answers;
  1272. Action.Params = Params;
  1273. Action.Answer = Answer;
  1274. Action.Type = Type;
  1275. // if the query is "error", present it as an "error" state in UI,
  1276. // however it is still handled as query by the action.
  1277. TQueueItem::TStatus ItemStatus =
  1278. (Action.Type == qtError ? TQueueItem::qsError : TQueueItem::qsQuery);
  1279. if (WaitForUserAction(ItemStatus, &Action))
  1280. {
  1281. Answer = Action.Answer;
  1282. }
  1283. }
  1284. }
  1285. //---------------------------------------------------------------------------
  1286. void __fastcall TTerminalItem::TerminalPromptUser(TTerminal * Terminal,
  1287. TPromptKind Kind, UnicodeString Name, UnicodeString Instructions, TStrings * Prompts,
  1288. TStrings * Results, bool & Result, void * Arg)
  1289. {
  1290. if (FItem == NULL)
  1291. {
  1292. // sanity, should not occur
  1293. assert(false);
  1294. Result = false;
  1295. }
  1296. else
  1297. {
  1298. USEDPARAM(Arg);
  1299. assert(Arg == NULL);
  1300. TPromptUserAction Action(FQueue->OnPromptUser);
  1301. Action.Terminal = Terminal;
  1302. Action.Kind = Kind;
  1303. Action.Name = Name;
  1304. Action.Instructions = Instructions;
  1305. Action.Prompts = Prompts;
  1306. Action.Results->AddStrings(Results);
  1307. if (WaitForUserAction(TQueueItem::qsPrompt, &Action))
  1308. {
  1309. Results->Clear();
  1310. Results->AddStrings(Action.Results);
  1311. Result = Action.Result;
  1312. }
  1313. }
  1314. }
  1315. //---------------------------------------------------------------------------
  1316. void __fastcall TTerminalItem::TerminalShowExtendedException(
  1317. TTerminal * Terminal, Exception * E, void * Arg)
  1318. {
  1319. USEDPARAM(Arg);
  1320. assert(Arg == NULL);
  1321. if ((FItem != NULL) &&
  1322. ShouldDisplayException(E))
  1323. {
  1324. TShowExtendedExceptionAction Action(FQueue->OnShowExtendedException);
  1325. Action.Terminal = Terminal;
  1326. Action.E = E;
  1327. WaitForUserAction(TQueueItem::qsError, &Action);
  1328. }
  1329. }
  1330. //---------------------------------------------------------------------------
  1331. void __fastcall TTerminalItem::OperationFinished(TFileOperation /*Operation*/,
  1332. TOperationSide /*Side*/, bool /*Temp*/, const UnicodeString & /*FileName*/,
  1333. bool /*Success*/, TOnceDoneOperation & /*OnceDoneOperation*/)
  1334. {
  1335. // nothing
  1336. }
  1337. //---------------------------------------------------------------------------
  1338. void __fastcall TTerminalItem::OperationProgress(
  1339. TFileOperationProgressType & ProgressData, TCancelStatus & Cancel)
  1340. {
  1341. if (FPause && !FTerminated && !FCancel)
  1342. {
  1343. assert(FItem != NULL);
  1344. TQueueItem::TStatus PrevStatus = FItem->GetStatus();
  1345. assert(PrevStatus == TQueueItem::qsProcessing);
  1346. // must be set before TFileOperationProgressType::Suspend(), because
  1347. // it invokes this method back
  1348. FPause = false;
  1349. ProgressData.Suspend();
  1350. try
  1351. {
  1352. FItem->SetStatus(TQueueItem::qsPaused);
  1353. WaitForEvent();
  1354. }
  1355. __finally
  1356. {
  1357. FItem->SetStatus(PrevStatus);
  1358. ProgressData.Resume();
  1359. }
  1360. }
  1361. if (FTerminated || FCancel)
  1362. {
  1363. if (ProgressData.TransferingFile)
  1364. {
  1365. Cancel = csCancelTransfer;
  1366. }
  1367. else
  1368. {
  1369. Cancel = csCancel;
  1370. }
  1371. }
  1372. assert(FItem != NULL);
  1373. FItem->SetProgress(ProgressData);
  1374. }
  1375. //---------------------------------------------------------------------------
  1376. bool __fastcall TTerminalItem::OverrideItemStatus(TQueueItem::TStatus & ItemStatus)
  1377. {
  1378. assert(FTerminal != NULL);
  1379. bool Result = (FTerminal->Status < ssOpened) && (ItemStatus == TQueueItem::qsProcessing);
  1380. if (Result)
  1381. {
  1382. ItemStatus = TQueueItem::qsConnecting;
  1383. }
  1384. return Result;
  1385. }
  1386. //---------------------------------------------------------------------------
  1387. // TQueueItem
  1388. //---------------------------------------------------------------------------
  1389. __fastcall TQueueItem::TQueueItem() :
  1390. FStatus(qsPending), FTerminalItem(NULL), FSection(NULL), FProgressData(NULL),
  1391. FQueue(NULL), FInfo(NULL), FCompleteEvent(INVALID_HANDLE_VALUE),
  1392. FCPSLimit(-1)
  1393. {
  1394. FSection = new TCriticalSection();
  1395. FInfo = new TInfo();
  1396. FInfo->SingleFile = false;
  1397. }
  1398. //---------------------------------------------------------------------------
  1399. __fastcall TQueueItem::~TQueueItem()
  1400. {
  1401. // we need to keep the total transfer size even after transfer completes
  1402. delete FProgressData;
  1403. Complete();
  1404. delete FSection;
  1405. delete FInfo;
  1406. }
  1407. //---------------------------------------------------------------------------
  1408. void __fastcall TQueueItem::Complete()
  1409. {
  1410. TGuard Guard(FSection);
  1411. if (FCompleteEvent != INVALID_HANDLE_VALUE)
  1412. {
  1413. SetEvent(FCompleteEvent);
  1414. FCompleteEvent = INVALID_HANDLE_VALUE;
  1415. }
  1416. }
  1417. //---------------------------------------------------------------------------
  1418. bool __fastcall TQueueItem::IsUserActionStatus(TStatus Status)
  1419. {
  1420. return (Status == qsQuery) || (Status == qsError) || (Status == qsPrompt);
  1421. }
  1422. //---------------------------------------------------------------------------
  1423. TQueueItem::TStatus __fastcall TQueueItem::GetStatus()
  1424. {
  1425. TGuard Guard(FSection);
  1426. return FStatus;
  1427. }
  1428. //---------------------------------------------------------------------------
  1429. void __fastcall TQueueItem::SetStatus(TStatus Status)
  1430. {
  1431. {
  1432. TGuard Guard(FSection);
  1433. FStatus = Status;
  1434. if (FStatus == qsDone)
  1435. {
  1436. FDoneAt = Now();
  1437. }
  1438. }
  1439. assert((FQueue != NULL) || (Status == qsPending));
  1440. if (FQueue != NULL)
  1441. {
  1442. FQueue->DoQueueItemUpdate(this);
  1443. }
  1444. }
  1445. //---------------------------------------------------------------------------
  1446. void __fastcall TQueueItem::SetProgress(
  1447. TFileOperationProgressType & ProgressData)
  1448. {
  1449. {
  1450. TGuard Guard(FSection);
  1451. // do not lose CPS limit override on "calculate size" operation,
  1452. // wait until the real transfer operation starts
  1453. if ((FCPSLimit >= 0) && ((ProgressData.Operation == foMove) || (ProgressData.Operation == foCopy)))
  1454. {
  1455. ProgressData.CPSLimit = static_cast<unsigned long>(FCPSLimit);
  1456. FCPSLimit = -1;
  1457. }
  1458. assert(FProgressData != NULL);
  1459. *FProgressData = ProgressData;
  1460. FProgressData->Reset();
  1461. }
  1462. FQueue->DoQueueItemUpdate(this);
  1463. }
  1464. //---------------------------------------------------------------------------
  1465. void __fastcall TQueueItem::GetData(TQueueItemProxy * Proxy)
  1466. {
  1467. TGuard Guard(FSection);
  1468. assert(Proxy->FProgressData != NULL);
  1469. if (FProgressData != NULL)
  1470. {
  1471. *Proxy->FProgressData = *FProgressData;
  1472. }
  1473. else
  1474. {
  1475. Proxy->FProgressData->Clear();
  1476. }
  1477. *Proxy->FInfo = *FInfo;
  1478. Proxy->FStatus = FStatus;
  1479. if (FTerminalItem != NULL)
  1480. {
  1481. FTerminalItem->OverrideItemStatus(Proxy->FStatus);
  1482. }
  1483. }
  1484. //---------------------------------------------------------------------------
  1485. void __fastcall TQueueItem::Execute(TTerminalItem * TerminalItem)
  1486. {
  1487. {
  1488. assert(FProgressData == NULL);
  1489. TGuard Guard(FSection);
  1490. FProgressData = new TFileOperationProgressType();
  1491. }
  1492. DoExecute(TerminalItem->FTerminal);
  1493. }
  1494. //---------------------------------------------------------------------------
  1495. void __fastcall TQueueItem::SetCPSLimit(unsigned long CPSLimit)
  1496. {
  1497. FCPSLimit = static_cast<long>(CPSLimit);
  1498. }
  1499. //---------------------------------------------------------------------------
  1500. unsigned long __fastcall TQueueItem::DefaultCPSLimit()
  1501. {
  1502. return 0;
  1503. }
  1504. //---------------------------------------------------------------------------
  1505. unsigned long __fastcall TQueueItem::GetCPSLimit()
  1506. {
  1507. unsigned long Result;
  1508. if (FCPSLimit >= 0)
  1509. {
  1510. Result = FCPSLimit;
  1511. }
  1512. else if (FProgressData != NULL)
  1513. {
  1514. Result = FProgressData->CPSLimit;
  1515. }
  1516. else
  1517. {
  1518. Result = DefaultCPSLimit();
  1519. }
  1520. return Result;
  1521. }
  1522. //---------------------------------------------------------------------------
  1523. // TQueueItemProxy
  1524. //---------------------------------------------------------------------------
  1525. __fastcall TQueueItemProxy::TQueueItemProxy(TTerminalQueue * Queue,
  1526. TQueueItem * QueueItem) :
  1527. FQueue(Queue), FQueueItem(QueueItem), FProgressData(NULL),
  1528. FQueueStatus(NULL), FInfo(NULL),
  1529. FProcessingUserAction(false), FUserData(NULL)
  1530. {
  1531. FProgressData = new TFileOperationProgressType();
  1532. FInfo = new TQueueItem::TInfo();
  1533. Update();
  1534. }
  1535. //---------------------------------------------------------------------------
  1536. __fastcall TQueueItemProxy::~TQueueItemProxy()
  1537. {
  1538. delete FProgressData;
  1539. delete FInfo;
  1540. }
  1541. //---------------------------------------------------------------------------
  1542. TFileOperationProgressType * __fastcall TQueueItemProxy::GetProgressData()
  1543. {
  1544. return (FProgressData->Operation == foNone) ? NULL : FProgressData;
  1545. }
  1546. //---------------------------------------------------------------------------
  1547. __int64 __fastcall TQueueItemProxy::GetTotalTransferred()
  1548. {
  1549. // want to show total transferred also for "completed" items,
  1550. // for which GetProgressData() is NULL
  1551. return
  1552. (FProgressData->Operation == Info->Operation) || (Status == TQueueItem::qsDone) ?
  1553. FProgressData->TotalTransfered : -1;
  1554. }
  1555. //---------------------------------------------------------------------------
  1556. bool __fastcall TQueueItemProxy::Update()
  1557. {
  1558. assert(FQueueItem != NULL);
  1559. TQueueItem::TStatus PrevStatus = Status;
  1560. bool Result = FQueue->ItemGetData(FQueueItem, this);
  1561. if ((FQueueStatus != NULL) && (PrevStatus != Status))
  1562. {
  1563. FQueueStatus->ResetStats();
  1564. }
  1565. return Result;
  1566. }
  1567. //---------------------------------------------------------------------------
  1568. bool __fastcall TQueueItemProxy::ExecuteNow()
  1569. {
  1570. return FQueue->ItemExecuteNow(FQueueItem);
  1571. }
  1572. //---------------------------------------------------------------------------
  1573. bool __fastcall TQueueItemProxy::Move(bool Sooner)
  1574. {
  1575. bool Result = false;
  1576. int I = Index;
  1577. if (Sooner)
  1578. {
  1579. if (I > 0)
  1580. {
  1581. Result = Move(FQueueStatus->Items[I - 1]);
  1582. }
  1583. }
  1584. else
  1585. {
  1586. if (I < FQueueStatus->Count - 1)
  1587. {
  1588. Result = FQueueStatus->Items[I + 1]->Move(this);
  1589. }
  1590. }
  1591. return Result;
  1592. }
  1593. //---------------------------------------------------------------------------
  1594. bool __fastcall TQueueItemProxy::Move(TQueueItemProxy * BeforeItem)
  1595. {
  1596. return FQueue->ItemMove(FQueueItem, BeforeItem->FQueueItem);
  1597. }
  1598. //---------------------------------------------------------------------------
  1599. bool __fastcall TQueueItemProxy::Delete()
  1600. {
  1601. return FQueue->ItemDelete(FQueueItem);
  1602. }
  1603. //---------------------------------------------------------------------------
  1604. bool __fastcall TQueueItemProxy::Pause()
  1605. {
  1606. return FQueue->ItemPause(FQueueItem, true);
  1607. }
  1608. //---------------------------------------------------------------------------
  1609. bool __fastcall TQueueItemProxy::Resume()
  1610. {
  1611. return FQueue->ItemPause(FQueueItem, false);
  1612. }
  1613. //---------------------------------------------------------------------------
  1614. bool __fastcall TQueueItemProxy::ProcessUserAction()
  1615. {
  1616. assert(FQueueItem != NULL);
  1617. bool Result;
  1618. FProcessingUserAction = true;
  1619. try
  1620. {
  1621. Result = FQueue->ItemProcessUserAction(FQueueItem, NULL);
  1622. }
  1623. __finally
  1624. {
  1625. FProcessingUserAction = false;
  1626. }
  1627. return Result;
  1628. }
  1629. //---------------------------------------------------------------------------
  1630. bool __fastcall TQueueItemProxy::GetCPSLimit(unsigned long & CPSLimit)
  1631. {
  1632. return FQueue->ItemGetCPSLimit(FQueueItem, CPSLimit);
  1633. }
  1634. //---------------------------------------------------------------------------
  1635. bool __fastcall TQueueItemProxy::SetCPSLimit(unsigned long CPSLimit)
  1636. {
  1637. return FQueue->ItemSetCPSLimit(FQueueItem, CPSLimit);
  1638. }
  1639. //---------------------------------------------------------------------------
  1640. int __fastcall TQueueItemProxy::GetIndex()
  1641. {
  1642. assert(FQueueStatus != NULL);
  1643. int Index = FQueueStatus->FList->IndexOf(this);
  1644. assert(Index >= 0);
  1645. return Index;
  1646. }
  1647. //---------------------------------------------------------------------------
  1648. // TTerminalQueueStatus
  1649. //---------------------------------------------------------------------------
  1650. __fastcall TTerminalQueueStatus::TTerminalQueueStatus() :
  1651. FList(NULL)
  1652. {
  1653. FList = new TList();
  1654. ResetStats();
  1655. }
  1656. //---------------------------------------------------------------------------
  1657. __fastcall TTerminalQueueStatus::~TTerminalQueueStatus()
  1658. {
  1659. for (int Index = 0; Index < FList->Count; Index++)
  1660. {
  1661. delete GetItem(Index);
  1662. }
  1663. delete FList;
  1664. FList = NULL;
  1665. }
  1666. //---------------------------------------------------------------------------
  1667. void __fastcall TTerminalQueueStatus::ResetStats()
  1668. {
  1669. FActiveCount = -1;
  1670. }
  1671. //---------------------------------------------------------------------------
  1672. void __fastcall TTerminalQueueStatus::SetDoneCount(int Value)
  1673. {
  1674. FDoneCount = Value;
  1675. ResetStats();
  1676. }
  1677. //---------------------------------------------------------------------------
  1678. int __fastcall TTerminalQueueStatus::GetActiveCount()
  1679. {
  1680. if (FActiveCount < 0)
  1681. {
  1682. FActiveCount = 0;
  1683. int Index = FDoneCount;
  1684. while ((Index < FList->Count) &&
  1685. (GetItem(Index)->Status != TQueueItem::qsPending))
  1686. {
  1687. FActiveCount++;
  1688. Index++;
  1689. }
  1690. }
  1691. return FActiveCount;
  1692. }
  1693. //---------------------------------------------------------------------------
  1694. int __fastcall TTerminalQueueStatus::GetDoneAndActiveCount()
  1695. {
  1696. return DoneCount + ActiveCount;
  1697. }
  1698. //---------------------------------------------------------------------------
  1699. int __fastcall TTerminalQueueStatus::GetActiveAndPendingCount()
  1700. {
  1701. return Count - DoneCount;
  1702. }
  1703. //---------------------------------------------------------------------------
  1704. void __fastcall TTerminalQueueStatus::Add(TQueueItemProxy * ItemProxy)
  1705. {
  1706. ItemProxy->FQueueStatus = this;
  1707. FList->Add(ItemProxy);
  1708. ResetStats();
  1709. }
  1710. //---------------------------------------------------------------------------
  1711. void __fastcall TTerminalQueueStatus::Delete(TQueueItemProxy * ItemProxy)
  1712. {
  1713. FList->Extract(ItemProxy);
  1714. ItemProxy->FQueueStatus = NULL;
  1715. ResetStats();
  1716. }
  1717. //---------------------------------------------------------------------------
  1718. int __fastcall TTerminalQueueStatus::GetCount()
  1719. {
  1720. return FList->Count;
  1721. }
  1722. //---------------------------------------------------------------------------
  1723. TQueueItemProxy * __fastcall TTerminalQueueStatus::GetItem(int Index)
  1724. {
  1725. return reinterpret_cast<TQueueItemProxy *>(FList->Items[Index]);
  1726. }
  1727. //---------------------------------------------------------------------------
  1728. TQueueItemProxy * __fastcall TTerminalQueueStatus::FindByQueueItem(
  1729. TQueueItem * QueueItem)
  1730. {
  1731. TQueueItemProxy * Item;
  1732. for (int Index = 0; Index < FList->Count; Index++)
  1733. {
  1734. Item = GetItem(Index);
  1735. if (Item->FQueueItem == QueueItem)
  1736. {
  1737. return Item;
  1738. }
  1739. }
  1740. return NULL;
  1741. }
  1742. //---------------------------------------------------------------------------
  1743. // TLocatedQueueItem
  1744. //---------------------------------------------------------------------------
  1745. __fastcall TLocatedQueueItem::TLocatedQueueItem(TTerminal * Terminal) :
  1746. TQueueItem()
  1747. {
  1748. assert(Terminal != NULL);
  1749. FCurrentDir = Terminal->CurrentDirectory;
  1750. }
  1751. //---------------------------------------------------------------------------
  1752. UnicodeString __fastcall TLocatedQueueItem::StartupDirectory()
  1753. {
  1754. return FCurrentDir;
  1755. }
  1756. //---------------------------------------------------------------------------
  1757. void __fastcall TLocatedQueueItem::DoExecute(TTerminal * Terminal)
  1758. {
  1759. assert(Terminal != NULL);
  1760. Terminal->CurrentDirectory = FCurrentDir;
  1761. }
  1762. //---------------------------------------------------------------------------
  1763. // TTransferQueueItem
  1764. //---------------------------------------------------------------------------
  1765. __fastcall TTransferQueueItem::TTransferQueueItem(TTerminal * Terminal,
  1766. TStrings * FilesToCopy, const UnicodeString & TargetDir,
  1767. const TCopyParamType * CopyParam, int Params, TOperationSide Side,
  1768. bool SingleFile) :
  1769. TLocatedQueueItem(Terminal), FFilesToCopy(NULL), FCopyParam(NULL)
  1770. {
  1771. FInfo->Operation = (Params & cpDelete ? foMove : foCopy);
  1772. FInfo->Side = Side;
  1773. FInfo->SingleFile = SingleFile;
  1774. assert(FilesToCopy != NULL);
  1775. FFilesToCopy = new TStringList();
  1776. for (int Index = 0; Index < FilesToCopy->Count; Index++)
  1777. {
  1778. FFilesToCopy->AddObject(FilesToCopy->Strings[Index],
  1779. ((FilesToCopy->Objects[Index] == NULL) || (Side == osLocal)) ? NULL :
  1780. dynamic_cast<TRemoteFile*>(FilesToCopy->Objects[Index])->Duplicate());
  1781. }
  1782. FTargetDir = TargetDir;
  1783. assert(CopyParam != NULL);
  1784. FCopyParam = new TCopyParamType(*CopyParam);
  1785. FParams = Params;
  1786. }
  1787. //---------------------------------------------------------------------------
  1788. __fastcall TTransferQueueItem::~TTransferQueueItem()
  1789. {
  1790. for (int Index = 0; Index < FFilesToCopy->Count; Index++)
  1791. {
  1792. delete FFilesToCopy->Objects[Index];
  1793. }
  1794. delete FFilesToCopy;
  1795. delete FCopyParam;
  1796. }
  1797. //---------------------------------------------------------------------------
  1798. unsigned long __fastcall TTransferQueueItem::DefaultCPSLimit()
  1799. {
  1800. return FCopyParam->CPSLimit;
  1801. }
  1802. //---------------------------------------------------------------------------
  1803. // TUploadQueueItem
  1804. //---------------------------------------------------------------------------
  1805. __fastcall TUploadQueueItem::TUploadQueueItem(TTerminal * Terminal,
  1806. TStrings * FilesToCopy, const UnicodeString & TargetDir,
  1807. const TCopyParamType * CopyParam, int Params, bool SingleFile) :
  1808. TTransferQueueItem(Terminal, FilesToCopy, TargetDir, CopyParam, Params, osLocal, SingleFile)
  1809. {
  1810. if (FilesToCopy->Count > 1)
  1811. {
  1812. if (FLAGSET(Params, cpTemporary))
  1813. {
  1814. FInfo->Source = "";
  1815. FInfo->ModifiedLocal = "";
  1816. }
  1817. else
  1818. {
  1819. ExtractCommonPath(FilesToCopy, FInfo->Source);
  1820. // this way the trailing backslash is preserved for root directories like D:\\
  1821. FInfo->Source = ExtractFileDir(IncludeTrailingBackslash(FInfo->Source));
  1822. FInfo->ModifiedLocal = FLAGCLEAR(Params, cpDelete) ? UnicodeString() :
  1823. IncludeTrailingBackslash(FInfo->Source);
  1824. }
  1825. }
  1826. else
  1827. {
  1828. if (FLAGSET(Params, cpTemporary))
  1829. {
  1830. FInfo->Source = ExtractFileName(FilesToCopy->Strings[0]);
  1831. FInfo->ModifiedLocal = L"";
  1832. }
  1833. else
  1834. {
  1835. assert(FilesToCopy->Count > 0);
  1836. FInfo->Source = FilesToCopy->Strings[0];
  1837. FInfo->ModifiedLocal = FLAGCLEAR(Params, cpDelete) ? UnicodeString() :
  1838. IncludeTrailingBackslash(ExtractFilePath(FInfo->Source));
  1839. }
  1840. }
  1841. FInfo->Destination =
  1842. UnixIncludeTrailingBackslash(TargetDir) + CopyParam->FileMask;
  1843. FInfo->ModifiedRemote = UnixIncludeTrailingBackslash(TargetDir);
  1844. }
  1845. //---------------------------------------------------------------------------
  1846. void __fastcall TUploadQueueItem::DoExecute(TTerminal * Terminal)
  1847. {
  1848. TTransferQueueItem::DoExecute(Terminal);
  1849. assert(Terminal != NULL);
  1850. Terminal->CopyToRemote(FFilesToCopy, FTargetDir, FCopyParam, FParams);
  1851. }
  1852. //---------------------------------------------------------------------------
  1853. // TDownloadQueueItem
  1854. //---------------------------------------------------------------------------
  1855. __fastcall TDownloadQueueItem::TDownloadQueueItem(TTerminal * Terminal,
  1856. TStrings * FilesToCopy, const UnicodeString & TargetDir,
  1857. const TCopyParamType * CopyParam, int Params, bool SingleFile) :
  1858. TTransferQueueItem(Terminal, FilesToCopy, TargetDir, CopyParam, Params, osRemote, SingleFile)
  1859. {
  1860. if (FilesToCopy->Count > 1)
  1861. {
  1862. if (!UnixExtractCommonPath(FilesToCopy, FInfo->Source))
  1863. {
  1864. FInfo->Source = Terminal->CurrentDirectory;
  1865. }
  1866. FInfo->Source = UnixExcludeTrailingBackslash(FInfo->Source);
  1867. FInfo->ModifiedRemote = FLAGCLEAR(Params, cpDelete) ? UnicodeString() :
  1868. UnixIncludeTrailingBackslash(FInfo->Source);
  1869. }
  1870. else
  1871. {
  1872. assert(FilesToCopy->Count > 0);
  1873. FInfo->Source = FilesToCopy->Strings[0];
  1874. if (UnixExtractFilePath(FInfo->Source).IsEmpty())
  1875. {
  1876. FInfo->Source = UnixIncludeTrailingBackslash(Terminal->CurrentDirectory) +
  1877. FInfo->Source;
  1878. FInfo->ModifiedRemote = FLAGCLEAR(Params, cpDelete) ? UnicodeString() :
  1879. UnixIncludeTrailingBackslash(Terminal->CurrentDirectory);
  1880. }
  1881. else
  1882. {
  1883. FInfo->ModifiedRemote = FLAGCLEAR(Params, cpDelete) ? UnicodeString() :
  1884. UnixExtractFilePath(FInfo->Source);
  1885. }
  1886. }
  1887. if (FLAGSET(Params, cpTemporary))
  1888. {
  1889. FInfo->Destination = L"";
  1890. }
  1891. else
  1892. {
  1893. FInfo->Destination =
  1894. IncludeTrailingBackslash(TargetDir) + CopyParam->FileMask;
  1895. }
  1896. FInfo->ModifiedLocal = IncludeTrailingBackslash(TargetDir);
  1897. }
  1898. //---------------------------------------------------------------------------
  1899. void __fastcall TDownloadQueueItem::DoExecute(TTerminal * Terminal)
  1900. {
  1901. TTransferQueueItem::DoExecute(Terminal);
  1902. assert(Terminal != NULL);
  1903. Terminal->CopyToLocal(FFilesToCopy, FTargetDir, FCopyParam, FParams);
  1904. }
  1905. //---------------------------------------------------------------------------
  1906. // TTerminalThread
  1907. //---------------------------------------------------------------------------
  1908. __fastcall TTerminalThread::TTerminalThread(TTerminal * Terminal) :
  1909. TSignalThread(false), FTerminal(Terminal)
  1910. {
  1911. FAction = NULL;
  1912. FActionEvent = CreateEvent(NULL, false, false, NULL);
  1913. FException = NULL;
  1914. FIdleException = NULL;
  1915. FOnIdle = NULL;
  1916. FUserAction = NULL;
  1917. FCancel = false;
  1918. FMainThread = GetCurrentThreadId();
  1919. FSection = new TCriticalSection();
  1920. FOnInformation = FTerminal->OnInformation;
  1921. FOnQueryUser = FTerminal->OnQueryUser;
  1922. FOnPromptUser = FTerminal->OnPromptUser;
  1923. FOnShowExtendedException = FTerminal->OnShowExtendedException;
  1924. FOnDisplayBanner = FTerminal->OnDisplayBanner;
  1925. FOnChangeDirectory = FTerminal->OnChangeDirectory;
  1926. FOnReadDirectory = FTerminal->OnReadDirectory;
  1927. FOnStartReadDirectory = FTerminal->OnStartReadDirectory;
  1928. FOnReadDirectoryProgress = FTerminal->OnReadDirectoryProgress;
  1929. FOnInitializeLog = FTerminal->OnInitializeLog;
  1930. FTerminal->OnInformation = TerminalInformation;
  1931. FTerminal->OnQueryUser = TerminalQueryUser;
  1932. FTerminal->OnPromptUser = TerminalPromptUser;
  1933. FTerminal->OnShowExtendedException = TerminalShowExtendedException;
  1934. FTerminal->OnDisplayBanner = TerminalDisplayBanner;
  1935. FTerminal->OnChangeDirectory = TerminalChangeDirectory;
  1936. FTerminal->OnReadDirectory = TerminalReadDirectory;
  1937. FTerminal->OnStartReadDirectory = TerminalStartReadDirectory;
  1938. FTerminal->OnReadDirectoryProgress = TerminalReadDirectoryProgress;
  1939. FTerminal->OnInitializeLog = TerminalInitializeLog;
  1940. Start();
  1941. }
  1942. //---------------------------------------------------------------------------
  1943. __fastcall TTerminalThread::~TTerminalThread()
  1944. {
  1945. Close();
  1946. CloseHandle(FActionEvent);
  1947. assert(FTerminal->OnInformation == TerminalInformation);
  1948. assert(FTerminal->OnQueryUser == TerminalQueryUser);
  1949. assert(FTerminal->OnPromptUser == TerminalPromptUser);
  1950. assert(FTerminal->OnShowExtendedException == TerminalShowExtendedException);
  1951. assert(FTerminal->OnDisplayBanner == TerminalDisplayBanner);
  1952. assert(FTerminal->OnChangeDirectory == TerminalChangeDirectory);
  1953. assert(FTerminal->OnReadDirectory == TerminalReadDirectory);
  1954. assert(FTerminal->OnStartReadDirectory == TerminalStartReadDirectory);
  1955. assert(FTerminal->OnReadDirectoryProgress == TerminalReadDirectoryProgress);
  1956. assert(FTerminal->OnInitializeLog == TerminalInitializeLog);
  1957. FTerminal->OnInformation = FOnInformation;
  1958. FTerminal->OnQueryUser = FOnQueryUser;
  1959. FTerminal->OnPromptUser = FOnPromptUser;
  1960. FTerminal->OnShowExtendedException = FOnShowExtendedException;
  1961. FTerminal->OnDisplayBanner = FOnDisplayBanner;
  1962. FTerminal->OnChangeDirectory = FOnChangeDirectory;
  1963. FTerminal->OnReadDirectory = FOnReadDirectory;
  1964. FTerminal->OnStartReadDirectory = FOnStartReadDirectory;
  1965. FTerminal->OnReadDirectoryProgress = FOnReadDirectoryProgress;
  1966. FTerminal->OnInitializeLog = FOnInitializeLog;
  1967. delete FSection;
  1968. }
  1969. //---------------------------------------------------------------------------
  1970. void __fastcall TTerminalThread::Cancel()
  1971. {
  1972. FCancel = true;
  1973. }
  1974. //---------------------------------------------------------------------------
  1975. void __fastcall TTerminalThread::Idle()
  1976. {
  1977. TGuard Guard(FSection);
  1978. // only when running user action already,
  1979. // so that the exception is caught, saved and actually
  1980. // passed back into the terminal thread, saved again
  1981. // and passed back to us
  1982. if ((FUserAction != NULL) && (FIdleException != NULL))
  1983. {
  1984. Rethrow(FIdleException);
  1985. }
  1986. FPendingIdle = true;
  1987. }
  1988. //---------------------------------------------------------------------------
  1989. void __fastcall TTerminalThread::TerminalOpen()
  1990. {
  1991. RunAction(TerminalOpenEvent);
  1992. }
  1993. //---------------------------------------------------------------------------
  1994. void __fastcall TTerminalThread::TerminalReopen()
  1995. {
  1996. RunAction(TerminalReopenEvent);
  1997. }
  1998. //---------------------------------------------------------------------------
  1999. void __fastcall TTerminalThread::RunAction(TNotifyEvent Action)
  2000. {
  2001. assert(FAction == NULL);
  2002. assert(FException == NULL);
  2003. assert(FIdleException == NULL);
  2004. assert(FOnIdle != NULL);
  2005. FCancelled = false;
  2006. FAction = Action;
  2007. try
  2008. {
  2009. try
  2010. {
  2011. TriggerEvent();
  2012. bool Done = false;
  2013. do
  2014. {
  2015. switch (WaitForSingleObject(FActionEvent, 50))
  2016. {
  2017. case WAIT_OBJECT_0:
  2018. Done = true;
  2019. break;
  2020. case WAIT_TIMEOUT:
  2021. if (FUserAction != NULL)
  2022. {
  2023. try
  2024. {
  2025. FUserAction->Execute(NULL);
  2026. }
  2027. catch (Exception & E)
  2028. {
  2029. SaveException(E, FException);
  2030. }
  2031. FUserAction = NULL;
  2032. TriggerEvent();
  2033. }
  2034. else
  2035. {
  2036. if (FOnIdle != NULL)
  2037. {
  2038. FOnIdle(NULL);
  2039. }
  2040. }
  2041. break;
  2042. default:
  2043. throw Exception(L"Error waiting for background session task to complete");
  2044. }
  2045. }
  2046. while (!Done);
  2047. Rethrow(FException);
  2048. }
  2049. __finally
  2050. {
  2051. FAction = NULL;
  2052. SAFE_DESTROY(FException);
  2053. }
  2054. }
  2055. catch(...)
  2056. {
  2057. if (FCancelled)
  2058. {
  2059. // even if the abort thrown as result of Cancel() was wrapped into
  2060. // some higher-level exception, normalize back to message-less fatal
  2061. // exception here
  2062. FatalAbort();
  2063. }
  2064. else
  2065. {
  2066. throw;
  2067. }
  2068. }
  2069. }
  2070. //---------------------------------------------------------------------------
  2071. void __fastcall TTerminalThread::TerminalOpenEvent(TObject * /*Sender*/)
  2072. {
  2073. FTerminal->Open();
  2074. }
  2075. //---------------------------------------------------------------------------
  2076. void __fastcall TTerminalThread::TerminalReopenEvent(TObject * /*Sender*/)
  2077. {
  2078. FTerminal->Reopen(0);
  2079. }
  2080. //---------------------------------------------------------------------------
  2081. void __fastcall TTerminalThread::ProcessEvent()
  2082. {
  2083. assert(FEvent != NULL);
  2084. assert(FException == NULL);
  2085. try
  2086. {
  2087. FAction(NULL);
  2088. }
  2089. catch(Exception & E)
  2090. {
  2091. SaveException(E, FException);
  2092. }
  2093. SetEvent(FActionEvent);
  2094. }
  2095. //---------------------------------------------------------------------------
  2096. void __fastcall TTerminalThread::Rethrow(Exception *& Exception)
  2097. {
  2098. if (Exception != NULL)
  2099. {
  2100. try
  2101. {
  2102. RethrowException(Exception);
  2103. }
  2104. __finally
  2105. {
  2106. SAFE_DESTROY(Exception);
  2107. }
  2108. }
  2109. }
  2110. //---------------------------------------------------------------------------
  2111. void __fastcall TTerminalThread::SaveException(Exception & E, Exception *& Exception)
  2112. {
  2113. assert(Exception == NULL);
  2114. Exception = CloneException(&E);
  2115. }
  2116. //---------------------------------------------------------------------------
  2117. void __fastcall TTerminalThread::FatalAbort()
  2118. {
  2119. FTerminal->FatalAbort();
  2120. }
  2121. //---------------------------------------------------------------------------
  2122. void __fastcall TTerminalThread::CheckCancel()
  2123. {
  2124. if (FCancel && !FCancelled)
  2125. {
  2126. FCancelled = true;
  2127. FatalAbort();
  2128. }
  2129. }
  2130. //---------------------------------------------------------------------------
  2131. void __fastcall TTerminalThread::WaitForUserAction(TUserAction * UserAction)
  2132. {
  2133. DWORD Thread = GetCurrentThreadId();
  2134. // we can get called from the main thread from within Idle,
  2135. // should be only to call HandleExtendedException
  2136. if (Thread == FMainThread)
  2137. {
  2138. if (UserAction != NULL)
  2139. {
  2140. UserAction->Execute(NULL);
  2141. }
  2142. }
  2143. else
  2144. {
  2145. // we should be called from our thread only,
  2146. // with exception noted above
  2147. assert(Thread == FThreadId);
  2148. bool DoCheckCancel =
  2149. ALWAYS_FALSE(UserAction == NULL) || !UserAction->Force();
  2150. if (DoCheckCancel)
  2151. {
  2152. CheckCancel();
  2153. }
  2154. // have to save it as we can go recursive via TQueryParams::TimerEvent,
  2155. // see TTerminalThread::TerminalQueryUser
  2156. TUserAction * PrevUserAction = FUserAction;
  2157. try
  2158. {
  2159. FUserAction = UserAction;
  2160. while (true)
  2161. {
  2162. {
  2163. TGuard Guard(FSection);
  2164. // If idle exception is already set, we are only waiting
  2165. // for the main thread to pick it up
  2166. // (or at least to finish handling the user action, so
  2167. // that we rethrow the idle exception below)
  2168. // Also if idle exception is set, it is probable that terminal
  2169. // is not active anyway.
  2170. if (FTerminal->Active && FPendingIdle && (FIdleException == NULL))
  2171. {
  2172. FPendingIdle = false;
  2173. try
  2174. {
  2175. FTerminal->Idle();
  2176. }
  2177. catch (Exception & E)
  2178. {
  2179. SaveException(E, FIdleException);
  2180. }
  2181. }
  2182. }
  2183. int WaitResult = WaitForEvent(1000);
  2184. if (WaitResult == 0)
  2185. {
  2186. SAFE_DESTROY(FIdleException);
  2187. FatalAbort();
  2188. }
  2189. else if (WaitResult > 0)
  2190. {
  2191. break;
  2192. }
  2193. }
  2194. Rethrow(FException);
  2195. if (FIdleException != NULL)
  2196. {
  2197. // idle exception was not used to cancel the user action
  2198. // (if it where it would be already cloned into the FException above
  2199. // and rethrown)
  2200. Rethrow(FIdleException);
  2201. }
  2202. }
  2203. __finally
  2204. {
  2205. FUserAction = PrevUserAction;
  2206. SAFE_DESTROY(FException);
  2207. }
  2208. // Contrary to a call before, this is unconditional,
  2209. // otherwise cancelling authentication won't work,
  2210. // if it is tried only after the last user action
  2211. // (what is common, when cancelling while waiting for
  2212. // resolving of unresolvable host name, where the last user action is
  2213. // "resolving hostname" information action)
  2214. CheckCancel();
  2215. }
  2216. }
  2217. //---------------------------------------------------------------------------
  2218. void __fastcall TTerminalThread::TerminalInformation(
  2219. TTerminal * Terminal, const UnicodeString & Str, bool Status, int Phase)
  2220. {
  2221. TInformationUserAction Action(FOnInformation);
  2222. Action.Terminal = Terminal;
  2223. Action.Str = Str;
  2224. Action.Status = Status;
  2225. Action.Phase = Phase;
  2226. WaitForUserAction(&Action);
  2227. }
  2228. //---------------------------------------------------------------------------
  2229. void __fastcall TTerminalThread::TerminalQueryUser(TObject * Sender,
  2230. const UnicodeString Query, TStrings * MoreMessages, unsigned int Answers,
  2231. const TQueryParams * Params, unsigned int & Answer, TQueryType Type, void * Arg)
  2232. {
  2233. USEDPARAM(Arg);
  2234. assert(Arg == NULL);
  2235. // note about TQueryParams::TimerEvent
  2236. // So far there is only one use for this, the TSecureShell::SendBuffer,
  2237. // which should be thread-safe, as long as the terminal thread,
  2238. // is stopped waiting for OnQueryUser to finish.
  2239. // note about TQueryButtonAlias::OnClick
  2240. // So far there is only one use for this, the TClipboardHandler,
  2241. // which is thread-safe.
  2242. TQueryUserAction Action(FOnQueryUser);
  2243. Action.Sender = Sender;
  2244. Action.Query = Query;
  2245. Action.MoreMessages = MoreMessages;
  2246. Action.Answers = Answers;
  2247. Action.Params = Params;
  2248. Action.Answer = Answer;
  2249. Action.Type = Type;
  2250. WaitForUserAction(&Action);
  2251. Answer = Action.Answer;
  2252. }
  2253. //---------------------------------------------------------------------------
  2254. void __fastcall TTerminalThread::TerminalInitializeLog(TObject * Sender)
  2255. {
  2256. if (FOnInitializeLog != NULL)
  2257. {
  2258. // never used, so not tested either
  2259. FAIL;
  2260. TNotifyAction Action(FOnInitializeLog);
  2261. Action.Sender = Sender;
  2262. WaitForUserAction(&Action);
  2263. }
  2264. }
  2265. //---------------------------------------------------------------------------
  2266. void __fastcall TTerminalThread::TerminalPromptUser(TTerminal * Terminal,
  2267. TPromptKind Kind, UnicodeString Name, UnicodeString Instructions, TStrings * Prompts,
  2268. TStrings * Results, bool & Result, void * Arg)
  2269. {
  2270. USEDPARAM(Arg);
  2271. assert(Arg == NULL);
  2272. TPromptUserAction Action(FOnPromptUser);
  2273. Action.Terminal = Terminal;
  2274. Action.Kind = Kind;
  2275. Action.Name = Name;
  2276. Action.Instructions = Instructions;
  2277. Action.Prompts = Prompts;
  2278. Action.Results->AddStrings(Results);
  2279. WaitForUserAction(&Action);
  2280. Results->Clear();
  2281. Results->AddStrings(Action.Results);
  2282. Result = Action.Result;
  2283. }
  2284. //---------------------------------------------------------------------------
  2285. void __fastcall TTerminalThread::TerminalShowExtendedException(
  2286. TTerminal * Terminal, Exception * E, void * Arg)
  2287. {
  2288. USEDPARAM(Arg);
  2289. assert(Arg == NULL);
  2290. TShowExtendedExceptionAction Action(FOnShowExtendedException);
  2291. Action.Terminal = Terminal;
  2292. Action.E = E;
  2293. WaitForUserAction(&Action);
  2294. }
  2295. //---------------------------------------------------------------------------
  2296. void __fastcall TTerminalThread::TerminalDisplayBanner(TTerminal * Terminal,
  2297. UnicodeString SessionName, const UnicodeString & Banner,
  2298. bool & NeverShowAgain, int Options)
  2299. {
  2300. TDisplayBannerAction Action(FOnDisplayBanner);
  2301. Action.Terminal = Terminal;
  2302. Action.SessionName = SessionName;
  2303. Action.Banner = Banner;
  2304. Action.NeverShowAgain = NeverShowAgain;
  2305. Action.Options = Options;
  2306. WaitForUserAction(&Action);
  2307. NeverShowAgain = Action.NeverShowAgain;
  2308. }
  2309. //---------------------------------------------------------------------------
  2310. void __fastcall TTerminalThread::TerminalChangeDirectory(TObject * Sender)
  2311. {
  2312. TNotifyAction Action(FOnChangeDirectory);
  2313. Action.Sender = Sender;
  2314. WaitForUserAction(&Action);
  2315. }
  2316. //---------------------------------------------------------------------------
  2317. void __fastcall TTerminalThread::TerminalReadDirectory(TObject * Sender, Boolean ReloadOnly)
  2318. {
  2319. TReadDirectoryAction Action(FOnReadDirectory);
  2320. Action.Sender = Sender;
  2321. Action.ReloadOnly = ReloadOnly;
  2322. WaitForUserAction(&Action);
  2323. }
  2324. //---------------------------------------------------------------------------
  2325. void __fastcall TTerminalThread::TerminalStartReadDirectory(TObject * Sender)
  2326. {
  2327. TNotifyAction Action(FOnStartReadDirectory);
  2328. Action.Sender = Sender;
  2329. WaitForUserAction(&Action);
  2330. }
  2331. //---------------------------------------------------------------------------
  2332. void __fastcall TTerminalThread::TerminalReadDirectoryProgress(
  2333. TObject * Sender, int Progress, bool & Cancel)
  2334. {
  2335. TReadDirectoryProgressAction Action(FOnReadDirectoryProgress);
  2336. Action.Sender = Sender;
  2337. Action.Progress = Progress;
  2338. Action.Cancel = Cancel;
  2339. WaitForUserAction(&Action);
  2340. Cancel = Action.Cancel;
  2341. }
  2342. //---------------------------------------------------------------------------
  2343. #pragma warn -8080