MessagePacker.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /****************************************************************************
  2. * Copyright 2019 Nreal Techonology Limited. All rights reserved.
  3. *
  4. * This file is part of NRSDK.
  5. *
  6. * https://www.nreal.ai/
  7. *
  8. *****************************************************************************/
  9. namespace NRKernal.Experimental.NetWork
  10. {
  11. using System;
  12. using System.Collections.Generic;
  13. /// <summary> A message packer. </summary>
  14. public class MessagePacker
  15. {
  16. /// <summary> The bytes. </summary>
  17. private List<byte> bytes = new List<byte>();
  18. /// <summary> Gets the package. </summary>
  19. /// <value> The package. </value>
  20. public byte[] Package
  21. {
  22. get { return bytes.ToArray(); }
  23. }
  24. /// <summary> Adds value. </summary>
  25. /// <param name="data"> The data to add.</param>
  26. /// <returns> A MessagePacker. </returns>
  27. public MessagePacker Add(byte[] data)
  28. {
  29. bytes.AddRange(data);
  30. return this;
  31. }
  32. /// <summary> Adds value. </summary>
  33. /// <param name="value"> The value to add.</param>
  34. /// <returns> A MessagePacker. </returns>
  35. public MessagePacker Add(ushort value)
  36. {
  37. byte[] data = BitConverter.GetBytes(value);
  38. bytes.AddRange(data);
  39. return this;
  40. }
  41. /// <summary> Adds value. </summary>
  42. /// <param name="value"> The value to add.</param>
  43. /// <returns> A MessagePacker. </returns>
  44. public MessagePacker Add(uint value)
  45. {
  46. byte[] data = BitConverter.GetBytes(value);
  47. bytes.AddRange(data);
  48. return this;
  49. }
  50. /// <summary> Adds value. </summary>
  51. /// <param name="value"> The value to add.</param>
  52. /// <returns> A MessagePacker. </returns>
  53. public MessagePacker Add(ulong value)
  54. {
  55. byte[] data = BitConverter.GetBytes(value);
  56. bytes.AddRange(data);
  57. return this;
  58. }
  59. }
  60. }