Exemple #1
0
        public override void DispatchMessage(Session session, object msgType, object msgData)
        {
            //Debug.Log("DispatchMessage:" + msgType + " Data:" );
            // Debug.Log("DispatchMessage:" + msgType + " Data:" + SimpleJsonUtils.ToJson(msgData));
            MessageHandlerDelegate handlerDelegate;
            bool canInvoke = true;

            if (messageHandlers.TryGetValue(msgType, out handlerDelegate))
            {
                if (isServer)
                {
                    if (!SimpleNetManager.PlayerManager.IsLogin(session))
                    {
                        canInvoke = false;
                        Debug.LogError("No Login cant use msg:" + msgType);
                    }
                }
            }
            else if (noLoginMessageHandlers.TryGetValue(msgType, out handlerDelegate))
            {
            }
            else
            {
                canInvoke = false;
            }

            if (canInvoke && handlerDelegate != null)
            {
                NetMessageData messageHandler = new NetMessageData(msgType, session, msgData);
                handlerDelegate.Invoke(messageHandler);
            }
        }
        private void OnLogoutMsg(NetMessageData messageHandler)
        {
            SimpleNetManager.Player player = SimpleNetManager.PlayerManager.GetPlayer(messageHandler.session);
            Debug.Log("服务端接收登出:" + player);

            LogoutAction(player);
        }
        private void OnRemoteInvokingEvent(NetMessageData msgHandler)
        {
            UseMethod2Server msg = msgHandler.GetMessage <UseMethod2Server>();
            //Debug.Log("Server接收到UseMethod2Server:" + JsonUtils.ToJson(msg));
            int    code  = 0;
            string error = "";

            try
            {
                MethodInfo mInfo = null;
                foreach (var mData in invokeMothodsInfos)
                {
                    if (mData.Key.FullName == msg.classFullName)
                    {
                        List <MethodInfo> methods = mData.Value;

                        foreach (var m in methods)
                        {
                            if (m.Name == msg.methodName)
                            {
                                mInfo = m;
                                break;
                            }
                        }
                    }
                }

                if (mInfo != null)
                {
                    List <object>   pValues    = new List <object>();
                    ParameterInfo[] parameters = mInfo.GetParameters();
                    for (int i = 0; i < parameters.Length; i++)
                    {
                        ParameterInfo p = parameters[i];
                        object        v = SimpleJsonUtils.FromJson(p.ParameterType, msg.paramNameValues[p.Name]);
                        pValues.Add(v);
                    }
                    mInfo.Invoke(null, pValues.ToArray());
                }
                else
                {
                    code = -2;
                }
            }
            catch (Exception e)
            {
                code  = -1;
                error = e.ToString();
                Debug.LogError(e);
            }
            UseMethod2Client toMsg = new UseMethod2Client();

            toMsg.code  = code;
            toMsg.error = error;
            netManager.Send(msgHandler.session, toMsg);
            //Debug.Log("发送UseMethod2Client:" + JsonUtils.ToJson(toMsg));
        }
        private void OnMsgFunctionSwitch(NetMessageData msgHandler)
        {
            FunctionSwitch2Server msg = msgHandler.GetMessage <FunctionSwitch2Server>();

            if (msg.functionName == FunctionName)
            {
                IsOpenFunction = msg.isOpenFunction;
                //Debug.Log("server 接受isOpenFunction:" + IsOpenFunction);
                SendSwitchState2Client(msgHandler.session);
            }
        }
 private void OnLogoutEvent(NetMessageData messageHandler)
 {
     Debug.Log("客户端接收登出");
     if (IsLogin)
     {
         Logout2Client msg = messageHandler.GetMessage <Logout2Client>();
         if (OnLogout != null)
         {
             OnLogout(msg);
         }
         RemovePlayer();
     }
 }
        /// <summary>
        /// Dequeue the message collection and sends the messages to their recipients.
        /// </summary>
        private void ProcessSendingQueue()
        {
            while (!_cancellationToken.IsCancellationRequested)
            {
                try
                {
                    NetMessageData message = _sendingCollection.Take(_cancellationToken);

                    if (message.Connection != null && message.Data != null)
                    {
                        SendMessage(message.Connection, message.Data);
                    }
                }
                catch (OperationCanceledException)
                {
                    // The operation has been cancelled: nothing to do
                }
            }
        }
        private void OnLoginEvent(NetMessageData messageHandler)
        {
            Login2Client msg = messageHandler.GetMessage <Login2Client>();

            if (msg.code == 0)
            {
                isLogin = true;
                if (string.IsNullOrEmpty(msg.playerID))
                {
                    msg.playerID = "001";
                }
                Player player = new Player(messageHandler.session);
                player.playerID = msg.playerID;
                player.AddData(msg.appData);

                SimpleNetManager.PlayerManager.AddPlayer(player);
            }
            if (Onlogin != null)
            {
                Onlogin(msg);
            }
            Debug.Log("登录返回:" + msg.code);
        }
 /// <inheritdoc />
 public void Send(NetMessageData message) => _sendingCollection.Add(message);
