Ejemplo n.º 1
0
        public void OnDataReceive2()
        {
            // Example receiving for the client
            ushort port = TestHelper.GetPort();

            using var server = new EasyTcpServer();
            server.Start(port);

            using var client = new EasyTcpClient();
            Assert.IsTrue(client.Connect(IPAddress.Any, port));

            const string message      = "Hello client!";
            int          receiveCount = 0;

            client.OnDataReceive += (sender, receivedMessage) =>
            {
                //Async lambda, so thread safe increase integer
                if (message.Equals(receivedMessage.ToString()))
                {
                    Interlocked.Increment(ref receiveCount);
                }
                Console.WriteLine($"[{receiveCount}]Received message: {receivedMessage.ToString()}");
            };

            server.SendAll(message);
            server.SendAll(message);

            TestHelper.WaitWhileTrue(() => receiveCount < 2);
            Assert.AreEqual(2, receiveCount);
        }
Ejemplo n.º 2
0
        public void SendString()
        {
            ushort port = TestHelper.GetPort();

            using var server = new EasyTcpServer();
            server.Start(port);

            using var client = new EasyTcpClient();
            Assert.IsTrue(client.Connect(IPAddress.Any, port));

            string data = "Data";

            server.SendAll(data);
            server.SendAll(data, Encoding.UTF32); //Send with different encoding
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Send data (byte[]) to connected clients
 /// </summary>
 /// <param name="server"></param>
 /// <param name="data">data to send to connected clients</param>
 /// <param name="compression">compress data using GZIP if set to true</param>
 public static void SendAll(this EasyTcpServer server, byte[] data, bool compression = false)
 {
     if (compression)
     {
         data = CompressionUtil.Compress(data);
     }
     server.SendAll(dataArray: data);
 }
Ejemplo n.º 4
0
 /// <summary>
 /// Send action (byte[]) to connected clients
 /// </summary>
 /// <param name="server"></param>
 /// <param name="action">action code</param>
 /// <param name="data">data to send to connected clients</param>
 /// <param name="compression">compress data using GZIP if set to true</param>
 public static void SendAllAction(this EasyTcpServer server, int action, byte[] data = null, bool compression = false)
 {
     if (compression && data != null)
     {
         data = CompressionUtil.Compress(data);
     }
     server.SendAll(BitConverter.GetBytes(action), data);
 }
Ejemplo n.º 5
0
        private static void OnDataReceive(object sender, Message message)
        {
            Console.WriteLine($"Server: Received data, received: {message}");
            Console.WriteLine("Server: Broadcasting received data");

            EasyTcpClient client = message.Client;        // Get client from message
            EasyTcpServer server = (EasyTcpServer)sender; // Convert sender to server or client

            server.SendAll(message);
        }
Ejemplo n.º 6
0
        public void SendAllArray()
        {
            ushort port   = TestHelper.GetPort();
            var    server = new EasyTcpServer().Start(port);

            using var client = new EasyTcpClient();
            Assert.IsTrue(client.Connect(IPAddress.Any, port));

            byte[] data = new byte[100];
            server.SendAll(data);
        }
Ejemplo n.º 7
0
        public void SendBool()
        {
            ushort port = TestHelper.GetPort();

            using var server = new EasyTcpServer();
            server.Start(port);

            using var client = new EasyTcpClient();
            Assert.IsTrue(client.Connect(IPAddress.Any, port));

            server.SendAll(true);
        }
Ejemplo n.º 8
0
        public void SendDouble()
        {
            ushort port = TestHelper.GetPort();

            using var server = new EasyTcpServer();
            server.Start(port);

            using var client = new EasyTcpClient();
            Assert.IsTrue(client.Connect(IPAddress.Any, port));

            double data = 123.0;

            server.SendAll(data);
        }
Ejemplo n.º 9
0
        public async Task TestReceive()
        {
            ushort port = TestHelper.GetPort();

            using var server = new EasyTcpServer().Start(port);

            using var client = new EasyTcpClient();
            Assert.IsTrue(client.Connect(IPAddress.Any, port));

            var receivedData = client.ReceiveAsync();

            TestHelper.WaitWhileFalse(() => server.ConnectedClientsCount == 1);
            server.SendAll("Data");
            Assert.AreEqual("Data", (await receivedData).ToString());
        }
Ejemplo n.º 10
0
        public void SendLong()
        {
            ushort port   = TestHelper.GetPort();
            var    server = new EasyTcpServer();

            server.Start(port);

            var client = new EasyTcpClient();

            Assert.IsTrue(client.Connect(IPAddress.Any, port));

            long data = 123;

            server.SendAll(data);
        }
Ejemplo n.º 11
0
        public static async Task Start()
        {
            using var server = new EasyTcpServer();
            var socket = server.BaseSocket; // Get baseSocket, null when server is disposed

            // Start server on given port (default address: "0.0.0.0")
            server.Start(Port);

            // Start server on given port and address
            server.Start("0.0.0.0", Port);
            server.Start(IPAddress.Any, Port);

            // Send message to all connected clients
            server.SendAll("Hello clients!");

            // Get copy of list with connected clients
            var connectedClientsList = server.GetConnectedClients();
            var connectedSocketsList = server.GetConnectedSockets();

            // Get amount of connected clients
            var connectedClientsCount = server.ConnectedClientsCount;

            server.OnDataReceive += (sender, message) =>
            {
                // Get client
                var client = message.Client;

                // Get endpoint or ip address of client
                var endPoint = client.GetEndPoint();
                var ip       = client.GetIp();

                // Use all the other clientUtil on client
                client.Send("Hello from server!");

                // Determines whether message is compressed
                var isCompressed = message.IsCompressed();

                /* Decompress message,
                 * ignored when message is not compressed
                 */
                var data = message.Decompress();
            };
        }
Ejemplo n.º 12
0
        public void PacketTest()
        {
            ushort port = TestHelper.GetPort();

            using var server = new EasyTcpServer().Start(port);
            using var client = new EasyTcpClient();
            Assert.IsTrue(client.Connect(IPAddress.Loopback, port));

            var testData = new ExamplePacket("Test data");
            int received = 0;

            client.OnDataReceive += (sender, message) =>
            {
                string data = message.ToPacket <ExamplePacket>().DataStr;
                if (data.Equals(testData.DataStr))
                {
                    Interlocked.Increment(ref received);
                }
            };

            server.SendAll(testData);
            TestHelper.WaitWhileFalse(() => received == 1);
            Assert.AreEqual(1, received);
        }
Ejemplo n.º 13
0
 /// <summary>
 /// Send data (double) to connected clients
 /// </summary>
 /// <param name="server"></param>
 /// <param name="data">data to send to connected clients</param>
 public static void SendAll(this EasyTcpServer server, double data) =>
 server.SendAll(BitConverter.GetBytes(data));
Ejemplo n.º 14
0
 /// <summary>
 /// Send action with data (custom class) to all connected clients
 /// </summary>
 /// <param name="server"></param>
 /// <param name="action">action code as string</param>
 /// <param name="o">custom class</param>
 public static void SendAll(this EasyTcpServer server, string action, object o, bool compression = false)
     => server.SendAll(action.ToActionCode(), o, compression);
Ejemplo n.º 15
0
 /// <summary>
 /// Send data (object) to connected clients
 /// </summary>
 /// <param name="server"></param>
 /// <param name="data">data to send to connected clients</param>
 /// <param name="compression">compress data using GZIP if set to true</param>
 public static void SendAll(this EasyTcpServer server, object data, bool compression = false)
 => server.SendAll(server?.Serialize(data), compression);
Ejemplo n.º 16
0
 /// <summary>
 /// Send data (IEasyTcpPacket) to connected clients
 /// </summary>
 /// <param name="server"></param>
 /// <param name="data">data to send to connected clients</param>
 /// <param name="compression">compress data using GZIP if set to true</param>
 public static void SendAll(this EasyTcpServer server, IEasyTcpPacket data, bool compression = false)
 => server.SendAll(data.Data, compression);
Ejemplo n.º 17
0
 /// <summary>
 /// Send data (string) to connected clients
 /// </summary>
 /// <param name="server"></param>
 /// <param name="data">data to send to connected clients</param>
 /// <param name="encoding">encoding type (Default: UTF8)</param>
 /// <param name="compression">compress data using GZIP if set to true</param>
 public static void SendAll(this EasyTcpServer server, string data, Encoding encoding = null,
                            bool compression = false)
 => server.SendAll((encoding ?? Encoding.UTF8).GetBytes(data), compression);