PipeStream.cs 8.2 KB

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