StreamReceivedEventArgs.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. namespace WatsonTcp
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. /// <summary>
  7. /// Event arguments for when a stream is received.
  8. /// </summary>
  9. public class StreamReceivedEventArgs : EventArgs
  10. {
  11. #region Public-Members
  12. /// <summary>
  13. /// Client metadata.
  14. /// </summary>
  15. public ClientMetadata Client { get; } = null;
  16. /// <summary>
  17. /// The metadata received from the endpoint.
  18. /// </summary>
  19. public Dictionary<string, object> Metadata
  20. {
  21. get
  22. {
  23. return _Metadata;
  24. }
  25. set
  26. {
  27. if (value == null) _Metadata = new Dictionary<string, object>();
  28. else _Metadata = value;
  29. }
  30. }
  31. /// <summary>
  32. /// The number of data bytes that should be read from DataStream.
  33. /// </summary>
  34. public long ContentLength { get; }
  35. /// <summary>
  36. /// The stream containing the message data.
  37. /// </summary>
  38. public Stream DataStream { get; }
  39. /// <summary>
  40. /// The data from DataStream.
  41. /// Using Data will fully read the contents of DataStream.
  42. /// </summary>
  43. public byte[] Data
  44. {
  45. get
  46. {
  47. if (_Data != null) return _Data;
  48. if (ContentLength <= 0) return null;
  49. _Data = ReadFromStream(DataStream, ContentLength);
  50. return _Data;
  51. }
  52. }
  53. #endregion
  54. #region Private-Members
  55. private Dictionary<string, object> _Metadata = new Dictionary<string, object>();
  56. private byte[] _Data = null;
  57. private int _BufferSize = 65536;
  58. #endregion
  59. #region Constructors-and-Factories
  60. internal StreamReceivedEventArgs(ClientMetadata client, Dictionary<string, object> metadata, long contentLength, Stream stream)
  61. {
  62. Client = client;
  63. Metadata = metadata;
  64. ContentLength = contentLength;
  65. DataStream = stream;
  66. }
  67. #endregion
  68. #region Public-Methods
  69. #endregion
  70. #region Private-Methods
  71. private byte[] ReadFromStream(Stream stream, long count)
  72. {
  73. if (count <= 0) return Array.Empty<byte>();
  74. byte[] buffer = new byte[_BufferSize];
  75. int read = 0;
  76. long bytesRemaining = count;
  77. MemoryStream ms = new MemoryStream();
  78. while (bytesRemaining > 0)
  79. {
  80. if (_BufferSize > bytesRemaining) buffer = new byte[bytesRemaining];
  81. read = stream.Read(buffer, 0, buffer.Length);
  82. if (read > 0)
  83. {
  84. ms.Write(buffer, 0, read);
  85. bytesRemaining -= read;
  86. }
  87. else
  88. {
  89. throw new IOException("Could not read from supplied stream.");
  90. }
  91. }
  92. byte[] data = ms.ToArray();
  93. return data;
  94. }
  95. #endregion
  96. }
  97. }