少年修仙传客户端代码仓库
client_Wu Xijin
2018-12-10 5635b0d770383333238f2dcdc0824392aefdc537
Core/NetworkPackage/GameNetSystem.cs
@@ -16,11 +16,9 @@
    DisconnectState disconnectState;
    NetState m_NetState;
    public NetState netState
    {
    public NetState netState {
        get { return this.m_NetState; }
        set
        {
        set {
            if (this.m_NetState != value)
            {
                switch (m_NetState)
@@ -71,17 +69,22 @@
        }
    }
    private ClientSocketController socketController;
    public bool socketConnected { get { return socketController == null ? false : socketController.Connected; } }
    private ClientSocket mainSocket;
    public bool mainSocketConnected { get { return mainSocket == null ? false : mainSocket.connected; } }
    Queue<GameNetPackBasic> packQueue = new Queue<GameNetPackBasic>();
    public float timeSinceMainSocketLastProtocol {
        get { return mainSocket == null ? 0f : (float)(DateTime.Now - mainSocket.lastPackageTime).TotalSeconds; }
    }
    DateTime m_LastPackageTime;
    public DateTime lastPackageTime { get { return m_LastPackageTime; } }
    private ClientSocket crossServerSocket;
    public bool crossServerSocketConnected { get { return crossServerSocket == null ? false : crossServerSocket.connected; } }
    Action onConnected;
    public event Action disconnectEvent;
    public event Action connectedEvent;
    public float timeSinceCrossServerSocketLastProtocol {
        get { return crossServerSocket == null ? 0f : (float)(DateTime.Now - crossServerSocket.lastPackageTime).TotalSeconds; }
    }
    Queue<GameNetPackBasic> mainProtocolQueue = new Queue<GameNetPackBasic>();
    Queue<GameNetPackBasic> crossSeverProtocolQueue = new Queue<GameNetPackBasic>();
    public GameNetSystem()
    {
@@ -100,13 +103,13 @@
        netState = NetState.NerverConnect;
    }
    public void BeginConnectGameServer(string _ip, int _port, Action _onConnected)
    public void BeginConnectGameServer(string ip, int port, Action onConnected)
    {
        try
        {
            if (socketController != null && socketController.Connected)
            if (mainSocketConnected)
            {
                socketController.CloseConnect();
                mainSocket.CloseConnect();
            }
        }
        catch (System.Exception ex)
@@ -114,77 +117,117 @@
            DebugEx.Log(ex);
        }
        socketController = new ClientSocketController();
        mainSocket = new ClientSocket(SocketType.Main);
        mainProtocolQueue.Clear();
        GameNetEncode.ResetEncodeIndex();
        lock (packQueue)
        mainSocket.Connect(ip, port, () =>
        {
            packQueue.Clear();
        }
        onConnected = _onConnected;
        socketController.Connect(_ip, _port, OnSocketConnected);
            if (onConnected != null)
            {
                onConnected();
            }
        });
    }
    public void SendInfo(GameNetPackBasic vNetPack)
    public void BeginConnectCrossServer(string ip, int port, Action onConnected)
    {
        if (socketController != null)
        try
        {
            socketController.SendInfo(vNetPack);
            DebugPkgCache.Push(vNetPack);
            if (crossServerSocketConnected)
            {
                crossServerSocket.CloseConnect();
            }
        }
        catch (System.Exception ex)
        {
            DebugEx.Log(ex);
        }
        crossServerSocket = new ClientSocket(SocketType.CrossSever);
        crossSeverProtocolQueue.Clear();
        this.crossServerSocket.Connect(ip, port, () =>
        {
            if (onConnected != null)
            {
                onConnected();
            }
        });
    }
    public void SendInfo(GameNetPackBasic protocol)
    {
        if (mainSocket != null)
        {
            mainSocket.SendInfo(protocol);
            DebugPkgCache.Push(protocol);
        }
    }
    public void SendInfo(byte[] vBytes)
    {
        if (socketController != null)
        if (mainSocket != null)
        {
            socketController.SendInfo(vBytes);
            mainSocket.SendInfo(vBytes);
        }
    }
    public void PushPackage(GameNetPackBasic _package)
    public void SendToCrossServer(GameNetPackBasic protocol)
    {
        if (_package == null)
        if (crossServerSocket != null)
        {
            return;
            crossServerSocket.SendInfo(protocol);
            DebugPkgCache.Push(protocol);
        }
    }
        m_LastPackageTime = DateTime.Now;
        lock (packQueue)
    public void PushPackage(GameNetPackBasic protocol, SocketType type)
    {
        lock (this)
        {
            lock (_package)
            if (protocol == null)
            {
                if (PackageRegedit.Contain(_package.cmd))
                {
                    packQueue.Enqueue(_package);
                    DebugPkgCache.Push(_package);
                }
                else
                {
                    DebugEx.LogWarningFormat("数据包(cmd:{0})未登记!", _package.cmd);
                }
                return;
            }
        }
            if (PackageRegedit.Contain(protocol.cmd))
            {
                switch (type)
                {
                    case SocketType.Main:
                        mainProtocolQueue.Enqueue(protocol);
                        break;
                    case SocketType.CrossSever:
                        crossSeverProtocolQueue.Enqueue(protocol);
                        break;
                    default:
                        break;
                }
                DebugPkgCache.Push(protocol);
            }
            else
            {
                DebugEx.LogWarningFormat("数据包(cmd:{0})未登记!", protocol.cmd);
            }
        }
    }
    public void Disconnect()
    {
        try
        {
            if (socketController != null)
            if (mainSocket != null)
            {
                socketController.CloseConnect();
                mainSocket.CloseConnect();
            }
            GameNetEncode.ResetEncodeIndex();
            lock (packQueue)
            if (crossServerSocket != null)
            {
                packQueue.Clear();
                crossServerSocket.CloseConnect();
            }
            mainProtocolQueue.Clear();
            crossSeverProtocolQueue.Clear();
        }
        catch (Exception ex)
        {
@@ -193,6 +236,8 @@
        finally
        {
            netState = NetState.DisConnected;
            var loginModel = ModelCenter.Instance.GetModel<LoginModel>();
            loginModel.busy = false;
        }
    }
@@ -201,15 +246,18 @@
    {
        try
        {
            if (socketController != null)
            if (mainSocket != null)
            {
                socketController.CloseConnect();
                mainSocket.CloseConnect();
            }
            GameNetEncode.ResetEncodeIndex();
            lock (packQueue)
            if (crossServerSocket != null)
            {
                packQueue.Clear();
                crossServerSocket.CloseConnect();
            }
            mainProtocolQueue.Clear();
            crossSeverProtocolQueue.Clear();
        }
        catch (Exception ex)
        {
@@ -218,7 +266,6 @@
        finally
        {
            netState = NetState.AccountLogin;
            var loginModel = ModelCenter.Instance.GetModel<LoginModel>();
            loginModel.busy = false;
            loginModel.ReAccountLogin();
@@ -229,15 +276,18 @@
    {
        try
        {
            if (socketController != null)
            if (mainSocket != null)
            {
                socketController.CloseConnect();
                mainSocket.CloseConnect();
            }
            GameNetEncode.ResetEncodeIndex();
            lock (packQueue)
            if (crossServerSocket != null)
            {
                packQueue.Clear();
                crossServerSocket.CloseConnect();
            }
            mainProtocolQueue.Clear();
            crossSeverProtocolQueue.Clear();
        }
        catch (Exception ex)
        {
@@ -252,7 +302,6 @@
            StageManager.Instance.LoadLoginStage();
            NetLinkWin.Hide();
        }
    }
    public void OnAccountLogin()
@@ -267,26 +316,14 @@
    void OnUpdate()
    {
        lock (packQueue)
        while (mainProtocolQueue.Count > 0)
        {
            if (packQueue != null)
            {
                while (packQueue.Count > 0)
                {
                    PackageRegedit.Distribute(packQueue.Dequeue());
                }
            }
            PackageRegedit.Distribute(mainProtocolQueue.Dequeue());
        }
    }
    private void OnSocketConnected()
    {
        m_LastPackageTime = DateTime.Now;
        if (onConnected != null)
        while (crossSeverProtocolQueue.Count > 0)
        {
            onConnected();
            onConnected = null;
            PackageRegedit.Distribute(crossSeverProtocolQueue.Dequeue());
        }
    }
@@ -300,4 +337,10 @@
        DisConnected = 6,
    }
    public enum SocketType
    {
        Main = 1,
        CrossSever = 2,
    }
}