Esempio n. 1
0
        protected IByteBuffer TryDecodeUnframedMessage(IChannelHandlerContext ctx,
                                                       IChannel channel,
                                                       IByteBuffer buffer,
                                                       TProtocolFactory inputProtocolFactory)
        {
            // Perform a trial decode, skipping through
            // the fields, to see whether we have an entire message available.

            int messageLength           = 0;
            int messageStartReaderIndex = buffer.ReaderIndex;

            try
            {
                using (TNiftyTransport decodeAttemptTransport = new TNiftyTransport(channel, buffer, ThriftTransportType.Unframed))
                {
                    int initialReadBytes = decodeAttemptTransport.GetReadByteCount();
                    using (TProtocol inputProtocol =
                               inputProtocolFactory.GetProtocol(decodeAttemptTransport))
                    {
                        // Skip through the message
                        inputProtocol.ReadMessageBegin();
                        TProtocolUtil.Skip(inputProtocol, TType.Struct);
                        inputProtocol.ReadMessageEnd();

                        messageLength = decodeAttemptTransport.GetReadByteCount() - initialReadBytes;
                    }
                }
            }
            catch (IndexOutOfRangeException)
            {
                // No complete message was decoded: ran out of bytes
                return(null);
            }
            catch (TTransportException)
            {
                // No complete message was decoded: ran out of bytes
                return(null);
            }
            finally
            {
                if (buffer.ReaderIndex - messageStartReaderIndex > _maxFrameSize)
                {
                    ctx.FireExceptionCaught(new TooLongFrameException("Maximum frame size of " + _maxFrameSize + " exceeded"));
                }

                buffer.SetReaderIndex(messageStartReaderIndex);
            }

            if (messageLength <= 0)
            {
                return(null);
            }

            // We have a full message in the read buffer, slice it off
            IByteBuffer messageBuffer =
                ExtractFrame(buffer, messageStartReaderIndex, messageLength);

            buffer.SetReaderIndex(messageStartReaderIndex + messageLength);
            return(messageBuffer);
        }
        public static TApplicationException WriteApplicationException(
            TProtocol outputProtocol,
            IRequestContext requestContext,
            String methodName,
            int sequenceId,
            TApplicationException applicationException,
            ILogger logger)
        {
            logger?.LogError(default(EventId), applicationException, applicationException.Message);
            TNiftyTransport requestTransport = (requestContext as NiftyRequestContext)?.NiftyTransport;

            // Application exceptions are sent to client, and the connection can be reused
            outputProtocol.WriteMessageBegin(new TMessage(methodName, TMessageType.Exception, sequenceId));
            applicationException.Write(outputProtocol);
            outputProtocol.WriteMessageEnd();
            if (requestTransport != null)
            {
                requestTransport.setTApplicationException(applicationException);
            }
            outputProtocol.Transport.Flush();

            return(applicationException);
        }