コード例 #1
0
        public async static void startClient()
        {
            TTransport transport = null;

            try
            {
                string ip       = "127.0.0.1";
                string port     = "6667";
                string username = "******";
                string password = "******";
                // Make socket
                transport = new TSocketTransport(ip, int.Parse(port));
                if (!transport.IsOpen)
                {
                    await transport.OpenAsync();
                }
                TProtocol protocol = new TCompactProtocol(transport);

                TSIService.Client client = new TSIService.Client(protocol);



                var result = await client.openSessionAsync(new TSOpenSessionReq()
                {
                    Client_protocol = TSProtocolVersion.IOTDB_SERVICE_PROTOCOL_V2,
                    Username        = username,
                    Password        = password
                });


                //await client.testInsertTabletAsync(new TSInsertTabletReq()
                //{
                //    DeviceId = "11",
                //    Measurements = new System.Collections.Generic.List<string>()
                //     {
                //          "001"
                //     },
                //    SessionId = result.SessionId,
                //    Size = 10,
                //    Timestamps =new byte[] { Convert.ToByte(DateTime.Now) },
                //    Values = new byte[] { Convert.ToByte(2.33d) }
                //});
                Console.WriteLine("Thrift client result =: " + result.SessionId);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.StackTrace);
            }
            finally
            {
                if (null != transport)
                {
                    //close
                    transport.Close();
                }
            }
        }
コード例 #2
0
        static async Task Main(string[] args)
        {
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                Console.SetWindowSize(ANCHO_ESCENARIO, ALTO_ESCENARIO);
                Console.BufferHeight = ALTO_ESCENARIO;
                Console.BufferWidth  = ANCHO_ESCENARIO;
            }
            posicionPelota = new Posicion {
                X = ANCHO_ESCENARIO / 2, Y = ALTO_ESCENARIO / 2
            };
            jugadores.Add(new Jugador
            {
                IdJugador = 0,
                Posicion  = new Posicion {
                    X = 0, Y = (ALTO_ESCENARIO / 2 - TAMAÑO_PALETA / 2)
                }
            });
            jugadores.Add(
                new Jugador
            {
                IdJugador = 1,
                Posicion  = new Posicion {
                    X = ANCHO_ESCENARIO - 1, Y = (ALTO_ESCENARIO / 2 - TAMAÑO_PALETA / 2)
                }
            });
            try
            {
                TTransport transporte = new TSocketTransport("localhost", 5000);
                TProtocol  protocolo  = new TBinaryProtocol(transporte);
                cliente = new ServicioPingPong.Client(protocolo);
                while (true)
                {
                    if (!jugando)
                    {
                        miId = await cliente.EntrarALaPartidaAsync();

                        idRival = miId % 2 == 0 ? miId + 1 : miId - 1;
                        await cliente.EnviarPosicionAsync(jugadores[miId]);

                        jugando = true;
                    }
                    if (!Console.KeyAvailable)
                    {
                        try
                        {
                            jugadores[idRival] = await cliente.ConsultarPosicionJugadorAsync(idRival);

                            partidaIniciada = true;
                        }
                        catch (JugadorNoEncontrado)
                        {
                            if (partidaIniciada)
                            {
                                partidaIniciada = false;
                            }
                            else
                            {
                                marcador = "Esperando un oponente";
                            }
                        }
                        InicializarEscenario();
                        if (partidaFinalizada)
                        {
                            break;
                        }
                        if (partidaIniciada)
                        {
                            marcador = puntJugador1 + "  vs  " + puntJugador2;
                            InicializarEscenario();
                            Console.SetCursorPosition(posicionPelota.X, posicionPelota.Y);
                            Console.Write("°");
                            MoverPelota();
                        }
                        Thread.Sleep(70);
                        continue;
                    }
                    switch (Console.ReadKey().Key)
                    {
                    case ConsoleKey.UpArrow:
                        if (jugadores[miId].Posicion.Y > 0)
                        {
                            jugadores[miId].Posicion.Y--;
                            await cliente.EnviarPosicionAsync(jugadores[miId]);
                        }
                        break;

                    case ConsoleKey.DownArrow:
                        if (jugadores[miId].Posicion.Y < (ALTO_ESCENARIO - TAMAÑO_PALETA))
                        {
                            jugadores[miId].Posicion.Y++;
                            await cliente.EnviarPosicionAsync(jugadores[miId]);
                        }
                        break;
                    }
                }
                transporte.Close();
                Console.Read();
            }
            catch (TApplicationException tApplicationException)
            {
                Console.Clear();
                Console.WriteLine(tApplicationException.StackTrace);
                Console.Read();
            }
            catch (Exception)
            {
                Console.Clear();
                Console.WriteLine(" Error al conectar con el servidor");
                Console.Read();
            }
        }
