PipeStream.cs 8.5 KB

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