ConsoleCommStruct.cs 7.7 KB

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