コード例 #1
0
 public LoggingMessagePipeline(string actionName, ILog log, IMessagePipeline innerMessagePipeline, long messageNumber)
 {
     this.actionName           = actionName;
     this.log                  = log;
     this.innerMessagePipeline = innerMessagePipeline;
     this.messageNumber        = messageNumber;
 }
コード例 #2
0
        public UnsubscribeAction Connect <TMessage>(IMessagePipeline pipeline, IPipelineSink <TMessage> sink)
            where TMessage : class
        {
            MessageRouterConfigurator routerConfigurator = For(pipeline);

            return(routerConfigurator.FindOrCreate <TMessage>().Connect(sink));
        }
コード例 #3
0
        public static IMessagePipeline <TSource, TDestination> Action <TSource, TDestination>(
            this IMessagePipeline <TSource, TDestination> pipeline,
            ILogger log,
            params Func <TDestination, Task>[] handlers)
        {
            return(pipeline.Block(new TransformBlock <TDestination, TDestination>(async x =>
            {
                foreach (var handler in handlers)
                {
                    try
                    {
                        await handler(x);
                    }
                    catch (Exception e)
                    {
                        log?.LogError(e, "Message handler failure");
                    }
                }

                return x;
            },
                                                                                  new ExecutionDataflowBlockOptions
            {
                BoundedCapacity = 1,
                EnsureOrdered = true
            })));
        }
コード例 #4
0
 public void Dispose()
 {
     _pipeline        = null;
     _bus             = null;
     _service         = null;
     _endpointFactory = null;
 }
コード例 #5
0
        /*
         * public static IMessagePipeline<TSource, IEnumerable<TDestination>> Batch<TSource, TDestination>(
         *  this IMessagePipeline<TSource, TDestination> pipeline,
         *  int size,
         *  int time = 0)
         * {
         *  if (size <= 1)
         *      throw new ArgumentException("Buffer size must be greater that 1");
         *
         *  var batch = new BatchBlock<TDestination>(size, new GroupingDataflowBlockOptions
         *  {
         *      EnsureOrdered = true,
         *      BoundedCapacity = size
         *  });
         *
         *  var timer = new Timer(_ => batch.TriggerBatch(), null, time, Timeout.Infinite);
         *
         *  var transform = new TransformBlock<TDestination[], TDestination[]>(x =>
         *      {
         *          timer.Change(time, Timeout.Infinite);
         *          return x;
         *      },
         *      new ExecutionDataflowBlockOptions
         *      {
         *          EnsureOrdered = true,
         *          BoundedCapacity = 1
         *      });
         *
         *  batch.LinkTo(transform);
         *
         *  return pipeline.Block(DataflowBlock.Encapsulate(batch, transform));
         * }*/

        public static IMessagePipeline <IMessage <T>, IMessageEnumerable <T> > Batch <T>(
            this IMessagePipeline <IMessage <T>, IMessage <T> > pipeline,
            int size,
            int time)
        {
            return(pipeline.Batch(size, TimeSpan.FromMilliseconds(time)));
        }
コード例 #6
0
        public static IMessagePipeline <IMessage <T>, IMessageEnumerable <T> > Batch <T>(
            this IMessagePipeline <IMessage <T>, IMessage <T> > pipeline,
            int size,
            TimeSpan time)
        {
            if (size <= 1)
            {
                throw new ArgumentException("Buffer size must be greater that 1");
            }

            var batch = new BatchBlock <IMessage <T> >(size, new GroupingDataflowBlockOptions
            {
                EnsureOrdered   = true,
                BoundedCapacity = size
            });

            var timer = time.TotalMilliseconds > 0
                ? new Timer(_ => batch.TriggerBatch(), null, (int)time.TotalMilliseconds, Timeout.Infinite)
                : null;

            var transform = new TransformBlock <IMessage <T>[], IMessageEnumerable <T> >(x =>
            {
                timer?.Change((int)time.TotalMilliseconds, Timeout.Infinite);
                return(new KafkaMessageEnumerable <T>(x));
            },
                                                                                         new ExecutionDataflowBlockOptions
            {
                EnsureOrdered   = true,
                BoundedCapacity = 1
            });

            batch.LinkTo(transform);

            return(pipeline.Block(DataflowBlock.Encapsulate(batch, transform)));
        }