コード例 #3
0
        /// <summary>
        /// Creates a game instance.
        /// </summary>
        /// <param name="args">If first value is given, it will be considered as Server IP</param>
        /// <returns>Task</returns>
        static async Task Main(string[] args)
        {
            string serverIp = "localhost";

            if (args.Length > 0)
            {
                serverIp = args[0];
            }
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                Console.SetWindowSize(CLIENT_WINDOW_WIDTH, CLIENT_WINDOW_HEIGHT);
                Console.BufferHeight = CLIENT_WINDOW_HEIGHT;
                Console.BufferWidth  = CLIENT_WINDOW_WIDTH;
            }
            try {
                TTransport transport = new TSocketTransport(serverIp, 5000);
                TProtocol  protocol  = new TBinaryProtocol(transport);
                client = new PingPongService.Client(protocol);
                while (true)
                {
                    if (!isGameRunning)
                    {
                        selfPlayerId = await client.JoinGameAsync();

                        opponentPlayerId = selfPlayerId % 2 == 0 ? selfPlayerId + 1 : selfPlayerId - 1;
                        await client.SendPlayerPadPositionAsync(players[selfPlayerId]);

                        isGameRunning = true;
                    }
                    if (!Console.KeyAvailable)
                    {
                        try {
                            var opponentPlayerData = await client.GetLatestPlayerPadPositionAsync(opponentPlayerId);

                            players[opponentPlayerId] = opponentPlayerData;
                            hasGameStarted            = true;
                        } catch (PlayerNotFoundException) {
                            if (hasGameStarted)
                            {
                                headerText     = "You won!";
                                hasGameStarted = false;
                            }
                            else
                            {
                                headerText = "Waiting for an opponent...";
                            }
                        }
                        DrawPads();
                        if (hasGameFinished)
                        {
                            break;
                        }
                        if (hasGameStarted)
                        {
                            DrawBall();
                            MoveBall();
                        }
                        Thread.Sleep(100);
                        continue;
                    }
                    switch (Console.ReadKey().Key)
                    {
                    case ConsoleKey.DownArrow:
                        if (players[selfPlayerId].Position.Y < (CLIENT_WINDOW_HEIGHT - 4))
                        {
                            players[selfPlayerId].Position.Y++;
                            await client.SendPlayerPadPositionAsync(players[selfPlayerId]);
                        }
                        break;

                    case ConsoleKey.UpArrow:
                        if (players[selfPlayerId].Position.Y > 0)
                        {
                            players[selfPlayerId].Position.Y--;
                            await client.SendPlayerPadPositionAsync(players[selfPlayerId]);
                        }
                        break;
                    }
                }
                transport.Close();
                Console.Read();
            } catch (TApplicationException tApplicationException) {
                Console.Clear();
                Console.WriteLine(tApplicationException.StackTrace);
                Console.Read();
            } catch (Exception exception) {
                Console.Clear();
                Console.WriteLine(">> No hay conexión con el servidor.\n\n> Stack trace:");
                Console.WriteLine(exception.StackTrace);
                Console.WriteLine(exception.Message);
                Console.WriteLine(exception.InnerException);
                Console.Read();
            }
        }