// not including modifying the shared game state
        public byte[] ToLowerLayer(RemoteInstruction instruction)
        {
            // string rawString = null;
            byte[] rawBytes = null;
            switch (instruction.Type)
            {
            case RemoteInstructionType.ChessChange:
                // rawString = JsonSerializer.Serialize((ChessChange)instruction);
                rawBytes = JsonSerializer.SerializeToUtf8Bytes((ChessChange)instruction);
                break;

            case RemoteInstructionType.ChessMove:
                // rawString = JsonSerializer.Serialize((ChessMove)instruction);
                rawBytes = JsonSerializer.SerializeToUtf8Bytes((ChessMove)instruction);
                break;

            case RemoteInstructionType.PushSharedGameState:
                rawBytes = Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject((PushSharedGameState)instruction));
                break;

            case RemoteInstructionType.UndoChessBoard:
            case RemoteInstructionType.RedoChessBoard:
                rawBytes = Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(instruction));
                break;
            }
            if (rawBytes == null)
            {
                return(null);
            }

            rawBytes = FramingProtocol.FromHighLayerToHere(rawBytes);
            return(rawBytes);
        }
Ejemplo n.º 2
0
        static async Task Profile(int count, SocketType socketType, bool useSsl,
                                  int serverSendBufferSize,
                                  int serverReciveBufferSize,
                                  int clientSendBufferSize,
                                  int clientReceiveBufferSize,
                                  FramingProtocol framingProtocol)
        {
            X509Certificate2 x509Cert = null;
            string           certName = null;

            if (useSsl)
            {
                x509Cert = GetXeenyTestCertificate(out certName);
            }

            var httpAddress = $"http://localhost/test";
            var tcpAddress  = "tcp://localhost:9988";

            var hostBuilder = new ServiceHostBuilder <Service>(InstanceMode.PerCall)
                              .WithCallback <ICallback>()
                              .WithMessagePackSerializer() //it is the default
                              .WithConsoleLogger();        //default is empty

            ConnectionBuilder <IService> clientBuilder = null;

            if (socketType == SocketType.WebSocket)
            {
                hostBuilder.AddWebSocketServer(httpAddress, options =>
                {
                    options.ReceiveBufferSize = serverReciveBufferSize;
                    options.SendBufferSize    = serverReciveBufferSize;
                    if (useSsl)
                    {
                        throw new NotImplementedException();
                    }
                });

                clientBuilder = new ConnectionBuilder <IService>()
                                .WithWebSocketTransport(httpAddress, options =>
                {
                    options.SendBufferSize    = clientSendBufferSize;
                    options.ReceiveBufferSize = serverReciveBufferSize;
                    if (useSsl)
                    {
                        throw new NotImplementedException();
                    }
                });
            }
            else if (socketType == SocketType.TCP)
            {
                hostBuilder.AddTcpServer(tcpAddress, options =>
                {
                    options.ReceiveBufferSize = serverReciveBufferSize;
                    options.SendBufferSize    = serverSendBufferSize;
                    options.FramingProtocol   = framingProtocol;
                    if (useSsl)
                    {
                        options.SecuritySettings = SecuritySettings.CreateForServer(x509Cert);
                    }
                });

                clientBuilder = new ConnectionBuilder <IService>()
                                .WithTcpTransport(tcpAddress, options =>
                {
                    options.SendBufferSize    = clientSendBufferSize;
                    options.ReceiveBufferSize = clientReceiveBufferSize;
                    options.FramingProtocol   = framingProtocol;
                    if (useSsl)
                    {
                        options.SecuritySettings = SecuritySettings.CreateForClient(certName);
                    }
                });
            }

            var host = hostBuilder.CreateHost();

            await host.Open();

            Console.WriteLine("Host is open");

            var client = await clientBuilder.CreateConnection();

            //1 KB, the actual message on the wire will be more than 1 KB (depends on method type, name, and parameters)
            var msg = new string('*', 1000);

            for (int j = 0; j < 100; j++)
            {
                await Task.Delay(1000);

                var sw = Stopwatch.StartNew();

                for (int i = 0; i < count; i++)
                {
                    var resp = await client.Echo(msg);

                    //Task.Run(async () =>
                    //{
                    //    var resp = await client.Echo(msg);
                    //    Console.WriteLine(resp.Length);
                    //});
                }

                sw.Stop();
                Console.WriteLine($">> {sw.ElapsedMilliseconds}");
            }
        }