Beispiel #1
0
        public override void HandleResponse(Message.Message requestMessage, PeerConnection peerConnection, bool sign, IResponder responder)
        {
            if (!(requestMessage.Type == Message.Message.MessageType.Request1 ||
                  requestMessage.Type == Message.Message.MessageType.Request2) &&
                requestMessage.Command == Rpc.Commands.DirectData.GetNr())
            {
                throw new ArgumentException("Message content is wrong for this handler.");
            }
            var responseMessage = CreateResponseMessage(requestMessage, Message.Message.MessageType.Ok);

            if (sign)
            {
                responseMessage.SetPublicKeyAndSign(PeerBean.KeyPair);
            }
            var rawDataReply2    = _rawDataReply;
            var objectDataReply2 = _objectDataReply;

            if (requestMessage.Type == Message.Message.MessageType.Request1 && rawDataReply2 == null)
            {
                responseMessage.SetType(Message.Message.MessageType.NotFound);
            }
            else if (requestMessage.Type == Message.Message.MessageType.Request2 && objectDataReply2 == null)
            {
                responseMessage.SetType(Message.Message.MessageType.NotFound);
            }
            else
            {
                var requestBuffer = requestMessage.Buffer(0);
                // The user can reply with null, indicating not found or returning
                // the request buffer, which means nothing is returned.
                // Or an exception can be thrown.
                if (requestMessage.Type == Message.Message.MessageType.Request1)
                {
                    Logger.Debug("Handling Request1.");
                    var responseBuffer = rawDataReply2.Reply(requestMessage.Sender, requestBuffer, requestMessage.IsDone);
                    if (responseBuffer == null && requestMessage.IsDone)
                    {
                        Logger.Warn("Raw reply is null, returning not found.");
                        responseMessage.SetType(Message.Message.MessageType.NotFound);
                    }
// ReSharper disable once PossibleUnintendedReferenceComparison
                    else if (responseBuffer != requestBuffer) // reference equality ok
                    {
                        // can be partial as well
                        if (!responseBuffer.IsComplete)
                        {
                            responseMessage.SetStreaming();
                        }
                        responseMessage.SetBuffer(responseBuffer);
                    }
                }
                else
                {
                    // no streaming here when we deal with objects
                    object obj = Utils.Utils.DecodeObject(requestBuffer.BackingBuffer);
                    Logger.Debug("Handling {0}.", obj);

                    object reply = objectDataReply2.Reply(requestMessage.Sender, obj);
                    if (reply == null)
                    {
                        responseMessage.SetType(Message.Message.MessageType.NotFound);
                    }
                    else if (reply == obj)
                    {
                        responseMessage.SetType(Message.Message.MessageType.Ok);
                    }
                    else
                    {
                        sbyte[] me = Utils.Utils.EncodeObject(reply);
                        responseMessage.SetBuffer(new Message.Buffer(Unpooled.WrappedBuffer(me)));
                    }
                }
            }

            responder.Response(responseMessage);
        }