Esempio n. 1
0
 /// <summary>
 /// 主机处理消息
 /// </summary>
 void HandleHostDataMsg()
 {
     TcpHost.SocketAccept[] clients = host.GetClientList();
     for (int i = 0; i < clients.Length; i++)
     {
         TcpHost.SocketAccept client = clients[i];
         if (client != null)
         {
             while (client.recvQueue.Count > 0)
             {
                 byte[] data = client.recvQueue.Dequeue();
                 if (data != null && data.Length > 0 && data[0] == Connection.PACKER_HEAD)
                 {
                     int    offset = Connection.PACKER_OFFSET;
                     byte[] proto  = data.Length == offset?null:SerializeUtil.GetContextData(data, offset, data.Length - offset);
                     ParseMsg(client, data[1], proto, data);
                 }
             }
             //心跳
             float heartbeatInterval = Time.time - client.heartbeatTime;
             if (heartbeatInterval > Connection.HEART_BEAT_TIME)
             {
                 if (client.heartbeatStatus > 0)
                 {
                     Debug.LogWarning(client.id + "心跳超时");
                     host.RemoveClient(client.id);
                 }
                 else
                 {
                     SendMsg(client.id, ProtoIDCfg.HEARTBEAT, null);
                     client.heartbeatStatus++;
                 }
                 client.heartbeatTime = Time.time;
             }
         }
     }
 }