PageRenderTime 164ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/mcs/class/System.Core/System.IO.Pipes/PipeWin32.cs

https://github.com/cschlote/mono
C# | 342 lines | 238 code | 56 blank | 48 comment | 34 complexity | 7b3520f9cbe61f5bf22e14f0f3959efa MD5 | raw file
  1. //
  2. // PipeWin32.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <atsushi@ximian.com>
  6. //
  7. // Copyright (C) 2009 Novell, Inc. http://www.novell.com
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.ComponentModel;
  30. using System.IO;
  31. using System.Linq;
  32. using System.Runtime.InteropServices;
  33. using System.Security.AccessControl;
  34. using System.Security.Permissions;
  35. using System.Security.Principal;
  36. using System.Text;
  37. using Microsoft.Win32;
  38. using Microsoft.Win32.SafeHandles;
  39. namespace System.IO.Pipes
  40. {
  41. abstract class Win32AnonymousPipe : IPipe
  42. {
  43. protected Win32AnonymousPipe ()
  44. {
  45. }
  46. public abstract SafePipeHandle Handle { get; }
  47. public void WaitForPipeDrain ()
  48. {
  49. throw new NotImplementedException ();
  50. }
  51. }
  52. class Win32AnonymousPipeClient : Win32AnonymousPipe, IAnonymousPipeClient
  53. {
  54. // AnonymousPipeClientStream owner;
  55. public Win32AnonymousPipeClient (AnonymousPipeClientStream owner, SafePipeHandle handle)
  56. {
  57. // this.owner = owner;
  58. this.handle = handle;
  59. }
  60. SafePipeHandle handle;
  61. public override SafePipeHandle Handle {
  62. get { return handle; }
  63. }
  64. }
  65. class Win32AnonymousPipeServer : Win32AnonymousPipe, IAnonymousPipeServer
  66. {
  67. // AnonymousPipeServerStream owner;
  68. public Win32AnonymousPipeServer (AnonymousPipeServerStream owner, PipeDirection direction, HandleInheritability inheritability, int bufferSize)
  69. {
  70. IntPtr r, w;
  71. SecurityAttributesHack att = new SecurityAttributesHack (inheritability == HandleInheritability.Inheritable);
  72. if (!Win32Marshal.CreatePipe (out r, out w, ref att, bufferSize))
  73. throw new Win32Exception (Marshal.GetLastWin32Error ());
  74. var rh = new SafePipeHandle (r, true);
  75. var wh = new SafePipeHandle (w, true);
  76. if (direction == PipeDirection.Out) {
  77. server_handle = wh;
  78. client_handle = rh;
  79. } else {
  80. server_handle = rh;
  81. client_handle = wh;
  82. }
  83. }
  84. public Win32AnonymousPipeServer (AnonymousPipeServerStream owner, SafePipeHandle serverHandle, SafePipeHandle clientHandle)
  85. {
  86. // this.owner = owner;
  87. this.server_handle = serverHandle;
  88. this.client_handle = clientHandle;
  89. }
  90. SafePipeHandle server_handle, client_handle;
  91. public override SafePipeHandle Handle {
  92. get { return server_handle; }
  93. }
  94. public SafePipeHandle ClientHandle {
  95. get { return client_handle; }
  96. }
  97. public void DisposeLocalCopyOfClientHandle ()
  98. {
  99. throw new NotImplementedException ();
  100. }
  101. }
  102. abstract class Win32NamedPipe : IPipe
  103. {
  104. string name_cache;
  105. public string Name {
  106. get {
  107. if (name_cache != null)
  108. return name_cache;
  109. int s, c, m, t;
  110. byte [] un = new byte [200];
  111. while (true) {
  112. if (!Win32Marshal.GetNamedPipeHandleState (Handle, out s, out c, out m, out t, un, un.Length)) {
  113. var xxx = Marshal.GetLastWin32Error ();
  114. throw new Win32Exception (xxx);
  115. }
  116. if (un [un.Length - 1] == 0)
  117. break;
  118. un = new byte [un.Length * 10];
  119. }
  120. name_cache = Encoding.Default.GetString (un);
  121. return name_cache;
  122. }
  123. }
  124. public abstract SafePipeHandle Handle { get; }
  125. public void WaitForPipeDrain ()
  126. {
  127. throw new NotImplementedException ();
  128. }
  129. }
  130. class Win32NamedPipeClient : Win32NamedPipe, INamedPipeClient
  131. {
  132. NamedPipeClientStream owner;
  133. // .ctor with existing handle
  134. public Win32NamedPipeClient (NamedPipeClientStream owner, SafePipeHandle safePipeHandle)
  135. {
  136. this.handle = safePipeHandle;
  137. this.owner = owner;
  138. // FIXME: retrieve is_async from state?
  139. }
  140. // .ctor without handle - create new
  141. public Win32NamedPipeClient (NamedPipeClientStream owner, string serverName, string pipeName, PipeAccessRights desiredAccessRights, PipeOptions options, HandleInheritability inheritability)
  142. {
  143. name = String.Format ("\\\\{0}\\pipe\\{1}", serverName, pipeName);
  144. var att = new SecurityAttributesHack (inheritability == HandleInheritability.Inheritable);
  145. is_async = (options & PipeOptions.Asynchronous) != PipeOptions.None;
  146. opener = delegate {
  147. var ret = Win32Marshal.CreateFile (name, desiredAccessRights, 0, ref att, 3, 0, IntPtr.Zero);
  148. if (ret == new IntPtr (-1L))
  149. throw new Win32Exception (Marshal.GetLastWin32Error ());
  150. return new SafePipeHandle (ret, true);
  151. };
  152. this.owner = owner;
  153. }
  154. Func<SafePipeHandle> opener;
  155. bool is_async;
  156. string name;
  157. SafePipeHandle handle;
  158. public override SafePipeHandle Handle {
  159. get { return handle; }
  160. }
  161. public bool IsAsync {
  162. get { return is_async; }
  163. }
  164. public void Connect ()
  165. {
  166. if (owner.IsConnected)
  167. throw new InvalidOperationException ("The named pipe is already connected");
  168. handle = opener ();
  169. }
  170. public void Connect (int timeout)
  171. {
  172. if (owner.IsConnected)
  173. throw new InvalidOperationException ("The named pipe is already connected");
  174. if (!Win32Marshal.WaitNamedPipe (name, timeout))
  175. throw new Win32Exception (Marshal.GetLastWin32Error ());
  176. Connect ();
  177. }
  178. public int NumberOfServerInstances {
  179. get {
  180. int s, c, m, t;
  181. byte [] un = null;
  182. if (!Win32Marshal.GetNamedPipeHandleState (Handle, out s, out c, out m, out t, un, 0))
  183. throw new Win32Exception (Marshal.GetLastWin32Error ());
  184. return c;
  185. }
  186. }
  187. }
  188. class Win32NamedPipeServer : Win32NamedPipe, INamedPipeServer
  189. {
  190. //NamedPipeServerStream owner;
  191. // .ctor with existing handle
  192. public Win32NamedPipeServer (NamedPipeServerStream owner, SafePipeHandle safePipeHandle)
  193. {
  194. handle = safePipeHandle;
  195. //this.owner = owner;
  196. }
  197. // .ctor without handle - create new
  198. public Win32NamedPipeServer (NamedPipeServerStream owner, string pipeName, int maxNumberOfServerInstances, PipeTransmissionMode transmissionMode, PipeAccessRights rights, PipeOptions options, int inBufferSize, int outBufferSize, HandleInheritability inheritability)
  199. {
  200. string name = String.Format ("\\\\.\\pipe\\{0}", pipeName);
  201. uint openMode = 0;
  202. if ((rights & PipeAccessRights.ReadData) != 0)
  203. openMode |= 1;
  204. if ((rights & PipeAccessRights.WriteData) != 0)
  205. openMode |= 2;
  206. if ((options & PipeOptions.WriteThrough) != 0)
  207. openMode |= 0x80000000;
  208. int pipeMode = 0;
  209. if ((owner.TransmissionMode & PipeTransmissionMode.Message) != 0)
  210. pipeMode |= 4;
  211. //if ((readTransmissionMode & PipeTransmissionMode.Message) != 0)
  212. // pipeMode |= 2;
  213. if ((options & PipeOptions.Asynchronous) != 0)
  214. pipeMode |= 1;
  215. // FIXME: is nDefaultTimeout = 0 ok?
  216. var att = new SecurityAttributesHack (inheritability == HandleInheritability.Inheritable);
  217. var ret = Win32Marshal.CreateNamedPipe (name, openMode, pipeMode, maxNumberOfServerInstances, outBufferSize, inBufferSize, 0, ref att, IntPtr.Zero);
  218. if (ret == new IntPtr (-1L))
  219. throw new Win32Exception (Marshal.GetLastWin32Error ());
  220. handle = new SafePipeHandle (ret, true);
  221. }
  222. SafePipeHandle handle;
  223. public override SafePipeHandle Handle {
  224. get { return handle; }
  225. }
  226. public void Disconnect ()
  227. {
  228. Win32Marshal.DisconnectNamedPipe (Handle);
  229. }
  230. public void WaitForConnection ()
  231. {
  232. if (!Win32Marshal.ConnectNamedPipe (Handle, IntPtr.Zero))
  233. throw new Win32Exception (Marshal.GetLastWin32Error ());
  234. }
  235. }
  236. [StructLayout (LayoutKind.Sequential)]
  237. struct SecurityAttributesHack
  238. {
  239. public readonly int Length;
  240. public readonly IntPtr SecurityDescriptor;
  241. public readonly bool Inheritable;
  242. public SecurityAttributesHack (bool inheritable)
  243. {
  244. Length = 0;
  245. SecurityDescriptor = IntPtr.Zero;
  246. Inheritable = inheritable;
  247. }
  248. }
  249. static class Win32Marshal
  250. {
  251. internal static bool IsWindows {
  252. get {
  253. switch (Environment.OSVersion.Platform) {
  254. case PlatformID.Win32S:
  255. case PlatformID.Win32Windows:
  256. case PlatformID.Win32NT:
  257. case PlatformID.WinCE:
  258. return true;
  259. default:
  260. return false;
  261. }
  262. }
  263. }
  264. // http://msdn.microsoft.com/en-us/library/aa365152%28VS.85%29.aspx
  265. [DllImport ("kernel32")]
  266. internal static extern bool CreatePipe (out IntPtr readHandle, out IntPtr writeHandle, ref SecurityAttributesHack pipeAtts, int size);
  267. // http://msdn.microsoft.com/en-us/library/aa365150%28VS.85%29.aspx
  268. [DllImport ("kernel32")]
  269. internal static extern IntPtr CreateNamedPipe (string name, uint openMode, int pipeMode, int maxInstances, int outBufferSize, int inBufferSize, int defaultTimeout, ref SecurityAttributesHack securityAttributes, IntPtr atts);
  270. // http://msdn.microsoft.com/en-us/library/aa365146%28VS.85%29.aspx
  271. [DllImport ("kernel32")]
  272. internal static extern bool ConnectNamedPipe (SafePipeHandle handle, IntPtr overlapped);
  273. // http://msdn.microsoft.com/en-us/library/aa365166%28VS.85%29.aspx
  274. [DllImport ("kernel32")]
  275. internal static extern bool DisconnectNamedPipe (SafePipeHandle handle);
  276. // http://msdn.microsoft.com/en-us/library/aa365443%28VS.85%29.aspx
  277. [DllImport ("kernel32")]
  278. internal static extern bool GetNamedPipeHandleState (SafePipeHandle handle, out int state, out int curInstances, out int maxCollectionCount, out int collectDateTimeout, byte [] userName, int maxUserNameSize);
  279. // http://msdn.microsoft.com/en-us/library/aa365800%28VS.85%29.aspx
  280. [DllImport ("kernel32")]
  281. internal static extern bool WaitNamedPipe (string name, int timeout);
  282. // http://msdn.microsoft.com/en-us/library/aa363858%28VS.85%29.aspx
  283. [DllImport ("kernel32")]
  284. internal static extern IntPtr CreateFile (string name, PipeAccessRights desiredAccess, FileShare fileShare, ref SecurityAttributesHack atts, int creationDisposition, int flags, IntPtr templateHandle);
  285. }
  286. }