PacketReader.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. using Microsoft.Xna.Framework;
  5. namespace Microsoft.Xna.Framework.Net
  6. {
  7. /// <summary>
  8. /// Reads network data packets in a format compatible with XNA's PacketReader.
  9. /// </summary>
  10. public class PacketReader : IDisposable
  11. {
  12. private readonly MemoryStream stream;
  13. private readonly BinaryReader reader;
  14. private bool disposed;
  15. /// <summary>
  16. /// Initializes a new PacketReader from byte data.
  17. /// </summary>
  18. internal PacketReader(byte[] data)
  19. {
  20. stream = new MemoryStream(data);
  21. reader = new BinaryReader(stream, Encoding.UTF8);
  22. }
  23. /// <summary>
  24. /// Initializes a new empty PacketReader.
  25. /// </summary>
  26. public PacketReader()
  27. {
  28. stream = new MemoryStream();
  29. reader = new BinaryReader(stream, Encoding.UTF8);
  30. }
  31. /// <summary>
  32. /// Gets the length of the packet data.
  33. /// </summary>
  34. public int Length => (int)stream.Length;
  35. /// <summary>
  36. /// Gets the current position in the packet data.
  37. /// </summary>
  38. public int Position
  39. {
  40. get => (int)stream.Position;
  41. set => stream.Position = value;
  42. }
  43. /// <summary>
  44. /// Gets the number of bytes remaining to be read.
  45. /// </summary>
  46. public int BytesRemaining => Length - Position;
  47. /// <summary>
  48. /// Reads a boolean value.
  49. /// </summary>
  50. public bool ReadBoolean()
  51. {
  52. return reader.ReadBoolean();
  53. }
  54. /// <summary>
  55. /// Reads a byte value.
  56. /// </summary>
  57. public byte ReadByte()
  58. {
  59. return reader.ReadByte();
  60. }
  61. /// <summary>
  62. /// Reads a signed byte value.
  63. /// </summary>
  64. public sbyte ReadSByte()
  65. {
  66. return reader.ReadSByte();
  67. }
  68. /// <summary>
  69. /// Reads a 16-bit integer.
  70. /// </summary>
  71. public short ReadInt16()
  72. {
  73. return reader.ReadInt16();
  74. }
  75. /// <summary>
  76. /// Reads an unsigned 16-bit integer.
  77. /// </summary>
  78. public ushort ReadUInt16()
  79. {
  80. return reader.ReadUInt16();
  81. }
  82. /// <summary>
  83. /// Reads a 32-bit integer.
  84. /// </summary>
  85. public int ReadInt32()
  86. {
  87. return reader.ReadInt32();
  88. }
  89. /// <summary>
  90. /// Reads an unsigned 32-bit integer.
  91. /// </summary>
  92. public uint ReadUInt32()
  93. {
  94. return reader.ReadUInt32();
  95. }
  96. /// <summary>
  97. /// Reads a 64-bit integer.
  98. /// </summary>
  99. public long ReadInt64()
  100. {
  101. return reader.ReadInt64();
  102. }
  103. /// <summary>
  104. /// Reads an unsigned 64-bit integer.
  105. /// </summary>
  106. public ulong ReadUInt64()
  107. {
  108. return reader.ReadUInt64();
  109. }
  110. /// <summary>
  111. /// Reads a single-precision floating point value.
  112. /// </summary>
  113. public float ReadSingle()
  114. {
  115. return reader.ReadSingle();
  116. }
  117. /// <summary>
  118. /// Reads a double-precision floating point value.
  119. /// </summary>
  120. public double ReadDouble()
  121. {
  122. return reader.ReadDouble();
  123. }
  124. /// <summary>
  125. /// Reads a string value.
  126. /// </summary>
  127. public string ReadString()
  128. {
  129. return reader.ReadString();
  130. }
  131. /// <summary>
  132. /// Reads a Vector2 value.
  133. /// </summary>
  134. public Vector2 ReadVector2()
  135. {
  136. return new Vector2(reader.ReadSingle(), reader.ReadSingle());
  137. }
  138. /// <summary>
  139. /// Reads a Vector3 value.
  140. /// </summary>
  141. public Vector3 ReadVector3()
  142. {
  143. return new Vector3(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle());
  144. }
  145. /// <summary>
  146. /// Reads a Vector4 value.
  147. /// </summary>
  148. public Vector4 ReadVector4()
  149. {
  150. return new Vector4(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle());
  151. }
  152. /// <summary>
  153. /// Reads a Quaternion value.
  154. /// </summary>
  155. public Quaternion ReadQuaternion()
  156. {
  157. return new Quaternion(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle());
  158. }
  159. /// <summary>
  160. /// Reads a Matrix value.
  161. /// </summary>
  162. public Matrix ReadMatrix()
  163. {
  164. return new Matrix(
  165. reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(),
  166. reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(),
  167. reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(),
  168. reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()
  169. );
  170. }
  171. /// <summary>
  172. /// Reads a Color value.
  173. /// </summary>
  174. public Color ReadColor()
  175. {
  176. return new Color(reader.ReadByte(), reader.ReadByte(), reader.ReadByte(), reader.ReadByte());
  177. }
  178. /// <summary>
  179. /// Reads a byte array.
  180. /// </summary>
  181. public byte[] ReadBytes()
  182. {
  183. int length = reader.ReadInt32();
  184. return reader.ReadBytes(length);
  185. }
  186. /// <summary>
  187. /// Reads a specified number of bytes.
  188. /// </summary>
  189. public byte[] ReadBytes(int count)
  190. {
  191. return reader.ReadBytes(count);
  192. }
  193. /// <summary>
  194. /// Closes the packet reader.
  195. /// </summary>
  196. public void Close()
  197. {
  198. Dispose();
  199. }
  200. /// <summary>
  201. /// Disposes the packet reader.
  202. /// </summary>
  203. public void Dispose()
  204. {
  205. if (!disposed)
  206. {
  207. reader?.Dispose();
  208. stream?.Dispose();
  209. disposed = true;
  210. }
  211. }
  212. }
  213. }