NamedPipeServerStream.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. //
  2. // NamedPipeServerStream.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  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. #if !BOOTSTRAP_BASIC
  29. using Microsoft.Win32.SafeHandles;
  30. using System;
  31. using System.IO;
  32. using System.Linq;
  33. using System.Threading;
  34. using System.Threading.Tasks;
  35. using System.Security.AccessControl;
  36. using System.Security.Permissions;
  37. using System.Security.Principal;
  38. namespace System.IO.Pipes
  39. {
  40. [MonoTODO ("working only on win32 right now")]
  41. [HostProtection (SecurityAction.LinkDemand, MayLeakOnAbort = true)]
  42. public sealed class NamedPipeServerStream : PipeStream
  43. {
  44. public const int MaxAllowedServerInstances = -1;
  45. public NamedPipeServerStream (string pipeName)
  46. : this (pipeName, PipeDirection.InOut)
  47. {
  48. }
  49. public NamedPipeServerStream (string pipeName, PipeDirection direction)
  50. : this (pipeName, direction, 1)
  51. {
  52. }
  53. public NamedPipeServerStream (string pipeName, PipeDirection direction, int maxNumberOfServerInstances)
  54. : this (pipeName, direction, maxNumberOfServerInstances, PipeTransmissionMode.Byte)
  55. {
  56. }
  57. public NamedPipeServerStream (string pipeName, PipeDirection direction, int maxNumberOfServerInstances, PipeTransmissionMode transmissionMode)
  58. : this (pipeName, direction, maxNumberOfServerInstances, transmissionMode, PipeOptions.None)
  59. {
  60. }
  61. public NamedPipeServerStream (string pipeName, PipeDirection direction, int maxNumberOfServerInstances, PipeTransmissionMode transmissionMode, PipeOptions options)
  62. : this (pipeName, direction, maxNumberOfServerInstances, transmissionMode, options, DefaultBufferSize, DefaultBufferSize)
  63. {
  64. }
  65. public NamedPipeServerStream (string pipeName, PipeDirection direction, int maxNumberOfServerInstances, PipeTransmissionMode transmissionMode, PipeOptions options, int inBufferSize, int outBufferSize)
  66. #if MOBILE
  67. : base (direction, inBufferSize)
  68. {
  69. throw new NotImplementedException ();
  70. }
  71. #else
  72. : this (pipeName, direction, maxNumberOfServerInstances, transmissionMode, options, inBufferSize, outBufferSize, null)
  73. {
  74. }
  75. #endif
  76. #if !MOBILE
  77. public NamedPipeServerStream (string pipeName, PipeDirection direction, int maxNumberOfServerInstances, PipeTransmissionMode transmissionMode, PipeOptions options, int inBufferSize, int outBufferSize, PipeSecurity pipeSecurity)
  78. : this (pipeName, direction, maxNumberOfServerInstances, transmissionMode, options, inBufferSize, outBufferSize, pipeSecurity, HandleInheritability.None)
  79. {
  80. }
  81. public NamedPipeServerStream (string pipeName, PipeDirection direction, int maxNumberOfServerInstances, PipeTransmissionMode transmissionMode, PipeOptions options, int inBufferSize, int outBufferSize, PipeSecurity pipeSecurity, HandleInheritability inheritability)
  82. : this (pipeName, direction, maxNumberOfServerInstances, transmissionMode, options, inBufferSize, outBufferSize, pipeSecurity, inheritability, PipeAccessRights.ReadData | PipeAccessRights.WriteData)
  83. {
  84. }
  85. [MonoTODO]
  86. public NamedPipeServerStream (string pipeName, PipeDirection direction, int maxNumberOfServerInstances, PipeTransmissionMode transmissionMode, PipeOptions options, int inBufferSize, int outBufferSize, PipeSecurity pipeSecurity, HandleInheritability inheritability, PipeAccessRights additionalAccessRights)
  87. : base (direction, transmissionMode, outBufferSize)
  88. {
  89. var rights = ToAccessRights (direction) | additionalAccessRights;
  90. // FIXME: reject some rights declarations (for ACL).
  91. if (IsWindows)
  92. impl = new Win32NamedPipeServer (this, pipeName, maxNumberOfServerInstances, transmissionMode,
  93. rights, options, inBufferSize, outBufferSize,
  94. pipeSecurity, inheritability);
  95. else
  96. impl = new UnixNamedPipeServer (this, pipeName, maxNumberOfServerInstances, transmissionMode,
  97. rights, options, inBufferSize, outBufferSize, inheritability);
  98. InitializeHandle (impl.Handle, false, (options & PipeOptions.Asynchronous) != PipeOptions.None);
  99. }
  100. #endif
  101. public NamedPipeServerStream (PipeDirection direction, bool isAsync, bool isConnected, SafePipeHandle safePipeHandle)
  102. : base (direction, DefaultBufferSize)
  103. {
  104. #if MOBILE
  105. throw new NotImplementedException ();
  106. #else
  107. if (IsWindows)
  108. impl = new Win32NamedPipeServer (this, safePipeHandle);
  109. else
  110. impl = new UnixNamedPipeServer (this, safePipeHandle);
  111. IsConnected = isConnected;
  112. InitializeHandle (safePipeHandle, true, isAsync);
  113. #endif
  114. }
  115. ~NamedPipeServerStream ()
  116. {
  117. // To be compatible with .net
  118. }
  119. INamedPipeServer impl;
  120. public void Disconnect ()
  121. {
  122. impl.Disconnect ();
  123. }
  124. #if !MOBILE
  125. [MonoTODO]
  126. [SecurityPermission (SecurityAction.Demand, Flags = SecurityPermissionFlag.ControlPrincipal)]
  127. public void RunAsClient (PipeStreamImpersonationWorker impersonationWorker)
  128. {
  129. throw new NotImplementedException ();
  130. }
  131. #endif
  132. public void WaitForConnection ()
  133. {
  134. impl.WaitForConnection ();
  135. IsConnected = true;
  136. }
  137. public Task WaitForConnectionAsync ()
  138. {
  139. return WaitForConnectionAsync (CancellationToken.None);
  140. }
  141. [MonoTODO]
  142. public Task WaitForConnectionAsync (CancellationToken cancellationToken)
  143. {
  144. throw new NotImplementedException ();
  145. }
  146. [MonoTODO]
  147. [SecurityPermission (SecurityAction.Demand, Flags = SecurityPermissionFlag.ControlPrincipal)]
  148. public string GetImpersonationUserName ()
  149. {
  150. throw new NotImplementedException ();
  151. }
  152. #if !MOBILE
  153. // async operations
  154. Action wait_connect_delegate;
  155. [HostProtection (SecurityAction.LinkDemand, ExternalThreading = true)]
  156. public IAsyncResult BeginWaitForConnection (AsyncCallback callback, object state)
  157. {
  158. if (wait_connect_delegate == null)
  159. wait_connect_delegate = new Action (WaitForConnection);
  160. return wait_connect_delegate.BeginInvoke (callback, state);
  161. }
  162. public void EndWaitForConnection (IAsyncResult asyncResult)
  163. {
  164. wait_connect_delegate.EndInvoke (asyncResult);
  165. }
  166. #endif
  167. }
  168. }
  169. #endif