/// <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); }
/// <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; } } } }