Exemplo n.º 1
0
        void DoWork() //핸들러 쓰레드
        {
            NetworkStream stream = null;

            try
            {
                int cli_num = dictionary_num[client];
                while (true)
                {
                    try
                    {
                        stream = client.GetStream();
                        int        bytes   = stream.Read(readbuffer, 0, readbuffer.Length);
                        CM_Library library = (CM_Library)CM_Library.Deserialize(readbuffer);
                        if (ClientEvent != null)
                        {
                            ClientEvent(library.type, readbuffer, client);
                        }
                        Set_byte(readbuffer);
                    }
                    catch (Exception)
                    {
                    }
                }
            }
            catch (Exception)
            {
            }
        }
Exemplo n.º 2
0
        private void Ready_Request()
        {
            while (true)
            {
                byte[]     bytes  = new byte[1024 * 4];
                Stream     stream = new NetworkStream(client.Client);
                int        len    = stream.Read(bytes, 0, bytes.Length);
                CM_Library Two    = (CM_Library)CM_Library.Deserialize(bytes);
                stream.Flush();
                switch (Two.type)
                {
                case (int)CM_All.클라이언트라벨:
                {
                    Ready ready = new Ready();
                    ready = (Ready)CM_Library.Deserialize(bytes);
                    for (int i = 0; i < ready.nickNameList.Length; i++)
                    {
                        if (ready.nickNameList[i] != null)
                        {
                            this.labels[i].Text = ready.nickNameList[i].ToString();
                        }
                    }
                    break;
                }

                case (int)CM_All.자기번호:
                {
                    Ready_On ready_On = new Ready_On();
                    ready_On = (Ready_On)CM_Library.Deserialize(bytes);
                    for (int i = 0; i < 4; i++)
                    {
                        if (ready_On.On[i] == true)
                        {
                            labels[i].BackColor = Color.Blue;
                        }
                    }
                    break;
                }
                }
            }
        }
Exemplo n.º 3
0
        //-------------스트림(타입에 따른)을 받아오는 쓰레드 핸들러 Request 구현 해야한다네 (메시지, 좌표, 턴, 정답 등)------------------------------//
        private void Request()
        {
            SendMyTurn();
            while (true)
            {
                byte[]     bytes  = new byte[1024 * 4];
                Stream     stream = new NetworkStream(client.Client);
                int        len    = stream.Read(bytes, 0, bytes.Length);
                CM_Library Two    = (CM_Library)CM_Library.Deserialize(bytes);
                stream.Flush();
                switch (Two.type)
                {
                case (int)CM_All.메시지:
                {
                    Library.Message message = (Library.Message)CM_Library.Deserialize(bytes);
                    //메시지 출력 구현해야함
                    this.txt_Mul.Invoke(new MethodInvoker(delegate() {
                            txt_Mul.Text += message.MMS + "\r\n";
                        }));
                    Set_byte(bytes);
                    break;
                }

                case (int)CM_All.좌표:
                {
                    GPoint gPoint = (GPoint)CM_Library.Deserialize(bytes);
                    //좌표찍는거 구현해야함
                    gh = pictureBox1.CreateGraphics();
                    //sel_Color(pen, gPoint.gColor);
                    //pen.Width = gPoint.gWidth;
                    gh.DrawLine(pen, point.X, point.Y, gPoint.X, gPoint.Y);
                    point.X = gPoint.X; point.Y = gPoint.Y;
                    Set_byte(bytes);
                    break;
                }

                case (int)CM_All.문제:
                {
                    Problem problem = (Problem)CM_Library.Deserialize(bytes);
                    //문제받기
                    this.Invoke(new MethodInvoker(delegate()
                        {
                            txt_problem.Text = problem.problem;
                        }));
                    if (this.CM_My.InvokeRequired)
                    {
                        this.CM_My.Invoke(new MethodInvoker(delegate()
                            {
                                CM_My.Text      = "MY TURN";
                                CM_My.BackColor = Color.Lime;
                            }));
                    }
                    else
                    {
                        CM_My.Text      = "MY TURN";
                        CM_My.BackColor = Color.Lime;
                    }
                    Set_byte(bytes);
                    break;
                }

                case (int)CM_All.점수:
                {
                    Upoint upoint = (Upoint)CM_Library.Deserialize(bytes);
                    User_Point[upoint.Nic] = upoint.point;
                    Set_byte(bytes);
                    if (j == 4)
                    {
                        Ranking();
                    }
                    break;
                }

                case (int)CM_All.처음좌표:
                {
                    GPoint gPoint = (GPoint)CM_Library.Deserialize(bytes);
                    //좌표찍는거 구현해야함
                    gh = pictureBox1.CreateGraphics();
                    sel_Color(pen, gPoint.gColor);
                    pen.Width = gPoint.gWidth;
                    point.X   = gPoint.X; point.Y = gPoint.Y;
                    Set_byte(bytes);
                    break;
                }

                case (int)CM_All.그림초기화:
                {
                    pictureBox1.Image = null;
                    Set_byte(bytes);
                    break;
                }
                }
            }
        }
