ConsoleCommStruct.cs 7.5 KB

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