Beispiel #1
0
        /// <summary>
        /// 发送主机信息
        /// </summary>
        /// <returns></returns>
        public int SendHostInfos(List <int> socketIDs, int type, List <ChatHostInfo> hostInfos)
        {
            int    hostInfosSize = hostInfos.Count;
            Binary bw            = new Binary();

            bw.WriteInt(hostInfosSize);
            bw.WriteInt(type);
            for (int i = 0; i < hostInfosSize; i++)
            {
                ChatHostInfo hostInfo = hostInfos[i];
                bw.WriteString(hostInfo.m_ip);
                bw.WriteInt(hostInfo.m_serverPort);
                bw.WriteInt(hostInfo.m_type);
                bw.WriteString(hostInfo.m_userID);
                bw.WriteString(hostInfo.m_userName);
            }
            byte[]   bytes   = bw.GetBytes();
            CMessage message = new CMessage(GroupID, ServiceID, FUNCTIONID_GETHOSTS, SessionID, DataCenter.ChatRequestID, 0, 0, CompressType, bytes.Length, bytes);

            foreach (int socketID in socketIDs)
            {
                message.m_socketID = socketID;
                Send(message);
            }
            bw.Close();
            return(1);
        }
Beispiel #2
0
 /// <summary>
 /// 客户端连接方法
 /// </summary>
 /// <param name="socketID">连接ID</param>
 /// <param name="localSID">本地连接ID</param>
 /// <param name="ip">IP地址</param>
 public override void OnClientConnect(int socketID, int localSID, string ip)
 {
     base.OnClientConnect(socketID, localSID, ip);
     lock (m_socketIDs)
     {
         if (!m_socketIDs.ContainsKey(socketID))
         {
             m_socketIDs[socketID] = new ChatHostInfo();
             String   strIPPort = ip.Replace("accept:", "");
             String[] strs      = strIPPort.Split(new String[] { ":" }, StringSplitOptions.RemoveEmptyEntries);
             m_socketIDs[socketID].m_ip = strs[0];
         }
     }
 }
Beispiel #3
0
 /// <summary>
 /// 添加
 /// </summary>
 /// <param name="hostInfo">主机信息</param>
 public void AddServerHosts(ChatHostInfo hostInfo)
 {
     lock (m_serverHosts)
     {
         int  serverHostsSize = m_serverHosts.Count;
         bool contains        = false;
         for (int i = 0; i < serverHostsSize; i++)
         {
             ChatHostInfo oldHostInfo = m_serverHosts[i];
             if (oldHostInfo.m_ip == hostInfo.m_ip && oldHostInfo.m_serverPort == hostInfo.m_serverPort)
             {
                 contains = true;
                 break;
             }
         }
         if (!contains)
         {
             m_serverHosts.Add(hostInfo);
         }
     }
 }
Beispiel #4
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);
        }