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