NamedPipeClientStream.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. //
  2. // NamedPipeClientStream.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 System;
  30. using System.ComponentModel;
  31. using System.IO;
  32. using System.Linq;
  33. using System.Runtime.InteropServices;
  34. using System.Security.AccessControl;
  35. using System.Security.Permissions;
  36. using System.Security.Principal;
  37. using System.Text;
  38. using System.Threading;
  39. using System.Threading.Tasks;
  40. using Microsoft.Win32;
  41. using Microsoft.Win32.SafeHandles;
  42. namespace System.IO.Pipes
  43. {
  44. [MonoTODO ("working only on win32 right now")]
  45. [HostProtection (SecurityAction.LinkDemand, MayLeakOnAbort = true)]
  46. public sealed class NamedPipeClientStream : PipeStream
  47. {
  48. public NamedPipeClientStream (string pipeName)
  49. : this (".", pipeName)
  50. {
  51. }
  52. public NamedPipeClientStream (string serverName, string pipeName)
  53. : this (serverName, pipeName, PipeDirection.InOut)
  54. {
  55. }
  56. public NamedPipeClientStream (string serverName, string pipeName, PipeDirection direction)
  57. : this (serverName, pipeName, direction, PipeOptions.None)
  58. {
  59. }
  60. public NamedPipeClientStream (string serverName, string pipeName, PipeDirection direction, PipeOptions options)
  61. : this (serverName, pipeName, direction, options, TokenImpersonationLevel.None)
  62. {
  63. }
  64. public NamedPipeClientStream (string serverName, string pipeName, PipeDirection direction, PipeOptions options, TokenImpersonationLevel impersonationLevel)
  65. : this (serverName, pipeName, direction, options, impersonationLevel, HandleInheritability.None)
  66. {
  67. }
  68. public NamedPipeClientStream (string serverName, string pipeName, PipeDirection direction, PipeOptions options, TokenImpersonationLevel impersonationLevel, HandleInheritability inheritability)
  69. #if MOBILE
  70. : base (direction, DefaultBufferSize)
  71. {
  72. throw new NotImplementedException ();
  73. }
  74. #else
  75. : this (serverName, pipeName, ToAccessRights (direction), options, impersonationLevel, inheritability)
  76. {
  77. }
  78. #endif
  79. public NamedPipeClientStream (PipeDirection direction, bool isAsync, bool isConnected, SafePipeHandle safePipeHandle)
  80. : base (direction, DefaultBufferSize)
  81. {
  82. #if MOBILE
  83. throw new NotImplementedException ();
  84. #else
  85. if (IsWindows)
  86. impl = new Win32NamedPipeClient (this, safePipeHandle);
  87. else
  88. impl = new UnixNamedPipeClient (this, safePipeHandle);
  89. IsConnected = isConnected;
  90. InitializeHandle (safePipeHandle, true, isAsync);
  91. #endif
  92. }
  93. #if !MOBILE
  94. public NamedPipeClientStream (string serverName, string pipeName, PipeAccessRights desiredAccessRights, PipeOptions options, TokenImpersonationLevel impersonationLevel, HandleInheritability inheritability)
  95. : base (ToDirection (desiredAccessRights), DefaultBufferSize)
  96. {
  97. if (impersonationLevel != TokenImpersonationLevel.None ||
  98. inheritability != HandleInheritability.None)
  99. throw ThrowACLException ();
  100. if (IsWindows)
  101. impl = new Win32NamedPipeClient (this, serverName, pipeName, desiredAccessRights, options, inheritability);
  102. else
  103. impl = new UnixNamedPipeClient (this, serverName, pipeName, desiredAccessRights, options, inheritability);
  104. }
  105. #endif
  106. ~NamedPipeClientStream () {
  107. Dispose (false);
  108. }
  109. INamedPipeClient impl;
  110. public void Connect ()
  111. {
  112. #if MOBILE
  113. throw new NotImplementedException ();
  114. #else
  115. impl.Connect ();
  116. InitializeHandle (impl.Handle, false, impl.IsAsync);
  117. IsConnected = true;
  118. #endif
  119. }
  120. public void Connect (int timeout)
  121. {
  122. #if MOBILE
  123. throw new NotImplementedException ();
  124. #else
  125. impl.Connect (timeout);
  126. InitializeHandle (impl.Handle, false, impl.IsAsync);
  127. IsConnected = true;
  128. #endif
  129. }
  130. public Task ConnectAsync ()
  131. {
  132. return ConnectAsync (Timeout.Infinite, CancellationToken.None);
  133. }
  134. public Task ConnectAsync (int timeout)
  135. {
  136. return ConnectAsync (timeout, CancellationToken.None);
  137. }
  138. public Task ConnectAsync (CancellationToken cancellationToken)
  139. {
  140. return ConnectAsync (Timeout.Infinite, cancellationToken);
  141. }
  142. public Task ConnectAsync (int timeout, CancellationToken cancellationToken)
  143. {
  144. throw new NotImplementedException ();
  145. }
  146. public int NumberOfServerInstances {
  147. get {
  148. CheckPipePropertyOperations ();
  149. return impl.NumberOfServerInstances;
  150. }
  151. }
  152. }
  153. }
  154. #endif