Exemplo n.º 1
0
        void CreateClient()
        {
            // Create the client
            var client = new SimplSocketClient();

            // Make the client connect automatically
            client.AutoConnect();

            // Wait until the connection is actually made
            client.WaitForConnection();

            // Create a byte array to send
            var arrayToSend1 = new byte[1000];

            // Send it
            client.Send(arrayToSend1);

            // Get a byte array from the memory pool
            var arrayToSend2 = PooledMessage.Rent(1000);

            client.Send(arrayToSend2);

            // We will not dispose the message directly, since it may still need to be send
            // Instead we use the ReturnAfterSend, which will return the message to the pool after sending
            arrayToSend2.ReturnAfterSend();
        }
Exemplo n.º 2
0
        private void SendFromPoolAndReceiveCheck(SimplSocketClient client)
        {
            Log("*** SendFromPoolAndReceiveCheck ***");
            var rnd = new Random();

            var randomData = PooledMessage.Rent(1000 * 512);

            rnd.NextBytes(randomData.Content);

            if (!client.IsConnected())
            {
                client.Connect(new IPEndPoint(IPAddress.Loopback, 5000));
            }

            var outputData = client.SendReceive(randomData);
            var same       = true;

            for (int i = 0; i < outputData.Length; i++)
            {
                if (outputData.Content[i] != randomData.Content[i])
                {
                    same = false;
                    break;
                }
            }
            randomData.Return();
            outputData.Return();
            Log(same ? "data is same" : "Data is not the same");
        }
Exemplo n.º 3
0
        void CreateServer()
        {
            // Create the server
            var server = new SimplSocketServer();

            // Start listening for client connections on loopback end poiny
            server.Listen(new IPEndPoint(IPAddress.Loopback, 5000));

            // Wait until a new client has connected
            var connectedClient = server.WaitForNewClient();

            // Create a byte array to send
            var arrayToSend1 = new byte[1000];

            // Send it
            Console.WriteLine($"Server is going to send message of {arrayToSend1.Length} bytes");
            server.Send(arrayToSend1, connectedClient);


            // Get a byte array from the memory pool
            var arrayToSend2 = PooledMessage.Rent(1000);

            Console.WriteLine($"Server is going to send a pooled message of {arrayToSend1.Length} bytes");
            server.Send(arrayToSend2, connectedClient);

            // Return message to pool
            arrayToSend2.ReturnAfterSend();
        }
Exemplo n.º 4
0
        private void SendFromPoolBenchmark(SimplSocketClient client)
        {
            Log("*** SendFromPoolBenchmark ***");
            var rnd         = new Random();
            var bufferSizes = new[] { 1, 10, 100, 1000 };
            var length      = 0;

            for (var test = 0; test < 4; test++)
            {
                length += bufferSizes[test] * 512;

                var countPerIteration = 100;
                var watch             = Stopwatch.StartNew();
                for (var i = 0; i < countPerIteration; i++)
                {
                    var randomData = PooledMessage.Rent(bufferSizes[test] * 512);
                    if (!client.IsConnected())
                    {
                        client.Connect(new IPEndPoint(IPAddress.Loopback, 5000));
                    }

                    client.Send(randomData);
                    randomData.Return();
                }

                watch.Stop();

                var speed       = (countPerIteration * length) / (watch.ElapsedMilliseconds / 1000.0);
                var scaledSpeed = ScaledSpeed(speed);
                Log($"{countPerIteration}x{length}: {watch.ElapsedMilliseconds}ms = {scaledSpeed} ");
            }
        }