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;
38 using System.Runtime.InteropServices;
40 namespace System.IO.Pipes
42 [PermissionSet (SecurityAction.InheritanceDemand, Name = "FullTrust")]
43 [HostProtection (SecurityAction.LinkDemand, MayLeakOnAbort = true)]
44 public abstract class PipeStream : Stream
46 // FIXME: not precise.
47 internal const int DefaultBufferSize = 0x400;
49 internal static bool IsWindows {
50 get { return Win32Marshal.IsWindows; }
53 internal Exception ThrowACLException ()
55 return new NotImplementedException ("ACL is not supported in Mono");
58 internal static PipeAccessRights ToAccessRights (PipeDirection direction)
61 case PipeDirection.In:
62 return PipeAccessRights.ReadData;
63 case PipeDirection.Out:
64 return PipeAccessRights.WriteData;
65 case PipeDirection.InOut:
66 return PipeAccessRights.ReadData | PipeAccessRights.WriteData;
68 throw new ArgumentOutOfRangeException ();
72 internal static PipeDirection ToDirection (PipeAccessRights rights)
74 bool r = (rights & PipeAccessRights.ReadData) != 0;
75 bool w = (rights & PipeAccessRights.WriteData) != 0;
78 return PipeDirection.InOut;
80 return PipeDirection.In;
83 return PipeDirection.Out;
85 throw new ArgumentOutOfRangeException ();
89 protected PipeStream (PipeDirection direction, int bufferSize)
90 : this (direction, PipeTransmissionMode.Byte, bufferSize)
94 protected PipeStream (PipeDirection direction, PipeTransmissionMode transmissionMode, int outBufferSize)
96 this.direction = direction;
97 this.transmission_mode = transmissionMode;
98 read_trans_mode = transmissionMode;
99 if (outBufferSize <= 0)
100 throw new ArgumentOutOfRangeException ("bufferSize must be greater than 0");
101 buffer_size = outBufferSize;
104 PipeDirection direction;
105 PipeTransmissionMode transmission_mode, read_trans_mode;
107 SafePipeHandle handle;
110 public override bool CanRead {
111 get { return (direction & PipeDirection.In) != 0; }
114 public override bool CanSeek {
115 get { return false; }
118 public override bool CanWrite {
119 get { return (direction & PipeDirection.Out) != 0; }
122 public virtual int InBufferSize {
123 get { return buffer_size; }
126 public bool IsAsync { get; private set; }
128 public bool IsConnected { get; protected set; }
130 internal Stream Stream {
133 throw new InvalidOperationException ("Pipe is not connected");
135 stream = new FileStream (handle.DangerousGetHandle (), CanRead ? (CanWrite ? FileAccess.ReadWrite : FileAccess.Read) : FileAccess.Write, true, buffer_size, IsAsync);
138 set { stream = value; }
141 protected bool IsHandleExposed { get; private set; }
144 public bool IsMessageComplete { get; private set; }
147 public virtual int OutBufferSize {
148 get { return buffer_size; }
151 public virtual PipeTransmissionMode ReadMode {
153 CheckPipePropertyOperations ();
154 return read_trans_mode;
157 CheckPipePropertyOperations ();
158 read_trans_mode = value;
162 public SafePipeHandle SafePipeHandle {
164 CheckPipePropertyOperations ();
169 public virtual PipeTransmissionMode TransmissionMode {
171 CheckPipePropertyOperations ();
172 return transmission_mode;
176 // initialize/dispose/state check
179 protected internal virtual void CheckPipePropertyOperations ()
184 protected internal void CheckReadOperations ()
187 throw new InvalidOperationException ("Pipe is not connected");
189 throw new NotSupportedException ("The pipe stream does not support read operations");
193 protected internal void CheckWriteOperations ()
196 throw new InvalidOperationException ("Pipe us not connected");
198 throw new NotSupportedException ("The pipe stream does not support write operations");
201 protected void InitializeHandle (SafePipeHandle handle, bool isExposed, bool isAsync)
203 this.handle = handle;
204 this.IsHandleExposed = isExposed;
205 this.IsAsync = isAsync;
208 protected override void Dispose (bool disposing)
210 if (handle != null && disposing)
216 public override long Length {
217 get { throw new NotSupportedException (); }
220 public override long Position {
222 set { throw new NotSupportedException (); }
225 public override void SetLength (long value)
227 throw new NotSupportedException ();
230 public override long Seek (long offset, SeekOrigin origin)
232 throw new NotSupportedException ();
235 [MonoNotSupported ("ACL is not supported in Mono")]
236 public PipeSecurity GetAccessControl ()
238 throw ThrowACLException ();
241 [MonoNotSupported ("ACL is not supported in Mono")]
242 public void SetAccessControl (PipeSecurity pipeSecurity)
244 throw ThrowACLException ();
249 public void WaitForPipeDrain ()
254 public override int Read ([In] byte [] buffer, int offset, int count)
256 CheckReadOperations ();
258 return Stream.Read (buffer, offset, count);
262 public override int ReadByte ()
264 CheckReadOperations ();
266 return Stream.ReadByte ();
270 public override void Write (byte [] buffer, int offset, int count)
272 CheckWriteOperations ();
274 Stream.Write (buffer, offset, count);
278 public override void WriteByte (byte value)
280 CheckWriteOperations ();
282 Stream.WriteByte (value);
286 public override void Flush ()
288 CheckWriteOperations ();
295 Func<byte [],int,int,int> read_delegate;
297 [HostProtection (SecurityAction.LinkDemand, ExternalThreading = true)]
298 public override IAsyncResult BeginRead (byte [] buffer, int offset, int count, AsyncCallback callback, object state)
300 if (read_delegate == null)
301 read_delegate = new Func<byte[],int,int,int> (Read);
302 return read_delegate.BeginInvoke (buffer, offset, count, callback, state);
305 Action<byte[],int,int> write_delegate;
307 [HostProtection (SecurityAction.LinkDemand, ExternalThreading = true)]
308 public override IAsyncResult BeginWrite (byte[] buffer, int offset, int count, AsyncCallback callback, object state)
310 if (write_delegate == null)
311 write_delegate = new Action<byte[],int,int> (Write);
312 return write_delegate.BeginInvoke (buffer, offset, count, callback, state);
315 public override int EndRead (IAsyncResult asyncResult)
317 return read_delegate.EndInvoke (asyncResult);
320 public override void EndWrite (IAsyncResult asyncResult)
322 write_delegate.EndInvoke (asyncResult);