コード例 #1
0
        /// <exception cref="EndOfStreamException">Unexpected end of stream.</exception>
        /// <exception cref="TarantoolProtocolViolationException">Unexpected read bytes count.</exception>
        /// <exception cref="UnpackException">
        ///     source is not valid MessagePack stream.
        /// </exception>
        /// <exception cref="MessageTypeException">
        ///     The unpacked result is not compatible to <see cref="T:System.UInt32" />.
        /// </exception>
        public static async Task <ServerMessage> ReadServerMessageAsync(this Stream stream,
                                                                        CancellationToken cancellationToken)
        {
            var packedMessageLength = await stream.ReadExactlyBytesAsync(5, cancellationToken);

            var unpackedMessageLength = Unpacking.UnpackUInt32(packedMessageLength);

            if (unpackedMessageLength.ReadCount != 5)
            {
                throw new TarantoolProtocolViolationException("Unexpected read bytes count.");
            }
            var messageLength = unpackedMessageLength.Value;
            var messageBytes  = await stream.ReadExactlyBytesAsync((int)messageLength, cancellationToken);

            var serverMessage = new ServerMessage(messageBytes);

            return(serverMessage);
        }