/// <summary> /// Handles the <see cref="E:ServerConnectionSucceeded" /> event. /// </summary> /// <param name="sender">The sender.</param> /// <param name="args">The <see cref="ServerConnectionEventArgs" /> instance containing the event data.</param> public void OnServerConnectionSucceeded(object sender, ServerConnectionEventArgs args) { try { args.ConnectionInfo = CurrentServerInfo; ServerConnectionSucceeded?.Invoke(this, args); HostnameUpdated?.Invoke(this, new HostnameEventArgs() { NewHostname = CurrentServerInfo.Hostname, Timestamp = DateTime.Now }); RequestAuth(CurrentServerInfo.Password); } 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); }