ExeSessionProcess.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Globalization;
  5. using System.IO;
  6. using System.Threading;
  7. using Microsoft.Win32;
  8. using Microsoft.Win32.SafeHandles;
  9. using System.Runtime.InteropServices;
  10. using System.Reflection;
  11. namespace WinSCP
  12. {
  13. internal class ExeSessionProcess : IDisposable
  14. {
  15. public event OutputDataReceivedEventHandler OutputDataReceived;
  16. public bool HasExited { get { return _process.HasExited; } }
  17. public int ExitCode { get { return _process.ExitCode; } }
  18. public ExeSessionProcess(Session session)
  19. {
  20. _session = session;
  21. _logger = session.Logger;
  22. _incompleteLine = string.Empty;
  23. using (_logger.CreateCallstack())
  24. {
  25. string executablePath = GetExecutablePath();
  26. _logger.WriteLine("EXE executable path resolved to {0}", executablePath);
  27. string assemblyFilePath = _logger.GetAssemblyFilePath();
  28. FileVersionInfo assemblyVersion = null;
  29. if (assemblyFilePath != null)
  30. {
  31. assemblyVersion = FileVersionInfo.GetVersionInfo(assemblyFilePath);
  32. }
  33. CheckVersion(executablePath, assemblyVersion);
  34. string configSwitch;
  35. if (_session.DefaultConfiguration)
  36. {
  37. configSwitch = "/ini=nul ";
  38. }
  39. else
  40. {
  41. if (!string.IsNullOrEmpty(_session.IniFilePath))
  42. {
  43. configSwitch = string.Format(CultureInfo.InvariantCulture, "/ini=\"{0}\" ", _session.IniFilePath);
  44. }
  45. else
  46. {
  47. configSwitch = "";
  48. }
  49. }
  50. string logSwitch = null;
  51. if (!string.IsNullOrEmpty(_session.SessionLogPath))
  52. {
  53. logSwitch = string.Format(CultureInfo.InvariantCulture, "/log=\"{0}\" ", LogPathEscape(_session.SessionLogPath));
  54. }
  55. string xmlLogSwitch = string.Format(CultureInfo.InvariantCulture, "/xmllog=\"{0}\" ", LogPathEscape(_session.XmlLogPath));
  56. string assemblyVersionStr =
  57. (assemblyVersion == null) ? "unk" :
  58. string.Format(CultureInfo.InvariantCulture, "{0}{1}{2} ", assemblyVersion.ProductMajorPart, assemblyVersion.ProductMinorPart, assemblyVersion.ProductBuildPart);
  59. string assemblyVersionSwitch =
  60. string.Format(CultureInfo.InvariantCulture, "/dotnet={0} ", assemblyVersionStr);
  61. string arguments =
  62. xmlLogSwitch + "/xmlgroups /nointeractiveinput " + assemblyVersionSwitch +
  63. configSwitch + logSwitch + _session.AdditionalExecutableArguments;
  64. Tools.AddRawParameters(ref arguments, _session.RawConfiguration, "/rawconfig");
  65. _process = new Process();
  66. _process.StartInfo.FileName = executablePath;
  67. _process.StartInfo.WorkingDirectory = Path.GetDirectoryName(executablePath);
  68. _process.StartInfo.Arguments = arguments;
  69. _process.StartInfo.UseShellExecute = false;
  70. _process.Exited += ProcessExited;
  71. if (_logger.Logging)
  72. {
  73. _process.OutputDataReceived += ProcessOutputDataReceived;
  74. _process.ErrorDataReceived += ProcessErrorDataReceived;
  75. }
  76. }
  77. }
  78. private static string LogPathEscape(string path)
  79. {
  80. return Tools.ArgumentEscape(path).Replace("!", "!!");
  81. }
  82. public void Abort()
  83. {
  84. using (_logger.CreateCallstack())
  85. {
  86. lock (_lock)
  87. {
  88. if ((_process != null) && !_process.HasExited)
  89. {
  90. _process.Kill();
  91. }
  92. }
  93. }
  94. }
  95. public void Start()
  96. {
  97. using (_logger.CreateCallstack())
  98. {
  99. InitializeConsole();
  100. InitializeChild();
  101. }
  102. }
  103. private void InitializeChild()
  104. {
  105. using (_logger.CreateCallstack())
  106. {
  107. _process.StartInfo.Arguments += string.Format(CultureInfo.InvariantCulture, " /console /consoleinstance={0}", _instanceName);
  108. _logger.WriteLine("Starting \"{0}\" {1}", _process.StartInfo.FileName, _process.StartInfo.Arguments);
  109. _process.Start();
  110. _logger.WriteLine("Started process {0}", _process.Id);
  111. _thread = new Thread(ProcessEvents);
  112. _thread.IsBackground = true;
  113. _thread.Start();
  114. }
  115. }
  116. private void ProcessExited(object sender, EventArgs e)
  117. {
  118. _logger.WriteLine("Process {0} exited with exit code {1}", _process.Id, _process.ExitCode);
  119. }
  120. private void ProcessOutputDataReceived(object sender, DataReceivedEventArgs e)
  121. {
  122. _logger.WriteLine("Process output: {0}", e.Data);
  123. }
  124. private void ProcessErrorDataReceived(object sender, DataReceivedEventArgs e)
  125. {
  126. _logger.WriteLine("Process error output: {0}", e.Data);
  127. }
  128. private bool AbortedOrExited()
  129. {
  130. if (_abort)
  131. {
  132. _logger.WriteLine("Aborted");
  133. return true;
  134. }
  135. else if (_process.HasExited)
  136. {
  137. _logger.WriteLine("Exited");
  138. return true;
  139. }
  140. else
  141. {
  142. return false;
  143. }
  144. }
  145. private void ProcessEvents()
  146. {
  147. using (_logger.CreateCallstack())
  148. {
  149. while (!AbortedOrExited())
  150. {
  151. if (_requestEvent.WaitOne(100, false))
  152. {
  153. ProcessEvent();
  154. }
  155. }
  156. }
  157. }
  158. private void ProcessEvent()
  159. {
  160. using (_logger.CreateCallstack())
  161. {
  162. using (ConsoleCommStruct commStruct = AcquireCommStruct())
  163. {
  164. switch (commStruct.Event)
  165. {
  166. case ConsoleEvent.Print:
  167. ProcessPrintEvent(commStruct.PrintEvent);
  168. break;
  169. case ConsoleEvent.Input:
  170. ProcessInputEvent(commStruct.InputEvent);
  171. break;
  172. case ConsoleEvent.Choice:
  173. ProcessChoiceEvent(commStruct.ChoiceEvent);
  174. break;
  175. case ConsoleEvent.Title:
  176. ProcessTitleEvent(commStruct.TitleEvent);
  177. break;
  178. case ConsoleEvent.Init:
  179. ProcessInitEvent(commStruct.InitEvent);
  180. break;
  181. case ConsoleEvent.Progress:
  182. ProcessProgressEvent(commStruct.ProgressEvent);
  183. break;
  184. default:
  185. throw new NotImplementedException();
  186. }
  187. }
  188. _responseEvent.Set();
  189. }
  190. }
  191. private void ProcessChoiceEvent(ConsoleChoiceEventStruct e)
  192. {
  193. using (_logger.CreateCallstack())
  194. {
  195. if (e.Timeouting)
  196. {
  197. Thread.Sleep((int)e.Timer);
  198. e.Result = e.Timeouted;
  199. }
  200. else
  201. {
  202. e.Result = e.Break;
  203. }
  204. }
  205. }
  206. private void ProcessTitleEvent(ConsoleTitleEventStruct e)
  207. {
  208. using (_logger.CreateCallstack())
  209. {
  210. _logger.WriteLine("Not-supported title event [{0}]", e.Title);
  211. }
  212. }
  213. private void ProcessInputEvent(ConsoleInputEventStruct e)
  214. {
  215. using (_logger.CreateCallstack())
  216. {
  217. while (!AbortedOrExited())
  218. {
  219. lock (_input)
  220. {
  221. if (_input.Count > 0)
  222. {
  223. e.Str = _input[0];
  224. e.Result = true;
  225. _input.RemoveAt(0);
  226. Print(false, e.Str + "\n");
  227. return;
  228. }
  229. }
  230. _inputEvent.WaitOne(100, false);
  231. }
  232. }
  233. }
  234. private void Print(bool fromBeginning, string message)
  235. {
  236. if (fromBeginning && ((message.Length == 0) || (message[0] != '\n')))
  237. {
  238. _lastFromBeginning = message;
  239. _logger.WriteLine("Buffered from-beginning message [{0}]", _lastFromBeginning);
  240. }
  241. else
  242. {
  243. if (!string.IsNullOrEmpty(_lastFromBeginning))
  244. {
  245. AddToOutput(_lastFromBeginning);
  246. _lastFromBeginning = null;
  247. }
  248. if (fromBeginning && (message.Length > 0) && (message[0] == '\n'))
  249. {
  250. AddToOutput("\n");
  251. _lastFromBeginning = message.Substring(1);
  252. _logger.WriteLine("Buffered from-beginning message [{0}]", _lastFromBeginning);
  253. }
  254. else
  255. {
  256. AddToOutput(message);
  257. }
  258. }
  259. }
  260. private void AddToOutput(string message)
  261. {
  262. string[] lines = (_incompleteLine + message).Split(new[] { '\n' });
  263. _incompleteLine = lines[lines.Length - 1];
  264. for (int i = 0; i < lines.Length - 1; ++i)
  265. {
  266. if (OutputDataReceived != null)
  267. {
  268. OutputDataReceived(this, new OutputDataReceivedEventArgs(lines[i]));
  269. }
  270. }
  271. }
  272. private void ProcessPrintEvent(ConsolePrintEventStruct e)
  273. {
  274. Print(e.FromBeginning, e.Message);
  275. }
  276. private void ProcessInitEvent(ConsoleInitEventStruct e)
  277. {
  278. using (_logger.CreateCallstack())
  279. {
  280. e.InputType = 3; // pipe
  281. e.OutputType = 3; // pipe
  282. e.WantsProgress = _session.WantsProgress;
  283. }
  284. }
  285. private void ProcessProgressEvent(ConsoleProgressEventStruct e)
  286. {
  287. using (_logger.CreateCallstack())
  288. {
  289. _logger.WriteLine(
  290. "File Name [{0}] - Directory [{1}] - Overall Progress [{2}] - File Progress [{3}] - CPS [{4}]",
  291. e.FileName, e.Directory, e.OverallProgress, e.FileProgress, e.CPS);
  292. FileTransferProgressEventArgs args = new FileTransferProgressEventArgs();
  293. switch (e.Operation)
  294. {
  295. case ConsoleProgressEventStruct.ProgressOperation.Copy:
  296. args.Operation = ProgressOperation.Transfer;
  297. break;
  298. default:
  299. throw new ArgumentOutOfRangeException("Unknown progress operation", (Exception)null);
  300. }
  301. switch (e.Side)
  302. {
  303. case ConsoleProgressEventStruct.ProgressSide.Local:
  304. args.Side = ProgressSide.Local;
  305. break;
  306. case ConsoleProgressEventStruct.ProgressSide.Remote:
  307. args.Side = ProgressSide.Remote;
  308. break;
  309. default:
  310. throw new ArgumentOutOfRangeException("Unknown progress side", (Exception)null);
  311. }
  312. args.FileName = e.FileName;
  313. args.Directory = e.Directory;
  314. args.OverallProgress = ((double)e.OverallProgress) / 100;
  315. args.FileProgress = ((double)e.FileProgress) / 100;
  316. args.CPS = (int) e.CPS;
  317. _session.ProcessProgress(args);
  318. }
  319. }
  320. private void InitializeConsole()
  321. {
  322. using (_logger.CreateCallstack())
  323. {
  324. int attempts = 0;
  325. Random random = new Random();
  326. int process = Process.GetCurrentProcess().Id;
  327. do
  328. {
  329. if (attempts > MaxAttempts)
  330. {
  331. throw new SessionLocalException(_session, "Cannot find unique name for event object.");
  332. }
  333. int instanceNumber = random.Next(1000);
  334. _instanceName = string.Format(CultureInfo.InvariantCulture, "_{0}_{1}_{2}", process, GetHashCode(), instanceNumber);
  335. _logger.WriteLine("Trying event {0}", _instanceName);
  336. if (!TryCreateEvent(ConsoleEventRequest + _instanceName, out _requestEvent))
  337. {
  338. _logger.WriteLine("Event {0} is not unique", _instanceName);
  339. _requestEvent.Close();
  340. _requestEvent = null;
  341. }
  342. else
  343. {
  344. _logger.WriteLine("Event {0} is unique", _instanceName);
  345. _responseEvent = CreateEvent(ConsoleEventResponse + _instanceName);
  346. _cancelEvent = CreateEvent(ConsoleEventCancel + _instanceName);
  347. string fileMappingName = ConsoleMapping + _instanceName;
  348. _fileMapping = CreateFileMapping(fileMappingName);
  349. if (Marshal.GetLastWin32Error() == UnsafeNativeMethods.ERROR_ALREADY_EXISTS)
  350. {
  351. throw new SessionLocalException(_session, string.Format(CultureInfo.InvariantCulture, "File mapping {0} already exists", fileMappingName));
  352. }
  353. if (_fileMapping.IsInvalid)
  354. {
  355. throw new SessionLocalException(_session, string.Format(CultureInfo.InvariantCulture, "Cannot create file mapping {0}", fileMappingName));
  356. }
  357. }
  358. ++attempts;
  359. }
  360. while (_requestEvent == null);
  361. using (ConsoleCommStruct commStruct = AcquireCommStruct())
  362. {
  363. commStruct.InitHeader();
  364. }
  365. if (_session.GuardProcessWithJobInternal)
  366. {
  367. string jobName = ConsoleJob + _instanceName;
  368. _job = new Job(_logger, jobName);
  369. }
  370. }
  371. }
  372. private static SafeFileHandle CreateFileMapping(string fileMappingName)
  373. {
  374. return
  375. UnsafeNativeMethods.CreateFileMapping(
  376. new SafeFileHandle(new IntPtr(-1), true), IntPtr.Zero, FileMapProtection.PageReadWrite, 0,
  377. ConsoleCommStruct.Size, fileMappingName);
  378. }
  379. private ConsoleCommStruct AcquireCommStruct()
  380. {
  381. return new ConsoleCommStruct(_session, _fileMapping);
  382. }
  383. private bool TryCreateEvent(string name, out EventWaitHandle ev)
  384. {
  385. bool createdNew;
  386. _logger.WriteLine("Creating event {0}", name);
  387. ev = new EventWaitHandle(false, EventResetMode.AutoReset, name, out createdNew);
  388. _logger.WriteLine("Created event {0} with handle {1}, new {2}", name, ev.SafeWaitHandle.DangerousGetHandle(), createdNew);
  389. return createdNew;
  390. }
  391. private EventWaitHandle CreateEvent(string name)
  392. {
  393. EventWaitHandle ev;
  394. if (!TryCreateEvent(name, out ev))
  395. {
  396. throw new SessionLocalException(_session, string.Format(CultureInfo.InvariantCulture, "Event {0} already exists", name));
  397. }
  398. return ev;
  399. }
  400. private void TestEventClosed(string name)
  401. {
  402. if (_session.TestHandlesClosedInternal)
  403. {
  404. _logger.WriteLine("Testing that event {0} is closed", name);
  405. EventWaitHandle ev;
  406. if (TryCreateEvent(name, out ev))
  407. {
  408. ev.Close();
  409. }
  410. else
  411. {
  412. _logger.WriteLine("Exception: Event {0} was not closed yet", name);
  413. }
  414. }
  415. }
  416. private void AddInput(string str)
  417. {
  418. Type structType = typeof(ConsoleInputEventStruct);
  419. FieldInfo strField = structType.GetField("Str");
  420. object[] attributes = strField.GetCustomAttributes(typeof(MarshalAsAttribute), false);
  421. if (attributes.Length != 1)
  422. {
  423. throw new InvalidOperationException("MarshalAs attribute not found for ConsoleInputEventStruct.Str");
  424. }
  425. MarshalAsAttribute marshalAsAttribute = (MarshalAsAttribute)attributes[0];
  426. if (marshalAsAttribute.SizeConst <= str.Length)
  427. {
  428. throw new SessionLocalException(
  429. _session,
  430. string.Format(CultureInfo.CurrentCulture, "Input [{0}] is too long ({1} limit)", str, marshalAsAttribute.SizeConst));
  431. }
  432. lock (_input)
  433. {
  434. _input.Add(str);
  435. _inputEvent.Set();
  436. }
  437. }
  438. public void ExecuteCommand(string command)
  439. {
  440. using (_logger.CreateCallstack())
  441. {
  442. AddInput(command);
  443. }
  444. }
  445. public void Close()
  446. {
  447. using (_logger.CreateCallstack())
  448. {
  449. int timeout;
  450. #if DEBUG
  451. // in debug build, we expect the winscp.exe to run in tracing mode, being very slow
  452. timeout = 10000;
  453. #else
  454. timeout = 2000;
  455. #endif
  456. _logger.WriteLine("Waiting for process to exit ({0} ms)", timeout);
  457. if (!_process.WaitForExit(timeout))
  458. {
  459. _logger.WriteLine("Killing process");
  460. _process.Kill();
  461. }
  462. }
  463. }
  464. public void Dispose()
  465. {
  466. using (_logger.CreateCallstack())
  467. {
  468. lock (_lock)
  469. {
  470. if (_session.TestHandlesClosedInternal)
  471. {
  472. _logger.WriteLine("Will test that handles are closed");
  473. }
  474. _abort = true;
  475. if (_thread != null)
  476. {
  477. _thread.Join();
  478. _thread = null;
  479. }
  480. if (_process != null)
  481. {
  482. _process.Dispose();
  483. _process = null;
  484. }
  485. if (_requestEvent != null)
  486. {
  487. _requestEvent.Close();
  488. TestEventClosed(ConsoleEventRequest + _instanceName);
  489. }
  490. if (_responseEvent != null)
  491. {
  492. _responseEvent.Close();
  493. TestEventClosed(ConsoleEventResponse + _instanceName);
  494. }
  495. if (_cancelEvent != null)
  496. {
  497. _cancelEvent.Close();
  498. TestEventClosed(ConsoleEventCancel + _instanceName);
  499. }
  500. if (_fileMapping != null)
  501. {
  502. _fileMapping.Dispose();
  503. _fileMapping = null;
  504. if (_session.TestHandlesClosedInternal)
  505. {
  506. _logger.WriteLine("Testing that file mapping is closed");
  507. string fileMappingName = ConsoleMapping + _instanceName;
  508. SafeFileHandle fileMapping = CreateFileMapping(fileMappingName);
  509. if (Marshal.GetLastWin32Error() == UnsafeNativeMethods.ERROR_ALREADY_EXISTS)
  510. {
  511. _logger.WriteLine("Exception: File mapping {0} was not closed yet", fileMappingName);
  512. }
  513. if (!fileMapping.IsInvalid)
  514. {
  515. fileMapping.Dispose();
  516. }
  517. }
  518. }
  519. if (_inputEvent != null)
  520. {
  521. _inputEvent.Close();
  522. _inputEvent = null;
  523. }
  524. if (_job != null)
  525. {
  526. _job.Dispose();
  527. _job = null;
  528. }
  529. }
  530. }
  531. }
  532. private string GetExecutablePath()
  533. {
  534. using (_logger.CreateCallstack())
  535. {
  536. string executablePath;
  537. if (!string.IsNullOrEmpty(_session.ExecutablePath))
  538. {
  539. executablePath = _session.ExecutablePath;
  540. if (!File.Exists(executablePath))
  541. {
  542. throw new SessionLocalException(_session, string.Format(CultureInfo.CurrentCulture, "{0} does not exists.", executablePath));
  543. }
  544. }
  545. else
  546. {
  547. if (!TryFindExecutableInPath(GetAssemblyPath(), out executablePath) &&
  548. !TryFindExecutableInPath(GetInstallationPath(Registry.CurrentUser), out executablePath) &&
  549. !TryFindExecutableInPath(GetInstallationPath(Registry.LocalMachine), out executablePath) &&
  550. !TryFindExecutableInPath(GetDefaultInstallationPath(), out executablePath))
  551. {
  552. throw new SessionLocalException(_session,
  553. string.Format(CultureInfo.CurrentCulture,
  554. "The {0} executable was not found at location of the assembly ({1}), nor in an installation path. You may use Session.ExecutablePath property to explicitly set path to {0}.",
  555. ExeExecutableFileName, GetAssemblyPath()));
  556. }
  557. }
  558. return executablePath;
  559. }
  560. }
  561. private static string GetDefaultInstallationPath()
  562. {
  563. return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "WinSCP");
  564. }
  565. private static string GetInstallationPath(RegistryKey rootKey)
  566. {
  567. RegistryKey key = rootKey.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Uninstall\winscp3_is1");
  568. return (key != null) ? (string)key.GetValue("Inno Setup: App Path") : null;
  569. }
  570. private bool TryFindExecutableInPath(string path, out string result)
  571. {
  572. if (string.IsNullOrEmpty(path))
  573. {
  574. result = null;
  575. }
  576. else
  577. {
  578. string executablePath = Path.Combine(path, ExeExecutableFileName);
  579. if (File.Exists(executablePath))
  580. {
  581. result = executablePath;
  582. _logger.WriteLine("Executable found in {0}", executablePath);
  583. }
  584. else
  585. {
  586. result = null;
  587. _logger.WriteLine("Executable not found in {0}", executablePath);
  588. }
  589. }
  590. return (result != null);
  591. }
  592. private string GetAssemblyPath()
  593. {
  594. string codeBasePath = _logger.GetAssemblyFilePath();
  595. string path = null;
  596. if (!string.IsNullOrEmpty(codeBasePath))
  597. {
  598. path = Path.GetDirectoryName(codeBasePath);
  599. Debug.Assert(path != null);
  600. }
  601. return path;
  602. }
  603. private void CheckVersion(string exePath, FileVersionInfo assemblyVersion)
  604. {
  605. using (_logger.CreateCallstack())
  606. {
  607. FileVersionInfo version = FileVersionInfo.GetVersionInfo(exePath);
  608. _logger.WriteLine("Version of {0} is {1}, product {2} version is {3}", exePath, version.FileVersion, version.ProductName, version.ProductVersion);
  609. if (_session.DisableVersionCheck)
  610. {
  611. _logger.WriteLine("Version check disabled (not recommended)");
  612. }
  613. else if (assemblyVersion == null)
  614. {
  615. _logger.WriteLine("Assembly version not known, cannot check version");
  616. }
  617. else if (assemblyVersion.ProductVersion != version.ProductVersion)
  618. {
  619. throw new SessionLocalException(
  620. _session, string.Format(CultureInfo.CurrentCulture,
  621. "The version of {0} ({1}) does not match version of this assembly {2} ({3}). You can disable this check using Session.DisableVersionCheck (not recommended).",
  622. exePath, version.ProductVersion, _logger.GetAssemblyFilePath(), assemblyVersion.ProductVersion));
  623. }
  624. }
  625. }
  626. public void WriteStatus()
  627. {
  628. string executablePath = GetExecutablePath();
  629. _logger.WriteLine("{0} - exists [{1}]", executablePath, File.Exists(executablePath));
  630. }
  631. private const int MaxAttempts = 10;
  632. private const string ConsoleMapping = "WinSCPConsoleMapping";
  633. private const string ConsoleEventRequest = "WinSCPConsoleEventRequest";
  634. private const string ConsoleEventResponse = "WinSCPConsoleEventResponse";
  635. private const string ConsoleEventCancel = "WinSCPConsoleEventCancel";
  636. private const string ConsoleJob = "WinSCPConsoleJob";
  637. private const string ExeExecutableFileName = "winscp.exe";
  638. private Process _process;
  639. private readonly object _lock = new object();
  640. private readonly Logger _logger;
  641. private readonly Session _session;
  642. private EventWaitHandle _requestEvent;
  643. private EventWaitHandle _responseEvent;
  644. private EventWaitHandle _cancelEvent;
  645. private SafeFileHandle _fileMapping;
  646. private string _instanceName;
  647. private Thread _thread;
  648. private bool _abort;
  649. private string _lastFromBeginning;
  650. private string _incompleteLine;
  651. private readonly List<string> _input = new List<string>();
  652. private AutoResetEvent _inputEvent = new AutoResetEvent(false);
  653. private Job _job;
  654. }
  655. }