PageRenderTime 50ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/mcs/class/System.Core/System.IO.Pipes/PipeStream.cs

https://bitbucket.org/steenlund/mono-2.6.7-for-amiga
C# | 321 lines | 236 code | 53 blank | 32 comment | 24 complexity | da096aa14e9c98467ea4d982db43c6e1 MD5 | raw file
Possible License(s): LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0, LGPL-2.1
  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. using Microsoft.Win32.SafeHandles;
  29. using System;
  30. using System.IO;
  31. using System.Linq;
  32. using System.Security.AccessControl;
  33. using System.Security.Permissions;
  34. using System.Security.Principal;
  35. namespace System.IO.Pipes
  36. {
  37. [PermissionSet (SecurityAction.InheritanceDemand, Name = "FullTrust")]
  38. [HostProtection (SecurityAction.LinkDemand, MayLeakOnAbort = true)]
  39. public abstract class PipeStream : Stream
  40. {
  41. // FIXME: not precise.
  42. internal const int DefaultBufferSize = 0x400;
  43. internal static bool IsWindows {
  44. get { return Win32Marshal.IsWindows; }
  45. }
  46. internal Exception ThrowACLException ()
  47. {
  48. return new NotImplementedException ("ACL is not supported in Mono");
  49. }
  50. internal static PipeAccessRights ToAccessRights (PipeDirection direction)
  51. {
  52. switch (direction) {
  53. case PipeDirection.In:
  54. return PipeAccessRights.ReadData;
  55. case PipeDirection.Out:
  56. return PipeAccessRights.WriteData;
  57. case PipeDirection.InOut:
  58. return PipeAccessRights.ReadData | PipeAccessRights.WriteData;
  59. default:
  60. throw new ArgumentOutOfRangeException ();
  61. }
  62. }
  63. internal static PipeDirection ToDirection (PipeAccessRights rights)
  64. {
  65. bool r = (rights & PipeAccessRights.ReadData) != 0;
  66. bool w = (rights & PipeAccessRights.WriteData) != 0;
  67. if (r) {
  68. if (w)
  69. return PipeDirection.InOut;
  70. else
  71. return PipeDirection.In;
  72. } else {
  73. if (w)
  74. return PipeDirection.Out;
  75. else
  76. throw new ArgumentOutOfRangeException ();
  77. }
  78. }
  79. protected PipeStream (PipeDirection direction, int bufferSize)
  80. : this (direction, PipeTransmissionMode.Byte, bufferSize)
  81. {
  82. }
  83. protected PipeStream (PipeDirection direction, PipeTransmissionMode transmissionMode, int outBufferSize)
  84. {
  85. this.direction = direction;
  86. this.transmission_mode = transmissionMode;
  87. read_trans_mode = transmissionMode;
  88. if (outBufferSize <= 0)
  89. throw new ArgumentOutOfRangeException ("bufferSize must be greater than 0");
  90. buffer_size = outBufferSize;
  91. }
  92. PipeDirection direction;
  93. PipeTransmissionMode transmission_mode, read_trans_mode;
  94. int buffer_size;
  95. SafePipeHandle handle;
  96. Stream stream;
  97. public override bool CanRead {
  98. get { return (direction & PipeDirection.In) != 0; }
  99. }
  100. public override bool CanSeek {
  101. get { return false; }
  102. }
  103. public override bool CanWrite {
  104. get { return (direction & PipeDirection.Out) != 0; }
  105. }
  106. public virtual int InBufferSize {
  107. get { return buffer_size; }
  108. }
  109. public bool IsAsync { get; private set; }
  110. public bool IsConnected { get; protected set; }
  111. internal Stream Stream {
  112. get {
  113. if (!IsConnected)
  114. throw new InvalidOperationException ("Pipe is not connected");
  115. if (stream == null)
  116. stream = new FileStream (handle.DangerousGetHandle (), CanRead ? (CanWrite ? FileAccess.ReadWrite : FileAccess.Read) : FileAccess.Write, true, buffer_size, IsAsync);
  117. return stream;
  118. }
  119. set { stream = value; }
  120. }
  121. protected bool IsHandleExposed { get; private set; }
  122. [MonoTODO]
  123. public bool IsMessageComplete { get; private set; }
  124. [MonoTODO]
  125. public virtual int OutBufferSize {
  126. get { return buffer_size; }
  127. }
  128. public virtual PipeTransmissionMode ReadMode {
  129. get {
  130. CheckPipePropertyOperations ();
  131. return read_trans_mode;
  132. }
  133. set {
  134. CheckPipePropertyOperations ();
  135. read_trans_mode = value;
  136. }
  137. }
  138. public SafePipeHandle SafePipeHandle {
  139. get {
  140. CheckPipePropertyOperations ();
  141. return handle;
  142. }
  143. }
  144. public virtual PipeTransmissionMode TransmissionMode {
  145. get {
  146. CheckPipePropertyOperations ();
  147. return transmission_mode;
  148. }
  149. }
  150. // initialize/dispose/state check
  151. [MonoTODO]
  152. protected internal virtual void CheckPipePropertyOperations ()
  153. {
  154. }
  155. [MonoTODO]
  156. protected internal void CheckReadOperations ()
  157. {
  158. if (!IsConnected)
  159. throw new InvalidOperationException ("Pipe is not connected");
  160. if (!CanRead)
  161. throw new NotSupportedException ("The pipe stream does not support read operations");
  162. }
  163. [MonoTODO]
  164. protected internal void CheckWriteOperations ()
  165. {
  166. if (!IsConnected)
  167. throw new InvalidOperationException ("Pipe us not connected");
  168. if (!CanWrite)
  169. throw new NotSupportedException ("The pipe stream does not support write operations");
  170. }
  171. protected void InitializeHandle (SafePipeHandle handle, bool isExposed, bool isAsync)
  172. {
  173. this.handle = handle;
  174. this.IsHandleExposed = isExposed;
  175. this.IsAsync = isAsync;
  176. }
  177. protected override void Dispose (bool disposing)
  178. {
  179. if (handle != null && disposing)
  180. handle.Dispose ();
  181. }
  182. // not supported
  183. public override long Length {
  184. get { throw new NotSupportedException (); }
  185. }
  186. public override long Position {
  187. get { return 0; }
  188. set { throw new NotSupportedException (); }
  189. }
  190. public override void SetLength (long value)
  191. {
  192. throw new NotSupportedException ();
  193. }
  194. public override long Seek (long offset, SeekOrigin origin)
  195. {
  196. throw new NotSupportedException ();
  197. }
  198. [MonoNotSupported ("ACL is not supported in Mono")]
  199. public PipeSecurity GetAccessControl ()
  200. {
  201. throw ThrowACLException ();
  202. }
  203. [MonoNotSupported ("ACL is not supported in Mono")]
  204. public void SetAccessControl (PipeSecurity pipeSecurity)
  205. {
  206. throw ThrowACLException ();
  207. }
  208. // pipe I/O
  209. public void WaitForPipeDrain ()
  210. {
  211. }
  212. [MonoTODO]
  213. public override int Read (byte [] buffer, int offset, int count)
  214. {
  215. CheckReadOperations ();
  216. return Stream.Read (buffer, offset, count);
  217. }
  218. [MonoTODO]
  219. public override int ReadByte ()
  220. {
  221. CheckReadOperations ();
  222. return Stream.ReadByte ();
  223. }
  224. [MonoTODO]
  225. public override void Write (byte [] buffer, int offset, int count)
  226. {
  227. CheckWriteOperations ();
  228. Stream.Write (buffer, offset, count);
  229. }
  230. [MonoTODO]
  231. public override void WriteByte (byte value)
  232. {
  233. CheckWriteOperations ();
  234. Stream.WriteByte (value);
  235. }
  236. [MonoTODO]
  237. public override void Flush ()
  238. {
  239. CheckWriteOperations ();
  240. Stream.Flush ();
  241. }
  242. // async
  243. Func<byte [],int,int,int> read_delegate;
  244. [HostProtection (SecurityAction.LinkDemand, ExternalThreading = true)]
  245. public override IAsyncResult BeginRead (byte [] buffer, int offset, int count, AsyncCallback callback, object state)
  246. {
  247. if (read_delegate == null)
  248. read_delegate = new Func<byte[],int,int,int> (Read);
  249. return read_delegate.BeginInvoke (buffer, offset, count, callback, state);
  250. }
  251. Action<byte[],int,int> write_delegate;
  252. [HostProtection (SecurityAction.LinkDemand, ExternalThreading = true)]
  253. public override IAsyncResult BeginWrite (byte[] buffer, int offset, int count, AsyncCallback callback, object state)
  254. {
  255. if (write_delegate == null)
  256. write_delegate = new Action<byte[],int,int> (Write);
  257. return write_delegate.BeginInvoke (buffer, offset, count, callback, state);
  258. }
  259. public override int EndRead (IAsyncResult asyncResult)
  260. {
  261. return read_delegate.EndInvoke (asyncResult);
  262. }
  263. public override void EndWrite (IAsyncResult asyncResult)
  264. {
  265. write_delegate.EndInvoke (asyncResult);
  266. }
  267. }
  268. }