Пример #1
0
 public void Abort(string gameName)
 {
     SendAndRecieve.Send(new StreamWriter(client.GetStream()), "close " + gameName);
 }
Пример #2
0
        /// <summary>
        /// Connects the client.
        /// </summary>
        public void ConnectClient()
        {
            bool isConected = true;

            while (isConected)
            {
                // wait for a message
                while (message.CompareTo("") == 0)
                {
                }

                Task task = new Task(() =>
                {
                    connectionAlive  = true;
                    TcpClient client = new TcpClient();
                    IPEndPoint ep    = new IPEndPoint(IPAddress.Parse(ServerIP), ServerPort);

                    // comnect:
                    try
                    {
                        client.Connect(ep);
                    }
                    catch (SocketException e)
                    {
                        connection.Invoke(this, new Recive("connection", "can't find server"));
                        isConected = false;
                        return;
                    }

                    using (NetworkStream stream = client.GetStream())
                        using (StreamReader reader = new StreamReader(stream))
                            using (StreamWriter writer = new StreamWriter(stream))
                            {
                                while (connectionAlive)
                                {
                                    string[] arr      = message.Split(' ');
                                    string commandKey = arr[0];

                                    SendAndRecieve.Send(writer, message);

                                    message = "";

                                    // check whats next (close connection or continue)
                                    if (commands.ContainsKey(commandKey) || updateCommands.ContainsKey(commandKey))
                                    {
                                        System.EventHandler <Recive> temp          = (commands.ContainsKey(commandKey)) ? reciveFromServer : updateFromServer;
                                        Dictionary <string, ICommand> tempCommands = (commands.ContainsKey(commandKey)) ? commands : updateCommands;

                                        string[] arguments      = arr.Skip(1).ToArray();
                                        command                 = tempCommands[commandKey];
                                        RecieveInfo recieveInfo = command.Execute(arguments, this, client);
                                        connectionAlive         = recieveInfo.connectionAlive;

                                        Task viewTask = new Task(() =>
                                        {
                                            if (!recieveInfo.typeCommand.Equals("close"))
                                            {
                                                temp.Invoke(this, new Recive(recieveInfo.typeCommand, recieveInfo.result));
                                            }
                                        });
                                        viewTask.Start();
                                    }
                                    if (connectionAlive)
                                    {
                                        // NEED other input
                                        while (message.CompareTo("") == 0)
                                        {
                                        }
                                    }
                                }
                            }
                    client.Close();
                    message = "";
                });
                task.Start();
                task.Wait();
            }
        }