예제 #1
0
파일: FormChat.cs 프로젝트: Nadi0998/ST_KR
        private void SendMessage()
        {
            string caption = "ChatForm Error!";
            // если не нужно отправлять, а просто вывести на экран
            // вдруг приватное сообщение отправлено самому себе
            string user = null;
            string msg  = "";

            if (msgInputField.Text.IndexOf('@') == 0)
            {
                int pos = msgInputField.Text.IndexOf(' ');
                if (pos == -1)
                {
                    string alertMsg = "Вы пытаетесь отправить сообщение, однако ничего не ввели" + Environment.NewLine
                                      + "@NICK MESSAGE";
                    MessageBox.Show(alertMsg, caption);
                    return;
                }
                if (pos < 2)
                {
                    string alertMsg = "Вы пытаетесь отправить сообщение, однако не выбрали получателя" + Environment.NewLine
                                      + "@NICK MESSAGE";
                    MessageBox.Show(alertMsg, caption);
                    return;
                }

                user = msgInputField.Text.Substring(1, pos - 1);
                msg  = msgInputField.Text.Substring(pos + 1);
                if (msg == "")
                {
                    string alertMsg = "Вы пытаетесь отправить сообщение, однако ничего не ввели" + Environment.NewLine
                                      + "@NICK MESSAGE";
                    MessageBox.Show(alertMsg, caption);
                    return;
                }
                if (user.Equals(username))
                {
                    LogBox.AppendText(msgInputField.Text + Environment.NewLine);
                    msgInputField.Text = "";
                    return;
                }
            }
            else
            {
                //TODO: FIX
                LogBox.AppendText(msgInputField.Text + Environment.NewLine);
                msgInputField.Text = "";
                return;
            }
            msgInputField.Text = "";

            DataController.UserMessage newMsg = new DataController.UserMessage(msg, getLogin.login, user);

            this.dataController.WriteQueue.Enqueue(newMsg);
            //if(newMsg.To != getLogin.login)
            //{
            //    AddMessage(newMsg);
            //}
        }
예제 #2
0
파일: FormChat.cs 프로젝트: Nadi0998/ST_KR
 private ListViewItem FormatEnvelope(DataController.UserMessage m)
 {
     string[] items = new string[HEADER_COUNT];
     items[MSG_ID_HEADER]        = m.ID.ToString();
     items[MSG_FROM_HEADER]      = m.From;
     items[MSG_TO_HEADER]        = m.To;
     items[MSG_SHORT_HEADER]     = m.Text.Length > 5 ? m.Text.Substring(0, 5) + "..." : m.Text;
     items[MSG_FULL_HEADER]      = m.Text;
     items[MSG_READ_BOOL_HEADER] = "0";
     return(new ListViewItem(items));
 }
예제 #3
0
파일: FormChat.cs 프로젝트: Nadi0998/ST_KR
 private static string FormatMessage(DataController.UserMessage m)
 {
     return(String.Format("@{0} > {1}: {2}" + Environment.NewLine,
                          m.From, m.To, m.Text));
 }
예제 #4
0
파일: FormChat.cs 프로젝트: Nadi0998/ST_KR
 //todo
 public void AddMessageSelf(DataController.UserMessage msg)
 {
 }
예제 #5
0
파일: FormChat.cs 프로젝트: Nadi0998/ST_KR
 public void AddMessage(DataController.UserMessage msg)
 {
     msgListView.Items.Add(FormatEnvelope(msg));
 }
예제 #6
0
        private List <UserMessage> RecvData(List <byte> data, out int newMessageID)
        {
            if (this.currentState == FrameType.IDPublish)
            {
                string logMsg = String.Format("# Соединение установлено ({0})", this.currentUserName);

                this.currentState = FrameType.Data;
                this.formChat.Invoke((Action <bool>)(delegate(bool enabled)
                {
                    this.formChat.msgInputField.Enabled = enabled;
                    this.formChat.sendButton.Enabled    = enabled;
                    this.formChat.LogBox.Text          += logMsg + Environment.NewLine;
                }), true);
            }

            newMessageID = Utils.GetInt(data);
            int messagesCount = Utils.GetInt(data);

            List <UserMessage> markerMessages = new List <UserMessage>(messagesCount);

            for (int i = 0; i < messagesCount; ++i)
            {
                int    mID       = Utils.GetInt(data);
                int    fromID    = Utils.GetInt(data);
                int    toID      = Utils.GetInt(data);
                int    recvCount = Utils.GetInt(data);
                string text      = Utils.GetString(data);

                markerMessages.Add(new UserMessage(mID, fromID, toID, recvCount, text));
            }

            List <UserMessage> retMessages = new List <UserMessage>(markerMessages.Count);

            foreach (var msg in markerMessages)
            {
                if (msg.FromID == this.currentUserID)
                {
                    System.IO.File.AppendAllText("log.txt", String.Format("I'm {0}, my ID {1}, msg ID {2}, from {3}, to {4}, rcvCnt {5}, Text {6} from this USER" + Environment.NewLine,
                                                                          currentUserName, currentUserID, msg.ID, msg.FromID, msg.ToID, msg.RecvCount, msg.Text));
                    if ((msg.RecvCount & 1) == 1)
                    {
                        sendedMessages.Remove(msg.ID);
                        if ((msg.RecvCount & 0xF0) != 0xF0)
                        {
                            DataController.UserMessage guiMsg;

                            User toUser = usersOnline.Find(x => x.ID == msg.ToID);
                            guiMsg    = new DataController.UserMessage(msg.Text, this.currentUserName, toUser.Name);
                            guiMsg.ID = msg.ID;
                            this.dataController.ReadQueue.Enqueue(guiMsg);
                        }
                        continue;
                    }
                }
                else if (msg.ToID == currentUserID)
                {
                    System.IO.File.AppendAllText("log.txt", String.Format("I'm {0}, my ID {1}, msg ID {2}, from {3}, to {4}, rcvCnt {5}, Text {6} to this USER" + Environment.NewLine,
                                                                          currentUserName, currentUserID, msg.ID, msg.FromID, msg.ToID, msg.RecvCount, msg.Text));
                    if ((msg.RecvCount & 0xF0) == 0xF0)
                    {
                        formChat.SetMessageRead(int.Parse(msg.Text));
                    }
                    else
                    {
                        DataController.UserMessage guiMsg;

                        User fromUser = this.usersOnline.Find(x => x.ID == msg.FromID);
                        //works as shit, searches for current user across all online users
                        User toUser = this.usersOnline.Find(x => x.ID == msg.ToID);
                        if (fromUser != null)
                        {
                            guiMsg    = new DataController.UserMessage(msg.Text, fromUser.Name, toUser.Name);
                            guiMsg.ID = msg.ID;
                            this.dataController.ReadQueue.Enqueue(guiMsg);
                        }
                    }
                    msg.RecvCountInc();
                }
                retMessages.Add(msg);
            }

            return(retMessages);
        }