示例#1
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);
            }
        }
示例#2
0
            public override void UserEventTriggered(ChannelHandlerContext ctx, object evt)
            {
                if (evt is IdleStateHandlerTomP2P)
                {
                    Logger.Warn("Channel timeout for channel {0} {1}.", _name, ctx.Channel);
                    PeerAddress recipient;
                    if (_tcsResponse != null)
                    {
                        // client-side
                        var requestMessage = (Message.Message)_tcsResponse.Task.AsyncState;

                        Logger.Warn("Request status is {0}.", requestMessage);
                        ctx.Channel.Closed +=
                            channel =>
                            _tcsResponse.SetException(
                                new TaskFailedException(String.Format("{0} is idle: {1}.", ctx.Channel, evt)));
                        ctx.FireTimeout();
                        ctx.Channel.Close();

                        recipient = requestMessage.Recipient;
                    }
                    else
                    {
                        // server-side

                        // .NET-specific:
                        // Don't close the channel, as this would close all service loops on a server.
                        // instead, set the session to be timed out.
                        ctx.FireTimeout();

                        // check if we have set an attribute at least
                        // (if we have already decoded the header)
                        var attrPeerAddr = ctx.Attr(Decoder.PeerAddressKey);
                        recipient = attrPeerAddr.Get();
                    }

                    if (_peerStatusListeners == null)
                    {
                        return;
                    }

                    var socketAddr = ctx.Channel.RemoteEndPoint;
                    if (socketAddr == null)
                    {
                        var attrInetAddr = ctx.Attr(Decoder.InetAddressKey);
                        socketAddr = attrInetAddr.Get();
                    }

                    lock (_peerStatusListeners)
                    {
                        foreach (var listener in _peerStatusListeners)
                        {
                            if (recipient != null)
                            {
                                listener.PeerFailed(recipient,
                                                    new PeerException(PeerException.AbortCauseEnum.Timeout, "Timeout!"));
                            }
                            else
                            {
                                if (socketAddr != null)
                                {
                                    listener.PeerFailed(new PeerAddress(Number160.Zero, socketAddr.Address),
                                                        new PeerException(PeerException.AbortCauseEnum.Timeout, "Timeout!"));
                                }
                                else
                                {
                                    Logger.Warn("Cannot determine the sender's address.");
                                }
                            }
                        }
                    }
                }
            }