Exemplo n.º 4
0
        public void Request(int Type, byte[] bytes, TcpClient client) //Type에 따라 달라지는 직렬화
        {
            switch (Type)
            {
            case (int)CM_All.메시지:
            {
                Library.Message message = new Library.Message();
                message = (Library.Message)CM_Library.Deserialize(bytes);
                DisplayText(message.MMS);
                //Set_byte(bytes);
                //bytes = CM_Library.Serialize(message);
                //모든 클라이언트에 보내주는 메소드 만들어야함
                for (int i = 0; i < count; i++)
                {
                    Stream stream4 = new NetworkStream(tcpClients[i].Client);
                    stream4.Write(bytes, 0, bytes.Length);
                    stream4.Flush();
                }
                break;
            }

            case (int)CM_All.레디:
            {
                Ready_Off ready_Off = new Ready_Off();
                ready_Off = (Ready_Off)CM_Library.Deserialize(bytes);
                on.On[Number_D[client]] = true;
                on.type = (int)CM_All.자기번호;
                Set_byte(bytes);
                bytes = CM_Library.Serialize(on);
                for (int i = 0; i < count; i++)
                {
                    Stream stream3 = new NetworkStream(tcpClients[i].Client);
                    stream3.Write(bytes, 0, bytes.Length);
                    stream3.Flush();
                }
                break;
            }

            case (int)CM_All.닉네임:
            {
                Login login = new Login();
                login = (Login)CM_Library.Deserialize(bytes);

                //login_D.Add(dictionary, login);
                User_point.Add(login.id, 0);
                Nick.Add(login.id);
                Set_byte(bytes);

                Ready ready = new Ready();
                ready.type = (int)CM_All.클라이언트라벨;
                for (int i = 0; i < count; i++)
                {
                    ready.nickNameList[i] = Nick[i].ToString();
                }
                bytes = CM_Library.Serialize(ready);
                for (int i = 0; i < count; i++)
                {
                    Stream stream2 = new NetworkStream(tcpClients[i].Client);
                    stream2.Write(bytes, 0, bytes.Length);
                    stream2.Flush();
                }
                break;
            }

            case (int)CM_All.좌표:
            {
                //GPoint gPoint = new GPoint();
                //gPoint = (GPoint)CM_Library.Deserialize(bytes);
                for (int i = 0; i < count; i++)
                {
                    if (i != ti_time)
                    {
                        Stream stream1 = new NetworkStream(tcpClients[i].Client);
                        stream1.Write(bytes, 0, bytes.Length);
                        stream1.Flush();
                    }
                }
                break;
            }

            case (int)CM_All.턴:
            {
                ++ti_time;
                if (ti_time == 0)
                {
                    Library.Message mms = new Library.Message();
                    mms.type = (int)CM_All.메시지;
                    mms.MMS  = "게임시작";
                    byte[] buffer = new byte[1024 * 4];
                    buffer = CM_Library.Serialize(mms);
                    foreach (var cl in tcpClients)
                    {
                        Stream stream_2 = new NetworkStream(cl.Client);
                        stream_2.Write(buffer, 0, buffer.Length);
                        stream_2.Flush();
                    }
                }
                Set_byte(bytes);
                Problem problem = new Problem();
                problem.type    = (int)CM_All.문제;
                problem.problem = Allproblem[ti_time].ToString();
                bytes           = CM_Library.Serialize(problem);
                Stream stream0 = new NetworkStream(tcpClients[ti_time].Client);
                stream0.Write(bytes, 0, bytes.Length);
                stream0.Flush();
                break;
            }

            case (int)CM_All.정답:
            {
                /*클라이언트의 답과 제시한 문제의 답과 같을 경우 점수를 주고 마지막 폼4번에서 점수를 출력해준다. <- 만들어야한다*/
                Answer answer = new Answer();
                answer = (Answer)CM_Library.Deserialize(bytes);
                if (Allproblem[ti_time] == answer.txtAnswer)
                {
                    User_point[answer.Nicname] += 10;
                    DisplayText(answer.Nicname + " : " + User_point[answer.Nicname].ToString());
                }
                //모두에게 각자의 점수를 보내준다
                Upoint upoint = new Upoint();
                upoint.Nic   = answer.Nicname;
                upoint.point = User_point[answer.Nicname];
                upoint.type  = (int)CM_All.점수;
                Set_byte(bytes);
                bytes = CM_Library.Serialize(upoint);
                for (int i = 0; i < count; i++)
                {
                    Stream stream_1 = new NetworkStream(tcpClients[i].Client);
                    stream_1.Write(bytes, 0, bytes.Length);
                    stream_1.Flush();
                }
                break;
            }

            case (int)CM_All.처음좌표:
            {
                for (int i = 0; i < count; i++)
                {
                    if (i != ti_time)
                    {
                        Stream stream1 = new NetworkStream(tcpClients[i].Client);
                        stream1.Write(bytes, 0, bytes.Length);
                        stream1.Flush();
                    }
                }
                break;
            }

            case (int)CM_All.그림초기화:
            {
                for (int i = 0; i < count; i++)
                {
                    if (i != ti_time)
                    {
                        Stream stream_2 = new NetworkStream(tcpClients[i].Client);
                        stream_2.Write(bytes, 0, bytes.Length);
                        stream_2.Flush();
                    }
                }
                break;
            }
            }
        }