Exemplo n.º 1
0
        public static MOPacket BuildPacket(this IMessage content)
        {
            var   actionId = Int32.Parse(content.GetType().Name.Substring(3));
            MOMsg msg      = new MOMsg();

            msg.ActionId = actionId;
            msg.UserId   = GameUser.Instance.UserId;
            msg.Token    = GameUser.Instance.Token;
            msg.MsgId    = GameUser.Instance.MsgId;
            if (content != null)
            {
                msg.Content = content.ToByteString();
            }
            var data = msg.ToByteString();

            msg.Sign = CryptoHelper.MD5_Encrypt(string.Format("{0}{1}", data, GlobalGame.Md5Key)).ToLower();
            return(new MOPacket(msg));
        }
Exemplo n.º 2
0
        public async Task DispatchIncomingPacket(MOMsg packet)
        {
            try
            {
                //System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
                //watch.Restart();
                //md5签名验证
                var sign = packet.Sign;
                packet.Sign = string.Empty;
                var data = packet.ToByteString();
                if (CryptoHelper.MD5_Encrypt($"{data}{_md5Key}").ToLower() != sign.ToLower())
                {
                    await DispatchOutcomingPacket(packet.ParseResult(MOErrorType.Hidden, "签名验证失败"));
                    await Close();

                    return;
                }

                //同步初始化
                if (!_IsInit)
                {
                    _tokenGrain = _client.GetGrain <ITokenGrain>(packet.UserId);
                    var tokenInfo = _tokenGrain.GetToken().Result;
                    if (tokenInfo.Token != packet.Token || tokenInfo.LastTime.AddSeconds(GameConstants.TOKENEXPIRE) < DateTime.Now)
                    {
                        await DispatchOutcomingPacket(packet.ParseResult(MOErrorType.Hidden, "Token验证失败"));
                        await Close();

                        return;
                    }
                    _userId = packet.UserId;
                    //_token = tokenInfo.Token;
                    _packetObserver    = new OutcomingPacketObserver(this);
                    _router            = _client.GetGrain <IPacketRouterGrain>(_userId);
                    _user              = _client.GetGrain <IUserGrain>(_userId);
                    _packetObserverRef = _client.CreateObjectReference <IPacketObserver>(_packetObserver).Result;
                    _user.BindPacketObserver(_packetObserverRef).Wait();
                    _IsInit = true;
                }
                else
                {
                    //if (_userId != packet.UserId || _token != packet.Token)
                    //{
                    //    await DispatchOutcomingPacket(packet.ParseResult(MOErrorType.Hidden, "Token验证失败"));
                    //    await Close();
                    //    return;
                    //}
                    var tokenInfo = _tokenGrain.GetToken().Result;
                    if (tokenInfo.Token != packet.Token || tokenInfo.LastTime.AddSeconds(GameConstants.TOKENEXPIRE) < DateTime.Now)
                    {
                        await DispatchOutcomingPacket(packet.ParseResult(MOErrorType.Hidden, "Token验证失败"));
                        await Close();

                        return;
                    }
                }

                //心跳包
                if (packet.ActionId == 1)
                {
                    await _tokenGrain.RefreshTokenTime();
                    await DispatchOutcomingPacket(packet.ParseResult());

                    return;
                }

                await _router.SendPacket(packet);

                //watch.Stop();
                //Console.WriteLine($"{packet.UserId} {watch.ElapsedMilliseconds}ms");
            }
            catch (Exception ex)
            {
                _logger.LogError(
                    $"DispatchIncomingPacket异常:\n" +
                    $"{ex.Message}\n" +
                    $"{ex.StackTrace}\n" +
                    $"{JsonConvert.SerializeObject(packet)}");
            }
        }