NamedPipeStream.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. //
  2. // System.Runtime.Remoting.Channels.Ipc.Win32.NamedPipeStream.cs
  3. //
  4. // Author: Robert Jordan ([email protected])
  5. //
  6. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  7. //
  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 NET_2_0
  29. using System;
  30. using System.IO;
  31. using System.Runtime.Remoting.Messaging;
  32. namespace System.Runtime.Remoting.Channels.Ipc.Win32
  33. {
  34. /// <summary>
  35. /// Provides the underlying stream of data for local Named Pipe access.
  36. /// </summary>
  37. internal class NamedPipeStream : Stream
  38. {
  39. readonly NamedPipeSocket socket;
  40. readonly bool ownsSocket;
  41. /// <summary>
  42. /// Creates a new instance of the NamedPipeStream class for the specified NamedPipeSocket.
  43. /// </summary>
  44. /// <param name="socket"></param>
  45. public NamedPipeStream(NamedPipeSocket socket, bool ownsSocket)
  46. {
  47. this.socket = socket;
  48. this.ownsSocket = ownsSocket;
  49. }
  50. public override bool CanRead
  51. {
  52. get { return true; }
  53. }
  54. public override bool CanSeek
  55. {
  56. get { return false; }
  57. }
  58. public override bool CanWrite
  59. {
  60. get { return true; }
  61. }
  62. public override long Length
  63. {
  64. get { throw new NotSupportedException(); }
  65. }
  66. public override long Position
  67. {
  68. get { throw new NotSupportedException(); }
  69. set { throw new NotSupportedException(); }
  70. }
  71. public override long Seek(long offset, SeekOrigin origin)
  72. {
  73. throw new NotSupportedException();
  74. }
  75. public override void SetLength(long value)
  76. {
  77. throw new NotSupportedException();
  78. }
  79. public override void Close()
  80. {
  81. if (ownsSocket) socket.Close();
  82. }
  83. public override void Flush()
  84. {
  85. }
  86. public override int Read(byte[] buffer, int offset, int count)
  87. {
  88. return socket.Receive(buffer, offset, count);
  89. }
  90. delegate int ReadMethod(byte[] buffer, int offset, int count);
  91. public override IAsyncResult BeginRead(byte[] buffer, int offset, int count,
  92. AsyncCallback callback, object state)
  93. {
  94. return new ReadMethod(Read).BeginInvoke(buffer, offset, count, callback, state);
  95. }
  96. public override int EndRead(IAsyncResult asyncResult)
  97. {
  98. AsyncResult ar = asyncResult as AsyncResult;
  99. return ((ReadMethod)ar.AsyncDelegate).EndInvoke(ar);
  100. }
  101. public override void Write(byte[] buffer, int offset, int count)
  102. {
  103. int written = socket.Send(buffer, offset, count);
  104. if (written != count)
  105. throw new IOException("Cannot write data");
  106. }
  107. delegate void WriteMethod(byte[] buffer, int offset, int count);
  108. public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count,
  109. AsyncCallback callback, object state)
  110. {
  111. return new WriteMethod(Write).BeginInvoke(buffer, offset, count, callback, state);
  112. }
  113. public override void EndWrite(IAsyncResult asyncResult)
  114. {
  115. AsyncResult ar = asyncResult as AsyncResult;
  116. ((WriteMethod)ar.AsyncDelegate).EndInvoke(asyncResult);
  117. }
  118. }
  119. }
  120. #endif