Exemplo n.º 1
0
        public override void Read(ChannelHandlerContext ctx, object msg)
        {
            // .NET-specific: use a content wrapper for TCP, similar to TomP2PSinglePacketUdp
            var piece = msg as StreamPiece;

            if (piece == null)
            {
                //ctx.FireRead(msg);
                return;
            }

            var buf       = piece.Content;
            var sender    = piece.Sender;
            var recipient = piece.Recipient;

            Logger.Debug("{0}: Cumulating TCP stream piece.", this);
            try
            {
                if (_cumulation == null)
                {
                    _cumulation = AlternativeCompositeByteBuf.CompBuffer(buf);
                }
                else
                {
                    // add to overhead from last TCP packet
                    _cumulation.AddComponent(buf);
                }
                Decoding(ctx, sender, recipient);
            }
            catch (Exception)
            {
                Logger.Error("Error in TCP decoding.");
                throw;
            }
            finally
            {
                // If the currently read buffer is read, it can be deallocated.
                // In case another TCP packet follows, a new buffer is created.
                if (_cumulation != null && !_cumulation.IsReadable)
                {
                    _cumulation = null;
                    // Java: no need to discard bytes as this was done in the decoder already
                }
            }
        }