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 (),
136 CanRead ? (CanWrite ? FileAccess.ReadWrite : FileAccess.Read)
137 : FileAccess.Write, true, buffer_size, IsAsync);
140 set { stream = value; }
143 protected bool IsHandleExposed { get; private set; }
146 public bool IsMessageComplete { get; private set; }
149 public virtual int OutBufferSize {
150 get { return buffer_size; }
153 public virtual PipeTransmissionMode ReadMode {
155 CheckPipePropertyOperations ();
156 return read_trans_mode;
159 CheckPipePropertyOperations ();
160 read_trans_mode = value;
164 public SafePipeHandle SafePipeHandle {
166 CheckPipePropertyOperations ();
171 public virtual PipeTransmissionMode TransmissionMode {
173 CheckPipePropertyOperations ();
174 return transmission_mode;
178 // initialize/dispose/state check
181 protected internal virtual void CheckPipePropertyOperations ()
186 protected internal void CheckReadOperations ()
189 throw new InvalidOperationException ("Pipe is not connected");
191 throw new NotSupportedException ("The pipe stream does not support read operations");
195 protected internal void CheckWriteOperations ()
198 throw new InvalidOperationException ("Pipe is not connected");
200 throw new NotSupportedException ("The pipe stream does not support write operations");
203 protected void InitializeHandle (SafePipeHandle handle, bool isExposed, bool isAsync)
205 this.handle = handle;
206 this.IsHandleExposed = isExposed;
207 this.IsAsync = isAsync;
210 protected override void Dispose (bool disposing)
212 if (handle != null && disposing)
218 public override long Length {
219 get { throw new NotSupportedException (); }
222 public override long Position {
224 set { throw new NotSupportedException (); }
227 public override void SetLength (long value)
229 throw new NotSupportedException ();
232 public override long Seek (long offset, SeekOrigin origin)
234 throw new NotSupportedException ();
237 public PipeSecurity GetAccessControl ()
239 return new PipeSecurity (SafePipeHandle,
240 AccessControlSections.Owner |
241 AccessControlSections.Group |
242 AccessControlSections.Access);
245 public void SetAccessControl (PipeSecurity pipeSecurity)
247 if (pipeSecurity == null)
248 throw new ArgumentNullException ("pipeSecurity");
250 pipeSecurity.Persist (SafePipeHandle);
255 public void WaitForPipeDrain ()
260 public override int Read ([In] byte [] buffer, int offset, int count)
262 CheckReadOperations ();
264 return Stream.Read (buffer, offset, count);
268 public override int ReadByte ()
270 CheckReadOperations ();
272 return Stream.ReadByte ();
276 public override void Write (byte [] buffer, int offset, int count)
278 CheckWriteOperations ();
280 Stream.Write (buffer, offset, count);
284 public override void WriteByte (byte value)
286 CheckWriteOperations ();
288 Stream.WriteByte (value);
292 public override void Flush ()
294 CheckWriteOperations ();
301 Func<byte [],int,int,int> read_delegate;
303 [HostProtection (SecurityAction.LinkDemand, ExternalThreading = true)]
304 public override IAsyncResult BeginRead (byte [] buffer, int offset, int count, AsyncCallback callback, object state)
306 if (read_delegate == null)
307 read_delegate = new Func<byte[],int,int,int> (Read);
308 return read_delegate.BeginInvoke (buffer, offset, count, callback, state);
311 Action<byte[],int,int> write_delegate;
313 [HostProtection (SecurityAction.LinkDemand, ExternalThreading = true)]
314 public override IAsyncResult BeginWrite (byte[] buffer, int offset, int count, AsyncCallback callback, object state)
316 if (write_delegate == null)
317 write_delegate = new Action<byte[],int,int> (Write);
318 return write_delegate.BeginInvoke (buffer, offset, count, callback, state);
321 public override int EndRead (IAsyncResult asyncResult)
323 return read_delegate.EndInvoke (asyncResult);
326 public override void EndWrite (IAsyncResult asyncResult)
328 write_delegate.EndInvoke (asyncResult);