Exemplo n.º 1
0
        /// <summary>
        /// 调用主线程返方法
        /// </summary>
        /// <param name="sender">调用者</param>
        /// <param name="args">参数</param>
        public void Invoke(object sender, object args)
        {
            CMessage message = args as CMessage;

            if (message != null)
            {
                if (message.m_serviceID == ChatService.SERVICEID_CHAT)
                {
                    if (message.m_functionID == ChatService.FUNCTIONID_SENDALL)
                    {
                        ChatData chatData = new ChatData();
                        ChatService.GetChatData(chatData, message.m_body, message.m_bodyLength);
                        CIndicator indicator = CFunctionEx.CreateIndicator2("", chatData, this);
                        indicator.Clear();
                        indicator.Dispose();
                    }
                    else if (message.m_functionID == ChatService.FUNCTIONID_GETHOSTS)
                    {
                        List <ChatHostInfo> datas = new List <ChatHostInfo>();
                        int type = 0;
                        ChatService.GetHostInfos(datas, ref type, message.m_body, message.m_bodyLength);
                        if (type != 2)
                        {
                            int datasSize = datas.Count;
                            for (int i = 0; i < datasSize; i++)
                            {
                                ChatHostInfo   hostInfo    = datas[i];
                                List <GridRow> rows        = m_gridHosts.m_rows;
                                int            rowsSize    = rows.Count;
                                bool           containsRow = false;
                                for (int j = 0; j < rowsSize; j++)
                                {
                                    GridRow oldRow = rows[j];
                                    if (oldRow.GetCell("colP1").GetString() == hostInfo.m_ip && oldRow.GetCell("colP2").GetInt() == hostInfo.m_serverPort)
                                    {
                                        containsRow = true;
                                    }
                                }
                                if (!containsRow)
                                {
                                    if (hostInfo.m_type == 1)
                                    {
                                        String      key = hostInfo.m_ip + ":" + hostInfo.m_serverPort;
                                        ChatService newServerService = DataCenter.GetClientChatService(key);
                                        if (newServerService == null)
                                        {
                                            newServerService            = new ChatService();
                                            newServerService.ServerIP   = hostInfo.m_ip;
                                            newServerService.ServerPort = hostInfo.m_serverPort;
                                            newServerService.ToServer   = true;
                                            BaseService.AddService(newServerService);
                                            DataCenter.AddClientChatService(key, newServerService);
                                        }
                                    }
                                    else
                                    {
                                        GridRow row = new GridRow();
                                        m_gridHosts.AddRow(row);
                                        row.AddCell("colP1", new GridStringCell(hostInfo.m_ip));
                                        row.AddCell("colP2", new GridIntCell(hostInfo.m_serverPort));
                                        if (hostInfo.m_type == 1)
                                        {
                                            row.AddCell("colP3", new GridStringCell("--"));
                                            row.AddCell("colP4", new GridStringCell("--"));
                                        }
                                        else
                                        {
                                            row.AddCell("colP3", new GridStringCell(hostInfo.m_userID));
                                            row.AddCell("colP4", new GridStringCell(hostInfo.m_userName));
                                        }
                                        row.AddCell("colP5", new GridStringCell(hostInfo.m_type == 1 ? "服务器" : "客户端"));
                                    }
                                }
                            }
                        }
                        else
                        {
                            Dictionary <String, String> removeHosts = new Dictionary <String, String>();
                            foreach (ChatHostInfo hostInfo in datas)
                            {
                                removeHosts[hostInfo.ToString()] = "";
                            }
                            List <GridRow> rows     = m_gridHosts.m_rows;
                            int            rowsSize = rows.Count;
                            if (rowsSize > 0)
                            {
                                for (int i = 0; i < rowsSize; i++)
                                {
                                    GridRow row = rows[i];
                                    String  key = row.GetCell("colP1").GetString() + ":" + row.GetCell("colP2").GetString();
                                    if (removeHosts.ContainsKey(key))
                                    {
                                        m_gridHosts.RemoveRow(row);
                                        i--;
                                        rowsSize--;
                                    }
                                }
                            }
                        }
                        SetHostGridRowVisible();
                    }
                    else if (message.m_functionID == ChatService.FUNCTIONID_SEND)
                    {
                        ChatData chatData = new ChatData();
                        ChatService.GetChatData(chatData, message.m_body, message.m_bodyLength);
                        CIndicator indicator = CFunctionEx.CreateIndicator2("", chatData, this);
                        indicator.Clear();
                        indicator.Dispose();
                    }
                }
            }
            String newStr = args as String;

            if (newStr != null)
            {
                if (newStr == "showchat")
                {
                    FlashWindow(m_mainForm.Handle, true);
                    SetForegroundWindow(m_mainForm.Handle);
                }
                else if (newStr == "shake")
                {
                    m_mainForm.Play();
                }
                else if (newStr.StartsWith("how:"))
                {
                    String  text    = newStr.Substring(4);
                    Barrage barrage = new Barrage();
                    barrage.Text = text;
                    barrage.Mode = 1;
                    m_barrageForm.BarrageDiv.AddBarrage(barrage);
                }
                else
                {
                    TextBoxA txtReceive = GetTextBox("txtReceive");
                    txtReceive.Text += newStr;
                    txtReceive.Invalidate();
                    if (txtReceive.VScrollBar != null && txtReceive.VScrollBar.Visible)
                    {
                        txtReceive.VScrollBar.ScrollToEnd();
                        txtReceive.Update();
                        txtReceive.Invalidate();
                    }
                }
            }
        }