Exemplo n.º 1
0
        protected override async Task Run(CancellationToken cancellationToken)
        {
            MemoryStream stream = null;

            try
            {
                var definition = _messageService.GetDefinition <TMessage>();
                var buffer     = BitConverter.GetBytes(_messageService.CreateMessageHash(definition));
                stream = new MemoryStream();
                await stream.WriteAsync(buffer, 0, buffer.Length, cancellationToken);

                using (var bufferStream = new MemoryStream())
                {
                    await _serializer.WriteMessage(Message, bufferStream);
                    await Send(cancellationToken, stream, bufferStream.ToArray());
                }
                await _client.SendMulticastAsync(stream.ToArray());

                RaiseSuccess(Message);
            }
            finally
            {
                if (stream != null)
                {
                    stream.Dispose();
                }
            }
        }
Exemplo n.º 2
0
 public void AppendToStore(string name, ICollection <MessageAttribute> attribs, long streamVersion, ICollection <object> messages)
 {
     using (var mem = new MemoryStream())
     {
         _serializer.WriteAttributes(attribs, mem);
         _serializer.WriteCompactInt(messages.Count(), mem);
         foreach (var message in messages)
         {
             _serializer.WriteMessage(message, message.GetType(), mem);
         }
         _appendOnlyStore.Append(name, mem.ToArray(), streamVersion);
     }
 }
Exemplo n.º 3
0
        public byte[] SaveEnvelopeData(ImmutableEnvelope envelope)
        {
            using (var mem = new MemoryStream())
            {
                byte[] data;
                using (var block = new MemoryStream())
                {
                    MessageSerializer.WriteAttributes(envelope.Attributes, block);
                    MessageSerializer.WriteMessage(envelope.Message, envelope.Message.GetType(), block);
                    data = block.ToArray();
                }

                MessageSerializer.WriteCompactInt(Signature, mem);

                StorageFramesEvil.WriteFrame(envelope.EnvelopeId, DateTime.UtcNow.Ticks, data, mem);
                return(mem.ToArray());
            }
        }
Exemplo n.º 4
0
        protected override async Task Run(CancellationToken cancellationToken)
        {
            ITcpClient client = null;

            try
            {
                if (!_messageService.CanSend(_userId, typeof(TMessage)))
                {
                    throw new SecurityAccessDeniedException(string.Format("Access denied for type message {0}", typeof(TMessage)));
                }
                client = _clientFactory.CreateTcpClient();
                await client.ConnectAsync(_userId);

                var definition = _messageService.GetDefinition <TMessage>();
                var buffer     = BitConverter.GetBytes(_messageService.CreateMessageHash(definition));
                await client.WriteStream.WriteAsync(buffer, 0, buffer.Length, cancellationToken);

                await client.WriteStream.FlushAsync(cancellationToken);

                if (!await GetResponse(client.ReadStream, cancellationToken))
                {
                    return;
                }
                if (IsCancellationRequested)
                {
                    throw new OperationCanceledException();
                }
                using (var memoryBuffer = new MemoryStream())
                {
                    await _serializer.WriteMessage(_message, memoryBuffer);
                    await Send(cancellationToken, client.WriteStream, memoryBuffer.ToArray());
                }

                if (IsCancellationRequested)
                {
                    throw new OperationCanceledException();
                }

                await client.WriteStream.FlushAsync(cancellationToken);

                if (!await GetResponse(client.ReadStream, cancellationToken))
                {
                    return;
                }

                //TODO: Check variable streaming for correct implementation!
                var streaming = Message as IStreamingMessage;
                if (streaming != null)
                {
                    var   readedCount = 0;
                    ulong readed      = 0;
                    RaiseReport(new ProgressInfo <TMessage>(_message, streaming.StreamLength, readed));
                    buffer = new byte[2048];
                    do
                    {
                        readedCount = await streaming.Stream.ReadAsync(buffer, 0, buffer.Length);

                        await client.WriteStream.WriteAsync(buffer, 0, readedCount, cancellationToken);

                        await client.WriteStream.FlushAsync(cancellationToken);

                        if (IsCancellationRequested)
                        {
                            throw new OperationCanceledException();
                        }
                        readed += (ulong)readedCount;
                        RaiseReport(new ProgressInfo <TMessage>(_message, streaming.StreamLength, readed));
                    } while (readedCount > 0);
                }
                RaiseSuccess(Message);
                await client.DisconnectAsync();
            }
            finally
            {
                if (client != null)
                {
                    client.Dispose();
                }
            }
        }