Beispiel #1
0
        // Update the client's window based on messages received from the server. This method is called as soon as the client
        // receives a message.
        public void GotMessage(object peer)
        {
            NetIncomingMessage inc;

            if (false == this.BeginCommunication)
            {
                // Continue reading messages until the requested update is received.
                while (!UpdateReceived && !this.Disconnected)
                {
                    Console.WriteLine(this.PlayerName + " stuck in begin");

                    // Iterate through all of the available messages.
                    while ((inc = (peer as NetPeer).ReadMessage()) != null)
                    {
                        if (inc.MessageType == NetIncomingMessageType.StatusChanged)
                        {
                            this.BeginCommunication = true;
                            UpdateReceived          = true;

                            // Close the wait message if it is open.
                            if (null != WaitMessage && !WaitMessage.CloseWindow)
                            {
                                CreateNewThread(new Action <Object>((sender) => { WaitMessage.CloseWindow = true; }));
                            }
                        }

                        Thread.Sleep(100);
                    }

                    Thread.Sleep(100);
                }
            }
            else
            {
                inc = (peer as NetPeer).ReadMessage();

                if (null != inc && inc.MessageType == NetIncomingMessageType.Data)
                {
                    Datatype messageType = (Datatype)inc.ReadByte();
                    //MessageBox.Show(this.PlayerName + " received " + messageType.ToString());

                    switch (messageType)
                    {
                    case Datatype.UpdatePlayerList:
                    {
                        PlayerList = (List <Player>)ServerUtilities.ReceiveMessage(inc, messageType);

                        if (null != this.PlayerList && this.PlayerList.Count > 0)
                        {
                            CreateNewThread(new Action <Object>((sender) => { this.LaunchGameButton.IsEnabled = (this.PlayerList[0].Name == this.PlayerName); }));
                        }

                        //// The parentheses around currentPlayerList are required. See http://bit.ly/19t9NEx.
                        //ThreadStart start = delegate() { Dispatcher.Invoke(DispatcherPriority.Normal, new Action<List<Player>>(UpdatePlayerListBox), (playerList)); };
                        CreateNewThread(new Action <Object>(UpdatePlayerListBox));

                        // Close the wait message if it is open.
                        if (null != WaitMessage && !WaitMessage.CloseWindow)
                        {
                            CreateNewThread(new Action <Object>((sender) => { WaitMessage.CloseWindow = true; }));
                        }

                        break;
                    }

                    case Datatype.LaunchGame:
                    {
                        Console.WriteLine(this.Player.Name + " received Launch");

                        // Receive the data related to the current turn from the server.
                        this.Turn = (Turn)ServerUtilities.ReceiveMessage(inc, Datatype.LaunchGame);

                        if (this.PlayerList[0].Name == this.PlayerName)
                        {
                            CreateNewThread(new Action <Object>(LaunchGame));
                        }

                        break;
                    }

                    case Datatype.TimeToConnect:
                    {
                        string playerToConnect = (String)ServerUtilities.ReceiveMessage(inc, messageType);

                        if (this.PlayerName == playerToConnect)
                        {
                            CreateNewThread(new Action <Object>(LaunchGame));
                        }

                        break;
                    }
                    }
                }
            }
        }