示例#1
0
 public HermesProducer(
     IChannelWriter channelWriter,
     IMessageAdapter messageAdapter)
 {
     _channelWriter  = channelWriter;
     _messageAdapter = messageAdapter;
 }
示例#2
0
        public virtual void SerializeObjects()
        {
            string         name = "test-SerializeObjects" + Salt;
            Queue <string> rq   = new Queue <string>(new string[] { "aaa", "aab", "abb", "bbb", "bbc", "bcc", "ccc" });
            Queue <string> wq   = new Queue <string>(new string[] { "aaa", "aab", "abb", "bbb", "bbc", "bcc", "ccc" });

            AbstractChannelsFactory fc = new RabbitMQChannelsFactory();

            Task tr = Task.Factory.StartNew(() => {
                using (IChannelReader <SerializeObjectsData> ch = fc.GetSubscribableChannel <SerializeObjectsData>(name).Subscribe())
                {
                    foreach (SerializeObjectsData item in ch.Enumerate())
                    {
                        Assert.AreEqual(item.OpId, rq.Dequeue());
                    }
                    Assert.IsTrue(ch.Drained);
                }
            });

            Task tw = Task.Factory.StartNew(() => {
                using (IChannelWriter <SerializeObjectsData> ch = fc.GetSubscribableChannel <SerializeObjectsData>(name))
                {
                    while (wq.Count > 0)
                    {
                        ch.Write(new SerializeObjectsData()
                        {
                            OpId = wq.Dequeue(), Status = "test", Message = "/messaggio/lungo/con/sbarre"
                        });
                    }
                    ch.Close();
                }
            });

            Task.WaitAll(tr, tw);
        }
示例#3
0
        public static IChannelWriter <T> WriteSelectWith <T>(this IChannelWriter <T> ext, int timeoutMillis, params IChannelWriter <T>[] channels)
        {
            List <IChannelWriter <T> > tmp = new List <IChannelWriter <T> >(channels);

            tmp.Add(ext);
            return(ChannelWriterSync.Select <T>(timeoutMillis, tmp.ToArray()));
        }
示例#4
0
        public static IEnumerable <IChannelWriter <T> > WriteBarrierWith <T>(this IChannelWriter <T> ext, int timeoutMillis, params IChannelWriter <T>[] channels)
        {
            List <IChannelWriter <T> > tmp = new List <IChannelWriter <T> >(channels);

            tmp.Add(ext);
            return(ChannelWriterSync.Barrier <T>(timeoutMillis, tmp.ToArray()));
        }
        protected override void RemoteCall(IJobExecutionContext context)
        {
            using (ILoggingOperation log = _logger.NormalOperation())
            {
                log.Wrap(() =>
                {
                    RabbitMQChannelsFactory factory = new RabbitMQChannelsFactory();

                    IChannelWriter <object> ch = null;
                    if (Broadcast)
                    {
                        ch = factory.GetSubscribableChannel <object>(ChannelName);
                    }
                    else
                    {
                        ch = factory.GetChannel <object>(ChannelName);
                    }

                    using (ch)
                    {
                        ch.Write(Message);
                    }
                });
            }
        }
示例#6
0
 internal PersistentChannelWriter(IChannelWriter <T> writer, bool singleWriter, long initialSize)
 {
     writeLock = singleWriter ? default : AsyncLock.Exclusive();
                 this.writer = writer;
                 fileOptions = new FileCreationOptions(FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read, FileOptions.Asynchronous | FileOptions.WriteThrough, initialSize);
                 cursor      = new ChannelCursor(writer.Location, StateFileName);
 }
示例#7
0
        /// <summary>
        /// Tries to propagate completion from the reader to the writer.
        /// </summary>
        /// <typeparam name="T">The type of the items in the reader and writer.</typeparam>
        /// <param name="reader">The reader.</param>
        /// <param name="writer">The writer.</param>
        /// <param name="passthroughError"><see langword="true"/> - passthrough error, <see langword="false"/> - mute error.</param>
        /// <returns>true, if completion was propagated, false otherwise.</returns>
        public static bool TryPropagateCompletion <T>(
            this IChannelReader <T> reader,
            IChannelWriter <T> writer,
            bool passthroughError = true)
        {
            VxArgs.NotNull(reader, nameof(reader));
            VxArgs.NotNull(writer, nameof(writer));

            if (reader.Completion.IsCompleted)
            {
                Exception error = null;
                try
                {
                    reader.Completion.GetAwaiter().GetResult();
                }
                catch (Exception ex)
                {
                    if (passthroughError)
                    {
                        error = ex;
                    }
                }

                return(writer.TryComplete(error));
            }

            return(false);
        }
