ConsoleCommStruct.cs 7.8 KB

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