ConsoleCommStruct.cs 7.3 KB

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