You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
1.1 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. namespace IllusionInjector.Utilities
  9. {
  10. public class EchoStream : MemoryStream
  11. {
  12. private ManualResetEvent m_dataReady = new ManualResetEvent(false);
  13. private byte[] m_buffer;
  14. private int m_offset;
  15. private int m_count;
  16. public override void Write(byte[] buffer, int offset, int count)
  17. {
  18. m_buffer = buffer;
  19. m_offset = offset;
  20. m_count = count;
  21. m_dataReady.Set();
  22. }
  23. public override int Read(byte[] buffer, int offset, int count)
  24. {
  25. if (m_buffer == null)
  26. {
  27. // Block until the stream has some more data.
  28. m_dataReady.Reset();
  29. m_dataReady.WaitOne();
  30. }
  31. Buffer.BlockCopy(m_buffer, m_offset, buffer, offset, (count < m_count) ? count : m_count);
  32. m_buffer = null;
  33. return (count < m_count) ? count : m_count;
  34. }
  35. }
  36. }