Example #1
0
        public void AddSubscriber(SubscribersModel <TResponse> subscriber)
        {
            bool added = Subscribers.TryAdd(subscriber.Id, subscriber);

            Logger.LogInformation($"New subscriber added: {subscriber.Id}");
            if (!added)
            {
                Logger.LogInformation($"could not add subscriber: {subscriber.Id}");
            }
        }
Example #2
0
 public void RemoveSubscriber(SubscribersModel <TResponse> subscriber)
 {
     try
     {
         Subscribers.TryRemove(subscriber.Id, out SubscribersModel <TResponse> item);
         Logger.LogInformation($"Force Remove: {item.Id} - no longer works");
     }
     catch (Exception ex)
     {
         Logger.LogError(ex, $"Could not remove {subscriber.Id}");
     }
 }
Example #3
0
        private async Task <SubscribersModel <TResponse> > SendMessageToSubscriber(SubscribersModel <TResponse> subscriber, TResponse message)
        {
            try
            {
                if (Filter(subscriber, message))
                {
                    Logger.LogInformation($"Sending TResponse: {message}");
                    await subscriber.Subscriber.WriteAsync(message);
                }

                return(null);
            }
            catch (Exception ex)
            {
                Logger.LogError(ex, "Could not send");
                return(subscriber);
            }
        }
Example #4
0
        public async Task CreateDuplexStreaming(
            IAsyncStreamReader <TRequest> requestStream,
            IServerStreamWriter <TResponse> responseStream,
            ServerCallContext context)
        {
            var httpContext = context.GetHttpContext();

            Logger.LogInformation($"Connection id: {httpContext.Connection.Id}");

            if (!await requestStream.MoveNext())
            {
                return;
            }

            var clientId = _messageProcessor.GetClientId(requestStream.Current);

            Logger.LogInformation($"{clientId} connected");
            var subscriber = new SubscribersModel <TResponse>
            {
                Subscriber = responseStream,
                Id         = $"{clientId}"
            };

            _serverGrpcSubscribers.AddSubscriber(subscriber);

            do
            {
                if (requestStream.Current == null)
                {
                    continue;
                }

                var resultMessage = _messageProcessor.Process(requestStream.Current);
                if (resultMessage == null)
                {
                    continue;
                }

                await _serverGrpcSubscribers.BroadcastMessageAsync(resultMessage);
            } while (await requestStream.MoveNext());

            _serverGrpcSubscribers.RemoveSubscriber(subscriber);
            Logger.LogInformation($"{clientId} disconnected");
        }
Example #5
0
 public virtual bool Filter(SubscribersModel <TResponse> subscriber, TResponse message) => true;