コード例 #1
0
ファイル: ChatService.cs プロジェクト: stonezhu870/iteam
 /// <summary>
 /// 接收消息
 /// </summary>
 /// <param name="message">消息</param>
 public override void OnReceive(CMessage message)
 {
     base.OnReceive(message);
     if (DataCenter.IsFull && message.m_functionID == FUNCTIONID_SENDALL)
     {
         DataCenter.ServerChatService.SendAll(message);
     }
     if (message.m_functionID == 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];
                 //全节点
                 if (hostInfo.m_type == 1)
                 {
                     if (hostInfo.m_ip != "127.0.0.1")
                     {
                         OwLibSV.ChatHostInfo serverHostInfo = new OwLibSV.ChatHostInfo();
                         serverHostInfo.m_ip         = hostInfo.m_ip;
                         serverHostInfo.m_serverPort = hostInfo.m_serverPort;
                         serverHostInfo.m_type       = hostInfo.m_type;
                         DataCenter.ServerChatService.AddServerHosts(serverHostInfo);
                         String newServer = hostInfo.m_ip + ":" + CStr.ConvertIntToStr(hostInfo.m_serverPort);
                         List <ChatHostInfo> hostInfos = new List <ChatHostInfo>();
                         UserCookie          cookie    = new UserCookie();
                         if (DataCenter.UserCookieService.GetCookie("DANDANSERVERS", ref cookie) > 0)
                         {
                             hostInfos = JsonConvert.DeserializeObject <List <ChatHostInfo> >(cookie.m_value);
                         }
                         int  hostInfosSize = hostInfos.Count;
                         bool contains      = false;
                         for (int j = 0; j < hostInfosSize; j++)
                         {
                             ChatHostInfo oldHostInfo = hostInfos[j];
                             String       key         = oldHostInfo.ToString();
                             if (key == newServer)
                             {
                                 contains = true;
                                 break;
                             }
                         }
                         if (!contains)
                         {
                             hostInfos.Add(hostInfo);
                             cookie.m_key   = "DANDANSERVERS";
                             cookie.m_value = JsonConvert.SerializeObject(hostInfos);
                             DataCenter.UserCookieService.AddCookie(cookie);
                         }
                         String            key2            = hostInfo.ToString();
                         OwLib.ChatService findChatService = DataCenter.GetClientChatService(key2);
                         if (findChatService == null)
                         {
                             int socketID = OwLib.BaseService.Connect(hostInfo.m_ip, hostInfo.m_serverPort);
                             if (socketID != -1)
                             {
                                 OwLib.ChatService clientChatService = new OwLib.ChatService();
                                 DataCenter.AddClientChatService(key2, clientChatService);
                                 OwLib.BaseService.AddService(clientChatService);
                                 clientChatService.Connected = true;
                                 clientChatService.ToServer  = type == 1;
                                 //clientChatService.RegisterListener(DataCenter.ChatRequestID, new ListenerMessageCallBack(GintechMessageCallBack));
                                 clientChatService.SocketID = socketID;
                                 clientChatService.Enter();
                             }
                         }
                         else
                         {
                             OwLib.ChatService clientChatService = DataCenter.GetClientChatService(key2);
                             if (!clientChatService.Connected)
                             {
                                 int socketID = OwLib.BaseService.Connect(hostInfo.m_ip, hostInfo.m_serverPort);
                                 if (socketID != -1)
                                 {
                                     clientChatService.Connected = true;
                                     clientChatService.SocketID  = socketID;
                                     clientChatService.Enter();
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     SendToListener(message);
 }
コード例 #2
0
        /// <summary>
        /// 登入
        /// </summary>
        /// <param name="message">消息</param>
        /// <returns>登入</returns>
        public int Enter(CMessage message)
        {
            int    rtnSocketID = message.m_socketID;
            Binary br          = new Binary();

            br.Write(message.m_body, message.m_bodyLength);
            String ip       = "";
            int    port     = br.ReadInt();
            int    type     = br.ReadInt();
            String userID   = br.ReadString();
            String userName = br.ReadString();

            br.Close();
            List <int>          sendSocketIDs = new List <int>();
            List <ChatHostInfo> hostInfos     = new List <ChatHostInfo>();

            lock (m_socketIDs)
            {
                if (m_socketIDs.ContainsKey(rtnSocketID))
                {
                    ip = m_socketIDs[rtnSocketID].m_ip;
                    m_socketIDs[rtnSocketID].m_serverPort = port;
                    m_socketIDs[rtnSocketID].m_type       = type;
                    m_socketIDs[rtnSocketID].m_userID     = userID;
                    m_socketIDs[rtnSocketID].m_userName   = userName;
                    hostInfos.Add(m_socketIDs[message.m_socketID]);
                    foreach (int socketID in m_socketIDs.Keys)
                    {
                        if (socketID != rtnSocketID)
                        {
                            ChatHostInfo gs = m_socketIDs[socketID];
                            if (gs.m_type == 0)
                            {
                                sendSocketIDs.Add(socketID);
                            }
                            else if (gs.m_type == 1)
                            {
                                if (type == 1)
                                {
                                    sendSocketIDs.Add(socketID);
                                }
                            }
                        }
                    }
                }
            }
            int sendSocketIDsSize = sendSocketIDs.Count;

            if (sendSocketIDsSize > 0)
            {
                SendHostInfos(sendSocketIDs, 1, hostInfos);
            }
            Dictionary <String, ChatHostInfo> allHostInfos = new Dictionary <string, ChatHostInfo>();

            lock (m_socketIDs)
            {
                foreach (int sid in m_socketIDs.Keys)
                {
                    if (sid != rtnSocketID)
                    {
                        allHostInfos[m_socketIDs[sid].ToString()] = m_socketIDs[sid];
                    }
                }
            }
            //发送本地IP地址
            if (DataCenter.HostInfo.m_localHost.Length > 0)
            {
                ChatHostInfo localHostInfo = new ChatHostInfo();
                localHostInfo.m_ip                     = DataCenter.HostInfo.m_localHost;
                localHostInfo.m_serverPort             = DataCenter.HostInfo.m_localPort;
                localHostInfo.m_type                   = 1;
                allHostInfos[localHostInfo.ToString()] = localHostInfo;
            }
            lock (m_serverHosts)
            {
                foreach (ChatHostInfo serverHost in m_serverHosts)
                {
                    allHostInfos[serverHost.ToString()] = serverHost;
                }
            }
            List <int> rtnSocketIDs = new List <int>();

            rtnSocketIDs.Add(rtnSocketID);
            List <ChatHostInfo> sendAllHosts = new List <ChatHostInfo>();

            foreach (ChatHostInfo sendHost in allHostInfos.Values)
            {
                sendAllHosts.Add(sendHost);
            }
            SendHostInfos(rtnSocketIDs, 0, sendAllHosts);
            sendAllHosts.Clear();
            rtnSocketIDs.Clear();
            hostInfos.Clear();
            sendSocketIDs.Clear();
            if (DataCenter.IsFull && type == 1)
            {
                if (DataCenter.GetClientChatServiceSize() == 0)
                {
                    int socketID = OwLib.BaseService.Connect(ip, port);
                    if (socketID != -1)
                    {
                        String            key = ip + ":" + CStr.ConvertIntToStr(port);
                        OwLib.ChatService clientChatService = new OwLib.ChatService();
                        DataCenter.AddClientChatService(key, clientChatService);
                        OwLib.BaseService.AddService(clientChatService);
                        clientChatService.ToServer  = true;
                        clientChatService.Connected = true;
                        clientChatService.SocketID  = socketID;
                        clientChatService.Enter();
                    }
                }
            }
            return(0);
        }
コード例 #3
0
ファイル: MainFrame.cs プロジェクト: owchart/iteam
        /// <summary>
        /// 发送消息
        /// </summary>
        private void Send(List <GridRow> rows)
        {
            byte[]       fileBytes   = null;
            String       text        = GetTextBox("txtSend").Text;
            RadioButtonA rbBarrage   = GetRadioButton("rbBarrage");
            RadioButtonA rbText      = GetRadioButton("rbText");
            RadioButtonA rbFile      = GetRadioButton("rbFile");
            RadioButtonA rbAttention = GetRadioButton("rbAttention");
            String       sayText     = text;

            if (rbFile.Checked)
            {
                OpenFileDialog openFileDialog = new OpenFileDialog();
                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    text      = "sendfile('" + new FileInfo(openFileDialog.FileName).Name + "');";
                    fileBytes = File.ReadAllBytes(openFileDialog.FileName);
                    sayText   = text;
                }
                else
                {
                    return;
                }
            }
            else
            {
                if (text == null || text.Trim().Length == 0)
                {
                    MessageBox.Show("请输入你想说的内容!", "提示");
                }
            }
            if (rbBarrage.Checked)
            {
                text = "addbarrage('" + text + "');";
            }
            else if (rbText.Checked)
            {
                text = "addtext('" + text + "');";
            }
            else if (rbAttention.Checked)
            {
                text = "how('" + GetTextBox("txtUserName").Text + "喊:" + text + "');";
            }
            int  rowsSize = rows.Count;
            bool sendAll  = false;

            if (rowsSize > 0)
            {
                for (int i = 0; i < rowsSize; i++)
                {
                    GridRow     thisRow     = rows[i];
                    String      ip          = thisRow.GetCell("colP1").GetString();
                    int         port        = thisRow.GetCell("colP2").GetInt();
                    String      userID      = thisRow.GetCell("colP3").GetString();
                    ChatService chatService = null;
                    String      key         = ip + ":" + CStr.ConvertIntToStr(port);
                    chatService = DataCenter.GetClientChatService(key);
                    if (chatService != null)
                    {
                        if (!chatService.Connected)
                        {
                            int socketID = OwLib.BaseService.Connect(ip, port);
                            if (socketID != -1)
                            {
                                chatService.Connected = true;
                                chatService.SocketID  = socketID;
                                chatService.Enter();
                            }
                            else
                            {
                                sendAll = true;
                            }
                        }
                    }
                    else
                    {
                        int type = thisRow.GetCell("colP5").GetInt();
                        if (type == 1)
                        {
                            continue;
                        }
                        else
                        {
                            int socketID = OwLib.BaseService.Connect(ip, port);
                            if (socketID != -1)
                            {
                                chatService            = new ChatService();
                                chatService.SocketID   = socketID;
                                chatService.ServerIP   = ip;
                                chatService.ServerPort = port;
                                chatService.ToServer   = false;
                                DataCenter.AddClientChatService(key, chatService);
                                BaseService.AddService(chatService);
                            }
                            else
                            {
                                sendAll = true;
                            }
                        }
                    }
                    ChatData chatData = new ChatData();
                    chatData.m_content = text;
                    if (fileBytes != null)
                    {
                        chatData.m_body       = fileBytes;
                        chatData.m_bodyLength = fileBytes.Length;
                    }
                    chatData.m_from = DataCenter.UserName;
                    if (sendAll)
                    {
                        chatData.m_to = userID;
                        DataCenter.SendAll(chatData);
                    }
                    else
                    {
                        chatService.Send(chatData);
                    }
                    if (rbBarrage.Checked)
                    {
                        CIndicator indicator = CFunctionEx.CreateIndicator("", text, this);
                        indicator.Clear();
                        indicator.Dispose();
                    }
                    TextBoxA txtReceive = GetTextBox("txtReceive");
                    txtReceive.Text += "我说:\r\n" + sayText + "\r\n";
                    txtReceive.Invalidate();
                    if (txtReceive.VScrollBar != null && txtReceive.VScrollBar.Visible)
                    {
                        txtReceive.VScrollBar.ScrollToEnd();
                        txtReceive.Update();
                        txtReceive.Invalidate();
                    }
                }
            }
        }
コード例 #4
0
ファイル: MainFrame.cs プロジェクト: owchart/iteam
        /// <summary>
        /// 开始启动服务
        /// </summary>
        public void StartConnect()
        {
            List <ChatHostInfo> hostInfos = new List <ChatHostInfo>();
            UserCookie          cookie    = new UserCookie();

            if (DataCenter.UserCookieService.GetCookie("DANDANSERVERS", ref cookie) > 0)
            {
                hostInfos = JsonConvert.DeserializeObject <List <ChatHostInfo> >(cookie.m_value);
            }
            else
            {
                if (DataCenter.HostInfo.m_defaultHost.Length > 0)
                {
                    ChatHostInfo defaultHostInfo = new ChatHostInfo();
                    defaultHostInfo.m_ip         = DataCenter.HostInfo.m_defaultHost;
                    defaultHostInfo.m_serverPort = DataCenter.HostInfo.m_defaultPort;
                    hostInfos.Add(defaultHostInfo);
                }
            }
            int hostInfosSize = hostInfos.Count;

            if (DataCenter.IsFull && hostInfosSize == 0)
            {
                ChatHostInfo defaultHostInfo = new ChatHostInfo();
                defaultHostInfo.m_ip         = "127.0.0.1";
                defaultHostInfo.m_serverPort = 16666;
                hostInfos.Add(defaultHostInfo);
            }
            if (hostInfosSize > 0)
            {
                Random rd = new Random();
                while (DataCenter.IsAppAlive())
                {
                    ChatHostInfo hostInfo = hostInfos[rd.Next(0, hostInfosSize)];
                    int          socketID = OwLib.BaseService.Connect(hostInfo.m_ip, hostInfo.m_serverPort);
                    if (socketID != -1)
                    {
                        String key = hostInfo.ToString();
                        if (m_mainForm != null)
                        {
                            m_mainForm.SetTitle(key);
                            m_mainForm.BeginInvoke(new EventHandler(m_mainForm.SetTitle));
                        }
                        Console.WriteLine(hostInfo.m_ip);
                        OwLib.ChatService clientChatService = new OwLib.ChatService();
                        DataCenter.AddClientChatService(key, clientChatService);
                        OwLib.BaseService.AddService(clientChatService);
                        clientChatService.ToServer  = true;
                        clientChatService.Connected = true;
                        if (!DataCenter.IsFull)
                        {
                            clientChatService.RegisterListener(DataCenter.ChatRequestID, new ListenerMessageCallBack(ChatMessageCallBack));
                        }
                        clientChatService.SocketID = socketID;
                        clientChatService.Enter();
                        m_isLogining = false;
                        DataCenter.CheckConnects();
                        return;
                    }
                }
            }
        }
コード例 #5
0
ファイル: MainFrame.cs プロジェクト: owchart/iteam
        /// <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();
                    }
                }
            }
        }