yyl
2026-05-08 ea4e25ceca21484cbb422b4ce49ec6bc1220441f
Main/Core/NetworkPackage/GameNetSystem.cs
@@ -2,10 +2,15 @@
using System;
using System.Collections;
using System.Collections.Generic;
using Cysharp.Threading.Tasks;
public class GameNetSystem : Singleton<GameNetSystem>
{
    //限制客户端的下一个包是登录包C0101_tagCPlayerLogin,如果不是登录包不允许发送
    bool waitLogin = false; //等待发送登录包,如果有其他包直接屏蔽,避免断线重连的情况发了攻击包之类的
    //等待服务端0403的包后才能发其他的功能包,只有C0123_tagCClientPackVersion 和 C0101_tagCPlayerLogin 可以发送
    bool waitLoginMap = false;
    NetUpdateBehaviour m_NetUpdateBehaviour;
    NeverConnectState neverConnectState;
    AccountLoginState accountLoginState;
@@ -113,7 +118,15 @@
            Debug.Log(ex);
        }
        Debug.unityLogger.logEnabled = true;
        mainSocket = new ClientSocket(ServerType.Main);
        //  websocket的断开链接需要处理一下
        mainSocket.OnDisconnected = () =>
        {
            netState = NetState.DisConnected;
            LoginManager.Instance.busy = false;
        };
        mainProtocolQueue.Clear();
        mainSocket.Connect(ip, port, (bool ok) =>
@@ -125,8 +138,61 @@
        });
    }
    //限制客户端的下一个包是登录包C0101_tagCPlayerLogin,如果不是登录包不允许发送
    public void SetIsWaitLogin(bool _wait)
    {
        waitLogin = _wait;
    }
    //等待服务端0403的包后才能发其他的功能包,只有C0123_tagCClientPackVersion 和 C0101_tagCPlayerLogin 可以发送
    public void SetIsWaitLoginMap(bool _wait)
    {
        waitLoginMap = _wait;
    }
    public bool GetIsWaitLoginMap()
    {
        return waitLoginMap;
    }
    //0403登录之前的包缓存
    Queue<GameNetPackBasic> sendQueue = new Queue<GameNetPackBasic>();
    public void SendCachePackage()
    {
        int cnt = sendQueue.Count;
        if (mainSocket != null)
        {
            while (sendQueue.Count > 0)
            {
                SendInfo(sendQueue.Dequeue());
            }
        }
        Debug.Log($"重点提醒:0403登录后 发送缓存包数量 {cnt} 个");
    }
    public void SendInfo(GameNetPackBasic protocol)
    {
        if (waitLogin)
        {
            if (protocol is not C0101_tagCPlayerLogin)
            {
                Debug.LogWarning("等待执行登录,不允许发送其他包 " + protocol.ToString());
                return;
            }
        }
        // 0102是从地图发送的 说明已登录,但可能卡顿导致通知route状态慢于客户端,依然需要防范
        if (waitLoginMap)
        {
            if (protocol is not C0123_tagCClientPackVersion && protocol is not C0101_tagCPlayerLogin)
            {
                Debug.Log("重点提醒:登录完成前的封包先加入队列 等0403回包后再一起发送服务端 " + protocol.ToString());
                sendQueue.Enqueue(protocol);
                return;
            }
        }
        if (mainSocket != null)
        {
            mainSocket.SendInfo(protocol);
@@ -134,6 +200,7 @@
        }
    }
#if UNITY_EDITOR
    public void SendInfo(byte[] vBytes)
    {
        if (mainSocket != null)
@@ -141,7 +208,7 @@
            mainSocket.SendInfo(vBytes);
        }
    }
#endif
    public void PushPackage(GameNetPackBasic protocol, ServerType type)
    {
@@ -235,7 +302,7 @@
            LoginManager.Instance.busy = false;
            StageManager.Instance.ReturnToLoginScene();
            StageManager.Instance.ReturnToLoginScene().Forget();
            NetLinkWin.Hide();
        }
    }
@@ -252,6 +319,8 @@
    void OnUpdate()
    {
        mainSocket?.DispatchMessageQueue();
        lock (this)
        {
            while (mainProtocolQueue.Count > 0)