游戏客户端做出某个操作动作,对应的操作返回到服务器中,服务器进行处理后再返回处理信息到游戏客户端。下面就给大家说说游戏客户端是怎么处理处理服务器响应的。
NetworkManager.cs中Update函数 调用DealWithMsg进行消息的不断处理
public void Update(float fDeltaTime) //逐帧调用
{
if (m_Client != null)
{
DealWithMsg(); //关注这一行
public void DealWithMsg()
{
while (mReceiveMsgIDs.Count>0 && mReceiveStreams.Count>0)
{
int type = mReceiveMsgIDs[0];
System.IO.MemoryStream iostream = mReceiveStreams[0];
mReceiveMsgIDs.RemoveAt(0);
mReceiveStreams.RemoveAt(0);
CGLCtrl_GameLogic.Instance.HandleNetMsg(iostream, type); //注意这行很重要
if (mReceiveStreamsPool.Count<100)
{
mReceiveStreamsPool.Add(iostream);
}
else
{
iostream = null;
}
#if UNITY_EDITOR
#else
}
catch (Exception ecp)
{
Debugger.LogError("Handle Error msgid: " + type);
}
#endif
}
}
public partial class CGLCtrl_GameLogic : UnitySingleton
{
const int PROTO_DESERIALIZE_ERROR = -1;
public void HandleNetMsg(System.IO.Stream stream, int n32ProtocalID)
{
//Debug.Log("n32ProtocalID " + (GSToGC.MsgID)n32ProtocalID);
switch (n32ProtocalID)
{
case (Int32)LSToGC.MsgID.eMsgToGCFromLS_NotifyServerBSAddr:
OnNetMsg_NotifyServerAddr(stream);
break;
备注: eMsgToGCFromLS_NotifyServerBSAddr 表示的数字是:513
Int32 OnNetMsg_NotifyServerAddr(Stream stream)
{
LoginCtrl.Instance.UpdateServerAddr(stream); //关注这一行
return (Int32)EErrorCode.eNormal;
}
//更新服务器列表
public void UpdateServerAddr(Stream stream)
{
LSToGC.ServerBSAddr pMsg = ProtoBuf.Serializer.Deserialize(stream);
SelectServerData.Instance.Clean();
for (int i = 0; i < pMsg.serverinfo.Count; i++)
{
string addr = pMsg.serverinfo.ElementAt(i).ServerAddr;
int state = pMsg.serverinfo.ElementAt(i).ServerState;
string name = pMsg.serverinfo.ElementAt(i).ServerName;
int port = pMsg.serverinfo.ElementAt(i).ServerPort;
SelectServerData.Instance.SetServerList(i, name, (SelectServerData.ServerState)state, addr, port);
}
NetworkManager.Instance.Close();
NetworkManager.Instance.canReconnect = false;
SelectServerData.Instance.SetDefaultServer();
EventCenter.Broadcast(EGameEvent.eGameEvent_SdkServerCheckSuccess); //这一行,处理UI的显隐
}
//SDK检查成功
void SdkServerCheckSuccess()
{
ShowServer(LOGINUI.Login); //这一行很重要
mWaitingParent.gameObject.SetActive(false);
#if UNITY_STANDALONE_WIN || UNITY_EDITOR|| SKIP_SDK
#else
SdkConector.ShowToolBar(0);
#endif
}
//显示服务器信息或者显示登录信息
void ShowServer(LOGINUI state)
{
bool showLogin = false;
bool showServer = false;
bool showSelectServer = false;
switch (state)
{
case LOGINUI.Login:
ShowSelectServerInfo();
showLogin = true; //战场选择按钮和登录按钮显示
showServer = false; //战场选择框关掉
showSelectServer = false; //游戏运行画面看到的UI(用户名、密码框和服务器以及选择登录服务器按钮)关掉
break;
case LOGINUI.SelectServer:
showLogin = false;
showServer = true;
showSelectServer = false;
break;
case LOGINUI.None:
showLogin = false;
showServer = false;
#if UNITY_STANDALONE_WIN || UNITY_EDITOR || SKIP_SDK
showSelectServer = true;
#endif
break;
}
mPlayParent.gameObject.SetActive(showLogin);
mServerParent.gameObject.SetActive(showServer);
mLoginParent.gameObject.SetActive(showSelectServer);
if (showLogin)
{
#if UNITY_STANDALONE_WIN || UNITY_EDITOR|| SKIP_SDK
mChangeAccountName.text = mLoginAccountInput.value;
#else
mChangeAccountName.text = SdkConector.NickName();
#endif
}
mChangeAccountBtn.gameObject.SetActive(showLogin);
}