Пример #1
0
        public override void ExceptionCaught(IChannelHandlerContext ctx, Exception cause)
        {
            NettyChannel channel = NettyChannel.GetOrAddChannel(ctx.Channel, _url, _handler);

            try
            {
                _handler.CaughtAsync(channel, cause);
            }
            finally
            {
                NettyChannel.RemoveChannelIfDisconnected(ctx.Channel);
            }
        }
Пример #2
0
        public override async Task DisconnectAsync(IChannelHandlerContext ctx)
        {
            NettyChannel channel = NettyChannel.GetOrAddChannel(ctx.Channel, _url, _handler);

            try
            {
                await _handler.DisconnectAsync(channel);
            }
            finally
            {
                NettyChannel.RemoveChannelIfDisconnected(ctx.Channel);
            }
        }
Пример #3
0
        public static NettyChannel GetOrAddChannel(DotNetty.Transport.Channels.IChannel ch, URL url, IChannelHandler handler)
        {
            if (ch == null)
            {
                return(null);
            }
            if (!ChannelMap.ContainsKey(ch))
            {
                var nettyChannel = new NettyChannel(ch, url, handler);
                ChannelMap.TryAdd(ch, nettyChannel);
            }

            return(ChannelMap[ch]);
        }
Пример #4
0
        public override async Task WriteAsync(IChannelHandlerContext ctx, object msg)
        {
            await base.WriteAsync(ctx, msg);

            NettyChannel channel = NettyChannel.GetOrAddChannel(ctx.Channel, _url, _handler);

            try
            {
                await _handler.SentAsync(channel, msg);
            }
            finally
            {
                NettyChannel.RemoveChannelIfDisconnected(ctx.Channel);
            }
        }
Пример #5
0
        public override void ChannelRead(IChannelHandlerContext ctx, object msg)
        {
            NettyChannel channel = NettyChannel.GetOrAddChannel(ctx.Channel, _url, _handler);

            try
            {
                //Console.WriteLine("channel read:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
                _handler.RecivedAsync(channel, msg).Wait();
                // Console.WriteLine("channel read completed:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
            }
            finally
            {
                NettyChannel.RemoveChannelIfDisconnected(ctx.Channel);
            }
        }
Пример #6
0
        protected override void Encode(IChannelHandlerContext context, object message, IByteBuffer output)
        {
            DotNetty.Transport.Channels.IChannel ch = context.Channel;
            NettyChannel channel = NettyChannel.GetOrAddChannel(ch, _url, _handler);

            try
            {
                //Console.WriteLine("encode:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
                _codec.Encode(channel, output, message);
                //Console.WriteLine("encoded:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
            }
            finally
            {
                NettyChannel.RemoveChannelIfDisconnected(ch);
            }
        }
Пример #7
0
        protected override void Decode(IChannelHandlerContext context, IByteBuffer input, List <object> output)
        {
            NettyChannel channel = NettyChannel.GetOrAddChannel(context.Channel, _url, _handler);

            Object msg;

            int saveReaderIndex;

            try
            {
                // decode object.
                do
                {
                    saveReaderIndex = input.ReaderIndex;
                    try
                    {
                        //Console.WriteLine("decode:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
                        msg = _codec.Decode(channel, input);
                        //Console.WriteLine("decoded:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
                    }
                    catch (IOException e)
                    {
                        throw e;
                    }
                    if (msg is DecodeResult result && result == DecodeResult.NeedMoreInput)
                    {
                        input.SetReaderIndex(saveReaderIndex);
                        break;
                    }
                    else
                    {
                        //is it possible to go here ?
                        if (saveReaderIndex == input.ReaderIndex)
                        {
                            throw new IOException("Decode without read data.");
                        }
                        if (msg != null)
                        {
                            output.Add(msg);
                        }
                    }
                } while (input.IsReadable());
            }