private async Task OnboardPeer(string connStr) { try { var peer = new PeerConnection(_serviceId, _version, NodeId); await peer.Connect(connStr); AttachEventHandlers(peer); _peerEvent.WaitOne(); try { _peerConnections.Add(peer); peer.Run(); } finally { _peerEvent.Set(); } } catch (Exception ex) { _logger.LogError($"Error connecting to {connStr} - {ex.Message}"); var peers = _peerRoundRobin.Where(x => x.ConnectionString == connStr); foreach (var peer in peers) { peer.Unreachable = true; } } }
public void Open() { _cancelTokenSource = new CancellationTokenSource(); _listener = new TcpListener(IPAddress.Any, (int)_port); _listener.Start(); Task.Factory.StartNew(async() => { try { while (!_cancelTokenSource.IsCancellationRequested) { var client = await _listener.AcceptTcpClientAsync(); _logger.LogDebug($"Client connected - {client.Client.RemoteEndPoint}"); var peer = new PeerConnection(_serviceId, _version, NodeId, client); AttachEventHandlers(peer); _peerEvent.WaitOne(); try { _peerConnections.Add(peer); peer.Run(); } finally { _peerEvent.Set(); } } } catch (Exception ex) { _logger.LogError($"Error listening - {ex.Message}"); } }); DiscoverOwnConnectionStrings(); AdvertiseToPeers(); _discoveryTimer = new Timer(async(state) => { await DiscoverPeers(); }, null, TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(30)); _sharePeersTimer = new Timer(SharePeers, null, TimeSpan.FromMinutes(1), TimeSpan.FromMinutes(1)); Task.Factory.StartNew(async() => { while (!_cancelTokenSource.IsCancellationRequested) { await Task.Delay(TimeSpan.FromMinutes(5)); var peers = GetActivePeers(); _logger.LogInformation($"Check in - Outgoing peers: {peers.Count(x => x.Outgoing)}"); _logger.LogInformation($"Check in - Incoming peers: {peers.Count(x => !x.Outgoing)}"); //var process = Process.GetCurrentProcess(); //_logger.LogInformation($"Check in - Thread count: {process.Threads.Count}"); //_logger.LogInformation($"Check in - Working set: {process.WorkingSet64}"); //_logger.LogInformation($"Check in - PrivateMemorySize: {process.PrivateMemorySize64}"); } }); }