/// <summary> /// Handles the <see cref="E:ServerConnectionStarting" /> event. /// </summary> /// <param name="sender">The sender.</param> /// <param name="args">The <see cref="ServerConnectionEventArgs" /> instance containing the event data.</param> private void OnServerConnectionStarting(object sender, ServerConnectionEventArgs args) { try { args.ConnectionInfo = CurrentServerInfo; ServerConnectionStarting?.Invoke(this, args); } catch { //ignored } }
/// <summary> /// Connects the specified hostname. /// </summary> /// <param name="hostname">The hostname.</param> /// <param name="port">The port.</param> /// <returns>Task<System.Boolean>.</returns> public async Task <bool> Connect(string hostname, int port) { ServerConnectionStarting?.Invoke(this, e: new ServerConnectionEventArgs { Message = $"Connecting to {hostname}:{port}...", Status = ServerConnectionStatusEnum.Connecting, Timestamp = DateTime.Now }); try { TcpClient = new TcpClient { NoDelay = true }; await TcpClient.ConnectAsync(hostname, port); } catch { ServerConnectionFailed?.Invoke(this, new ServerConnectionEventArgs { Message = "Failed to connect! Make sure the server is running and that your hostname and port are correct.", Status = ServerConnectionStatusEnum.Disconnected, Timestamp = DateTime.Now }); } if (!IsConnected) { return(false); } TcpClientStream = TcpClient.GetStream(); ServerConnectionSucceeded?.Invoke(this, new ServerConnectionEventArgs { Message = "Successfully connected.", Status = ServerConnectionStatusEnum.Connected, Timestamp = DateTime.Now }); Keepalive.Reset(); return(true); }