Exemplo n.º 1
0
        /// <summary>
        /// This function handles the received data. It primarily prints the data into the conversation board.
        /// </summary>
        /// <param name="a_asyncRes"></param>
        // It receives the data from the server.
        private void ReceiveData(IAsyncResult a_asyncRes)
        {
            /// Gets the socket.
            Socket socket   = (Socket)a_asyncRes.AsyncState;
            int    received = socket.EndReceive(a_asyncRes);

            byte[] dataBuf = new byte[received];

            /// Copies the content into the data buffer and then converts it into the string.
            Array.Copy(m_globalBuffer, dataBuf, received);

            string receivedString = Encoding.ASCII.GetString(dataBuf);

            /// Checks what kind of information is received.
            /// If it is $client$, it consists of all the clients who are online at the moment. This is only received
            /// if people join/leave the online status.
            /// Otherwise, it is perceived as receiving conversation message. Then, it handles the received conversation
            /// message accordingly.
            if (receivedString.StartsWith("$clients$"))
            {
                /// This means that user is receiving the list of clients.
                receivedString = receivedString.Substring(9);
                writeToList(receivedString);
            }
            else
            {
                /// Parses the message to check who sent the message.
                string[] splitted = receivedString.Split(new string[] { g_convoLogic }, 2, StringSplitOptions.None);
                string   email    = splitted[0];

                /// If the conversation is co-existing, then update into the existing conversation.
                /// Else, make a new conversation.
                if (m_conversations.ContainsKey(email))
                {
                    m_conversations[email].HandleReceivedData(splitted[1]);
                    if (m_conversations[email].InvokeRequired)
                    {
                        Invoke(new SetCallback(BringConversationToFront), new object[] { email });
                    }
                    else
                    {
                        BringConversationToFront(email);
                    }
                }
                else
                {
                    m_conversations[email] = new SingleChat(m_currentUserEmail, email);
                    m_conversations[email].HandleReceivedData(splitted[1]);
                    m_conversations[email].AssignClient(m_clientSocket);
                    m_conversations[email].ShowDialog();
                }
            }

            /// Begins listening to the socket again.
            m_clientSocket.BeginReceive(m_globalBuffer, 0, m_globalBuffer.Length, SocketFlags.None, new AsyncCallback(ReceiveData), m_clientSocket);
        }
Exemplo n.º 2
0
        /// <summary>
        /// This function is triggered if an online user is double clicked resulting in opening new form to chat.
        /// </summary>
        /// <param name="a_sender">It holds the sender.</param>
        /// <param name="a_event">It holds the event arguments.</param>
        public void ListDoubleClicked(Object a_sender, EventArgs a_event)
        {
            /// Gets the selected user and opens up the conversation.
            string selected = userListView.SelectedItems[0].Text;

            if (!m_conversations.ContainsKey(selected))
            {
                m_conversations[selected] = new SingleChat(m_currentUserEmail, selected);
                m_conversations[selected].AssignClient(m_clientSocket);
                m_conversations[selected].Show();
            }
            else
            {
                m_conversations[selected].BringToFront();
            }
        }