Esempio n. 1
0
        private void RunSession()
        {
            TcpClient tcpClient;

            try
            {
                tcpClient = new TcpClient(Address, Port);
            }
            catch (SocketException ex)
            {
                MessageBox.Show("Unable to connect to game (" + ex.Message + ")", "Unable to connect", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Application.Restart();
                return;
            }

            while (Program.CurrentGame.Playable)
            {
                if (NewCommand)
                {
retry:
                    if (SharedNetworking.Send(tcpClient, NextCommand))
                    {
                        NewCommand   = false;
                        LastResponse = SharedNetworking.ReceiveString(tcpClient);
                        if (LastResponse.Contains(Environment.NewLine))
                        {
                            LastResponse = LastResponse.Replace(Environment.NewLine, "");
                        }
                        if (!string.IsNullOrEmpty(LastResponse))
                        {
                            NewResponse = true;
                        }
                        else
                        {
                            if (MessageBox.Show("A network error has occurred while getting the response to the last command", "Network Error", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error) == DialogResult.Retry)
                            {
                                NewResponse = false;
                                NewCommand  = true;
                                goto retry;
                            }
                            else
                            {
                                Application.Restart();
                            }
                        }
                    }
                    else
                    {
                        if (MessageBox.Show("A network error has occurred while sending the last command", "Network Error", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error) == DialogResult.Retry)
                        {
                            NewResponse = false;
                            NewCommand  = true;
                            goto retry;
                        }
                        else
                        {
                            Application.Restart();
                        }
                    }
                }
                else
                {
                    Thread.Sleep(500);
                }
            }
            tcpClient.Close();
        }
Esempio n. 2
0
        private void ClientLink(object myClientObj)
        {
            TcpClient myClient   = (TcpClient)myClientObj;
            Player    thisPlayer = new Player("FAKE", new List <Card>());

            while (myClient.Connected)
            {
                string clientText = SharedNetworking.ReceiveString(myClient);
                if (string.IsNullOrEmpty(clientText))
                {
                    goto drop;
                }
                string   serverText  = "";
                string[] ClientTexts = clientText.Split(' ');
                switch (ClientTexts[0])
                {
                /* Get Game Details
                 * EXPECTS
                 * [0]Command
                 *
                 * RESPONSE
                 * HosterName PlayerInGame FreeSeats GUID_Version:GUID_Version
                 */
                case "HELLO":
                    serverText = HandleHello();
                    break;

                /* Try and (Re)Join the game.
                 * EXPECTS:
                 * [0]Command
                 * [1]Username
                 * [2]Hash of Password
                 *
                 * RESPONSE:
                 * SUCCESS
                 * FAILED NoSpace
                 */
                case "JOIN":
                    Player newPlayer = HandleJoin(thisPlayer, ClientTexts);
                    if (newPlayer.Name.StartsWith("FAILED "))
                    {
                        serverText = newPlayer.Name;
                    }
                    else
                    {
                        thisPlayer = newPlayer;
                        serverText = "SUCCESS";
                    }
                    break;

                /*
                 * Gets the GameSet.
                 * EXPECTS
                 * [0]Command
                 *
                 * RESPONSE:
                 * Base64 Represenation of GameSet
                 * SETERROR
                 */
                case "GETCARDS":
                    serverText = HandleGetCards();
                    break;

                /*
                 * Exits the game
                 * EXPECTS:
                 * [0]Command
                 *
                 * RESPONSE:
                 * RuleName:Value RuleName:Value RuleName:Value
                 */
                case "GETRULES":
                    serverText = HandleGetRules();
                    break;

                /*
                 * Updates the status of the game
                 * EXPECTS:
                 * [0]Command
                 *
                 * RESPONSE:
                 * WAITING (PreGame)
                 * PLAYING BlackCardBase64 Round
                 * VOTING AnswersBase64
                 * END
                 */
                case "GAMEUPDATE":
                    serverText = HandleGameUpdate();
                    break;

                /*
                 * Updates the leaderboard
                 * EXPECTS:
                 * [0]Command
                 *
                 * RESPONSE:
                 * PlayerName:Score,PlayerName:Score,PlayerName:Score
                 */
                case "GAMESCORES":
                    serverText = HandleGameScores();
                    break;

                /*
                 * Gets the players White Cards
                 * EXPECTS:
                 * [0]Command
                 *
                 * RESPONSE
                 * CardID,CardID,CardID,CardID,CardID,CardID,CardID,CardID,CardID,CardID
                 * JoinFirst
                 */
                case "MYCARDS":
                    serverText = HandleMyCards(thisPlayer);
                    break;

                /* Find out if an action is required
                 * EXPECTS
                 * [0]Command
                 *
                 * RESPONSE
                 * YES
                 * NO
                 */
                case "NEED":
                    serverText = HandleNeed(thisPlayer, ClientTexts);
                    break;

                /*
                 * Submits the players answer
                 * EXPECTS:
                 * [0]Command
                 * [1]Base64 Encoded Answer
                 *
                 * RESPONSE:
                 * SUBMITTED
                 * ERROR
                 */
                case "SUBMIT":
                    serverText = HandleSubmit(thisPlayer, ClientTexts);
                    break;

                /*
                 * Submits the players vote
                 * EXPECTS:
                 * [0]Command
                 * [1]Base64 Vote
                 *
                 * RESPONSE:
                 * SUBMITTED
                 * ERROR
                 *
                 */
                case "VOTE":
                    serverText = HandleVote(thisPlayer, ClientTexts);
                    break;

                /*
                 * Updates the winners of the last round.
                 *
                 * EXPECTS:
                 * [0]Command
                 *
                 * RETURNS:
                 * [Binary Representation of Game.Winners]
                 */
                case "GETWINNER":
                    serverText = HandleGetWinner();
                    break;

                /*
                 * Performs a Special Action (e.g. Rebooting the Universe)
                 * EXPECTS:
                 * [0]Command
                 * [1]SpecialAbility
                 *
                 * RESPONSE:
                 * SUCCESS
                 * FAILURE
                 *
                 */
                case "SPECIAL":
                    serverText = HandleSpecial(thisPlayer, ClientTexts);
                    break;

                /*
                 * Performs a Cheating Action
                 * EXPECTS:
                 * [0]Command
                 * [1]SpecialAbility
                 *
                 * RESPONSE:
                 * SUCCESS
                 * FAILURE
                 *
                 */
                case "CHEAT":
                    serverText = HandleCheat(thisPlayer, ClientTexts);
                    break;

                /*
                 * Exits the game
                 * EXPECTS:
                 * [0]Command
                 */
                case "EXIT":
                    goto drop;

                //Reject HTTP Connections
                case "GET":
                    goto drop;

                // Catch All
                default:
                    serverText = "Shut up meg!";
                    break;
                }
                serverText = serverText + Environment.NewLine;
retry:
                if (!SharedNetworking.Send(myClient, serverText))
                {
                    if (MessageBox.Show("A network error has occurred while sending the last response", "Network Error", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error) == DialogResult.Retry)
                    {
                        goto retry;
                    }
                    else
                    {
                        Application.Restart();
                    }
                }
            }
drop:
            myClient.Close();
        }