Example #1
0
        public SenderConfiguration Send <T>(Action <T> msgInitializer, string destinationQueue) where T : class
        {
            var sequence = new MessageSequence();

            _componentBeingConfigured.AddSequence(sequence);

            return(ConfigurationStepCreator.CreateSendWithNoBind(_componentBeingConfigured, sequence, msgInitializer, destinationQueue));
        }
        public void Encode(IKMessageContext <TNodeId> context, IBufferWriter <byte> buffer, KMessageSequence <TNodeId> sequence)
        {
            var p = new MessageSequence();

            p.Network  = sequence.Network;
            p.Messages = Encode(context, sequence).ToArray();
            global::MessagePack.MessagePackSerializer.Serialize(buffer, p);
        }
        public void Encode(IKMessageContext <TNodeId> context, IBufferWriter <byte> buffer, KMessageSequence <TNodeId> sequence)
        {
            var p = new MessageSequence();

            p.Network = sequence.Network;
            p.Messages.AddRange(Encode(context, sequence));
            p.WriteTo(buffer);
        }
        public MessageSequence <TMessage> Complete <TMessage>()
        {
            _logger.LogDebug($"Message Sequence for '{_globalMessageId}' completes with '{typeof(TMessage).Name}'.");
            var sequenceDef = _repository.GetOrCreate(_globalMessageId);

            var messageTcs = new TaskCompletionSource <TMessage>();
            var sequence   = new MessageSequence <TMessage>
            {
                Task = messageTcs.Task
            };

            sequenceDef.TaskCompletionSource.Task.ContinueWith(tObj =>
            {
                UpdateSequenceFinalState(sequence);
                messageTcs.TrySetResult((TMessage)tObj.Result);
            });

            Func <TMessage, TMessageContext, Task> func = (message, context) =>
            {
                Task
                .WhenAll(sequenceDef.State.HandlerTasks)
                .ContinueWith(t => sequenceDef.TaskCompletionSource.TrySetResult(message));
                return(Task.FromResult(true));
            };

            var bindTask = _chainTopology.BindToExchange <TMessage>(_globalMessageId);

            _dispatcher.AddMessageHandler(_globalMessageId, func);

            Task
            .WhenAll(bindTask)
            .ContinueWith(t => _publishAsync())
            .Unwrap()
            .Wait();

            Timer timeoutTimer = null;

            timeoutTimer = new Timer(state =>
            {
                timeoutTimer?.Dispose();
                var seq = _repository.Get(_globalMessageId);
                if (seq != null)
                {
                    seq.State.Aborted = true;
                    _repository.Update(seq);
                    UpdateSequenceFinalState(sequence);
                }

                messageTcs.TrySetException(
                    new TimeoutException(
                        $"Unable to complete sequence {_globalMessageId} in {_mainCfg.RequestTimeout.ToString("g")}. Operation Timed out."));
            }, null, _mainCfg.RequestTimeout, new TimeSpan(-1));

            return(sequence);
        }
        private void UpdateSequenceFinalState <TMessage>(MessageSequence <TMessage> sequence)
        {
            var final = _repository.Get(_globalMessageId);

            if (final == null)
            {
                return;
            }
            _logger.LogDebug($"Updating Sequence for '{_globalMessageId}'.");
            sequence.Aborted   = final.State.Aborted;
            sequence.Completed = final.State.Completed;
            sequence.Skipped   = final.State.Skipped;
            foreach (var step in final.StepDefinitions)
            {
                _chainTopology.UnbindFromExchange(step.Type, _globalMessageId);
            }
            _repository.Remove(_globalMessageId);
        }
Example #6
0
        private void ConstructAndSendMessages(ReflectInsightPackage[] messages)
        {
            if (DateTime.Now.Subtract(LastUnsuccessfulConnection).TotalSeconds < LastUnsuccessfulWaitTime)
            {
                // The last successful connection was less than the specified time, just return.
                // We need to try again later
                return;
            }

            // serialize the packages to a binary blob
            Byte[] bData = null;
            using (var pool = FastFormatterPool.Pool.Container())
            {
                bData = FastSerializerEnumerable <ReflectInsightPackage> .Serialize(pool.Instance, messages);
            }

            ListenerRequest.DecompressedLength = 0;
            if (bData.Length > MessageRequestConstants.MAX_COMPRESSION)
            {
                ListenerRequest.DecompressedLength = bData.Length;

                // compress data
                using (var ms = new MemoryStream())
                {
                    using (GZipStream msCompressed = new GZipStream(ms, CompressionMode.Compress))
                    {
                        msCompressed.Write(bData, 0, bData.Length);
                    }

                    bData = ms.ToArray();
                }
            }

            ListenerRequest.RequestId     = CryptoServices.RandomIdToUInt64();
            ListenerRequest.SequenceCount = (Int16)((bData.Length / MessageRequestConstants.MAX_CHUNKSIZE) + ((bData.Length % MessageRequestConstants.MAX_CHUNKSIZE) > 0 ? 1 : 0));

            try
            {
                if (!MessageWriter.IsOpen())
                {
                    MessageWriter.Open();
                }

                try
                {
                    WriteRequest(MessageRequestType.Request, ListenerRequest);

                    // now send data in chunks if larger than 3 MB
                    Int16 nextSequence = 1;
                    Int32 atSource     = 0;
                    Int32 remaining    = bData.Length;
                    Int32 chunkSize    = remaining < MessageRequestConstants.MAX_CHUNKSIZE ? remaining : MessageRequestConstants.MAX_CHUNKSIZE;

                    while (remaining > 0)
                    {
                        Byte[] bChunk = new Byte[chunkSize];
                        Array.Copy(bData, atSource, bChunk, 0, bChunk.Length);

                        atSource  += chunkSize;
                        remaining -= chunkSize;
                        chunkSize  = remaining < MessageRequestConstants.MAX_CHUNKSIZE ? remaining : MessageRequestConstants.MAX_CHUNKSIZE;

                        MessageSequence sequence = new MessageSequence();
                        sequence.SessionId = ListenerRequest.SessionId;
                        sequence.RequestId = ListenerRequest.RequestId;
                        sequence.Sequence  = nextSequence++;
                        sequence.Chunk     = bChunk;

                        WriteRequest(MessageRequestType.Sequence, sequence);
                    }
                }
                finally
                {
                    if (!MessageWriter.IsThreadSafe())
                    {
                        MessageWriter.Close();
                    }
                }
            }
            catch (Exception)
            {
                LastUnsuccessfulConnection = DateTime.Now;
                throw;
            }
        }