private void OnPeerConnectedEventHandle(IPeer peer) { // Check if max number of connections has been reached if (maxConnections > 0 && connectedPeers.Count >= maxConnections) { peer.Disconnect("The max number of connections has been reached"); return; } logger.Debug($"Client {peer.Id} connected to server. Total clients are: {connectedPeers.Count + 1}"); // Listen to messages peer.OnMessageReceivedEvent += OnMessageReceived; // Save the peer connectedPeers[peer.Id] = peer; // Create the security extension var extension = peer.AddExtension(new SecurityInfoPeerExtension()); // Set default permission level extension.PermissionLevel = 0; // Create a unique peer guid extension.UniqueGuid = Guid.NewGuid(); peersByGuidLookup[extension.UniqueGuid] = peer; // Invoke the event OnPeerConnectedEvent?.Invoke(peer); OnPeerConnected(peer); }
private void PeerConnected(object sender, IPeer peer) { try { _connectedPeers.Add(peer); ListenForIncomingMessages(peer); InitiateHandshaking(peer); } catch (Exception e) { _logger.LogWarning($"Something went wrong with {peer}. Exception: {e}"); peer.Disconnect(); } }
/// <summary> /// Peer connected Event /// </summary> /// <param name="sender">Sender</param> /// <param name="peer">Peer</param> private void PeerConnected(object sender, IPeer peer) { try { if (_acl.IsAllowed(peer.EndPoint) == false) { throw new UnauthorizedAccessException($"The endpoint \"{peer.EndPoint}\" is prohibited by ACL."); } _connectedPeers.Add(peer); _peerMessageListener.StartFor(peer, _messageListenerTokenSource.Token); } catch (Exception e) { _logger.LogWarning($"Something went wrong with {peer}. Exception: {e}"); peer.Disconnect(); } }
private void PeerConnected(object sender, IPeer peer) { try { if (_acl != null && !_acl.IsAllowed(peer)) { throw new UnauthorizedAccessException(); } this._connectedPeers.Add(peer); this.ListenForMessages(peer, this._messageListenerTokenSource.Token); // initiate handshake peer.Send(this.VersionMessage); } catch (Exception e) { this._logger.LogWarning($"Something went wrong with {peer}. Exception: {e}"); peer.Disconnect(); } }
/// <summary> /// Peer connected Event /// </summary> /// <param name="sender">Sender</param> /// <param name="peer">Peer</param> private void PeerConnected(object sender, IPeer peer) { try { if (_acl.IsAllowed(peer.EndPoint) == false) { throw new UnauthorizedAccessException($"The endpoint \"{peer.EndPoint}\" is prohibited by ACL."); } if (!_serverContext.ConnectedPeers.TryAdd(peer.EndPoint, peer)) { throw new InvalidOperationException($"The peer with endpoint \"{peer.EndPoint}\" is already connected."); } peer.OnDisconnect += (s, e) => _serverContext.ConnectedPeers.TryRemove(peer.EndPoint, out _); _peerMessageListener.StartFor(peer, _messageListenerTokenSource.Token); } catch (Exception e) { _logger.LogWarning($"Something went wrong with {peer}. Exception: {e}"); peer.Disconnect(); } }