2 // NamedPipeServerStream.cs
5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C) 2009 Novell, Inc. http://www.novell.com
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
31 using Microsoft
.Win32
.SafeHandles
;
35 using System
.Security
.AccessControl
;
36 using System
.Security
.Permissions
;
37 using System
.Security
.Principal
;
39 namespace System
.IO
.Pipes
41 [MonoTODO ("working only on win32 right now")]
42 [HostProtection (SecurityAction
.LinkDemand
, MayLeakOnAbort
= true)]
43 public sealed class NamedPipeServerStream
: PipeStream
46 public const int MaxAllowedServerInstances
= 1;
48 public NamedPipeServerStream (string pipeName
)
49 : this (pipeName
, PipeDirection
.InOut
)
53 public NamedPipeServerStream (string pipeName
, PipeDirection direction
)
54 : this (pipeName
, direction
, 1)
58 public NamedPipeServerStream (string pipeName
, PipeDirection direction
, int maxNumberOfServerInstances
)
59 : this (pipeName
, direction
, maxNumberOfServerInstances
, PipeTransmissionMode
.Byte
)
63 public NamedPipeServerStream (string pipeName
, PipeDirection direction
, int maxNumberOfServerInstances
, PipeTransmissionMode transmissionMode
)
64 : this (pipeName
, direction
, maxNumberOfServerInstances
, transmissionMode
, PipeOptions
.None
)
68 public NamedPipeServerStream (string pipeName
, PipeDirection direction
, int maxNumberOfServerInstances
, PipeTransmissionMode transmissionMode
, PipeOptions options
)
69 : this (pipeName
, direction
, maxNumberOfServerInstances
, transmissionMode
, options
, DefaultBufferSize
, DefaultBufferSize
)
73 public NamedPipeServerStream (string pipeName
, PipeDirection direction
, int maxNumberOfServerInstances
, PipeTransmissionMode transmissionMode
, PipeOptions options
, int inBufferSize
, int outBufferSize
)
74 : this (pipeName
, direction
, maxNumberOfServerInstances
, transmissionMode
, options
, inBufferSize
, outBufferSize
, null)
78 public NamedPipeServerStream (string pipeName
, PipeDirection direction
, int maxNumberOfServerInstances
, PipeTransmissionMode transmissionMode
, PipeOptions options
, int inBufferSize
, int outBufferSize
, PipeSecurity pipeSecurity
)
79 : this (pipeName
, direction
, maxNumberOfServerInstances
, transmissionMode
, options
, inBufferSize
, outBufferSize
, pipeSecurity
, HandleInheritability
.None
)
83 public NamedPipeServerStream (string pipeName
, PipeDirection direction
, int maxNumberOfServerInstances
, PipeTransmissionMode transmissionMode
, PipeOptions options
, int inBufferSize
, int outBufferSize
, PipeSecurity pipeSecurity
, HandleInheritability inheritability
)
84 : this (pipeName
, direction
, maxNumberOfServerInstances
, transmissionMode
, options
, inBufferSize
, outBufferSize
, pipeSecurity
, inheritability
, PipeAccessRights
.ReadData
| PipeAccessRights
.WriteData
)
89 public NamedPipeServerStream (string pipeName
, PipeDirection direction
, int maxNumberOfServerInstances
, PipeTransmissionMode transmissionMode
, PipeOptions options
, int inBufferSize
, int outBufferSize
, PipeSecurity pipeSecurity
, HandleInheritability inheritability
, PipeAccessRights additionalAccessRights
)
90 : base (direction
, transmissionMode
, outBufferSize
)
92 if (pipeSecurity
!= null)
93 throw ThrowACLException ();
94 var rights
= ToAccessRights (direction
) | additionalAccessRights
;
95 // FIXME: reject some rights declarations (for ACL).
98 impl
= new Win32NamedPipeServer (this, pipeName
, maxNumberOfServerInstances
, transmissionMode
, rights
, options
, inBufferSize
, outBufferSize
, inheritability
);
100 impl
= new UnixNamedPipeServer (this, pipeName
, maxNumberOfServerInstances
, transmissionMode
, rights
, options
, inBufferSize
, outBufferSize
, inheritability
);
102 InitializeHandle (impl
.Handle
, false, (options
& PipeOptions
.Asynchronous
) != PipeOptions
.None
);
105 public NamedPipeServerStream (PipeDirection direction
, bool isAsync
, bool isConnected
, SafePipeHandle safePipeHandle
)
106 : base (direction
, DefaultBufferSize
)
109 impl
= new Win32NamedPipeServer (this, safePipeHandle
);
111 impl
= new UnixNamedPipeServer (this, safePipeHandle
);
112 IsConnected
= isConnected
;
113 InitializeHandle (safePipeHandle
, true, isAsync
);
116 INamedPipeServer impl
;
118 public void Disconnect ()
124 [SecurityPermission (SecurityAction
.Demand
, Flags
= SecurityPermissionFlag
.ControlPrincipal
)]
125 public void RunAsClient (PipeStreamImpersonationWorker impersonationWorker
)
127 throw new NotImplementedException ();
130 public void WaitForConnection ()
132 impl
.WaitForConnection ();
137 [SecurityPermission (SecurityAction
.Demand
, Flags
= SecurityPermissionFlag
.ControlPrincipal
)]
138 public string GetImpersonationUserName ()
140 throw new NotImplementedException ();
145 Action wait_connect_delegate
;
147 [HostProtection (SecurityAction
.LinkDemand
, ExternalThreading
= true)]
148 public IAsyncResult
BeginWaitForConnection (AsyncCallback callback
, object state
)
150 if (wait_connect_delegate
== null)
151 wait_connect_delegate
= new Action (WaitForConnection
);
152 return wait_connect_delegate
.BeginInvoke (callback
, state
);
155 public void EndWaitForConnection (IAsyncResult asyncResult
)
157 wait_connect_delegate
.EndInvoke (asyncResult
);