コード例 #7
0
        /// <summary>
        /// 在ReceiveStart调用之前设置pipline.
        /// </summary>
        /// <param name="pipline"></param>
        /// <returns></returns>
        public async Task <UdpRemote> ListenAsync(ReceiveCallback receiveHandle, IMessagePipeline pipline)
        {
            IsListening = true;
            System.Threading.ThreadPool.QueueUserWorkItem(state =>
            {
                AcceptAsync();
            });

            if (connected.TryDequeue(out var remote))
            {
                if (remote != null)
                {
                    remote.MessagePipeline = pipline;
                    remote.ReceiveStart();
                    return(remote);
                }
            }
            if (TaskCompletionSource == null)
            {
                TaskCompletionSource = new TaskCompletionSource <UdpRemote>();
            }

            var res = await TaskCompletionSource.Task;

            TaskCompletionSource   = null;
            res.MessagePipeline    = pipline;
            res.OnReceiveCallback += receiveHandle;
            res.ReceiveStart();
            return(res);
        }
コード例 #8
0
        public static UnsubscribeAction ConnectEndpoint(this IMessagePipeline pipeline, Type messageType, IEndpoint endpoint)
        {
            object sink = FastActivator.Create(typeof(EndpointMessageSink <>).MakeGenericType(messageType),
                                               new object[] { endpoint });

            return(pipeline.FastInvoke <IMessagePipeline, UnsubscribeAction>("ConnectToRouter", sink));
        }
コード例 #9
0
        public static void Trace(IMessagePipeline pipeline)
        {
            PipelineViewer viewer = new PipelineViewer();

            pipeline.Inspect(viewer);

            System.Diagnostics.Trace.WriteLine(viewer.Text);
        }
コード例 #10
0
        public static WebSocketSession New(
            HttpContext httpContext,
            IMessagePipeline messagePipeline)
        {
            var connection = WebSocketConnection.New(httpContext);

            return(new WebSocketSession(connection, messagePipeline, true));
        }
コード例 #11
0
        public SubscriberContext(IMessagePipeline pipeline, IObjectBuilder builder, ISubscriptionEvent subscriptionEvent, object data)
        {
            _subscriptionEvent = subscriptionEvent;

            Pipeline = pipeline;
            Builder  = builder;
            Data     = data;
        }
コード例 #12
0
        public static UnsubscribeAction ConnectToRouter <T>(this IMessagePipeline pipeline, IPipelineSink <T> sink, Func <UnsubscribeAction> subscribedTo)
            where T : class
        {
            UnsubscribeAction result = pipeline.ConnectToRouter(sink);

            UnsubscribeAction remove = subscribedTo();

            return(() => result() && remove());
        }
コード例 #13
0
 public MessageProcessor(
     ISocketConnection connection,
     IMessagePipeline pipeline,
     PipeReader reader)
 {
     _connection = connection;
     _pipeline   = pipeline;
     _reader     = reader;
 }
コード例 #14
0
        private void ParseMiddlewares(IMessagePipeline pipeline, XmlNodeList nodes)
        {
            foreach (XmlNode node in nodes)
            {
                var parameters = new Dictionary <string, string>();
                if (node.NodeType != XmlNodeType.Element)
                {
                    continue;
                }
                if (node.Attributes == null)
                {
                    throw new MessagesConfigurationException("Middleware tag does not have attributes.");
                }
                foreach (XmlAttribute attr in node.Attributes)
                {
                    parameters.Add(attr.Name, attr.Value);
                }

                var typeName = node.Attributes["type"].Value;
                if (string.IsNullOrWhiteSpace(typeName))
                {
                    throw new MessagesConfigurationException("Middleware tag does not have \"type\" attribute.");
                }

                var type = Type.GetType(typeName);
                if (type == null)
                {
                    throw new MessagesConfigurationException($"Cannot load type {typeName}.");
                }

                var ctor = type.GetConstructors().FirstOrDefault(c => c.GetParameters().Length == 1 &&
                                                                 c.GetParameters()[0].ParameterType == typeof(IDictionary <string, string>));
                if (ctor == null)
                {
                    ctor = type.GetConstructors().FirstOrDefault(c => c.GetParameters().Length == 0);
                }

                if (ctor == null)
                {
                    var msg = "Cannot find public parameterless constructor or constructor that accepts IDictionary<string, string>.";
                    throw new MessagesConfigurationException(msg);
                }

                var middleware = ctor.GetParameters().Length == 1
                    ? ctor.Invoke(new object[] { parameters }) as IMessagePipelineMiddleware
                    : ctor.Invoke(new object[] { }) as IMessagePipelineMiddleware;
                if (middleware == null)
                {
                    throw new MessagesConfigurationException($"Cannot instaniate pipeline middleware {type.Name}.");
                }

                SetIdProperty(middleware, node);

                pipeline.AddMiddlewares(middleware);
            }
        }
