当前位置: 首页>>代码示例>>C++>>正文


C++ NetworkString::gui32方法代码示例

本文整理汇总了C++中NetworkString::gui32方法的典型用法代码示例。如果您正苦于以下问题:C++ NetworkString::gui32方法的具体用法?C++ NetworkString::gui32怎么用?C++ NetworkString::gui32使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在NetworkString的用法示例。


在下文中一共展示了NetworkString::gui32方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: notifyEventAsynchronous

bool StartGameProtocol::notifyEventAsynchronous(Event* event)
{
    NetworkString data = event->data();
    if (data.size() < 5)
    {
        Log::error("StartGameProtocol", "Too short message.");
        return true;
    }
    uint32_t token = data.gui32();
    uint8_t ready = data.gui8(4);
    STKPeer* peer = (*(event->peer));
    if (peer->getClientServerToken() != token)
    {
        Log::error("StartGameProtocol", "Bad token received.");
        return true;
    }
    if (m_listener->isServer() && ready) // on server, player is ready
    {
        Log::info("StartGameProtocol", "One of the players is ready.");
        m_player_states[peer->getPlayerProfile()] = READY;
        m_ready_count++;
        if (m_ready_count == m_game_setup->getPlayerCount())
        {
            // everybody ready, synchronize
            SynchronizationProtocol* protocol = static_cast<SynchronizationProtocol*>(m_listener->getProtocol(PROTOCOL_SYNCHRONIZATION));
            if (protocol)
            {
                protocol->startCountdown(5000); // 5 seconds countdown
                Log::info("StartGameProtocol", "All players ready, starting countdown.");
                m_ready = true;
                return true;
            }
            else
                Log::error("StartGameProtocol", "The Synchronization protocol hasn't been started.");
        }
    }
    else // on the client, we shouldn't even receive messages.
    {
        Log::error("StartGameProtocol", "Received a message with bad format.");
    }
    return true;
}
开发者ID:Shreesha-S,项目名称:stk-code,代码行数:42,代码来源:start_game_protocol.cpp

示例2: notifyEventAsynchronous

bool SynchronizationProtocol::notifyEventAsynchronous(Event* event)
{
    if (event->type != EVENT_TYPE_MESSAGE)
        return true;
    NetworkString data = event->data();
    if (data.size() < 10)
    {
        Log::warn("SynchronizationProtocol", "Received a message too short.");
        return true;
    }
    uint8_t talk_id = data.gui8();
    uint32_t token = data.gui32(1);
    uint32_t request = data.gui8(5);
    uint32_t sequence = data.gui32(6);

    std::vector<STKPeer*> peers = NetworkManager::getInstance()->getPeers();
    assert(peers.size() > 0);

    if (m_listener->isServer())
    {
        if (talk_id > peers.size())
        {
            Log::warn("SynchronizationProtocol", "The ID isn't known.");
            return true;
        }
    }

    uint8_t peer_id = 0;
    for (unsigned int i = 0; i < peers.size(); i++)
    {
        if (peers[i]->isSamePeer(*event->peer))
        {
            peer_id = i;
        }
    }
    if (peers[peer_id]->getClientServerToken() != token)
    {
        Log::warn("SynchronizationProtocol", "Bad token from peer %d", talk_id);
        return true;
    }

    if (request)
    {
        NetworkString response;
        response.ai8(data.gui8(talk_id)).ai32(token).ai8(0).ai32(sequence);
        m_listener->sendMessage(this, peers[peer_id], response, false);
        Log::verbose("SynchronizationProtocol", "Answering sequence %u", sequence);
        if (data.size() == 14 && !m_listener->isServer()) // countdown time in the message
        {
            uint32_t time_to_start = data.gui32(10);
            Log::debug("SynchronizationProtocol", "Request to start game in %d.", time_to_start);
            if (!m_countdown_activated)
                startCountdown(time_to_start);
            else
                m_countdown = (double)(time_to_start/1000.0);
        }
        else
            Log::verbose("SynchronizationProtocol", "No countdown for now.");
    }
    else // response
    {
        if (sequence >= m_pings[peer_id].size())
        {
            Log::warn("SynchronizationProtocol", "The sequence# %u isn't known.", sequence);
            return true;
        }
        double current_time = StkTime::getRealTime();
        m_total_diff[peer_id] += current_time - m_pings[peer_id][sequence];
        Log::verbose("SynchronizationProtocol", "InstantPing is %u",
            (unsigned int)((current_time - m_pings[peer_id][sequence])*1000));
        m_successed_pings[peer_id]++;
        m_average_ping[peer_id] = (int)((m_total_diff[peer_id]/m_successed_pings[peer_id])*1000.0);

        Log::debug("SynchronizationProtocol", "Ping is %u", m_average_ping[peer_id]);
    }
    return true;
}
开发者ID:kmangame0,项目名称:stk,代码行数:77,代码来源:synchronization_protocol.cpp


注:本文中的NetworkString::gui32方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。