summaryrefslogtreecommitdiffstats
blob: 8e9e7f379a26835e8524dbb30c4088960e2e6c1c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*
 * Copyright (c) 2010-2016 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

#include <Swiften/StreamManagement/StanzaAckRequester.h>

#include <iostream>

#include <boost/numeric/conversion/cast.hpp>

#include <Swiften/Elements/Message.h>

namespace Swift {

static const unsigned int MAX_HANDLED_STANZA_COUNT = boost::numeric_cast<unsigned int>((1ULL<<32) - 1);

StanzaAckRequester::StanzaAckRequester() : lastHandledStanzasCount(0) {

}

void StanzaAckRequester::handleStanzaSent(std::shared_ptr<Stanza> stanza) {
    unackedStanzas.push_back(stanza);
    if (std::dynamic_pointer_cast<Message>(stanza)) {
        onRequestAck();
    }
}

void StanzaAckRequester::handleAckReceived(unsigned int handledStanzasCount) {
    unsigned int i = lastHandledStanzasCount;
    while (i != handledStanzasCount) {
        if (unackedStanzas.empty()) {
            std::cerr << "Warning: Server acked more stanzas than we sent" << std::endl;
            break;
        }
        std::shared_ptr<Stanza> ackedStanza = unackedStanzas.front();
        unackedStanzas.pop_front();
        onStanzaAcked(ackedStanza);
        i = (i == MAX_HANDLED_STANZA_COUNT ? 0 : i + 1);
    }
    lastHandledStanzasCount = handledStanzasCount;
}

}