Exemplo n.º 1
0
        static async Task RunClientAsync(string ip, int port)
        {
            var group = new MultithreadEventLoopGroup();

            string targetHost = null;

            try
            {
                var bootstrap = new Bootstrap();
                bootstrap
                .Group(group)
                .Channel <TcpSocketChannel>()
                .Option(ChannelOption.TcpNodelay, true)
                .Handler(new ActionChannelInitializer <ISocketChannel>(channel =>
                {
                    IChannelPipeline pipeline = channel.Pipeline;

                    pipeline.AddLast(new DelimiterBasedFrameDecoder(8192, Delimiters.LineDelimiter()));
                    pipeline.AddLast(new StringEncoder(), new StringDecoder(), handler);
                }));

                IChannel bootstrapChannel = await bootstrap.ConnectAsync(new IPEndPoint(IPAddress.Parse(ip), port));

                for (;;)
                {
                    Console.Write("$> ");
                    var line = Console.ReadLine();
                    if (string.IsNullOrEmpty(line))
                    {
                        continue;
                    }

                    try
                    {
                        var response = GameHandler.HandleClientCmd(handler.GetEventHandler(), line);
                        if (response != null)
                        {
                            var serObj = SerializeHandler.SerializeObj(response);
                            await bootstrapChannel.WriteAndFlushAsync(serObj + "\r\n");
                        }
                    }
                    catch (Exception e)
                    {
                        Console.Error.WriteLine(e.Message);
                    }
                    if (string.Equals(line, "bye", StringComparison.OrdinalIgnoreCase))
                    {
                        await bootstrapChannel.CloseAsync();

                        break;
                    }
                }

                await bootstrapChannel.CloseAsync();
            }
            finally
            {
                group.ShutdownGracefullyAsync().Wait(1000);
            }
        }
Exemplo n.º 2
0
        public void HandleTurnResponse(IChannelHandlerContext context, string msg)
        {
            try
            {
                var tResponse =
                    SerializeHandler.DeserializeObject <TurnResponse>(msg);

                var currentPlayer = Table.GetPlayerByContext(context);
                if (!TurnResponseHandler.Handle(tResponse, currentPlayer,
                                                Table))
                {
                    Console.WriteLine("[KO] Player play an invalid card, we will inform him");
                    if (currentPlayer == Table.CurrentPlayer)
                    {
                        Console.WriteLine("[OK] Ask to player a new turn");
                    }
                }
            }
            catch (SerializeHandlerException)
            {
                var e = new Event(EventType.Error, null,
                                  Table)
                {
                    ErrorMsg = "The card choose is unvalid"
                };
                var serObj = SerializeHandler.SerializeObj(e);
                context.WriteAndFlushAsync(serObj + "\r\n");
            }
            catch (Exception)
            {
                var e = new Event(EventType.Error, null,
                                  Table)
                {
                    ErrorMsg = "The game conditions don't permit you to game now, wait to be invited."
                };
                var serObj = SerializeHandler.SerializeObj(e);
                context.WriteAndFlushAsync(serObj + "\r\n");
            }
        }