/// <summary> /// Waits for server. /// </summary> /// <param name="address">The address.</param> /// <param name="port">The port.</param> /// <param name="timeout">The timeout.</param> /// <returns></returns> public static Client WaitForServer(ClientConnectionParameters connectionParameters, TimeSpan?timeout = null) { var end = DateTime.Now.Add(timeout ?? DateTime.Now.AddYears(1).Subtract(DateTime.Now)); // get the response. var response = WaitForServer(connectionParameters, end, null); // always initialize on the public API response.Initialize(); return(response); }
/// <summary> /// Waits for server. /// </summary> /// <param name="address">The address.</param> /// <param name="port">The port.</param> /// <param name="timeout">The timeout.</param> /// <returns></returns> public static async Task <Client> WaitForServer(ClientConnectionParameters connectionParameters, TimeSpan?timeout = null) { var end = DateTime.Now.Add(timeout ?? TimeSpan.FromDays(365.5)); // get the response. var response = await WaitForServer(connectionParameters, end, null); // always initialize on the public API response.Initialize(); return(response); }
/// <summary> /// Initializes a new instance of the <see cref="Client"/> class. /// </summary> /// <param name="address">The address.</param> /// <param name="port">The port.</param> public Client(ClientConnectionParameters parameters) { this.ConnectionParameters = parameters; this.Serialization = ConnectionParameters.Serializer; this.socket = ConnectionParameters.IsValid ? new TcpClient(this.Address, this.Port).Client : new TcpClient().Client; this.Initialize(); }
/// <summary> /// Waits for server. /// This is the internal call to handle Reconnection. /// </summary> /// <param name="connectionParameters">The connection parameters.</param> /// <param name="until">try until Time.</param> /// <returns></returns> private static async Task <Client> WaitForServer(ClientConnectionParameters connectionParameters, DateTime until, Action <bool> connectionChange) { var response = new Client { Serialization = connectionParameters.Serializer, ConnectionParameters = connectionParameters, ConnectionChanged = connectionChange }; var tcp = new TcpClient(); await Task.Delay(1); while (DateTime.Now < until) { try { // create a connection. var connect = tcp.BeginConnect(connectionParameters.Address, connectionParameters.Port, _ => { }, null); // wait 500 ms connect.AsyncWaitHandle.WaitOne(250); if (!tcp.Connected) { continue; } // end the connection tcp.EndConnect(connect); // set our socket to the currently connected socket. response.socket = tcp.Client; // we should be in a good state so break out of the loop. return(response); } catch { } Extensions.Wait(null, 150); } return(response); }