SocketStream.cs 2.3 KB

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