Ejemplo n.º 1
0
        private async void Receive_Message()
        {
            bool flag = false;
            int  Len;

            byte[] Stream_data   = new byte[1024];
            string message       = null;
            string spliting_user = null;

            try
            {
                await Task.Run(() =>
                {
                    while (true)
                    {
                        flag = false;
                        if (N_stream.CanRead && N_stream != null)
                        {
                            Len     = N_stream.Read(Stream_data, 0, Stream_data.Length); //비동기 코드
                            message = Encoding.Default.GetString(Stream_data, 0, Len);
                            if (message.Equals(Packet.Shutdown))
                            {
                                N_stream.Close();
                                client.Close();
                                N_stream = null;
                                client   = null;
                                goto EXIT;
                            }
                            else if (message.Equals(Packet.Approve))
                            {
                                MessageBox.Show("환영합니다.");
                            }
                            else if (message.Equals(Packet.ChangeSignal))//상태변화
                            {
                                MessageBox.Show("Updating FriendsList...");
                                UpdateFriendsList_Click(this, new EventArgs());
                            }
                            else if (message.StartsWith("[&") && message.EndsWith("&]"))
                            {
                                string[] userArray = message.Split('&');//userArray[1]=채팅시작한 얘
                                spliting_user      = null;
                                for (int i = 1; i < userArray.Length - 1; i++)
                                {
                                    spliting_user += userArray[i];
                                    if (!(i == userArray.Length - 2))
                                    {
                                        spliting_user += ",";
                                    }
                                    if (this.id.Equals(userArray[i]))
                                    {
                                        flag = true;
                                    }
                                }
                                if (flag == true)
                                {
                                    if (MessageBox.Show("USER : "******"\n채팅을 시작하시겠습니까?", "채팅알림", MessageBoxButtons.YesNo) == DialogResult.Yes)
                                    {
                                        if (DupleChecker.isOpen)
                                        {
                                            string DataFormating = "^#" + this.id + "*" + userArray[1] + "*" + Packet.ChatResponse_ok + "#^";
                                            byte[] myOpinion     = Encoding.Default.GetBytes(DataFormating);
                                            N_stream.Write(myOpinion, 0, myOpinion.Length);
                                            chatForm.Close();
                                            chatForm = null;
                                            chatForm = new ChatForm(id, spliting_user);//내 아이디랑 채팅하는 사람들의 id
                                            Func <DialogResult> DoChat = new Func <DialogResult>(chatForm.ShowDialog);
                                            process_Chat        = new Task <DialogResult>(DoChat);
                                            DupleChecker.isOpen = true;
                                            process_Chat.Start();
                                        }
                                        else
                                        {
                                            string DataFormating = "^#" + this.id + "*" + userArray[1] + "*" + Packet.ChatResponse_ok + "#^";
                                            byte[] myOpinion     = Encoding.Default.GetBytes(DataFormating);
                                            N_stream.Write(myOpinion, 0, myOpinion.Length);
                                            DupleChecker.isOpen        = true;
                                            chatForm                   = new ChatForm(id, spliting_user);//내 아이디랑 채팅하는 사람들의 id
                                            Func <DialogResult> DoChat = new Func <DialogResult>(chatForm.ShowDialog);
                                            process_Chat               = new Task <DialogResult>(DoChat);
                                            process_Chat.Start();
                                        }
                                    }
                                    else
                                    {//거부의사
                                        string DataFormating = "^#" + this.id + "*" + userArray[1] + "*" + Packet.ChatResponse_no + "#^";
                                        byte[] myOpinion     = Encoding.Default.GetBytes(DataFormating);
                                        N_stream.Write(myOpinion, 0, myOpinion.Length);
                                    }
                                }
                            }
                            else if (message.StartsWith("^#") && message.EndsWith("#^"))
                            {
                                string devideCharp  = message.Split('#')[1];
                                string responser    = devideCharp.Split('*')[0];  //응답한 사람
                                string responsePack = devideCharp.Split('*')[2];
                                for (int i = 0; i < selectedUserArray.Count; i++) //채팅하고자 하는 사람들 중
                                {
                                    if (selectedUserArray[i].Equals(responser) && responsePack.Equals(Packet.ChatResponse_no))
                                    {
                                        selectedUserArray.Remove(responser);
                                    }
                                }

                                count++;
                                if (count == max)
                                {
                                    int j         = 0;
                                    int sum       = 0;
                                    spliting_user = this.id;
                                    for (int i = 1; i < selectedUserArray.Count; i++)
                                    {
                                        spliting_user += "," + selectedUserArray[i];
                                    }
                                    DupleChecker.isOpen = true;
                                    chatForm            = new ChatForm(id, spliting_user, true);//내 아이디랑 채팅하는 사람들의 id
                                    while (j <= 200)
                                    {
                                        sum += j;
                                        j++;
                                    }//딜레이
                                    Func <DialogResult> DoChat       = new Func <DialogResult>(chatForm.ShowDialog);
                                    Task <DialogResult> process_Chat = new Task <DialogResult>(DoChat);
                                    process_Chat.Start();
                                    count = 0;
                                    max   = 0;
                                }
                            }
                        }
                    }
                    EXIT : MessageBox.Show("turn off the HomeForm...");
                    this.Close();
                });
            }
            catch (SocketException errcode)
            {
                MessageBox.Show(errcode.Message);
            }
        }