public async Task <CQConnection> ConnectTo(string hostport) { var parts = hostport.Split(new[] { ':' }, StringSplitOptions.RemoveEmptyEntries); var hostName = parts[0]; var portNumber = parts.Length > 1 ? int.Parse(parts[1]) : 18593; var connection = await CQConnection.Create(_crypt, hostName, portNumber, _rsa, _onStatusChange); ValidateWhitelistAndSetAlias(connection); lock (_connections) { // NOTE: prior connections to same peer are not removed, by design _connections.Add(connection); } try { NewConnection?.Invoke(this, new CQConnectionEventArgs { Connection = connection }); } catch (Exception ex) { ex.Log(); } $"Connected to [{connection}]".Log(); return(connection); }
public async Task OnConnectionAccepted(TcpClient tcpClient) { var connection = await CQConnection.Create( _crypt, tcpClient, _rsa, _onStatusChange); connection.ConversationEnded += (s, e) => { lock (_connections) { if (_connections.Remove(connection)) { $"Removed {connection}".Log(); } } }; ValidateWhitelistAndSetAlias(connection); $"Accepted [{connection}]".Log(); lock (_connections) { _connections.Add(connection); try { NewConnection?.Invoke(this, new CQConnectionEventArgs { Connection = connection }); } catch (Exception ex) { ex.Log(); } } }