Пример #1
0
        internal void OnNewPipeSubscriber(Type messageType, PipeServer pipe)
        {
            Task.Run(async() =>
            {
                if (_dict.TryGetValue(messageType, out var queue))
                {
                    List <DelayedMessageScope> filteredMessages = null;
                    while (queue.TryDequeue(out DelayedMessageScope scope))
                    {
                        try
                        {
                            if (!scope.IsTimedOut)
                            {
                                if (pipe.IsShouldSend(scope.Message) &&
                                    await pipe.SendMessage(scope.Message, PipeMessageType.Message))
                                {
                                    scope.Tcs.SetResult(true);
                                }
                                else
                                {
                                    if (filteredMessages == null)
                                    {
                                        filteredMessages = new List <DelayedMessageScope>();
                                    }
                                    filteredMessages.Add(scope);
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            scope.Tcs.SetException(ex);
                        }
                    }

                    // put to the queue again all messages that are not sent
                    if (filteredMessages != null)
                    {
                        foreach (var item in filteredMessages)
                        {
                            queue.Enqueue(item);
                        }
                    }
                }
            });
        }
Пример #2
0
        void StartNext(string pipeName, Func <NamedPipeServerStream> configure)
        {
            _pendingServer = new PipeServer(_hub, _logger, _onNewPipeSubscriber);

            _pendingServer.Connected += async(s, a) =>
            {
                try
                {
                    lock (_serversLock)
                        _servers = _servers.Add((PipeServer)s);

                    _logger.Info($"Client connected, total count: {_servers.Count}");

                    // start waiting for the next connection
                    StartNext(pipeName, configure);

                    await _hub.Publish(new RemoteClientConnectedEvent()
                    {
                        Timestamp         = DateTime.Now,
                        TotalClientsCount = _servers.Count
                    });
                }
                catch (Exception ex)
                {
                    _logger.Error(ex);
                }
            };

            _pendingServer.Disconnected += async(s, a) =>
            {
                try
                {
                    lock (_serversLock)
                        _servers = _servers.Remove((PipeServer)s);

                    _logger.Info($"Client disconnected, remained: {_servers.Count}");

                    await _hub.Publish(new RemoteClientDisconnectedEvent()
                    {
                        Timestamp         = DateTime.Now,
                        TotalClientsCount = _servers.Count
                    });
                }
                catch (Exception ex)
                {
                    _logger.Error(ex);
                }
            };

            Task.Run(async() =>
            {
                try
                {
                    await _pendingServer.Start(pipeName, configure);
                }
                catch (OperationCanceledException)
                {
                }
                catch (Exception ex)
                {
                    _logger.Error(ex);
                }
            });
        }