PipeStream.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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 (),
  119. CanRead ? (CanWrite ? FileAccess.ReadWrite : FileAccess.Read)
  120. : FileAccess.Write, true, buffer_size, IsAsync);
  121. return stream;
  122. }
  123. set { stream = value; }
  124. }
  125. protected bool IsHandleExposed { get; private set; }
  126. [MonoTODO]
  127. public bool IsMessageComplete { get; private set; }
  128. [MonoTODO]
  129. public virtual int OutBufferSize {
  130. get { return buffer_size; }
  131. }
  132. public virtual PipeTransmissionMode ReadMode {
  133. get {
  134. CheckPipePropertyOperations ();
  135. return read_trans_mode;
  136. }
  137. set {
  138. CheckPipePropertyOperations ();
  139. read_trans_mode = value;
  140. }
  141. }
  142. public SafePipeHandle SafePipeHandle {
  143. get {
  144. CheckPipePropertyOperations ();
  145. return handle;
  146. }
  147. }
  148. public virtual PipeTransmissionMode TransmissionMode {
  149. get {
  150. CheckPipePropertyOperations ();
  151. return transmission_mode;
  152. }
  153. }
  154. // initialize/dispose/state check
  155. [MonoTODO]
  156. protected internal virtual void CheckPipePropertyOperations ()
  157. {
  158. }
  159. [MonoTODO]
  160. protected internal void CheckReadOperations ()
  161. {
  162. if (!IsConnected)
  163. throw new InvalidOperationException ("Pipe is not connected");
  164. if (!CanRead)
  165. throw new NotSupportedException ("The pipe stream does not support read operations");
  166. }
  167. [MonoTODO]
  168. protected internal void CheckWriteOperations ()
  169. {
  170. if (!IsConnected)
  171. throw new InvalidOperationException ("Pipe is not connected");
  172. if (!CanWrite)
  173. throw new NotSupportedException ("The pipe stream does not support write operations");
  174. }
  175. protected void InitializeHandle (SafePipeHandle handle, bool isExposed, bool isAsync)
  176. {
  177. this.handle = handle;
  178. this.IsHandleExposed = isExposed;
  179. this.IsAsync = isAsync;
  180. }
  181. protected override void Dispose (bool disposing)
  182. {
  183. if (handle != null && disposing)
  184. handle.Dispose ();
  185. }
  186. // not supported
  187. public override long Length {
  188. get { throw new NotSupportedException (); }
  189. }
  190. public override long Position {
  191. get { return 0; }
  192. set { throw new NotSupportedException (); }
  193. }
  194. public override void SetLength (long value)
  195. {
  196. throw new NotSupportedException ();
  197. }
  198. public override long Seek (long offset, SeekOrigin origin)
  199. {
  200. throw new NotSupportedException ();
  201. }
  202. public PipeSecurity GetAccessControl ()
  203. {
  204. return new PipeSecurity (SafePipeHandle,
  205. AccessControlSections.Owner |
  206. AccessControlSections.Group |
  207. AccessControlSections.Access);
  208. }
  209. public void SetAccessControl (PipeSecurity pipeSecurity)
  210. {
  211. if (pipeSecurity == null)
  212. throw new ArgumentNullException ("pipeSecurity");
  213. pipeSecurity.Persist (SafePipeHandle);
  214. }
  215. // pipe I/O
  216. public void WaitForPipeDrain ()
  217. {
  218. }
  219. [MonoTODO]
  220. public override int Read ([In] byte [] buffer, int offset, int count)
  221. {
  222. CheckReadOperations ();
  223. return Stream.Read (buffer, offset, count);
  224. }
  225. [MonoTODO]
  226. public override int ReadByte ()
  227. {
  228. CheckReadOperations ();
  229. return Stream.ReadByte ();
  230. }
  231. [MonoTODO]
  232. public override void Write (byte [] buffer, int offset, int count)
  233. {
  234. CheckWriteOperations ();
  235. Stream.Write (buffer, offset, count);
  236. }
  237. [MonoTODO]
  238. public override void WriteByte (byte value)
  239. {
  240. CheckWriteOperations ();
  241. Stream.WriteByte (value);
  242. }
  243. [MonoTODO]
  244. public override void Flush ()
  245. {
  246. CheckWriteOperations ();
  247. Stream.Flush ();
  248. }
  249. // async
  250. Func<byte [],int,int,int> read_delegate;
  251. [HostProtection (SecurityAction.LinkDemand, ExternalThreading = true)]
  252. public override IAsyncResult BeginRead (byte [] buffer, int offset, int count, AsyncCallback callback, object state)
  253. {
  254. if (read_delegate == null)
  255. read_delegate = new Func<byte[],int,int,int> (Read);
  256. return read_delegate.BeginInvoke (buffer, offset, count, callback, state);
  257. }
  258. Action<byte[],int,int> write_delegate;
  259. [HostProtection (SecurityAction.LinkDemand, ExternalThreading = true)]
  260. public override IAsyncResult BeginWrite (byte[] buffer, int offset, int count, AsyncCallback callback, object state)
  261. {
  262. if (write_delegate == null)
  263. write_delegate = new Action<byte[],int,int> (Write);
  264. return write_delegate.BeginInvoke (buffer, offset, count, callback, state);
  265. }
  266. public override int EndRead (IAsyncResult asyncResult)
  267. {
  268. return read_delegate.EndInvoke (asyncResult);
  269. }
  270. public override void EndWrite (IAsyncResult asyncResult)
  271. {
  272. write_delegate.EndInvoke (asyncResult);
  273. }
  274. }
  275. }
  276. #endif