Esempio n. 1
0
 public override void ChannelInactive(IChannelHandlerContext context)
 {
     serverSessionManager.RemoveById(context.Channel.Id.AsShortText());
     Parallel.ForEach(configuration.BrabchServer, x =>
     {
         var client = tcpClientManager.GetTcpClient(x.IpAdress);
         client.CloseAsync(context.Channel.Id.AsShortText());
     });
     base.ChannelInactive(context);
 }
        protected override void ChannelRead0(IChannelHandlerContext ctx, byte[] msg)
        {
            var channelId = ctx.Channel.Id.AsShortText();

            Parallel.ForEach(tcpClientManager.GetTcpClients().Where(x => configuration.BrabchServer.Where(item => item.NeedReply).Select(item => item.MatchId).Contains(x.Id)).Select(x => x.GetSession(channelId)?.MatchId).Where(x => !string.IsNullOrEmpty(x)), x =>
            {
                serverSessionManager.Send(x, msg).ContinueWith(task =>
                {
                    if (logger.IsEnabled(LogLevel.Error))
                    {
                        logger.LogError(task.Exception, "下发数据发生异常");
                    }
                    serverSessionManager.RemoveById(x);
                }, TaskContinuationOptions.NotOnRanToCompletion);
            });
            if (logger.IsEnabled(LogLevel.Trace))
            {
                logger.LogTrace($"分发链路收到服务器 {ctx.Channel.RemoteAddress} 的数据 {msg.ToHexString()}");
            }
        }