summaryrefslogtreecommitdiff
path: root/src/utils/tokencollector.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils/tokencollector.cpp')
-rw-r--r--src/utils/tokencollector.cpp126
1 files changed, 126 insertions, 0 deletions
diff --git a/src/utils/tokencollector.cpp b/src/utils/tokencollector.cpp
new file mode 100644
index 00000000..304caf8e
--- /dev/null
+++ b/src/utils/tokencollector.cpp
@@ -0,0 +1,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.
+}