using UnityEngine;
|
using System;
|
using System.Collections;
|
using System.Collections.Generic;
|
|
|
public class GameNetSystem : Singleton<GameNetSystem>
|
{
|
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<GameNetPackBasic> mainProtocolQueue = new Queue<GameNetPackBasic>();
|
|
|
public GameNetSystem()
|
{
|
var gameObject = new GameObject("NetUpdateBehaviour");
|
GameObject.DontDestroyOnLoad(gameObject);
|
m_NetUpdateBehaviour = gameObject.AddComponent<NetUpdateBehaviour>();
|
m_NetUpdateBehaviour.RegisterUpdateCallBack(OnUpdate);
|
|
neverConnectState = gameObject.AddComponent<NeverConnectState>();
|
accountLoginState = gameObject.AddComponent<AccountLoginState>();
|
// createOrSelectRoleState = gameObject.AddComponent<CreateOrSelectRoleState>();
|
roleLoginState = gameObject.AddComponent<RoleLoginState>();
|
connectedState = gameObject.AddComponent<ConnectedState>();
|
disconnectState = gameObject.AddComponent<DisconnectState>();
|
|
netState = NetState.NerverConnect;
|
}
|
|
public void BeginConnectGameServer(string ip, int port, Action<bool> 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);
|
}
|
});
|
}
|
|
public void SendInfo(GameNetPackBasic protocol)
|
{
|
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;
|
// var loginModel = ModelCenter.Instance.GetModel<LoginModel>();
|
// loginModel.busy = false;
|
}
|
}
|
|
|
|
public void Reconnect()
|
{
|
try
|
{
|
if (mainSocket != null)
|
{
|
mainSocket.CloseConnect();
|
}
|
|
mainProtocolQueue.Clear();
|
}
|
catch (Exception ex)
|
{
|
Debug.Log(ex);
|
}
|
finally
|
{
|
netState = NetState.AccountLogin;
|
// var loginModel = ModelCenter.Instance.GetModel<LoginModel>();
|
// loginModel.busy = false;
|
// loginModel.ReAccountLogin();
|
}
|
}
|
|
public void LoginOut()
|
{
|
// if (CameraController.Instance != null)
|
// CameraController.Instance.SetInGame(false);
|
// DTC0102_tagCDBPlayer.isAfterPlayerDataInitialize = false;
|
|
try
|
{
|
// ynmbxxjUtil.Instance.RoleLoginOut();
|
|
if (mainSocket != null)
|
{
|
mainSocket.CloseConnect();
|
}
|
|
mainProtocolQueue.Clear();
|
}
|
catch (Exception ex)
|
{
|
Debug.Log(ex);
|
}
|
finally
|
{
|
netState = NetState.NerverConnect;
|
// TODO YYL
|
|
// var loginModel = ModelCenter.Instance.GetModel<LoginModel>();
|
// loginModel.busy = false;
|
|
// StageLoad.Instance.PushSceneLoadCommand(new StageLoad.StageLoadCommand()
|
// {
|
// toMapId = 1,
|
// toLineId = 0,
|
// needEmpty = false,
|
// needLoadResource = true,
|
// serverType = ServerType.Main,
|
// isClientLoadMap = true
|
// });
|
// //CameraManager.uiCamera.clearFlags = CameraClearFlags.SolidColor;
|
// //GameObject obj = GameObject.Find("NormalCanvas");
|
// //int count = obj.transform.childCount;
|
// //for (int i = count - 1; i >= 0; --i)
|
// //{
|
// // //Debug.LogError(obj.transform.GetChild(i).name);
|
// // GameObject.Destroy(obj.transform.GetChild(i).gameObject);
|
// //}
|
// //SceneManager.LoadScene("Empty");
|
NetLinkWin.Hide();
|
// DTC0403_tagPlayerLoginLoadOK.neverLoginOk = true;
|
}
|
}
|
|
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,
|
}
|