ConsoleCommStruct.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. using System.Runtime.InteropServices;
  2. using System;
  3. using Microsoft.Win32.SafeHandles;
  4. namespace WinSCP
  5. {
  6. public enum ConsoleEvent { None, Print, Input, Choice, Title, Init, Progress }
  7. [StructLayout(LayoutKind.Sequential)]
  8. internal class ConsoleInitEventStruct
  9. {
  10. public uint InputType;
  11. public uint OutputType;
  12. public bool WantsProgress;
  13. };
  14. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
  15. internal class ConsolePrintEventStruct
  16. {
  17. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 10240)]
  18. public string Message; // wide since version 4
  19. [MarshalAs(UnmanagedType.I1)]
  20. public bool FromBeginning;
  21. };
  22. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
  23. internal class ConsoleInputEventStruct
  24. {
  25. [MarshalAs(UnmanagedType.I1)]
  26. public bool Echo;
  27. [MarshalAs(UnmanagedType.I1)]
  28. public bool Result;
  29. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 10240)]
  30. public string Str; // wide since version 4
  31. public uint Timer; // since version 2
  32. };
  33. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
  34. internal class ConsoleChoiceEventStruct
  35. {
  36. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)]
  37. public string Options; // wide since version 4
  38. public int Cancel;
  39. public int Break;
  40. public int Result;
  41. public int Timeouted; // since version 2
  42. public uint Timer; // since version 2
  43. [MarshalAs(UnmanagedType.I1)]
  44. public bool Timeouting; // since version 4
  45. }
  46. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
  47. internal class ConsoleTitleEventStruct
  48. {
  49. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 10240)]
  50. public string Title; // wide since version 4
  51. }
  52. // Since version 6
  53. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
  54. internal class ConsoleProgressEventStruct
  55. {
  56. public enum ProgressOperation { Copy }
  57. public enum ProgressSide { Local, Remote }
  58. public ProgressOperation Operation;
  59. public ProgressSide Side;
  60. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1024)]
  61. public string FileName;
  62. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1024)]
  63. public string Directory;
  64. public uint OverallProgress;
  65. public uint FileProgress;
  66. public uint CPS;
  67. }
  68. [StructLayout(LayoutKind.Sequential)]
  69. internal class ConsoleCommHeader
  70. {
  71. public uint Size;
  72. public int Version;
  73. public ConsoleEvent Event;
  74. }
  75. internal class ConsoleCommStruct : IDisposable
  76. {
  77. public const int CurrentVersion = 0x0006;
  78. public ConsoleCommStruct(Session session, SafeFileHandle fileMapping)
  79. {
  80. _session = session;
  81. _fileMapping = fileMapping;
  82. _ptr = UnsafeNativeMethods.MapViewOfFile(_fileMapping, FileMapAccess.FileMapAllAccess, 0, 0, UIntPtr.Zero);
  83. _payloadPtr = new IntPtr(_ptr.ToInt64() + 12);
  84. _header = (ConsoleCommHeader)Marshal.PtrToStructure(_ptr, typeof(ConsoleCommHeader));
  85. }
  86. ~ConsoleCommStruct()
  87. {
  88. Dispose();
  89. }
  90. public void Dispose()
  91. {
  92. if (_ptr != IntPtr.Zero)
  93. {
  94. if (_headerInvalidated)
  95. {
  96. Marshal.StructureToPtr(_header, _ptr, false);
  97. }
  98. if (_payload != null)
  99. {
  100. if ((Event != ConsoleEvent.Print) && (Event != ConsoleEvent.Title))
  101. {
  102. Marshal.StructureToPtr(_payload, _payloadPtr, false);
  103. }
  104. }
  105. if (!UnsafeNativeMethods.UnmapViewOfFile(_ptr))
  106. {
  107. throw new SessionLocalException(_session, "Cannot release file mapping");
  108. }
  109. _ptr = IntPtr.Zero;
  110. }
  111. GC.SuppressFinalize(this);
  112. }
  113. public ConsoleEvent Event
  114. {
  115. get
  116. {
  117. return _header.Event;
  118. }
  119. }
  120. public ConsolePrintEventStruct PrintEvent { get { return UnmarshalPayload<ConsolePrintEventStruct>(ConsoleEvent.Print); } }
  121. public ConsoleInitEventStruct InitEvent { get { return UnmarshalPayload<ConsoleInitEventStruct>(ConsoleEvent.Init); } }
  122. public ConsoleInputEventStruct InputEvent { get { return UnmarshalPayload<ConsoleInputEventStruct>(ConsoleEvent.Input); } }
  123. public ConsoleChoiceEventStruct ChoiceEvent { get { return UnmarshalPayload<ConsoleChoiceEventStruct>(ConsoleEvent.Choice); } }
  124. public ConsoleTitleEventStruct TitleEvent { get { return UnmarshalPayload<ConsoleTitleEventStruct>(ConsoleEvent.Title); } }
  125. public ConsoleProgressEventStruct ProgressEvent { get { return UnmarshalPayload<ConsoleProgressEventStruct>(ConsoleEvent.Progress); } }
  126. private T UnmarshalPayload<T>(ConsoleEvent e)
  127. {
  128. CheckNotDisposed();
  129. if (e != Event)
  130. {
  131. throw new InvalidOperationException();
  132. }
  133. if (_payload == null)
  134. {
  135. _payload = Marshal.PtrToStructure(_payloadPtr, typeof(T));
  136. }
  137. return (T)_payload;
  138. }
  139. private void CheckNotDisposed()
  140. {
  141. if (_ptr == IntPtr.Zero)
  142. {
  143. throw new InvalidOperationException();
  144. }
  145. }
  146. public static int Size
  147. {
  148. get
  149. {
  150. Type[] types =
  151. new[] {
  152. typeof(ConsolePrintEventStruct), typeof(ConsoleInitEventStruct), typeof(ConsoleInputEventStruct),
  153. typeof(ConsoleChoiceEventStruct), typeof(ConsoleTitleEventStruct), typeof(ConsoleProgressEventStruct) };
  154. int maxSize = 0;
  155. foreach (Type type in types)
  156. {
  157. maxSize = Math.Max(maxSize, Marshal.SizeOf(type));
  158. }
  159. return (Marshal.SizeOf(typeof(ConsoleCommHeader)) + maxSize);
  160. }
  161. }
  162. public void InitHeader()
  163. {
  164. _headerInvalidated = true;
  165. _header.Size = (uint) Size;
  166. _header.Version = CurrentVersion;
  167. _header.Event = ConsoleEvent.None;
  168. }
  169. private IntPtr _ptr;
  170. private readonly ConsoleCommHeader _header;
  171. private bool _headerInvalidated;
  172. private readonly IntPtr _payloadPtr;
  173. private object _payload;
  174. private readonly SafeFileHandle _fileMapping;
  175. private readonly Session _session;
  176. }
  177. }