示例#8
0
        public static AbstractShovelThread <S, D> ShovelFrom <S, D>(this IChannelWriter <D> ext, IChannelReader <S> source, Func <S, D> transform, TimeSpan lifeTime, bool closeOnDrain)
        {
            AbstractShovelThread <S, D> th = new FuncShovelThread <S, D>(transform, source, ext, lifeTime, closeOnDrain);

            th.Start();
            return(th);
        }
 internal static void RemoveNamedWriter <T>(string name, IChannelWriter <T> writer)
 {
     if (_namedChannels.ContainsKey(name))
     {
         RemoveWriter <T>(writer);
         _namedChannels[name].Item2.Remove(writer);
         if (_namedChannels[name].Item1.Count == 0 && _namedChannels[name].Item2.Count == 0)
         {
             _namedChannels.Remove(name);
         }
     }
 }
示例#10
0
        public AbstractShovelThread(IChannelReader <S> source, IChannelWriter <D> destination, TimeSpan lifeTime, bool closeOnDrain)
            : base(source, lifeTime)
        {
            if (destination == null)
            {
                throw new ArgumentNullException("'destination' cannot be null.");
            }
            _destination  = destination;
            _closeOnDrain = closeOnDrain;

            _th = new Thread(Run);

            StoppedEvent += ChannelDrainedHandling;
        }
 internal static Queue <T> AddWriter <T>(IChannelWriter <T> writer, Queue <T> queue)
 {
     if (_writers.ContainsKey(writer))
     {
         if (_writers[writer] != queue)
         {
             throw new InvalidOperationException("Impossible to associate a different queue to the already created channel end.");
         }
         return((Queue <T>)_writers[writer]);
     }
     else
     {
         Queue <T> qu = queue == null ? new Queue <T>() : queue;
         _writers.Add(writer, qu);
         return(qu);
     }
 }
        internal static Queue <T> AddNamedWriter <T>(string name, IChannelWriter <T> writer, Queue <T> queue)
        {
            queue = AddWriter <T>(writer, queue);

            if (!_namedChannels.ContainsKey(name))
            {
                _namedChannels.Add(name, new Tuple <List <object>, List <object> >(null, null));
            }
            if (_namedChannels[name].Item2.Contains(writer))
            {
                throw new Exception("Cannot attach mutiple times the same channel");
            }
            else
            {
                _namedChannels[name].Item2.Add(writer);
            }

            return(queue);
        }
示例#13
0
 public FuncShovelThread(Func <S, D> transform, IChannelReader <S> source, IChannelWriter <D> destination, TimeSpan lifeTime, bool closeOnDrain)
     : base(source, destination, lifeTime, closeOnDrain)
 {
     _func = transform;
 }
示例#14
0
 public DuplexHermesChannel(string channelName, NetworkStream networkStream)
 {
     _inputChannel  = new InputHermesChannel($"input_{channelName}", networkStream);
     _outputChannel = new OutputHermesChannel($"output_{channelName}", networkStream);
     Name           = channelName;
 }
示例#15
0
 public InnerWriterHandler(ChannelWriterMultiplexer <T> owner, IChannelWriter <T> innerWriter)
 {
     InnerWriter = innerWriter;
     _owner      = owner;
 }
 public AbstractChannelInputAdapter(IChannelWriter <D> channel)
 {
     _channel = channel;
 }
