Valhalla Legends Forums Archive | C/C++ Programming | Methods for creating a message queue

AuthorMessageTime
warz
Before I begin adding output to bnet, I thought I'd ask how other people have gone about adding a message queue, or anti-flood feature to their client?

I  could store messages in a map I guess. Have the message text associated with an int value. The message with the lowest value would have top priority in the queue. How would you go about determining when to send the messages? A timer sending every 3-5 seconds sounds a little clunky. Is there a method for doing this I should know of?

Thanks
April 4, 2006, 8:24 AM
K
How about using the std::priority_queue?

quick example:
[code]
#include <queue>
#include <string>
#include <iostream>

using namespace std;


struct Message
{
string Text;
int Priority;

Message();

Message(const string& t, int p)
: Text(t), Priority(p) {}

};


class MessageComparer
{
public:
bool operator()(const Message& first, const Message& second)
{
// in this case, lower priorities will end up at the front of the queue.
// reverse this compare to have higher priorities end up first.
return (first.Priority > second.Priority);
}
};

int main(int argc, char* argv[])
{
priority_queue<Message, vector<Message>, MessageComparer> q;

q.push(Message("hello", 3));
q.push(Message("hi there", 2));
q.push(Message("whats up", 2));
q.push(Message("me first!", 1));

while(!q.empty())
{
Message m = q.top();
q.pop();
cout << "[" << m.Priority << "] " << m.Text << endl;
}
}
[/code]

Edit: It should be noted that the order of items with the same priority is undefined, so you may see the two items I listed above with priority "2" in either order.  For thie most part, this shouldn't be an issue, but I bet if you needed to, you could come up with a good workaround that involved something like this:

[code]
struct Message
{
private:
static int instance_count;
public:
string Text;
int Priority;
int Instance;

Message()
{
Instance = ++instance_count;
}

Message(const string& t, int p)
: Text(t), Priority(p)
{
Instance = ++instance_count;
}

};

int Message::instance_count = 0;

class MessageComparer
{
public:
bool operator()(const Message& first, const Message& second)
{
if (first.Priority != second.Priority)
return (first.Priority > second.Priority);
else
return (first.Instance > second.Instance);
}
};
[/code]
April 4, 2006, 8:34 AM
warz
perfect. i love the stl. how do you usually determine when to send messages that are queued? from what i remember, a long time ago, bnet changed their flood limit to something halfway random? is just the safest bet to send like 2, or 3 quick messages then start waiting like 3 or 4 seconds before sending the next ones?

Edit: Just an update, this looks like a pretty good solution. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/using_timer_queues.asp
April 4, 2006, 8:38 AM

Search