QuickManager.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using CScript.Utilities;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using System;
  6. using System.Runtime.InteropServices;
  7. using System.Threading;
  8. using CScript.Net;
  9. using CScript.App;
  10. using System.IO;
  11. namespace CScript.Quick
  12. {
  13. public class QuickManager : MonoSingleton<QuickManager>
  14. {
  15. public PackageHandler packageHandler = new PackageHandler(null);
  16. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  17. public delegate int OnNewConnCallbackDelegate();
  18. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  19. public delegate int OnReadCallbackDelegate(IntPtr ptr, int ptrLen, int ptrIndex);
  20. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  21. public delegate int OnConnClosedCallbackDelegate();
  22. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  23. public delegate int OnLogCallbackDelegate(float plr);
  24. [DllImport("ClientDll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, EntryPoint = "QuicConnect")]
  25. private static extern IntPtr QuicConnect(string alpn, string H, string s,
  26. OnNewConnCallbackDelegate onNewConnCb, OnReadCallbackDelegate onReadCb, OnConnClosedCallbackDelegate onConnClosedCb, OnLogCallbackDelegate onLogCb);
  27. private Thread _quickThread;
  28. private int _rcvBufLen = 0;// 测试的一个属性
  29. private int _quickReturnValue = 0;
  30. private string _IPPort;
  31. const int DEF_RECV_BUFFER_SIZE = 409600 * 2;
  32. private MemoryStream _receiveBuffer = new MemoryStream(DEF_RECV_BUFFER_SIZE);
  33. public void Init(string ip,string port)
  34. {
  35. _IPPort = ip.ToString() + ":" + port.ToString();
  36. if (_quickThread == null || !_quickThread.IsAlive)
  37. {
  38. _quickThread = new Thread(StartQuick);
  39. _quickThread.Start();
  40. }
  41. }
  42. void StartQuick()
  43. {
  44. Debug.LogWarning("StartQuick");
  45. QuicConnect("echo", "www.example.com", _IPPort,
  46. OnNewConnCallBack,
  47. OnReadCallBack,
  48. OnConnClosedCallBack,
  49. OnLogCallBack
  50. );
  51. }
  52. private int OnNewConnCallBack()
  53. {
  54. Debug.Log("c# OnNewConnCallBack");
  55. NetDistribute.Instance.OnConnect(true, "success");
  56. return 0;
  57. }
  58. private int OnReadCallBack(IntPtr ptr, int ptrLen, int ptrIndex)
  59. {
  60. //byte[] buf = new byte[ptrLen];
  61. Marshal.Copy(ptr, this._receiveBuffer.GetBuffer(), 0, ptrLen);
  62. this.packageHandler.ReceiveQuicData(this._receiveBuffer.GetBuffer(), 0, ptrLen);
  63. //string bufStr = System.Text.Encoding.ASCII.GetString(buf);
  64. //_rcvBufLen += ptrLen;
  65. //Debug.Log("c# OnReadCallBack bufLen=" + ptrLen.ToString() + ",rcvBufLen=" + _rcvBufLen.ToString());
  66. ////Console.WriteLine(bufStr);
  67. //if (_rcvBufLen >= 442275)
  68. //{
  69. // Debug.Log("c# OnReadCallBack 主动关闭quic连接");
  70. // return 10;// return 10表示关闭quic建立起来的连接
  71. //}
  72. return _quickReturnValue;
  73. }
  74. private int OnConnClosedCallBack()
  75. {
  76. Debug.Log("c# OnConnClosedCallBack");
  77. this._receiveBuffer.Position = 0;
  78. this._receiveBuffer.SetLength(0);
  79. this.packageHandler.Reset();
  80. NetDistribute.Instance.OnDisconnect(0,"success");
  81. return 0;
  82. }
  83. static int OnLogCallBack(float plr)
  84. {
  85. //Debug.LogWarning("c# OnLogCallBack " + plr.ToString());
  86. return 0;
  87. }
  88. public void Close()
  89. {
  90. _quickReturnValue = 10;
  91. }
  92. }
  93. }