Public Group

# problem with a timer loop

This topic is 4053 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Problem - servers are getting spammed with messages and causes servers to crash. Solution - Make it so you can only type 3 messages then have to wait 5 seconds to type again. Whats been done - I put in a loop for 5 seconds and I put in a counter to count to 3 to start the timer. I thought that I would reset the counter in the timer to allow it to start over. So here is my problem. The code works for 3 messages then crashes. If I just use the timer it works fine but you can only do 1 message every 5 seconds. Can someone look at my code at let me know where I went wrong. It is coded in c++. Everything is int to zero at the top of the file (not shown). Thank you.
if (maxTeamMessageLimit < 3)//number of messages until timer kicks in
{
messagetosend = new game_PlayerMessage(team_observers, msg, player);

++ maxTeamMessageLimit;
}
else if  ((teamCurrentTime - TeamMessageLastTime) >= 5000 )//1000 = 1 second
{
maxTeamMessageLimit = 0;

TeamMessageLastTime = teamCurrentTime;
}
}



##### Share on other sites
The logic of your algorithm is a bit fuzzy. Do you mean that every time I send 3 messages (even with 20 minutes between them) I cannot send a fourth for 5 seconds? Or is the condition something else?

##### Share on other sites
yes that is correct. i don't care how much time is used as long as there is a 5 second pause after 3 messages. I would not have a problem having it reset the counter after every 5 seconds but that seems like a lot more coding. The over all goal is to stop people from using .cfg scripts to spam and crash the servers. With a 5 second pause the server can catch up and avoid crashing.

##### Share on other sites

static int messagesSinceLastPause = 0;static int startAgainTime = time();if (startAgainTime < time()) {  SendMessage();  if (++messagesSinceLastPause == 3)  {    messagesSinceLastPause = 0;    startAgainTime = time() + 5000;  }}else{  RejectMessage();}

##### Share on other sites
Why 3 messages and 5 seconds? Perhaps it'll still be too heavy, and you'll need to change those hard-coded variabeles and recompile the game. Why not create variabeles for these, so you can tweak them at run-time, and read them from a config file if you want to? :)

It looks like you're checking if 5 seconds have passed since the first message, not since the 3d message. When there's more than 5 seconds between the first and third message, the delay is over when the 3d message is sent, so you'll never get in that else if part.
So, where and when does it crash? Does it crash when you've sent 3 messages, and send the 4th before 5 seconds have passed? Or also when that 4th message is passed after the 5 seconds are over? And does it really crash or are messages simply ignored?

##### Share on other sites
Quote:
 And does it really crash or are messages simply ignored?

^ What he said.

At the very least it looks like it's going to ignore the first attempt to send a message after you've sent three.

Your else branch doesn't send a message - it merely resets the counter which appears to mean that the next message you try to send will work. And so on. So you'll send three messages then a fourth one will get lost, even if enough time has lapsed, then it'll work again.

[ edit ] Sorry, I think I was assuming that your code was only called if a message was being processed. On reflection, it's probably always running in a loop so the above problem wouldn't exist. [ /edit ]

Also, using your code to reset the counter every five seconds seems to be simple. I think this should do just that (and fix the possibly skipped fourth message issue):

if  ((teamCurrentTime - TeamMessageLastTime) >= 5000 )//1000 = 1 second{   maxTeamMessageLimit = 0;}if (maxTeamMessageLimit < 3)//number of messages until timer kicks in{   messagetosend = new game_PlayerMessage(team_observers, msg, player);   if ( maxTeamMessageLimit == 0 ) TeamMessageLastTime = teamCurrentTime; // set current time if first message since reset   ++ maxTeamMessageLimit;}

[Edited by - CaspianB on May 14, 2007 10:44:59 PM]

##### Share on other sites
ToohrVyk - I'll try running it and see what happens.

Captain P - the boss said 3 for 5. I thought that it may not be enough. If one person runs 2 copies and then spams the server alerting the 5 seconds between each version then he could take down the server. The other issue is we do not want to have problems for the normal gamer not being able to talk. We are not exporting to a .cfg because we do not want the server admins to have a problem with this or find a hack to edit their own .cfg file and override ours. Also we now have a nice autoupdater so if it isn't enough the boss will send the word and I will have the update out within about a min or two later.

Currently the hacked .cfg file sends out the messages as fast as possible and I would say they are hitting in the micro-second range. So hopefully if we bring it up to 5 seconds, it won't be a problem.

##### Share on other sites
Oops I forgot to add this in. Yes it really crashes the game. I get the XP error box with debug, send to M\$ or don't send. Then it closes the server back to the desktop.

##### Share on other sites
Well I took care of the crash problem. Turns out I encapsulated the wrong file. I got the right one now.

I used the code from CaspianB only cause it already had everything in place. Now my 4th message and beyond get ignored. I never get another message in even after 5 seconds.

##### Share on other sites
Well I fixed everything. It turns out I had my getTickCount in the wrong spot. Everything works great now. Thanks guys. We also decide to go to 3 messages during a 10 second period. We think that will be good to get us by.

• 16
• 11
• 11
• 9
• 49
• ### Forum Statistics

• Total Topics
631394
• Total Posts
2999749
×