Exemplo n.º 1
0
        /// <summary>
        /// opens a new multiplayer connection to the client
        /// </summary>
        public void Open()
        {
            // sets bools to true and message to null
            messageReceived = null;
            isMultiActive   = true;
            isOpen          = true;

            // create a client and Open the streams
            client = new TcpClient();
            client.Connect(ep);
            stream = client.GetStream();
            reader = new BinaryReader(stream);
            writer = new BinaryWriter(stream);

            // task to listen for the receiving stream. ready for messages from the server
            receiver = new Task(() =>
            {
                // do as long as the isOpen bool is true
                while (isOpen)
                {
                    // read only if the last message received was already taken
                    if (messageReceived == null)
                    {
                        string result;
                        Statues statues;
                        try
                        {
                            // get a message from the server and parse it to Statues
                            result  = reader.ReadString();
                            statues = Statues.FromJson(result);
                        } catch (Exception e)
                        {
                            // an error in the connection
                            statues = new Statues();
                            statues.SetStatues(Status.Close, "Error" + e.ToString());
                            result = "Error";
                            break;
                        }
                        // switch through the different options received from the server
                        switch (statues.Stat)
                        {
                        case Status.Disconnect:
                            {
                                Close();
                                break;
                            }

                        case Status.Close:
                            {
                                Close();
                                break;
                            }

                        case Status.PrintAndContinue:
                            {
                                Console.WriteLine(statues.Message);
                                continue;
                            }

                        case Status.PrintAndStop:
                            {
                                // print the message and close the connection
                                Console.WriteLine(statues.Message);
                                writer.Write("exit");
                                string feedback = reader.ReadString();
                                Close();
                                break;
                            }

                        case Status.Error:
                            {
                                // print the error message
                                Console.WriteLine(statues.Message);
                                break;
                            }

                        default:
                            {
                                break;
                            }
                        }
                        // put the message from the server into the messageReceived var.
                        messageReceived = result;
                    }
                    // wait for some one to receive the message
                    else
                    {
                        Thread.Sleep(1);
                    }
                }
            });
            // start the thread
            receiver.Start();
        }
Exemplo n.º 2
0
        /// <summary>
        /// opens a new multiplayer connection to the client
        /// </summary>
        public void Open()
        {
            // sets bools to true and message to null
            isMultiActive = true;
            isOpen        = true;

            // create a client and Open the streams
            client = new TcpClient();
            client.Connect(ep);
            stream = client.GetStream();
            reader = new BinaryReader(stream);
            writer = new BinaryWriter(stream);

            // task to listen for the receiving stream. ready for messages from the server
            receiver = new Task(() =>
            {
                // do as long as the isOpen bool is true
                while (isOpen)
                {
                    // read only if the last message received was already taken
                    if (statues == null)
                    {
                        string result;
                        Status status;
                        try
                        {
                            // get a message from the server and parse it to Statues
                            result  = reader.ReadString();
                            statues = Statues.FromJson(result);
                            status  = statues.Stat;
                            NotifyAboutMessage?.Invoke(this, new StatuesEventArgs(statues));
                        }
                        catch (Exception)
                        {
                            MessageBox.Show("Connect to the server have been closed");
                            // an error in the connection
                            statues = new Statues();
                            statues.SetStatues(Status.Close, "Error receiveing message from the server");
                            status = Status.Disconnect;
                        }
                        // switch through the different options received from the server
                        switch (status)
                        {
                        case Status.Disconnect:
                            {
                                Close();
                                break;
                            }

                        case Status.Close:
                            {
                                Close();
                                break;
                            }

                        case Status.Play:
                            {
                                statues = null;
                                continue;
                            }

                        case Status.CloseGame:
                            {
                                // close the connection
                                writer.Write("exit");
                                Close();
                                break;
                            }

                        case Status.Finish:
                            {
                                // close the connection
                                writer.Write("exit");
                                Close();
                                break;
                            }

                        default:
                            {
                                break;
                            }
                        }
                    }
                    // wait for some one to receive the message
                    else
                    {
                        Thread.Sleep(1);
                    }
                }
            });
            // start the thread
            receiver.Start();
        }