// ServerQueue.C -*- C++ -*-
// Copyright (c) 1997, 1998 Etienne BERNARD
-// Copyright (C) 2002,2005 Clinton Ebadi
+// Copyright (C) 2002,2005,2008 Clinton Ebadi
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
#include "config.h"
#endif
-//#include <limits>
#include "ServerQueue.H"
+
+#include <limits>
+
+#include "Bot.H"
+#include "ServerQueueItem.H"
#include "Utils.H"
+#ifdef USESCRIPTS
+#include "BotInterp.H"
+#include "Interp.H"
+#endif
+
+int ServerQueue::max_penalty = 20;
+
ServerQueue::ServerQueue(Socket * s, bool d)
: Queue(s,d), penalty(0)
{
ServerQueue::~ServerQueue()
{
- penalty = INT_MIN;
+ penalty = std::numeric_limits<int>::min ();
flush();
}
void
ServerQueue::addItem(ServerQueueItem *sqi)
{
+ BotLock queue_lock (queue_mutex);
std::list<ServerQueueItem *>::iterator it, it2;
for (it = serverQueue.begin(); it != serverQueue.end(); ++it)
{
- if ((*it)->priority > sqi->priority)
+ if (**it < *sqi)
{
break;
}
bool
ServerQueue::flush()
{
+ // Locking around the entire queue flush prevents another thread
+ // from spamming the queue and preventing lower priority messages
+ // from ever being sent
+ BotLock flush_lock (queue_mutex);
+
// Called every second, we decrement the penalty
if (penalty > 0)
{
penalty--;
}
- while (!serverQueue.empty() && (penalty < 5))
+ while (!serverQueue.empty() && (penalty < max_penalty))
{
- ServerQueueItem * sqi = (*serverQueue.begin());
- penalty += sqi->penalty + sqi->getLine().length()/100 + 1;
+ ServerQueueItem * sqi = serverQueue.front ();
+ penalty += sqi->penalty + sqi->getLine().length()/100;
bool res = sendLine(sqi->getLine());
- serverQueue.erase(serverQueue.begin());
+
+ serverQueue.pop_front ();
delete sqi;
if (!res)
Interp::bot->botInterp->RunHooks (Hook::SEND_ACTION,
MNICK + " " + to +
" " + message,
- scm_listify (Utils::
+ scm_list_n (Utils::
str2scm (MNICK),
Utils::
str2scm (to),
Interp::bot->botInterp->RunHooks (Hook::SEND_CTCP,
MNICK + " " + to + " " +
command + " " + message,
- scm_listify (Utils::
+ scm_list_n (Utils::
str2scm (MNICK),
Utils::
str2scm (to),
Interp::bot->botInterp->RunHooks (Hook::SEND_PUBLIC,
Interp::bot->nickName + " " + dest +
" " + message,
- scm_listify
+ scm_list_n
(Utils::str2scm
(Interp::bot->nickName),
Utils::str2scm (dest),
(Hook::SEND_MESSAGE,
Interp::bot->nickName + " " + dest +
message,
- scm_listify (Utils::str2scm (Interp::bot->nickName),
+ scm_list_n (Utils::str2scm (Interp::bot->nickName),
Utils::str2scm (dest),
Utils::str2scm
(message), SCM_UNDEFINED));