コード例 #15
0
        /// <summary>
        /// Returns middleware in pipeline with specified id or generates <see cref="InvalidOperationException" />.
        /// </summary>
        /// <param name="pipeline">Pipeline to search.</param>
        /// <param name="id">Middleware id.</param>
        /// <returns>Found middleware.</returns>
        public static IMessagePipelineMiddleware GetMiddlewareById(this IMessagePipeline pipeline, string id)
        {
            var middleware = pipeline.Middlewares.FirstOrDefault(m => m.Id == id);

            if (middleware == null)
            {
                throw new InvalidOperationException($"Cannot find middleware with id {id} in pipeline {pipeline}.");
            }
            return(middleware);
        }
コード例 #16
0
		private static void PublishBatch(IMessagePipeline _pipeline, int _batchSize)
		{
			_batchId = Guid.NewGuid();
			for (int i = 0; i < _batchSize; i++)
			{
				IndividualBatchMessage message = new IndividualBatchMessage(_batchId, _batchSize);

				_pipeline.Dispatch(message);
			}
		}
コード例 #17
0
        private static void PublishBatch(IMessagePipeline _pipeline, int _batchSize)
        {
            _batchId = Guid.NewGuid();
            for (int i = 0; i < _batchSize; i++)
            {
                IndividualBatchMessage message = new IndividualBatchMessage(_batchId, _batchSize);

                _pipeline.Dispatch(message);
            }
        }
コード例 #18
0
        public static UnsubscribeAction Subscribe <TMessage, TKey>(this IMessagePipeline pipeline, TKey correlationId, IEndpoint endpoint)
            where TMessage : class, CorrelatedBy <TKey>
        {
            var correlatedConfigurator = CorrelatedMessageRouterConfigurator.For(pipeline);

            var router = correlatedConfigurator.FindOrCreate <TMessage, TKey>();

            UnsubscribeAction result = router.Connect(correlationId, new EndpointMessageSink <TMessage>(endpoint));

            return(result);
        }
コード例 #19
0
        public static UnregisterAction Filter <TMessage>(this IMessagePipeline pipeline, string description, Func <TMessage, bool> allow)
            where TMessage : class
        {
            MessageFilterConfigurator configurator = MessageFilterConfigurator.For(pipeline);

            var filter = configurator.Create(description, allow);

            UnregisterAction result = () => { throw new NotSupportedException("Removal of filters not yet supported"); };

            return(result);
        }
コード例 #20
0
        public static UnsubscribeAction ConnectToRouter <T>(this IMessagePipeline pipeline, IPipelineSink <T> sink)
            where T : class
        {
            MessageRouterConfigurator routerConfigurator = MessageRouterConfigurator.For(pipeline);

            MessageRouter <T> router = routerConfigurator.FindOrCreate <T>();

            UnsubscribeAction result = router.Connect(sink);

            return(() => result() && (router.SinkCount == 0));
        }
コード例 #21
0
        /// <summary>
        /// 创建TCPRemote并ReceiveStart.在ReceiveStart调用之前设置pipline,以免设置不及时漏掉消息.
        /// </summary>
        /// <param name="pipline"></param>
        /// <returns></returns>
        public async Task <TCPRemote> ListenAsync(ReceiveCallback receiveHandle, IMessagePipeline pipline)
        {
            var remoteSocket = await Accept();

            var remote = new TCPRemote(remoteSocket);

            remote.MessagePipeline    = pipline;
            remote.OnReceiveCallback += receiveHandle;
            remote.ReceiveStart();
            return(remote);
        }
コード例 #22
0
        private WebSocketSession(
            ISocketConnection connection,
            IMessagePipeline messagePipeline,
            bool disposeConnection)
        {
            _connection        = connection;
            _disposeConnection = disposeConnection;

            _keepAlive        = new KeepConnectionAliveJob(connection);
            _messageProcessor = new MessageProcessor(connection, messagePipeline, _pipe.Reader);
            _messageReceiver  = new MessageReceiver(connection, _pipe.Writer);
        }
