Beispiel #1
0
        protected void StartReadLoop()
        {
            Task.Run(async() =>
            {
                try
                {
                    while (_reader != null)
                    {
                        var message = await _reader.ReadLineAsync();

                        if (message != null)
                        {
                            ProcessPacket(message);
                        }
                        else
                        {
                            OnDisconnected();
                            break;
                        }
                    }
                }
                catch (Exception ex)
                {
                    _logger.Error(ex);
                    OnDisconnected();
                }
            });
        }
        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);
                }
            });
        }