2
0

SocketStream.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using System;
  2. using System.IO;
  3. using System.Net.Sockets;
  4. namespace SocketHttpListener
  5. {
  6. public class SocketStream : Stream
  7. {
  8. private readonly Socket _socket;
  9. public SocketStream(Socket socket, bool ownsSocket)
  10. {
  11. _socket = socket;
  12. }
  13. public override void Flush()
  14. {
  15. }
  16. public override bool CanRead
  17. {
  18. get { return true; }
  19. }
  20. public override bool CanSeek
  21. {
  22. get { return false; }
  23. }
  24. public override bool CanWrite
  25. {
  26. get { return true; }
  27. }
  28. public override long Length
  29. {
  30. get { throw new NotImplementedException(); }
  31. }
  32. public override long Position
  33. {
  34. get { throw new NotImplementedException(); }
  35. set { throw new NotImplementedException(); }
  36. }
  37. public override void Write(byte[] buffer, int offset, int count)
  38. {
  39. _socket.Send(buffer, offset, count, SocketFlags.None);
  40. }
  41. public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
  42. {
  43. return _socket.BeginSend(buffer, offset, count, SocketFlags.None, callback, state);
  44. }
  45. public override void EndWrite(IAsyncResult asyncResult)
  46. {
  47. _socket.EndSend(asyncResult);
  48. }
  49. public override void SetLength(long value)
  50. {
  51. throw new NotImplementedException();
  52. }
  53. public override long Seek(long offset, SeekOrigin origin)
  54. {
  55. throw new NotImplementedException();
  56. }
  57. public override int Read(byte[] buffer, int offset, int count)
  58. {
  59. return _socket.Receive(buffer, offset, count, SocketFlags.None);
  60. }
  61. public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
  62. {
  63. return _socket.BeginReceive(buffer, offset, count, SocketFlags.None, callback, state);
  64. }
  65. public override int EndRead(IAsyncResult asyncResult)
  66. {
  67. return _socket.EndReceive(asyncResult);
  68. }
  69. }
  70. }