/// <summary>
        /// executes the start command
        /// </summary>
        /// <param name="args">arguments of the command</param>
        /// <param name="client">to give the command</param>
        /// <returns> the Status</returns>
        public Status Execute(string[] args, TcpClient client)
        {
            string message = String.Join(" ", args);

            // send and receive a message
            messageRec.SendMessage(message);
            string result = Statues.FromJson(messageRec.GetMassage()).Message;

            Console.WriteLine(result);
            return(Status.KeepConnection);
        }
Exemple #2
0
 /// <summary>
 /// executes the close command
 /// </summary>
 /// <param name="args">arguments of the command</param>
 /// <param name="client">to give the command</param>
 /// <returns></returns>
 public Status Execute(string[] args, TcpClient client)
 {
     if (messageRec.IsMultiActive)
     {
         string message = String.Join(" ", args);
         // send and receive a message
         messageRec.SendMessage(message);
         Statues stat = Statues.FromJson(messageRec.GetMassage());
         Console.WriteLine(stat.Message);
         return(stat.Stat);
     }
     Console.WriteLine("Can't close a game without playing");
     return(Status.Disconnect);
 }
Exemple #3
0
        /// <summary>
        /// sends a single message to the server and wait for response back
        /// </summary>
        /// <param name="message"> to send </param>
        public void SendSingleMessage(string message)
        {
            //  create a client and establish a connection
            TcpClient newClient = new TcpClient();

            newClient.Connect(ep);
            using (NetworkStream newStream = newClient.GetStream())
                using (BinaryWriter newWriter = new BinaryWriter(newStream))
                    using (BinaryReader newReader = new BinaryReader(newStream))
                    {
                        // write and erad a message
                        newWriter.Write(message);
                        string result = newReader.ReadString();
                        statues = Statues.FromJson(result);
                    }
            // close the client
            newClient.Close();
        }
Exemple #4
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();
        }
Exemple #5
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();
        }