Example #1
0
 public ClientSession(TcpClient tcpClient, IGrainFactory grainFactory)
 {
     _sessionId                 = Guid.NewGuid();
     _tcpClient                 = tcpClient;
     _grainFactory              = grainFactory;
     _outcomingPacketObserver   = new OutcomingPacketObserver(this);
     _outcomingPacketDispatcher = new ActionBlock <UncompressedPacket>(SendOutcomingPacket);
 }
Example #2
0
 public ClientSession(TcpClient tcpClient, IGrainFactory grainFactory, IBufferPool <byte> bufferPool, ObjectPool <UncompressedPacket> uncompressedPacketObjectPool)
 {
     _sessionId    = Guid.NewGuid();
     _tcpClient    = tcpClient;
     _grainFactory = grainFactory;
     _bufferPool   = bufferPool;
     _uncompressedPacketObjectPool = uncompressedPacketObjectPool;
     _outcomingPacketObserver      = new OutcomingPacketObserver(this);
     _outcomingPacketDispatcher    = new ActionBlock <object>(SendOutcomingPacket);
 }
Example #3
0
 public ClientSession(SessionScope sessionScope, IPacketRouter packetRouter, IBufferPool <byte> bufferPool, ObjectPool <UncompressedPacket> uncompressedPacketObjectPool)
 {
     SessionScope = sessionScope;
     _tcpClient   = new TcpClient();
     _bufferPool  = bufferPool;
     _uncompressedPacketObjectPool = uncompressedPacketObjectPool;
     _packetRouter              = packetRouter;
     _outcomingPacketObserver   = new OutcomingPacketObserver(this);
     _outcomingPacketDispatcher = new ActionBlock <UncompressedPacket>(SendOutcomingPacket);
 }
Example #4
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)}");
            }
        }