ConsoleCommStruct.cs 7.5 KB

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