示例#17
0
        /// <summary>
        /// Performs copy processing from the reader to the writer.
        /// </summary>
        /// <typeparam name="T">The type of the items to copy.</typeparam>
        /// <param name="reader">The source.</param>
        /// <param name="writer">The target.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>Async execution TPL task.</returns>
        public static async Task Copy <T>(
            this IChannelReader <T> reader,
            IChannelWriter <T> writer,
            CancellationToken cancellationToken = default,
            ChannelCompletionPassthroughProc producerCompletionPassthroughProc = null,
            ChannelCompletionPassthroughProc consumerCompletionPassthroughProc = null,
            Func <T, ValueTask> handleLostItemsProc = null)
        {
            VxArgs.NotNull(reader, nameof(reader));
            VxArgs.NotNull(writer, nameof(writer));

            CancellationToken readCancellationToken = cancellationToken;

            if (producerCompletionPassthroughProc == null)
            {
                producerCompletionPassthroughProc = (error, isFromChannel) =>
                {
                    writer.TryComplete(error);
                    return(new ValueTask());
                };
            }

            if (consumerCompletionPassthroughProc == null)
            {
                consumerCompletionPassthroughProc = (error, isFromChannel) =>
                {
                    reader.TryComplete(error);
                    return(new ValueTask());
                };
            }

            Exception handlersError = null;

            try
            {
                while (true)
                {
                    T item;
                    try
                    {
                        if (!reader.TryRead(out item))
                        {
                            if (!await reader.WaitToReadAsync(readCancellationToken))
                            {
                                Exception error = null;
                                try
                                {
                                    await reader.Completion;
                                }
                                catch (Exception ex)
                                {
                                    error = ex;
                                }
                                finally
                                {
                                    try
                                    {
                                        await producerCompletionPassthroughProc(error);
                                    }
                                    catch (Exception ex)
                                    {
                                        handlersError = ex;
                                    }
                                }

                                return;
                            }
                            else
                            {
                                continue;
                            }
                        }
                    }
                    catch (Exception error)
                    {
                        try
                        {
                            await producerCompletionPassthroughProc(error, false);
                        }
                        catch (Exception ex)
                        {
                            handlersError = ex;
                        }

                        return;
                    }

                    // Trying to write, until target closed, or cancellation raised.
                    while (true)
                    {
                        try
                        {
                            if (writer.TryWrite(item))
                            {
                                break;
                            }

                            if (!await writer.WaitToWriteAsync(readCancellationToken))
                            {
                                if (handleLostItemsProc != null)
                                {
                                    try
                                    {
                                        await handleLostItemsProc(item);
                                    }
                                    catch (Exception ex)
                                    {
                                        handlersError = ex;
                                    }
                                }

                                Exception error = null;
                                try
                                {
                                    await writer.Completion;
                                }
                                catch (Exception ex)
                                {
                                    error = ex;
                                }
                                finally
                                {
                                    try
                                    {
                                        await consumerCompletionPassthroughProc(error);
                                    }
                                    catch (Exception ex)
                                    {
                                        handlersError = ex;
                                    }
                                }

                                return;
                            }
                        }
                        catch (Exception error)
                        {
                            try
                            {
                                if (handleLostItemsProc != null)
                                {
                                    try
                                    {
                                        await handleLostItemsProc(item);
                                    }
                                    catch (Exception ex)
                                    {
                                        handlersError = ex;
                                    }
                                }
                            }
                            finally
                            {
                                try
                                {
                                    await consumerCompletionPassthroughProc(error, false);
                                }
                                catch (Exception ex)
                                {
                                    handlersError = ex;
                                }
                            }

                            return;
                        }
                    }
                }
            }
            finally
            {
                if (handlersError != null)
                {
                    throw handlersError;
                }
            }
        }
示例#18
0
 public static IChannelWriter <S> AdaptedInput <S, D>(this IChannelWriter <D> ext, Func <S, D> transform)
 {
     return(new FuncChannelInputAdapter <S, D>(ext, transform));
 }
示例#19
0
 public static IChannel <T> Compose <T>(this IChannelReader <T> ext, IChannelWriter <T> source)
 {
     return(new CompositeChannel <T>(source, ext));
 }
 internal static void RemoveWriter <T>(IChannelWriter <T> writer)
 {
     _writers.Remove(writer);
 }
示例#21
0
 public CompositeChannel(IChannelWriter <T> source, IChannelReader <T> destiantion)
 {
     _source      = source;
     _destiantion = destiantion;
 }
示例#22
0
 public static IChannel <T> Compose <T>(this IChannelWriter <T> ext, IChannelReader <T> destination)
 {
     return(new CompositeChannel <T>(ext, destination));
 }
 public FuncChannelInputAdapter(IChannelWriter <D> channel, Func <S, D> transform)
     : base(channel)
 {
     _func = transform;
 }
示例#24
0
 /// <summary>
 /// Initializes a new instance of the <see cref="WriterProxyWithDropOnErrors{T}"/> class.
 /// </summary>
 /// <param name="innerWriter">The inner writer.</param>
 /// <param name="dropItemAction">The drop action.</param>
 public WriterProxyWithDropOnErrors(IChannelWriter <T> innerWriter, Action <T> dropItemAction = null)
 {
     _innerWriter    = innerWriter;
     _dropItemAction = dropItemAction;
     Completion      = new ValueTask(_writeCompletionTcs.Task);
 }