Queue.cpp 92 KB

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