using UnityEngine; using System; using System.Collections; using System.Collections.Generic; public class GameNetSystem : Singleton { //限制客户端的下一个包是登录包C0101_tagCPlayerLogin,如果不是登录包不允许发送 bool waitLogin = false; //等待发送登录包,如果有其他包直接屏蔽,避免断线重连的情况发了攻击包之类的 //等待服务端0403的包后才能发其他的功能包,只有C0123_tagCClientPackVersion 和 C0101_tagCPlayerLogin 可以发送 bool waitLoginMap = false; NetUpdateBehaviour m_NetUpdateBehaviour; NeverConnectState neverConnectState; AccountLoginState accountLoginState; // CreateOrSelectRoleState createOrSelectRoleState; RoleLoginState roleLoginState; ConnectedState connectedState; DisconnectState disconnectState; NetState m_NetState; public NetState netState { get { return this.m_NetState; } set { if (this.m_NetState != value) { switch (m_NetState) { case NetState.NerverConnect: neverConnectState.OnExit(); break; case NetState.AccountLogin: accountLoginState.OnExit(); break; // case NetState.CreateOrSelectRole: // // createOrSelectRoleState.OnExit(); // break; case NetState.RoleLogin: roleLoginState.OnExit(); break; case NetState.Connected: connectedState.OnExit(); break; case NetState.DisConnected: disconnectState.OnExit(); break; } this.m_NetState = value; switch (m_NetState) { case NetState.NerverConnect: neverConnectState.OnEnter(); break; case NetState.AccountLogin: accountLoginState.OnEnter(); break; // case NetState.CreateOrSelectRole: // // createOrSelectRoleState.OnEnter(); // break; case NetState.RoleLogin: roleLoginState.OnEnter(); break; case NetState.Connected: connectedState.OnEnter(); break; case NetState.DisConnected: disconnectState.OnEnter(); break; } } } } private ClientSocket mainSocket; public bool mainSocketConnected { get { return mainSocket == null ? false : mainSocket.connected; } } public float timeSinceMainSocketLastProtocol { get { return mainSocket == null ? Time.time : (float)(DateTime.Now - mainSocket.lastPackageTime).TotalSeconds; } } Queue mainProtocolQueue = new Queue(); public GameNetSystem() { var gameObject = new GameObject("NetUpdateBehaviour"); GameObject.DontDestroyOnLoad(gameObject); m_NetUpdateBehaviour = gameObject.AddComponent(); m_NetUpdateBehaviour.RegisterUpdateCallBack(OnUpdate); neverConnectState = gameObject.AddComponent(); accountLoginState = gameObject.AddComponent(); // createOrSelectRoleState = gameObject.AddComponent(); roleLoginState = gameObject.AddComponent(); connectedState = gameObject.AddComponent(); disconnectState = gameObject.AddComponent(); netState = NetState.NerverConnect; } public void BeginConnectGameServer(string ip, int port, Action onConnected) { try { if (mainSocketConnected) { mainSocket.CloseConnect(); } } catch (System.Exception ex) { Debug.Log(ex); } mainSocket = new ClientSocket(ServerType.Main); mainProtocolQueue.Clear(); mainSocket.Connect(ip, port, (bool ok) => { if (onConnected != null) { onConnected(ok); } }); } //限制客户端的下一个包是登录包C0101_tagCPlayerLogin,如果不是登录包不允许发送 public void SetIsWaitLogin(bool _wait) { waitLogin = _wait; } //等待服务端0403的包后才能发其他的功能包,只有C0123_tagCClientPackVersion 和 C0101_tagCPlayerLogin 可以发送 public void SetIsWaitLoginMap(bool _wait) { waitLoginMap = _wait; } public void SendInfo(GameNetPackBasic protocol) { if (waitLogin) { if (protocol is not C0101_tagCPlayerLogin) { Debug.LogWarning("等待执行登录,不允许发送其他包 " + protocol.ToString()); return; } } if (waitLoginMap) { if (protocol is not C0123_tagCClientPackVersion && protocol is not C0101_tagCPlayerLogin) { Debug.LogWarning("等待0403包,不允许发送其他包 " + protocol.ToString()); return; } } if (mainSocket != null) { mainSocket.SendInfo(protocol); DebugPkgCache.Push(protocol); } } public void SendInfo(byte[] vBytes) { if (mainSocket != null) { mainSocket.SendInfo(vBytes); } } public void PushPackage(GameNetPackBasic protocol, ServerType type) { lock (this) { if (protocol == null) { return; } if (PackageRegedit.Contain(protocol.cmd)) { mainProtocolQueue.Enqueue(protocol); DebugPkgCache.Push(protocol); } else { Debug.LogErrorFormat("数据包(cmd:{0})未登记!", protocol.cmd); } } } public void Disconnect() { try { if (mainSocket != null) { mainSocket.CloseConnect(); } mainProtocolQueue.Clear(); } catch (Exception ex) { Debug.Log(ex); } finally { netState = NetState.DisConnected; LoginManager.Instance.busy = false; } } public void Reconnect() { try { if (mainSocket != null) { mainSocket.CloseConnect(); } mainProtocolQueue.Clear(); } catch (Exception ex) { Debug.Log(ex); } finally { netState = NetState.AccountLogin; LoginManager.Instance.busy = false; LoginManager.Instance.ReAccountLogin(); } } public void LoginOut() { try { SDKUtils.Instance.RoleLoginOut(); if (mainSocket != null) { mainSocket.CloseConnect(); } mainProtocolQueue.Clear(); } catch (Exception ex) { Debug.Log(ex); } finally { netState = NetState.NerverConnect; LoginManager.Instance.busy = false; StageManager.Instance.ReturnToLoginScene(); NetLinkWin.Hide(); } } public void OnAccountLogin() { netState = NetState.AccountLogin; } public void OnEnterWorld() { netState = NetState.RoleLogin; } void OnUpdate() { lock (this) { while (mainProtocolQueue.Count > 0) { var package = mainProtocolQueue.Dequeue(); if (package != null) { PackageRegedit.Distribute(package); } } } } public enum NetState { NerverConnect = 1, AccountLogin = 2, // CreateOrSelectRole = 3, RoleLogin = 4, Connected = 5, DisConnected = 6, } } public enum ServerType { Main = 1, B430 = 2, MainFight = 3, }