public void TestConnectedClients() { ushort port = TestHelper.GetPort(); using var server = new EasyTcpServer().Start(port); Assert.IsEmpty(server.GetConnectedClients()); using var client = new EasyTcpClient(); client.Connect(IPAddress.Loopback, port); TestHelper.WaitWhileFalse(() => server.ConnectedClientsCount == 1); Assert.AreEqual(1, server.ConnectedClientsCount); using var client2 = new EasyTcpClient(); client2.Connect(IPAddress.Loopback, port); TestHelper.WaitWhileFalse(() => server.ConnectedClientsCount == 2); Assert.AreEqual(2, server.ConnectedClientsCount); client.Dispose(); TestHelper.WaitWhileTrue(() => server.ConnectedClientsCount == 2); Assert.AreEqual(1, server.ConnectedClientsCount); client2.Dispose(); TestHelper.WaitWhileTrue(() => server.ConnectedClientsCount == 1); Assert.AreEqual(0, server.ConnectedClientsCount); }
/// <summary> /// Establish connection with remote host /// </summary> /// <param name="client"></param> /// <param name="endPoint">endPoint of remote host</param> /// <param name="socket">base socket for EasyTcpClient, new one is created when null</param> /// <returns>determines whether the client connected successfully</returns> public static async Task <bool> ConnectAsync(this EasyTcpClient client, EndPoint endPoint, Socket socket = null) { if (client == null) { throw new ArgumentException("Could not connect: client is null"); } if (endPoint == null) { throw new ArgumentException("Could not connect: endpoint is null"); } if (client.BaseSocket != null) { throw new ArgumentException("Could not connect: client is already connected"); } try { client.BaseSocket = socket ?? client.Protocol.GetSocket(endPoint.AddressFamily); await client.BaseSocket.ConnectAsync(endPoint); if (client.BaseSocket.Connected && client.Protocol.OnConnect(client)) { client.FireOnConnect(); return(true); } } catch { // Ignore exception, dispose (&disconnect) client and return false } client.Dispose(); // Set socket to null return(false); }
/// <summary> /// Establishes connection with remote host /// </summary> /// <param name="client"></param> /// <param name="endPoint">endPoint of remote host</param> /// <param name="timeout">maximum time for connecting with remote host</param> /// <param name="socket">socket for EasyTcpClient, new one is create when null</param> /// <returns>determines whether the client connected successfully</returns> public static bool Connect(this EasyTcpClient client, EndPoint endPoint, TimeSpan?timeout = null, Socket socket = null) { if (client == null) { throw new ArgumentException("Could not connect: client is null"); } if (endPoint == null) { throw new ArgumentException("Could not connect: endpoint is null"); } if (client.BaseSocket != null) { throw new ArgumentException("Could not connect: client is still connected"); } try { client.BaseSocket = socket ?? client.Protocol.GetSocket(endPoint.AddressFamily); client.BaseSocket.ConnectAsync(endPoint).Wait(DefaultTimeout); if (client.BaseSocket.Connected && client.Protocol.OnConnect(client)) { client.FireOnConnect(); return(true); } } catch { //Ignore exception, dispose (&disconnect) client and return false } client.Dispose(); return(false); }
private async void PollServer(object source, ElapsedEventArgs e) { Console.WriteLine("Polling Server"); try { using var client = new EasyTcpClient(); var pingRecived = client.Connect(_discordSecrets.IpAddress, 25565); client.Dispose(); if (pingRecived != _lastStatusRepo.WasPreviouslyOnline()) { _lastStatusRepo.SaveIsOnlineStatus(pingRecived); if (pingRecived) { await SendServerOnlineMessage(true); } else { await SendServerOnlineMessage(false); } } } catch (Exception exception) { Console.WriteLine("Unable to complete command:"); Console.WriteLine(exception.Message); } }
/// <summary> /// Try to connect to server until connected /// </summary> private async Task Connect(EasyTcpClient client, string ip, ushort port) { client.Dispose(); // Reset client while (!await client.ConnectAsync(ip, port)) { await Task.Delay(ConnectTimeout); } }
public void IsConnected1() { using var client = new EasyTcpClient(); client.Connect(IPAddress.Any, _port); Assert.IsTrue(client.IsConnected(true)); client.Dispose(); // Disconnect Assert.IsFalse(client.IsConnected()); }
/// <summary> /// Determines if client is still connected to endpoint /// </summary> /// <param name="client"></param> /// <param name="poll">uses poll if set to true, can be more accurate but decreases performance</param> /// <returns>determines whether the client is still connected</returns> public static bool IsConnected(this EasyTcpClient client, bool poll = false) { if (client?.BaseSocket == null) { return(false); } if (!client.BaseSocket.Connected || !poll && client.BaseSocket.Poll(0, SelectMode.SelectRead) && client.BaseSocket.Available.Equals(0)) { client.FireOnDisconnect(); client.Dispose(); return(false); } return(true); }
public static void Connect() { /* Create new instance of EasyEncrypt with a password and (hardcoded) salt * Default algorithm is Aes */ _encrypter = new EasyEncrypt("Password", "Salt2135321"); // Create new client with encryption var client = new EasyTcpClient().UseClientEncryption(_encrypter); client.Connect("127.0.0.1", Port); // All data is now encrypted before sending client.Send("Data"); // Encrypter gets disposed with client + protocol client.Dispose(); }
public void OnDisconnectServer() { var port = TestHelper.GetPort(); using var server = new EasyTcpServer().Start(port); int x = 0; server.OnDisconnect += (o, c) => Interlocked.Increment(ref x); var client = new EasyTcpClient(); Assert.IsTrue(client.Connect(IPAddress.Any, port)); client.Dispose(); TestHelper.WaitWhileFalse(() => x == 1); Assert.AreEqual(1, x); }
public void OnDisconnectServer() { var certificate = new X509Certificate2("certificate.pfx", "password"); var port = TestHelper.GetPort(); using var server = new EasyTcpServer().UseSsl(certificate).Start(port); int x = 0; server.OnDisconnect += (o, c) => Interlocked.Increment(ref x); var client = new EasyTcpClient().UseSsl("localhost", true); Assert.IsTrue(client.Connect(IPAddress.Any, port)); client.Dispose(); TestHelper.WaitWhileFalse(() => x == 1); Assert.AreEqual(1, x); }
/// <summary> /// Establishes a connection to a remote host /// </summary> /// <param name="client"></param> /// <param name="ipAddress">ipAddress of remote host</param> /// <param name="port">port of remote host</param> /// <param name="timeout">maximum time for connecting to remote host</param> /// <param name="socket">socket for EasyTcpClient, new one is create when null</param> /// <returns>determines whether the client connected successfully</returns> public static bool Connect(this EasyTcpClient client, IPAddress ipAddress, ushort port, TimeSpan?timeout = null, Socket socket = null) { if (client == null) { throw new ArgumentException("Could not connect: client is null"); } if (ipAddress == null) { throw new ArgumentException("Could not connect: ipAddress is null"); } if (port == 0) { throw new ArgumentException("Could not connect: Invalid port"); } if (client.BaseSocket != null) { throw new ArgumentException("Could not connect: client is still connected"); } try { client.BaseSocket = socket ?? new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp); var result = client.BaseSocket.BeginConnect(ipAddress, port, null, null); result.AsyncWaitHandle.WaitOne(timeout ?? TimeSpan.FromMilliseconds(DefaultTimeout)); client.BaseSocket.EndConnect(result); if (client.BaseSocket.Connected) { client.FireOnConnect(); client.StartInternalDataReceiver(); return(true); } } catch { //Ignore exception, dispose (&disconnect) client and return false } client.Dispose(); return(false); }
/// <summary> /// Establishes a connection to a remote host /// </summary> /// <param name="client"></param> /// <param name="ipAddress">ipAddress of remote host</param> /// <param name="port">port of remote host</param> /// <returns>determines whether the client connected successfully</returns> public static async Task <bool> ConnectAsync(this EasyTcpClient client, IPAddress ipAddress, ushort port) { if (client == null) { throw new ArgumentException("Could not connect: client is null"); } if (ipAddress == null) { throw new ArgumentException("Could not connect: ipAddress is null"); } if (port == 0) { throw new ArgumentException("Could not connect: Invalid port"); } if (client.BaseSocket != null) { throw new ArgumentException("Could not connect: client is still connected"); } try { client.BaseSocket = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp); await client.BaseSocket.ConnectAsync(ipAddress, port); if (client.BaseSocket.Connected) { client.FireOnConnect(); client.StartListening(); return(true); } } catch { //Ignore exception, dispose (&disconnect) client and return false } client.Dispose(); return(false); }
public void Dispose() { Client?.Dispose(); Server?.Dispose(); }
/// <summary> /// Fire OnDisconnectEvent and dispose client /// </summary> /// <param name="client"></param> protected virtual void HandleDisconnect(EasyTcpClient client) { client.FireOnDisconnect(); client.Dispose(); }
/// <summary> /// Handle disconnect for a specific client /// </summary> /// <param name="client"></param> internal static void HandleDisconnect(this EasyTcpClient client) { client.FireOnDisconnect(); client.Dispose(); }