PseudoConsolePipe.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. using Microsoft.Win32.SafeHandles;
  2. using System;
  3. using static MiniTerm.Native.PseudoConsoleApi;
  4. namespace MiniTerm
  5. {
  6. /// <summary>
  7. /// A pipe used to talk to the pseudoconsole, as described in:
  8. /// https://docs.microsoft.com/en-us/windows/console/creating-a-pseudoconsole-session
  9. /// </summary>
  10. /// <remarks>
  11. /// We'll have two instances of this class, one for input and one for output.
  12. /// </remarks>
  13. internal sealed class PseudoConsolePipe : IDisposable
  14. {
  15. public readonly SafeFileHandle ReadSide;
  16. public readonly SafeFileHandle WriteSide;
  17. public PseudoConsolePipe()
  18. {
  19. if (!CreatePipe(out ReadSide, out WriteSide, IntPtr.Zero, 0))
  20. {
  21. throw new InvalidOperationException("failed to create pipe");
  22. }
  23. }
  24. #region IDisposable
  25. void Dispose(bool disposing)
  26. {
  27. if (disposing)
  28. {
  29. ReadSide?.Dispose();
  30. WriteSide?.Dispose();
  31. }
  32. }
  33. public void Dispose()
  34. {
  35. Dispose(true);
  36. GC.SuppressFinalize(this);
  37. }
  38. #endregion
  39. }
  40. }