Пример #1
0
        // Nothing but actually sending here. Worry about timeouts and retries somewhere
        // else
        public static async Task Send(Stream stream, OutgoingMessageBatch batch, byte[] messageBytes,
                                      ISenderCallback callback)
        {
            messageBytes = messageBytes ?? Envelope.Serialize(batch.Messages);

            var lengthBytes = BitConverter.GetBytes(messageBytes.Length);


            await stream.WriteAsync(lengthBytes, 0, lengthBytes.Length);

            await stream.WriteAsync(messageBytes, 0, messageBytes.Length);

            // All four of the possible receive confirmation messages are the same length: 8 characters long encoded in UTF-16.
            var confirmationBytes = await stream.ReadBytesAsync(ReceivedBuffer.Length).ConfigureAwait(false);

            if (confirmationBytes.SequenceEqual(ReceivedBuffer))
            {
                await callback.Successful(batch);

                await stream.WriteAsync(AcknowledgedBuffer, 0, AcknowledgedBuffer.Length);
            }
            else if (confirmationBytes.SequenceEqual(ProcessingFailureBuffer))
            {
                await callback.ProcessingFailure(batch);
            }
            else if (confirmationBytes.SequenceEqual(SerializationFailureBuffer))
            {
                await callback.SerializationFailure(batch);
            }
            else if (confirmationBytes.SequenceEqual(QueueDoesNotExistBuffer))
            {
                await callback.QueueDoesNotExist(batch);
            }
        }
Пример #2
0
        // Nothing but actually sending here. Worry about timeouts and retries somewhere
        // else
        public static async Task Send(Stream stream, OutgoingMessageBatch batch, ISenderCallback callback)
        {
            var messageBytes = Envelope.Serialize(batch.Messages);

            var lengthBytes = BitConverter.GetBytes(messageBytes.Length);


            await stream.WriteAsync(lengthBytes, 0, lengthBytes.Length);

            await stream.WriteAsync(messageBytes, 0, messageBytes.Length);

            var bytes = await stream.ReadBytesAsync(Constants.ReceivedBuffer.Length).ConfigureAwait(false);

            if (bytes.SequenceEqual(Constants.ReceivedBuffer))
            {
                callback.Successful(batch);

                await stream.WriteAsync(Constants.AcknowledgedBuffer, 0, Constants.AcknowledgedBuffer.Length);
            }
            else if (bytes.SequenceEqual(Constants.ProcessingFailureBuffer))
            {
                callback.ProcessingFailure(batch);
            }
            else if (bytes.SequenceEqual(Constants.SerializationFailureBuffer))
            {
                callback.SerializationFailure(batch);
            }
            else if (bytes.SequenceEqual(Constants.QueueDoesNotExistBuffer))
            {
                callback.QueueDoesNotExist(batch);
            }
        }