123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- using CScript.Utilities;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using System;
- using System.Runtime.InteropServices;
- using System.Threading;
- using CScript.Net;
- using CScript.App;
- using System.IO;
- namespace CScript.Quick
- {
- public class QuickManager : MonoSingleton<QuickManager>
- {
- public PackageHandler packageHandler = new PackageHandler(null);
- [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
- public delegate int OnNewConnCallbackDelegate();
- [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
- public delegate int OnReadCallbackDelegate(IntPtr ptr, int ptrLen, int ptrIndex);
- [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
- public delegate int OnConnClosedCallbackDelegate();
- [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
- public delegate int OnLogCallbackDelegate(float plr);
- [DllImport("ClientDll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, EntryPoint = "QuicConnect")]
- private static extern IntPtr QuicConnect(string alpn, string H, string s,
- OnNewConnCallbackDelegate onNewConnCb, OnReadCallbackDelegate onReadCb, OnConnClosedCallbackDelegate onConnClosedCb, OnLogCallbackDelegate onLogCb);
- private Thread _quickThread;
- private int _rcvBufLen = 0;// 测试的一个属性
- private int _quickReturnValue = 0;
- private string _IPPort;
- const int DEF_RECV_BUFFER_SIZE = 409600 * 2;
- private MemoryStream _receiveBuffer = new MemoryStream(DEF_RECV_BUFFER_SIZE);
- public void Init(string ip,string port)
- {
- _IPPort = ip.ToString() + ":" + port.ToString();
- if (_quickThread == null || !_quickThread.IsAlive)
- {
- _quickThread = new Thread(StartQuick);
- _quickThread.Start();
- }
- }
- void StartQuick()
- {
- Debug.LogWarning("StartQuick");
- QuicConnect("echo", "www.example.com", _IPPort,
- OnNewConnCallBack,
- OnReadCallBack,
- OnConnClosedCallBack,
- OnLogCallBack
- );
- }
- private int OnNewConnCallBack()
- {
- Debug.Log("c# OnNewConnCallBack");
- NetDistribute.Instance.OnConnect(true, "success");
- return 0;
- }
- private int OnReadCallBack(IntPtr ptr, int ptrLen, int ptrIndex)
- {
- //byte[] buf = new byte[ptrLen];
- Marshal.Copy(ptr, this._receiveBuffer.GetBuffer(), 0, ptrLen);
- this.packageHandler.ReceiveQuicData(this._receiveBuffer.GetBuffer(), 0, ptrLen);
- //string bufStr = System.Text.Encoding.ASCII.GetString(buf);
- //_rcvBufLen += ptrLen;
- //Debug.Log("c# OnReadCallBack bufLen=" + ptrLen.ToString() + ",rcvBufLen=" + _rcvBufLen.ToString());
- ////Console.WriteLine(bufStr);
- //if (_rcvBufLen >= 442275)
- //{
- // Debug.Log("c# OnReadCallBack 主动关闭quic连接");
- // return 10;// return 10表示关闭quic建立起来的连接
- //}
- return _quickReturnValue;
- }
- private int OnConnClosedCallBack()
- {
- Debug.Log("c# OnConnClosedCallBack");
- this._receiveBuffer.Position = 0;
- this._receiveBuffer.SetLength(0);
- this.packageHandler.Reset();
- NetDistribute.Instance.OnDisconnect(0,"success");
- return 0;
- }
- static int OnLogCallBack(float plr)
- {
- //Debug.LogWarning("c# OnLogCallBack " + plr.ToString());
- return 0;
- }
- public void Close()
- {
- _quickReturnValue = 10;
- }
- }
- }
|