Merge pull request #3199 from lambdageek/dev/proxy-setter
[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 #if MOBILE
76                         : base (direction, DefaultBufferSize)
77                 {
78                         throw new NotImplementedException ();
79                 }
80 #else
81                         : this (serverName, pipeName, ToAccessRights (direction), options, impersonationLevel, inheritability)
82                 {
83                 }
84 #endif
85
86                 public NamedPipeClientStream (PipeDirection direction, bool isAsync, bool isConnected, SafePipeHandle safePipeHandle)
87                         : base (direction, DefaultBufferSize)
88                 {
89 #if MOBILE
90                         throw new NotImplementedException ();
91 #else
92                         if (IsWindows)
93                                 impl = new Win32NamedPipeClient (this, safePipeHandle);
94                         else
95                                 impl = new UnixNamedPipeClient (this, safePipeHandle);
96                         IsConnected = isConnected;
97                         InitializeHandle (safePipeHandle, true, isAsync);
98 #endif
99                 }
100
101 #if !MOBILE
102                 public NamedPipeClientStream (string serverName, string pipeName, PipeAccessRights desiredAccessRights, PipeOptions options, TokenImpersonationLevel impersonationLevel, HandleInheritability inheritability)
103                         : base (ToDirection (desiredAccessRights), DefaultBufferSize)
104                 {
105                         if (impersonationLevel != TokenImpersonationLevel.None ||
106                             inheritability != HandleInheritability.None)
107                                 throw ThrowACLException ();
108
109                         if (IsWindows)
110                                 impl = new Win32NamedPipeClient (this, serverName, pipeName, desiredAccessRights, options, inheritability);
111                         else
112                                 impl = new UnixNamedPipeClient (this, serverName, pipeName, desiredAccessRights, options, inheritability);
113                 }
114 #endif
115
116                 INamedPipeClient impl;
117
118                 public void Connect ()
119                 {
120 #if MOBILE
121                         throw new NotImplementedException ();
122 #else
123                         impl.Connect ();
124                         InitializeHandle (impl.Handle, false, impl.IsAsync);
125                         IsConnected = true;
126 #endif
127                 }
128
129                 public void Connect (int timeout)
130                 {
131 #if MOBILE
132                         throw new NotImplementedException ();
133 #else                   
134                         impl.Connect (timeout);
135                         InitializeHandle (impl.Handle, false, impl.IsAsync);
136                         IsConnected = true;
137 #endif
138                 }
139
140                 public int NumberOfServerInstances {
141                         get {
142                                 CheckPipePropertyOperations ();
143                                 return impl.NumberOfServerInstances;
144                         }
145                 }
146         }
147 }
148
149 #endif