/// <summary> /// 发送消息 /// </summary> /// <param name="message">消息</param> /// <returns>状态</returns> public int SendMsg(CMessage message) { SendToListener(message); return(1); }
/// <summary> /// 接收数据 /// </summary> /// <param name="message">消息</param> public override void OnReceive(CMessage message) { base.OnReceive(message); SendToListener(message); }
/// <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); }
/// <summary> /// 发送指标 /// </summary> /// <param name="message">消息</param> /// <param name="layouts">布局列表</param> /// <param name="broadCast">是否广播</param> /// <returns>状态</returns> public int Send(CMessage message, List <IndicatorLayout> layouts, bool broadCast) { Binary bw = new Binary(); int size = layouts.Count; bw.WriteInt(size); for (int i = 0; i < size; i++) { IndicatorLayout layout = layouts[i]; bw.WriteString(layout.m_layoutID); bw.WriteInt(layout.m_userID); bw.WriteString(layout.m_name); bw.WriteString(layout.m_text); bw.WriteInt(layout.m_type); bw.WriteInt(layout.m_orderNum); } byte[] bytes = bw.GetBytes(); message.m_bodyLength = bytes.Length; message.m_body = bytes; int userID = 0; if (size > 0) { userID = layouts[0].m_userID; } else { broadCast = false; } lock (m_sockets) { SocketArray socketArray = null; if (m_sockets.ContainsKey(userID)) { socketArray = m_sockets[userID]; } else { socketArray = new SocketArray(); m_sockets[userID] = socketArray; } if (message.m_sessionID > 0) { socketArray.AddSocket(message.m_socketID); } if (broadCast) { List <int> sockets = new List <int>(); socketArray.GetSocketList(sockets); int socketsSize = sockets.Count; for (int i = 0; i < socketsSize; i++) { message.m_socketID = sockets[i]; int ret = Send(message); if (ret == -1) { socketArray.RemoveSocket(sockets[i]); } } } else { Send(message); } } bw.Close(); return(1); }