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