Queue.cpp 92 KB

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