コード例 #23
0
ファイル: Router.cs プロジェクト: ErikRBergman/MiniMQ
        private Task ProcessGet(string messageHandlerName, IMessagePipeline pipeline)
        {
            var messageHandler = this.messageHandlerContainer.GetMessageHandler(messageHandlerName);
            var message        = messageHandler.ReceiveMessageOrNull();

            if (message != null)
            {
                return(pipeline.SendMessage(message));
            }

            return(Task.CompletedTask);
        }
コード例 #24
0
        /// <summary>
        /// The receive message async.
        /// </summary>
        /// <param name="pipeline"></param>
        /// <param name="cancellationToken">
        /// The cancellation token.
        /// </param>
        /// <returns>
        /// The <see cref="Task"/>.
        /// </returns>
        public async Task <IMessage> ReceiveMessageAsync(IMessagePipeline pipeline, CancellationToken cancellationToken)
        {
            await this.semaphore.WaitAsync(cancellationToken);

            IMessage message;

            // this will only fail if there is a bug in the program
            this.messages.TryDequeue(out message);
            await pipeline.SendMessageAsync(message);

            return(message);
        }
コード例 #25
0
 public ApplicationWebSocketSubscriber(
     IMessageHandler messageHandler,
     IWebSocketClient webSocketClient,
     IMessagePipeline pipeline,
     WebSubscriberSettings settings)
 {
     this.messageHandler          = messageHandler;
     this.webSocketClient         = webSocketClient;
     this.pipeline                = pipeline;
     this.settings                = settings;
     this.cancellationTokenSource = new CancellationTokenSource();
 }
コード例 #26
0
 public SubscriptionMiddleware(
     RequestDelegate next,
     IMessagePipeline messagePipeline,
     SubscriptionMiddlewareOptions options)
 {
     _next = next
             ?? throw new ArgumentNullException(nameof(next));
     _messagePipeline = messagePipeline
                        ?? throw new ArgumentNullException(nameof(messagePipeline));
     _options = options
                ?? throw new ArgumentNullException(nameof(options));
 }
コード例 #27
0
        public async Task <IMessage> ReceiveMessageAsync(IMessagePipeline messagePipeline, string messageUniqueIdentifier, CancellationToken token)
        {
            var inputStream = new WebSocketInputStream(this.webSocket);
            var message     = await this.messageFactory.CreateMessage(inputStream, messageUniqueIdentifier);

            if (messagePipeline != null)
            {
                await messagePipeline.SendMessageAsync(message);
            }

            return(message);
        }
コード例 #28
0
 public static IMessagePipeline <TSource, IMessageOffset> Commit <TSource>(
     this IMessagePipeline <TSource, IMessageOffset> pipeline)
 {
     return(pipeline.Block(new TransformBlock <IMessageOffset, IMessageOffset>(x =>
     {
         x.Commit(true);
         return x;
     },
                                                                               new ExecutionDataflowBlockOptions
     {
         BoundedCapacity = 1,
         EnsureOrdered = true
     })));
 }
コード例 #29
0
        public MySubscriptionMiddleware(
            RequestDelegate next,
            IMessagePipeline messagePipeline,
            SubscriptionMiddlewareOptions options,
            MyWebSocketManager socketManager)
        {
            _next = next
                    ?? throw new ArgumentNullException(nameof(next));
            _messagePipeline = messagePipeline
                               ?? throw new ArgumentNullException(nameof(messagePipeline));
            _options = options
                       ?? throw new ArgumentNullException(nameof(options));

            this.socketManager = socketManager;
        }
コード例 #30
0
        public static IMessagePipeline <TSource, TDestination> Buffer <TSource, TDestination>(
            this IMessagePipeline <TSource, TDestination> pipeline,
            int size)
        {
            if (size <= 1)
            {
                throw new ArgumentException("Buffer size must be greater that 1");
            }

            return(pipeline.Block(new TransformBlock <TDestination, TDestination>(x => x, new ExecutionDataflowBlockOptions
            {
                BoundedCapacity = size,
                EnsureOrdered = true
            })));
        }
コード例 #31
0
        public async Task <IMessage> ReceiveMessageAsync(IMessagePipeline pipeline, CancellationToken cancellationToken)
        {
            await this.WaitForNewMessageAsync(cancellationToken);

            IMessage message;

            this.requestMessages.TryDequeue(out message);

            if (pipeline != null)
            {
                await pipeline.SendMessageAsync(message);
            }

            return(message);
        }