コード例 #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 messageToSend = new byte[1000];

            // Send it
            Console.WriteLine("Client will send message and wait for reply");
            var receivedMessage = client.SendReceive(messageToSend);

            if (receivedMessage == null)
            {
                Console.WriteLine("Client received no answer");
            }
            else
            {
                Console.WriteLine($"Client received answer of {receivedMessage.Length} bytes");

                // Return message to pool
                receivedMessage.Return();
            }
        }
コード例 #2
0
ファイル: SocketClient.cs プロジェクト: toroerp/SimplMessage
        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");
        }
コード例 #3
0
ファイル: SocketClient.cs プロジェクト: toroerp/SimplMessage
        private void SendAndReceiveCheck(SimplSocketClient client)
        {
            Log("*** SendAndReceiveCheck ***");
            var rnd = new Random();

            byte[] randomData = new byte[1000 * 512];
            rnd.NextBytes(randomData);

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

            var outputData = client.SendReceive(randomData);

            if (outputData == null)
            {
                Log("No answer received"); return;
            }

            var same = true;

            for (int i = 0; i < outputData.Length; i++)
            {
                if (outputData.Content[i] != randomData[i])
                {
                    same = false;
                    break;
                }
            }
            // We need to return the output data to pool
            outputData.Return();
            Log(same?"data is same":"Data is not the same");
        }
コード例 #4
0
ファイル: Program.cs プロジェクト: zHaytam/simplsockets
        //[Benchmark(OperationsPerInvoke = Ops)]
        public long c1_s1()
        {
            long x = 0;

            using (var client = new SimplSocketClient(CreateSocket))
            {
                client.Connect(s1);
                for (int i = 0; i < Ops; i++)
                {
                    var response = client.SendReceive(_data);
                    x += response.Length;
                }
            }
            return(AssertResult(x));
        }
コード例 #5
0
ファイル: SocketClient.cs プロジェクト: toroerp/SimplMessage
        private void SendFromPoolAndReceiveBenchmark(SimplSocketClient client)
        {
            Log("*** SendFromPoolAndReceiveBenchmark ***");
            var rnd         = new Random();
            var bufferSizes = new[] { 1, 10, 100, 1000 };
            var length      = 0;

            for (var test = 0; test < 4; test++)
            {
                var randomData = PooledMessage.Rent(bufferSizes[test] * 512);
                length += randomData.Length;
                rnd.NextBytes(randomData.Content);

                var countPerIteration = 100;
                var watch             = Stopwatch.StartNew();
                for (var i = 0; i < countPerIteration; i++)
                {
                    if (!client.IsConnected())
                    {
                        client.Connect(new IPEndPoint(IPAddress.Loopback, 5000));
                    }

                    var response = client.SendReceive(randomData);
                    if (response == null)
                    {
                        Log("No response ");
                    }
                    else
                    {
                        response.Return();
                    }
                }

                watch.Stop();
                var speed       = (countPerIteration * length) / (watch.ElapsedMilliseconds / 1000.0);
                var scaledSpeed = ScaledSpeed(speed);
                Log($"{countPerIteration}x{length}: {watch.ElapsedMilliseconds}ms = {scaledSpeed} ");
                randomData.Return();
            }
        }
コード例 #6
0
        public TOut SendReceive <TIn, TOut>(string identifier, TIn message, int replyTimeout = 0)
        {
            var rawMessage = _serializer.Serialize(identifier, message);

            if (rawMessage == null)
            {
                return(default(TOut));
            }
            var rawResponse = _simplSocketClient.SendReceive(rawMessage);

            if (rawResponse == null)
            {
                return(default(TOut));
            }
            var response = _serializer.Deserialize <TOut>(rawResponse.Content);

            rawResponse.Dispose();
            if (response == null)
            {
                return(default(TOut));
            }
            return(response);
        }