public LoggingMessagePipeline(string actionName, ILog log, IMessagePipeline innerMessagePipeline, long messageNumber) { this.actionName = actionName; this.log = log; this.innerMessagePipeline = innerMessagePipeline; this.messageNumber = messageNumber; }
public UnsubscribeAction Connect <TMessage>(IMessagePipeline pipeline, IPipelineSink <TMessage> sink) where TMessage : class { MessageRouterConfigurator routerConfigurator = For(pipeline); return(routerConfigurator.FindOrCreate <TMessage>().Connect(sink)); }
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 }))); }
public void Dispose() { _pipeline = null; _bus = null; _service = null; _endpointFactory = null; }
/* * 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))); }
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))); }
/// <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); }
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)); }
public static void Trace(IMessagePipeline pipeline) { PipelineViewer viewer = new PipelineViewer(); pipeline.Inspect(viewer); System.Diagnostics.Trace.WriteLine(viewer.Text); }
public static WebSocketSession New( HttpContext httpContext, IMessagePipeline messagePipeline) { var connection = WebSocketConnection.New(httpContext); return(new WebSocketSession(connection, messagePipeline, true)); }
public SubscriberContext(IMessagePipeline pipeline, IObjectBuilder builder, ISubscriptionEvent subscriptionEvent, object data) { _subscriptionEvent = subscriptionEvent; Pipeline = pipeline; Builder = builder; Data = data; }
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()); }
public MessageProcessor( ISocketConnection connection, IMessagePipeline pipeline, PipeReader reader) { _connection = connection; _pipeline = pipeline; _reader = reader; }
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); } }
/// <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); }
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); } }
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); }
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); }
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)); }
/// <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); }
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); }
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); }
/// <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); }
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(); }
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)); }
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); }
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 }))); }
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; }
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 }))); }
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); }