/// <summary> /// Synchronously connect to a bond server /// </summary> /// <param name="server">The server to connect to</param> /// <param name="port">The port the server is listening on</param> /// <param name="createProxyCallback">The CreateProxyCallback</param> public BondTcpConnection <TProxy> Connect(string server, int port, CreateProxyCallback createProxyCallback) { Contract.Requires(server != null); Contract.Requires(server.Length > 0); Contract.Requires(port > 0); Contract.Requires(createProxyCallback != null); return(ConnectAsync(server, port, createProxyCallback).Result); }
public async Task <BondTcpConnection <TProxy> > ConnectAsync( string server, int port, CreateProxyCallback createProxyCallback) { Contract.Requires(server != null); Contract.Requires(server.Length > 0); Contract.Requires(port > 0); Contract.Requires(createProxyCallback != null); m_logger.Debug( "Connecting to {0} with timeout {1} ms", GetServerDescription(server, port), m_options.TimeoutInMs); NetlibConnectionConfig clientConfig = NetlibConnectionConfig.Default; clientConfig.Timeout = m_options.TimeoutInMs == 0 ? SocketConfiguration.InfiniteTimeout : TimeSpan.FromMilliseconds(m_options.TimeoutInMs); var connection = new NetlibConnection(clientConfig) { AlwaysReconnect = m_options.ReconnectAutomatically, }; try { var completionSource = TaskSourceSlim.Create <SocketError>(); connection.ConnectComplete += (sender, status) => completionSource.TrySetResult(status); connection.Disconnected += (sender, status) => m_logger.Debug("Disconnected from {0} ({1})", GetServerDescription(server, port), status); // NetlibConnection uses Dns.GetDnsEntry to resolve even if the server string contains an IP address. // CB uses IPs in the production environment and reverse lookup is not available there. // IP address can also belong to a machine behind a VIP where reverse lookup doesn't make sense. IPAddress ip; if (IPAddress.TryParse(server, out ip)) { connection.ConnectAsync(new IPEndPoint(ip, port)); } else { connection.ConnectAsync(server, port); } var result = await completionSource.Task; return(OnConnectComplete(server, port, createProxyCallback, connection, result)); } catch (Exception) { connection.Dispose(); throw; } }
private BondTcpConnection <TProxy> OnConnectComplete( string server, int port, CreateProxyCallback createProxyCallback, IOutgoingConnection connection, SocketError status) { if (status != SocketError.Success) { throw new IOException( string.Format( CultureInfo.InvariantCulture, "Failed to connect to {0} ({1})", GetServerDescription(server, port), status)); } var bondClient = new BondNetlibClient(connection, new BinaryProtocolFactory()); TProxy proxy = createProxyCallback(bondClient); m_logger.Debug("Connected to {0}", GetServerDescription(server, port)); return(new BondTcpConnection <TProxy>(connection, proxy)); }