Exemple #9
0
 private void OnClearLogEvent(NetMessageData msgHandler)
 {
     logDatas.Clear();
 }
        private void OnLoginMsg(NetMessageData messageHandler)
        {
            Debug.Log("接受到登陆消息!");
            Login2Server msg = messageHandler.GetMessage <Login2Server>();

            bool isRightDecryptPW = true;

            //密码解码
            if (!string.IsNullOrEmpty(msg.password))
            {
                try
                {
                    string temp = msg.password;
                    //获得密码md5串长度
                    int length = int.Parse(temp.Substring(0, 4));
                    //Debug.Log("length:" + temp.Substring(0, 4));

                    string md5Ery = temp.Substring(4, length);
                    //Debug.Log("md5Ery:" + md5Ery);
                    string aesKey = temp.Substring(4 + length);
                    //Debug.Log("aesKey:" + aesKey);
                    string pwMD5 = AESUtils.AESDecrypt(md5Ery, aesKey);
                    //Debug.Log("pwMD5:" + pwMD5);
                    msg.password = pwMD5;
                }
                catch (Exception e)
                {
                    Debug.LogError("password Decrypt error:" + msg.password + "\n" + e);
                    isRightDecryptPW = false;
                }
            }

            Login2Client resMsg = new Login2Client();

            resMsg.appData = new AppData();
            resMsg.appData.serverAppName    = Application.productName;
            resMsg.appData.serverAppVersion = Application.version;
            resMsg.appData.bundleIdentifier = Application.identifier;

            Player player = null;

            if (isRightDecryptPW)
            {
                if (SimpleNetManager.PlayerManager.IsLogin(messageHandler.session))
                {
                    //重复登陆
                    resMsg.code = 100;
                }

                else if (playerLoginHandler != null)
                {
                    resMsg.code = playerLoginHandler.LoginLogic(msg, messageHandler.session, out player);
                    // player = new Player(messageHandler.connectionId);
                    //player.playerID = "1000";
                    if (resMsg.code == 0)
                    {
                        if (SimpleNetManager.PlayerManager.IsLogin(player.playerID))
                        {
                            //当前账号已登录
                            resMsg.code = 103;
                        }
                        else
                        {
                            resMsg.playerID = player.playerID;
                        }
                    }
                }
                else
                {
                    //其他错误
                    resMsg.code = 101;
                }
            }
            else
            {
                //密码解析错误
                resMsg.code = 104;
            }

            netManager.Send(messageHandler.session, resMsg);
            SimpleNetManager.PlayerManager.AddPlayer(player);
            if (resMsg.code == 0)
            {
                if (OnPlayerLogin != null)
                {
                    OnPlayerLogin(player);
                }

                if (OnPlayerLoginAfter != null)
                {
                    OnPlayerLoginAfter(player);
                }
            }
        }