Exemplo n.º 1
0
        /// <summary>
        /// Main listener thread for each client.
        /// </summary>
        /// <param name="client">The client we are handling</param>
        /// <returns></returns>
        public async Task ClientListener(TcpClient client)
        {
            byte[] buffer = new byte[256];

            while (isRunning)
            {
                Data message = await GetMessage(buffer, client);

                buffer = message.buffer;

                switch (Encoding.UTF8.GetString(message.message).Split(' ')[0])
                {
                case "getboard":
                    SendMessageAsync(client.GetStream(), "board " + board.GetNetworkString());
                    break;

                case "movepiece":
                    int  startX = int.Parse(Encoding.UTF8.GetString(message.message).Split(' ')[1].Split(',')[0]);
                    int  startY = int.Parse(Encoding.UTF8.GetString(message.message).Split(' ')[1].Split(',')[1]);
                    int  endX   = int.Parse(Encoding.UTF8.GetString(message.message).Split(' ')[2].Split(',')[0]);
                    int  endY   = int.Parse(Encoding.UTF8.GetString(message.message).Split(' ')[2].Split(',')[1]);
                    bool result = board.MovePiece(startX, startY, endX, endY);
                    if (result)
                    {
                        SendMessageAsync(client.GetStream(), string.Format("moveok {0},{1},{2},{3}", startX, startY, endX, endY));
                    }
                    else
                    {
                        SendMessageAsync(client.GetStream(), string.Format("movebad {0},{1},{2},{3}", startX, startY, endX, endY));
                    }
                    break;

                default:
                    SendMessageAsync(client.GetStream(), "badmessage");
                    break;
                }
            }
        }