ConsoleCommStruct.cs 7.7 KB

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