Commit f52e9c23 authored by jan.koester's avatar jan.koester
Browse files

test

parent f5b97a82
Loading
Loading
Loading
Loading
+52 −27
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#include <iostream>
#include <algorithm>
#include <cstring>
#include <thread>

#include <netplus/exception.h>

@@ -71,28 +72,49 @@ gameinfo::HldsView::HldsView(const HldsView &view){

void gameinfo::HldsView::refresh(HldsData &info) {
    const std::lock_guard<std::mutex> lock(rlock);

    clientSocket->connect(serverSocket);
    clientSocket->setTimeout(1);

    size_t rcv=0;
    // -------- internal retry loop --------
    auto start = std::chrono::steady_clock::now();
    auto deadline = start + std::chrono::milliseconds(1000);

    auto recvWithRetry = [&](netplus::buffer& buf) -> size_t {
        while (std::chrono::steady_clock::now() < deadline) {
            try {
        netplus::buffer send(A2S_INFO_PAYLOAD_LEN_WITH_CHALLENGE);
        memcpy(send.data.buf, payload_template, A2S_INFO_PAYLOAD_LEN_WITHOUT_CHALLENGE);
                return clientSocket->recvData(buf);
            } catch (netplus::NetException& e) {
                if (e.getErrorType() == netplus::NetException::Note) {
                    // kurz warten und erneut probieren
                    std::this_thread::sleep_for(std::chrono::milliseconds(5));
                    continue;
                }
                throw; // echte Fehler
            }
        }
        netplus::NetException e;
        e[netplus::NetException::Error] << "timeout waiting for HLDS reply";
        throw e;
    };

    try {
        netplus::buffer send(A2S_INFO_PAYLOAD_LEN_WITH_CHALLENGE);
        std::memcpy(send.data.buf, payload_template, A2S_INFO_PAYLOAD_LEN_WITHOUT_CHALLENGE);
        send.size = A2S_INFO_PAYLOAD_LEN_WITHOUT_CHALLENGE;

        clientSocket->sendData(send);

        netplus::buffer hrecv(1400);
        rcv=clientSocket->recvData(hrecv);
        size_t rcv = recvWithRetry(hrecv);

        if(hrecv.data.buf[4] == 0x41){
            memcpy(challange, hrecv.data.buf+5,4);
        if (rcv > 8 && (uint8_t)hrecv.data.buf[4] == 0x41) {
            std::memcpy(challange, hrecv.data.buf + 5, 4);
        }

        AGAIN:
        for (;;) {
            if (challange[0] != 0) {
            memcpy(send.data.buf + A2S_INFO_PAYLOAD_CHALLENGE_OFFSET, &challange, sizeof (challange));
                std::memcpy(send.data.buf + A2S_INFO_PAYLOAD_CHALLENGE_OFFSET,
                            challange, sizeof(challange));
                send.size = A2S_INFO_PAYLOAD_LEN_WITH_CHALLENGE;
            } else {
                send.size = A2S_INFO_PAYLOAD_LEN_WITHOUT_CHALLENGE;
@@ -100,20 +122,23 @@ void gameinfo::HldsView::refresh(HldsData &info){

            clientSocket->sendData(send);

            rcv = recvWithRetry(hrecv);

        rcv=clientSocket->recvData(hrecv);

        if(rcv >4 && hrecv.data.buf[4] == 0x41){
            memcpy(challange, hrecv.data.buf+5,4);
            goto AGAIN;
            if (rcv > 8 && (uint8_t)hrecv.data.buf[4] == 0x41) {
                std::memcpy(challange, hrecv.data.buf + 5, 4);
                continue; // erneut senden
            }

            _parse(info, hrecv.data.buf, rcv);
            return;
        }

    } catch (netplus::NetException &exp) {
        GameInfoException ee;
        ee[GameInfoException::Error] << exp.what();
        throw ee;
    } catch (GameInfoException &exp) {
        throw exp;
        throw;
    } catch (...) {
        GameInfoException ee;
        ee[GameInfoException::Error] << "Gameinfo unknown error!";