Moving BSTR conv to native code in SecureStringToBSTR.
[mono.git] / mcs / class / System.Core / System.IO.Pipes / NamedPipeClientStream.cs
1 //
2 // NamedPipeClientStream.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
29 #if !BOOTSTRAP_BASIC
30
31 using System;
32 using System.ComponentModel;
33 using System.IO;
34 using System.Linq;
35 using System.Runtime.InteropServices;
36 using System.Security.AccessControl;
37 using System.Security.Permissions;
38 using System.Security.Principal;
39 using System.Text;
40 using Microsoft.Win32;
41 using Microsoft.Win32.SafeHandles;
42
43 namespace System.IO.Pipes
44 {
45         [MonoTODO ("working only on win32 right now")]
46         [HostProtection (SecurityAction.LinkDemand, MayLeakOnAbort = true)]
47         public sealed class NamedPipeClientStream : PipeStream
48         {
49                 public NamedPipeClientStream (string pipeName)
50                         : this (".", pipeName)
51                 {
52                 }
53
54                 public NamedPipeClientStream (string serverName, string pipeName)
55                         : this (serverName, pipeName, PipeDirection.InOut)
56                 {
57                 }
58
59                 public NamedPipeClientStream (string serverName, string pipeName, PipeDirection direction)
60                         : this (serverName, pipeName, direction, PipeOptions.None)
61                 {
62                 }
63
64                 public NamedPipeClientStream (string serverName, string pipeName, PipeDirection direction, PipeOptions options)
65                         : this (serverName, pipeName, direction, options, TokenImpersonationLevel.None)
66                 {
67                 }
68
69                 public NamedPipeClientStream (string serverName, string pipeName, PipeDirection direction, PipeOptions options, TokenImpersonationLevel impersonationLevel)
70                         : this (serverName, pipeName, direction, options, impersonationLevel, HandleInheritability.None)
71                 {
72                 }
73
74                 public NamedPipeClientStream (string serverName, string pipeName, PipeDirection direction, PipeOptions options, TokenImpersonationLevel impersonationLevel, HandleInheritability inheritability)
75                         : this (serverName, pipeName, ToAccessRights (direction), options, impersonationLevel, inheritability)
76                 {
77                 }
78
79                 public NamedPipeClientStream (PipeDirection direction, bool isAsync, bool isConnected, SafePipeHandle safePipeHandle)
80                         : base (direction, DefaultBufferSize)
81                 {
82                         if (IsWindows)
83                                 impl = new Win32NamedPipeClient (this, safePipeHandle);
84                         else
85                                 impl = new UnixNamedPipeClient (this, safePipeHandle);
86                         IsConnected = isConnected;
87                         InitializeHandle (safePipeHandle, true, isAsync);
88                 }
89
90                 public NamedPipeClientStream (string serverName, string pipeName, PipeAccessRights desiredAccessRights, PipeOptions options, TokenImpersonationLevel impersonationLevel, HandleInheritability inheritability)
91                         : base (ToDirection (desiredAccessRights), DefaultBufferSize)
92                 {
93                         if (impersonationLevel != TokenImpersonationLevel.None ||
94                             inheritability != HandleInheritability.None)
95                                 throw ThrowACLException ();
96
97                         if (IsWindows)
98                                 impl = new Win32NamedPipeClient (this, serverName, pipeName, desiredAccessRights, options, inheritability);
99                         else
100                                 impl = new UnixNamedPipeClient (this, serverName, pipeName, desiredAccessRights, options, inheritability);
101                 }
102
103                 INamedPipeClient impl;
104
105                 public void Connect ()
106                 {
107                         impl.Connect ();
108                         InitializeHandle (impl.Handle, false, impl.IsAsync);
109                         IsConnected = true;
110                 }
111
112                 public void Connect (int timeout)
113                 {
114                         impl.Connect (timeout);
115                         InitializeHandle (impl.Handle, false, impl.IsAsync);
116                         IsConnected = true;
117                 }
118
119                 public int NumberOfServerInstances {
120                         get {
121                                 CheckPipePropertyOperations ();
122                                 return impl.NumberOfServerInstances;
123                         }
124                 }
125         }
126 }
127
128 #endif