PipeStream.cs 8.6 KB

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