129b910be6d3dd6cb83559b9df4ce70bde7f9f95
[mono.git] / mcs / class / System.Core / System.IO.Pipes / PipeStream.cs
1 //
2 // PipeStream.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 using System.Runtime.InteropServices;
39
40 namespace System.IO.Pipes
41 {
42         [PermissionSet (SecurityAction.InheritanceDemand, Name = "FullTrust")]
43         [HostProtection (SecurityAction.LinkDemand, MayLeakOnAbort = true)]
44         public abstract class PipeStream : Stream
45         {
46                 // FIXME: not precise.
47                 internal const int DefaultBufferSize = 0x400;
48
49                 internal static bool IsWindows {
50                         get { return Win32Marshal.IsWindows; }
51                 }
52
53                 internal Exception ThrowACLException ()
54                 {
55                         return new NotImplementedException ("ACL is not supported in Mono");
56                 }
57
58                 internal static PipeAccessRights ToAccessRights (PipeDirection direction)
59                 {
60                         switch (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;
67                         default:
68                                 throw new ArgumentOutOfRangeException ();
69                         }
70                 }
71
72                 internal static PipeDirection ToDirection (PipeAccessRights rights)
73                 {
74                         bool r = (rights & PipeAccessRights.ReadData) != 0;
75                         bool w = (rights & PipeAccessRights.WriteData) != 0;
76                         if (r) {
77                                 if (w)
78                                         return PipeDirection.InOut;
79                                 else
80                                         return PipeDirection.In;
81                         } else {
82                                 if (w)
83                                         return PipeDirection.Out;
84                                 else
85                                         throw new ArgumentOutOfRangeException ();
86                         }
87                 }
88
89                 protected PipeStream (PipeDirection direction, int bufferSize)
90                         : this (direction, PipeTransmissionMode.Byte, bufferSize)
91                 {
92                 }
93
94                 protected PipeStream (PipeDirection direction, PipeTransmissionMode transmissionMode, int outBufferSize)
95                 {
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;
102                 }
103
104                 PipeDirection direction;
105                 PipeTransmissionMode transmission_mode, read_trans_mode;
106                 int buffer_size;
107                 SafePipeHandle handle;
108                 Stream stream;
109
110                 public override bool CanRead {
111                         get { return (direction & PipeDirection.In) != 0; }
112                 }
113
114                 public override bool CanSeek {
115                         get { return false; }
116                 }
117
118                 public override bool CanWrite {
119                         get { return (direction & PipeDirection.Out) != 0; }
120                 }
121
122                 public virtual int InBufferSize {
123                         get { return buffer_size; }
124                 }
125
126                 public bool IsAsync { get; private set; }
127
128                 public bool IsConnected { get; protected set; }
129
130                 internal Stream Stream {
131                         get {
132                                 if (!IsConnected)
133                                         throw new InvalidOperationException ("Pipe is not connected");
134                                 if (stream == null)
135                                         stream = new FileStream (handle.DangerousGetHandle (), CanRead ? (CanWrite ? FileAccess.ReadWrite : FileAccess.Read) : FileAccess.Write, true, buffer_size, IsAsync);
136                                 return stream;
137                         }
138                         set { stream = value; }
139                 }
140
141                 protected bool IsHandleExposed { get; private set; }
142
143                 [MonoTODO]
144                 public bool IsMessageComplete { get; private set; }
145
146                 [MonoTODO]
147                 public virtual int OutBufferSize {
148                         get { return buffer_size; }
149                 }
150
151                 public virtual PipeTransmissionMode ReadMode {
152                         get {
153                                 CheckPipePropertyOperations ();
154                                 return read_trans_mode;
155                         }
156                         set {
157                                 CheckPipePropertyOperations ();
158                                 read_trans_mode = value;
159                         }
160                 }
161
162                 public SafePipeHandle SafePipeHandle {
163                         get {
164                                 CheckPipePropertyOperations ();
165                                 return handle;
166                         }
167                 }
168
169                 public virtual PipeTransmissionMode TransmissionMode {
170                         get {
171                                 CheckPipePropertyOperations ();
172                                 return transmission_mode;
173                         }
174                 }
175
176                 // initialize/dispose/state check
177
178                 [MonoTODO]
179                 protected internal virtual void CheckPipePropertyOperations ()
180                 {
181                 }
182
183                 [MonoTODO]
184                 protected internal void CheckReadOperations ()
185                 {
186                         if (!IsConnected)
187                                 throw new InvalidOperationException ("Pipe is not connected");
188                         if (!CanRead)
189                                 throw new NotSupportedException ("The pipe stream does not support read operations");
190                 }
191
192                 [MonoTODO]
193                 protected internal void CheckWriteOperations ()
194                 {
195                         if (!IsConnected)
196                                 throw new InvalidOperationException ("Pipe us not connected");
197                         if (!CanWrite)
198                                 throw new NotSupportedException ("The pipe stream does not support write operations");
199                 }
200
201                 protected void InitializeHandle (SafePipeHandle handle, bool isExposed, bool isAsync)
202                 {
203                         this.handle = handle;
204                         this.IsHandleExposed = isExposed;
205                         this.IsAsync = isAsync;
206                 }
207
208                 protected override void Dispose (bool disposing)
209                 {
210                         if (handle != null && disposing)
211                                 handle.Dispose ();
212                 }
213
214                 // not supported
215
216                 public override long Length {
217                         get { throw new NotSupportedException (); }
218                 }
219
220                 public override long Position {
221                         get { return 0; }
222                         set { throw new NotSupportedException (); }
223                 }
224
225                 public override void SetLength (long value)
226                 {
227                         throw new NotSupportedException ();
228                 }
229
230                 public override long Seek (long offset, SeekOrigin origin)
231                 {
232                         throw new NotSupportedException ();
233                 }
234
235                 [MonoNotSupported ("ACL is not supported in Mono")]
236                 public PipeSecurity GetAccessControl ()
237                 {
238                         throw ThrowACLException ();
239                 }
240
241                 [MonoNotSupported ("ACL is not supported in Mono")]
242                 public void SetAccessControl (PipeSecurity pipeSecurity)
243                 {
244                         throw ThrowACLException ();
245                 }
246
247                 // pipe I/O
248
249                 public void WaitForPipeDrain ()
250                 {
251                 }
252
253                 [MonoTODO]
254                 public override int Read ([In] byte [] buffer, int offset, int count)
255                 {
256                         CheckReadOperations ();
257
258                         return Stream.Read (buffer, offset, count);
259                 }
260
261                 [MonoTODO]
262                 public override int ReadByte ()
263                 {
264                         CheckReadOperations ();
265
266                         return Stream.ReadByte ();
267                 }
268
269                 [MonoTODO]
270                 public override void Write (byte [] buffer, int offset, int count)
271                 {
272                         CheckWriteOperations ();
273
274                         Stream.Write (buffer, offset, count);
275                 }
276
277                 [MonoTODO]
278                 public override void WriteByte (byte value)
279                 {
280                         CheckWriteOperations ();
281
282                         Stream.WriteByte (value);
283                 }
284
285                 [MonoTODO]
286                 public override void Flush ()
287                 {
288                         CheckWriteOperations ();
289
290                         Stream.Flush ();
291                 }
292
293                 // async
294
295                 Func<byte [],int,int,int> read_delegate;
296
297                 [HostProtection (SecurityAction.LinkDemand, ExternalThreading = true)]
298                 public override IAsyncResult BeginRead (byte [] buffer, int offset, int count, AsyncCallback callback, object state)
299                 {
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);
303                 }
304
305                 Action<byte[],int,int> write_delegate;
306
307                 [HostProtection (SecurityAction.LinkDemand, ExternalThreading = true)]
308                 public override IAsyncResult BeginWrite (byte[] buffer, int offset, int count, AsyncCallback callback, object state)
309                 {
310                         if (write_delegate == null)
311                                 write_delegate = new Action<byte[],int,int> (Write);
312                         return write_delegate.BeginInvoke (buffer, offset, count, callback, state);
313                 }
314
315                 public override int EndRead (IAsyncResult asyncResult)
316                 {
317                         return read_delegate.EndInvoke (asyncResult);
318                 }
319
320                 public override void EndWrite (IAsyncResult asyncResult)
321                 {
322                         write_delegate.EndInvoke (asyncResult);
323                 }
324         }
325 }
326
327 #endif