QuicPackageHandler.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using UnityEngine;
  6. namespace CScript.Net
  7. {
  8. public class QuicPackageHandler
  9. {
  10. private MemoryStream _stream = new MemoryStream(4096000);
  11. private int _readOffset = 0;
  12. private string _startSign;//#号表示一个数据的开始
  13. private bool _startWriteSteam = false;
  14. public QuicPackageHandler()
  15. {
  16. }
  17. public void ReceiveData(byte[] data, int offset, int count)
  18. {
  19. if (!_startWriteSteam)
  20. {
  21. while (_readOffset + 13 < count)
  22. {
  23. _startSign = System.Text.Encoding.UTF8.GetString(data, _readOffset, 1);
  24. int packageDataLen = BitConverter.ToInt32(data, _readOffset + 9);
  25. if (_startSign == "#" && packageDataLen >= 0)
  26. {
  27. _startWriteSteam = true;
  28. break;
  29. }
  30. else
  31. {
  32. _readOffset++;
  33. }
  34. }
  35. }
  36. else
  37. {
  38. if (_stream.Position + count > _stream.Capacity)
  39. {
  40. throw new Exception("PackageHandler write buffer overflow");
  41. }
  42. _stream.Write(data, offset, count);
  43. //ParsePackage();
  44. }
  45. }
  46. }
  47. }