private Connection(string host, int port, TcpClient tcpClient, ConnectionConfiguration configuration) { _host = host; _port = port; _tcpClient = tcpClient; _configuration = configuration; }
public static async Task<INode> ConnectAsync(string host, int port, ConnectionPoolConfiguration poolConfiguration, ConnectionConfiguration connectionConfiguration) { var masterPool = await ConnectionPool.ConnectAsync(host, port, poolConfiguration, connectionConfiguration); var infoCommand = Command.From("INFO"); var response = await masterPool.SendAsync(infoCommand); if (response.IsError) throw ExceptionBecause.Node.GotErrorResponseFromNodeInfo(host, port); if (response.Value.IsArray) throw ExceptionBecause.MalformedData.InNodeInfo(host, port, response); var slavePoolTasks = response.Value.ToString() .Split(new [] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries) .Where(s => s.StartsWith("slave")) .Select(slaveInfo => { var components = slaveInfo.Split(','); if (components.Length != 5) throw ExceptionBecause.MalformedData.InMasterSlaveInfo(host, port, slaveInfo); var nodeHost = components[2]; var nodePort = int.Parse(components[3]); return ConnectionPool.ConnectAsync(nodeHost, nodePort, poolConfiguration, connectionConfiguration); }); var slavePools = await Task.WhenAll(slavePoolTasks); return new Node(host, port, masterPool, slavePools); }
public static async Task<IConnection> ConnectAsync(string host, int port, ConnectionConfiguration configuration) { var tcpClient = new TcpClient(); var connection = new Connection(host, port, tcpClient, configuration); await connection.Connect(); return connection; }
public static async Task<IConnectionPool> ConnectAsync(string host, int port, ConnectionPoolConfiguration poolConfiguration, ConnectionConfiguration connectionConfiguration) { var tasks = new List<Task<IConnection>>(); for (var i = 0; i < poolConfiguration.ConnectionsPerPool; i++) tasks.Add(Connection.ConnectAsync(host, port, connectionConfiguration)); var connections = await Task.WhenAll(tasks); return new ConnectionPool(poolConfiguration, connections); }