Ejemplo n.º 1
0
        public void Write(ChannelHandlerContext ctx, object msg)
        {
            try
            {
                AlternativeCompositeByteBuf buf;
                bool done;
                var message = msg as Message;
                if (message != null)
                {
                    buf = _preferDirect ? _alloc.CompDirectBuffer() : _alloc.CompBuffer();
                    // null means create signature
                    done = _encoder.Write(buf, message, null);
                }
                else
                {
                    ctx.FireWrite(msg); // TODO ok? Java uses ctx.write() (3x)
                    return;
                }

                message = _encoder.Message;

                // send buffer
                if (buf.IsReadable)
                {
                    // sender/recipient information is extracted in MyUdpClient.SendAsync()
                    ctx.FireWrite(buf);

                    if (done)
                    {
                        message.SetDone(true);
                        // we wrote the complete message, reset state
                        _encoder.Reset();
                    }
                }
                else
                {
                    ctx.FireWrite(Unpooled.EmptyBuffer);
                }
            }
            catch (Exception ex)
            {
                ctx.FireExceptionCaught(ex);
            }
        }
Ejemplo n.º 2
0
        public bool Decode(ChannelHandlerContext ctx, AlternativeCompositeByteBuf buffer, IPEndPoint recipient, IPEndPoint sender)
        {
            Logger.Debug("Decoding of TomP2P starts now. Readable: {0}.", buffer.ReadableBytes);

            try
            {
                long readerBefore = buffer.ReaderIndex;
                // set the sender of this message for handling timeout
                var attrInetAddr = ctx.Attr(InetAddressKey);
                attrInetAddr.Set(sender);

                if (Message == null)
                {
                    bool doneHeader = DecodeHeader(buffer, recipient, sender);
                    if (doneHeader)
                    {
                        // store the sender as an attribute
                        var attrPeerAddr = ctx.Attr(PeerAddressKey);
                        attrPeerAddr.Set(Message.Sender);

                        Message.SetIsUdp(ctx.Channel.IsUdp);
                        if (Message.IsFireAndForget() && Message.IsUdp)
                        {
                            TimeoutFactory.RemoveTimeout(ctx);
                        }
                    }
                    else
                    {
                        return false;
                    }
                }

                bool donePayload = DecodePayload(buffer);
                //DecodeSignature(buffer, readerBefore, donePayload);

                // TODO discardSomeReadBytes -> performance improvement
                return donePayload;
            }
            catch (Exception ex)
            {
                ctx.FireExceptionCaught(ex);
                Console.WriteLine(ex.ToString());
                return true;
            }
        }