各个网络模块_英雄联盟

强联网:

    必须输入用户名和密码等服务器确认,才可以进入游戏。

面板操作登陆

  LoginCtrl.Instance.Login(mLoginAccountInput.value, mLoginPassInput.value);//验证登录名和登录密码

Control控制

public void Login(string account, string pass)
        {
            //设置用户名和密码
            SelectServerData.Instance.SetServerInfo((int)SdkManager.Instance.GetPlatFrom(), account, pass);
            //网络先切断,再连接
            NetworkManager.Instance.canReconnect = false;
            NetworkManager.Instance.Close();

            NetworkManager.Instance.Init(GameLogic.Instance.LoginServerAdress,GameLogic.Instance.LoginServerPort, NetworkManager.ServerType.LoginServer);
        }

下面的方法会在Update中执行

登陆错误,广播一条消息

登陆成功

开始游戏   

登陆信息保存起来

网络处理

找到连接的分类

对连接的protobuf进行填充,然后序列化

 public void SendMsg(ProtoBuf.IExtensible pMsg, Int32 n32MsgID)
        {
            if (m_Client != null)
            {
                //清除stream
                mSendStream.SetLength(0);
                mSendStream.Position = 0;

               
                //序列到stream
                ProtoBuf.Serializer.Serialize(mSendStream, pMsg);
                CMsg pcMsg = new CMsg((int)mSendStream.Length);
                pcMsg.SetProtocalID(n32MsgID);
                pcMsg.Add(mSendStream.ToArray(), 0, (int)mSendStream.Length);//放入到队列中
                //ms.Close();
#if UNITY_EDITOR
#else
                try
                {
#endif

#if LOG_FILE && UNITY_EDITOR
                if (n32MsgID != 8192 && n32MsgID != 16385)
                {
                    string msgName = "";
                    if (Enum.IsDefined(typeof(GCToBS.MsgNum), n32MsgID))
                    {
                        msgName = ((GCToBS.MsgNum)n32MsgID).ToString();
                    }
                    else if (Enum.IsDefined(typeof(GCToCS.MsgNum), n32MsgID))
                    {
                        msgName = ((GCToCS.MsgNum)n32MsgID).ToString();
                    }
                    else if (Enum.IsDefined(typeof(GCToLS.MsgID), n32MsgID))
                    {
                        msgName = ((GCToLS.MsgID)n32MsgID).ToString();
                    }
                    else if (Enum.IsDefined(typeof(GCToSS.MsgNum), n32MsgID))
                    {
                        msgName = ((GCToSS.MsgNum)n32MsgID).ToString();
                    }

                    using (System.IO.StreamWriter sw = new System.IO.StreamWriter(@"E:\Log.txt", true))
                    {
                        sw.WriteLine(Time.time + "   发送消息:\t" + n32MsgID + "\t" + msgName);
                    }
                }
#endif
                m_Client.GetStream().Write(pcMsg.GetMsgBuffer(), 0, (int)pcMsg.GetMsgSize());//写入流里面以便发出去
#if UNITY_EDITOR
#else
                }
                catch (Exception exc)
                {
                    Debugger.LogError(exc.ToString());
                    Close();
                }
#endif
            }
        }ProtoBuf.Serializer.Serialize(mSendStream, pMsg);
                CMsg pcMsg = new CMsg((int)mSendStream.Length);
                pcMsg.SetProtocalID(n32MsgID);
                pcMsg.Add(mSendStream.ToArray(), 0, (int)mSendStream.Length);//放入到队列中
                //ms.Close();
#if UNITY_EDITOR
#else
                try
                {
#endif

#if LOG_FILE && UNITY_EDITOR
                if (n32MsgID != 8192 && n32MsgID != 16385)
                {
                    string msgName = "";
                    if (Enum.IsDefined(typeof(GCToBS.MsgNum), n32MsgID))
                    {
                        msgName = ((GCToBS.MsgNum)n32MsgID).ToString();
                    }
                    else if (Enum.IsDefined(typeof(GCToCS.MsgNum), n32MsgID))
                    {
                        msgName = ((GCToCS.MsgNum)n32MsgID).ToString();
                    }
                    else if (Enum.IsDefined(typeof(GCToLS.MsgID), n32MsgID))
                    {
                        msgName = ((GCToLS.MsgID)n32MsgID).ToString();
                    }
                    else if (Enum.IsDefined(typeof(GCToSS.MsgNum), n32MsgID))
                    {
                        msgName = ((GCToSS.MsgNum)n32MsgID).ToString();
                    }

                    using (System.IO.StreamWriter sw = new System.IO.StreamWriter(@"E:\Log.txt", true))
                    {
                        sw.WriteLine(Time.time + "   发送消息:\t" + n32MsgID + "\t" + msgName);
                    }
                }
#endif
                m_Client.GetStream().Write(pcMsg.GetMsgBuffer(), 0, (int)pcMsg.GetMsgSize());//写入流里面以便发出去
#if UNITY_EDITOR
#else
                }
                catch (Exception exc)
                {
                    Debugger.LogError(exc.ToString());
                    Close();
                }
#endif
            }
        }

猜你喜欢

转载自blog.csdn.net/qq_35647121/article/details/80964293