Exemple #1
0
        public void Game()
        {
            try
            {
                form.netThread.Suspend();
                time = DateTime.Now;

                string returnData;
                while (true)
                {
                    if (!newClient.Connected)
                    {
                        break; //выход
                    }
                    // получение ответа от сервера
                    returnData = reader.ReadLine();

                    if (returnData.IndexOf("PAINT+") > -1)
                    {
                        returnData = reader.ReadLine();
                        form.Invoke(new Action <string>((str) => CallBackMy.paintAddHandler(str)), returnData);
                    }
                }
                Disconnect();
            }
            catch (Exception exp) { }
        }
Exemple #2
0
        public void Run()
        {
            try
            {
                newClient = new TcpClient();

                newClient.Connect(ip, PORT);

                form.Invoke(new Action <string>((str) => form.statusCl = str), "Подключен");

                // создание потоков по чтению и записи
                writer = newClient.GetStream();
                reader = new StreamReader(newClient.GetStream());

                //отправка ника
                showMessage(name);

                int num = int.Parse(reader.ReadLine());
                PlayerColor = num - (int)(num / 8) * 8;

                //получение карты
                int    len   = int.Parse(reader.ReadLine());
                char[] sdata = new char[len];
                reader.ReadBlock(sdata, 0, len);
                byte[] data = Encoding.ASCII.GetBytes(sdata);

                BinaryFormatter formatter = new BinaryFormatter();
                MemoryStream    ms        = new MemoryStream(data);
                map = (Map)formatter.Deserialize(ms);

                string returnData;
                while (true)
                {
                    if (!newClient.Connected)
                    {
                        break; //выход
                    }
                    // получение ответа от сервера
                    returnData = reader.ReadLine();

                    if (returnData.IndexOf("USER+") > -1)
                    {
                        returnData = reader.ReadLine();
                        form.Invoke(new Action <string>((str) => form.AddUser(str)), returnData);
                    }
                    if (returnData.IndexOf("USER-") > -1)
                    {
                        returnData = reader.ReadLine();
                        form.Invoke(new Action <int>((n) => form.DelUser(n)), int.Parse(returnData));
                    }
                    if (returnData.IndexOf("PAINT+") > -1)
                    {
                        returnData = reader.ReadLine();
                        form.Invoke(new Action <string>((str) => CallBackMy.paintAddHandler(str)), returnData);
                    }
                    if (returnData.IndexOf("START_GAME") > -1)
                    {
                        form.Invoke(new Action <int>((n) => form.StartGame()), 0);
                    }
                }
                Disconnect();
            }
            catch (Exception exp)
            {
                try
                {
                    form.Invoke(new Action <string>((str) => form.statusCl = str), "ошибка");
                    form.Invoke(new Action <string>((str) => form.ClearUser()), "");
                    form.netThread.Abort();
                }
                catch (Exception ex) { }
            }
        }