PipeStream.cs 8.5 KB

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