示例#1
0
        void Listen()
        {
            Thread listenThread = new Thread(() =>
            {
                try
                {
                    SocketData data = (SocketData)sck.ReceiveData();
                    ProcessData(data);
                }
                catch (Exception ex)
                {
                    var a = ex;
                }
            });

            listenThread.IsBackground = true;
            listenThread.Start();
        }
示例#2
0
        void Listen()
        {
            Thread listenThread = new Thread(() =>
            {
                try
                {
                    SocketData data = (SocketData)socket.Receive();

                    ProcessData(data);
                }
                catch
                {
                }
            });

            listenThread.IsBackground = true;
            listenThread.Start();
        }
示例#3
0
        private void ProcessData(SocketData Data)
        {
            switch (Data.Command)
            {
            case (int)SocketCommand.SEND_POINT:
                this.Invoke((MethodInvoker)(() =>
                {
                    pnlBanCo.Enabled = true;
                    newBanCo.OtherPlayerMark(Data.Point);
                }));
                break;

            case (int)SocketCommand.NOTIFY:
                MessageBox.Show(Data.Message);
                break;

            case (int)SocketCommand.NEW_GAME:
                this.Invoke((MethodInvoker)(() =>
                {
                    newBanCo.NewGameLAN();
                }));
                break;

            case (int)SocketCommand.UNDO:
                this.Invoke((MethodInvoker)(() =>
                {
                    newBanCo.UndoMethod();
                }));

                break;

            case (int)SocketCommand.REDO:
                break;

            case (int)SocketCommand.EXIT:
                MessageBox.Show("Người chơi còn lại đã thoát!");
                break;

            default:
                break;
            }
            Listen();
        }
示例#4
0
        private void ProcessData(SocketData data)
        {
            switch (data.Command)
            {
            case (int)SocketCommand.NOTIFY:
            {
                MessageBox.Show(data.Message);
            }
            break;

            case (int)SocketCommand.SEND_POINT:
            {
                panelChessBroad.Enabled = true;
                chessBroad.OtherPlayerAction((Point)data.Point);

                pcbCoolDown.Value = 0;
                timerCoolDown.Start();
            }
            break;

            case (int)SocketCommand.PLAYER_NAME:
            {
                if (data.Message != this.OtherPlayerName)
                {
                    if (sck.isServer && OtherPlayerName == null)
                    {
                        this.statusBar.Text = "client is connected";
                    }
                    this.OtherPlayerName = data.Message;
                    chessBroad.SetPlayerName(txbPlayerName.Text, this.OtherPlayerName);
                    sck.SendData(new SocketData((int)SocketCommand.PLAYER_NAME, txbPlayerName.Text, null));
                }
            }

            break;

            case (int)SocketCommand.NEW_GAME:
            {
                this.Invoke(new control(StartGame));
            }
            break;

            case (int)SocketCommand.READY:
            {
                this.ready = !this.ready;

                this.statusBar.Text = "client is ready";
            }
            break;

            case (int)SocketCommand.FIRSTPLAY:
            {
                if (data.Message == "2")
                {
                    this.btnClientFirst.Checked = true;
                }
                else if (data.Message == "1")
                {
                    this.btnServerFirst.Checked = true;
                }
            }
            break;

            case (int)SocketCommand.QUIT:
            {
                this.otherPlayQuit = true;
                this.Invoke(new control(ResetChessBroad));
            }
            break;

            case (int)SocketCommand.PAUSE_GAME:
            {
                this.Invoke(new control(Pause));
            }
            break;

            case (int)SocketCommand.UNPAUSE_GAME:
            {
                this.Invoke(new control(UnPause));
            }
            break;

            case (int)SocketCommand.CHAT:
            {
                this.Invoke(new chat(UpdateChat), new object[] { data.Message });
            }
            break;


            default:

                break;
            }
            Listen();
        }
示例#5
0
        private void Connect()
        {
            if (sck != null)
            {
                sck.Close();
            }
            sck                 = new SocketMangaer();
            sck.IP              = txbIP.Text;
            sck.PORT            = (int)txtPort.Value;
            this.statusBar.Text = "processing....";
            Thread thread = new Thread(() =>
            {
                btnLan.Enabled       = false;
                btnDiconnect.Enabled = true;
                if (!sck.ConnectServer())
                {
                    bool kq = true;
                    sck.CreateServer(out kq);

                    if (kq == true)
                    {
                        if (!otherPlayQuit)
                        {
                            this.statusBar.Text = "you are server";
                        }
                        else
                        {
                            this.statusBar.Text = "other player is quit ";
                        }
                        this.FirstPlay = true;
                        otherPlayQuit  = false;


                        Thread listenThread = new Thread(() =>
                        {
                            while (true)
                            {
                                try
                                {
                                    SocketData data = (SocketData)sck.ReceiveData();
                                    ProcessData(data);

                                    if (btnServerFirst.Checked)
                                    {
                                        sck.SendData(new SocketData((int)SocketCommand.FIRSTPLAY, "1", null));

                                        this.FirstPlay = true;
                                    }
                                    else
                                    {
                                        sck.SendData(new SocketData((int)SocketCommand.FIRSTPLAY, "2", null));

                                        this.FirstPlay = false;
                                    }
                                    break;
                                }
                                catch
                                {
                                }
                            }
                        });
                        listenThread.IsBackground = true;
                        listenThread.Start();
                    }
                    else
                    {
                        this.statusBar.Text = "cant connect pls check your port";
                    }
                }
                else
                {
                    this.Invoke(new control(clientUI));
                    Listen();

                    sck.SendData(new SocketData((int)SocketCommand.PLAYER_NAME, txbPlayerName.Text, null));
                }
            });

            thread.IsBackground = true;
            thread.Start();
        }