summaryrefslogtreecommitdiff
path: root/src/utils/tokencollector.cpp
blob: 304caf8e2939c8ef55daec06065f19e1de89225e (plain) (blame)
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
 *  The Mana World Server
 *  Copyright 2007 The Mana World Development Team
 *
 *  This file is part of The Mana World.
 *
 *  The Mana World  is free software; you can redistribute  it and/or modify it
 *  under the terms of the GNU General  Public License as published by the Free
 *  Software Foundation; either version 2 of the License, or any later version.
 *
 *  The Mana  World is  distributed in  the hope  that it  will be  useful, but
 *  WITHOUT ANY WARRANTY; without even  the implied warranty of MERCHANTABILITY
 *  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 *  more details.
 *
 *  You should  have received a  copy of the  GNU General Public  License along
 *  with The Mana  World; if not, write to the  Free Software Foundation, Inc.,
 *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 * $Id$
 */

#include "utils/tokencollector.hpp"

/* We are optimistic and we assume that clients connect as soon as possible.
   It means that pending data are mainly outdated ones, except for the newer
   ones. So we search the lists backward (from newer to older) for already
   inserted data. On the contrary, data are removed when there was a network
   failure, especially a timeout. So we search the lists forward when removing
   data, in order to start from the older ones. */

void TokenCollectorBase::insertClient(std::string const &token, intptr_t data)
{
    for (std::list<Item>::reverse_iterator it = mPendingConnects.rbegin(),
         it_end = mPendingConnects.rend(); it != it_end; ++it)
    {
        if (it->token == token)
        {
            foundMatch(data, it->data);
            mPendingConnects.erase(--it.base());
            return;
        }
    }

    time_t current = time(NULL);

    Item item;
    item.token = token;
    item.data = data;
    item.timeStamp = current;
    mPendingClients.push_back(item);

    removeOutdated(current);
}

void TokenCollectorBase::insertConnect(std::string const &token, intptr_t data)
{
    for (std::list<Item>::reverse_iterator it = mPendingClients.rbegin(),
         it_end = mPendingClients.rend(); it != it_end; ++it)
    {
        if (it->token == token)
        {
            foundMatch(it->data, data);
            mPendingClients.erase(--it.base());
            return;
        }
    }

    time_t current = time(NULL);

    Item item;
    item.token = token;
    item.data = data;
    item.timeStamp = current;
    mPendingConnects.push_back(item);

    removeOutdated(current);
}

void TokenCollectorBase::removeClient(intptr_t data)
{
    for (std::list<Item>::iterator it = mPendingClients.begin(),
         it_end = mPendingClients.end(); it != it_end; ++it)
    {
        if (it->data == data)
        {
            mPendingClients.erase(it);
            return;
        }
    }
}

void TokenCollectorBase::removeOutdated(time_t current)
{
    // Timeout happens after 30 seconds. Much longer may actually pass, though.
    time_t threshold = current - 30;
    if (threshold < mLastCheck) return;

    std::list<Item>::iterator it;
    
    it = mPendingConnects.begin();
    while (it != mPendingConnects.end() && it->timeStamp < threshold)
    {
        removedConnect(it->data);
        it = mPendingConnects.erase(it);
    }

    it = mPendingClients.begin();
    while (it != mPendingClients.end() && it->timeStamp < threshold)
    {
        removedClient(it->data);
        it = mPendingClients.erase(it);
    }

    mLastCheck = current;
}

TokenCollectorBase::TokenCollectorBase():
    mLastCheck(time(NULL))
{
}

TokenCollectorBase::~TokenCollectorBase()
{
    // Not declared inline, as the list destructors are not trivial.
}