Throttle datagrams as well as streamed messages.

git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@5974 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Andrzej Kapolka
2009-09-27 02:23:17 +00:00
parent 2d044ec3f5
commit 8cbc3fcf1a
@@ -40,6 +40,7 @@ import java.nio.channels.SocketChannel;
import com.samskivert.util.LoopingThread; import com.samskivert.util.LoopingThread;
import com.samskivert.util.Queue; import com.samskivert.util.Queue;
import com.samskivert.util.StringUtil; import com.samskivert.util.StringUtil;
import com.samskivert.util.Throttle;
import com.threerings.io.ByteBufferInputStream; import com.threerings.io.ByteBufferInputStream;
import com.threerings.io.ByteBufferOutputStream; import com.threerings.io.ByteBufferOutputStream;
@@ -526,6 +527,23 @@ public class BlockingCommunicator extends Communicator
return false; return false;
} }
/**
* Throttles an outgoing message operation in a thread-safe manner.
*/
protected void throttleOutgoingMessage ()
{
Throttle throttle = _client.getOutgoingMessageThrottle();
synchronized(throttle) {
while (throttle.throttleOp()) {
try {
Thread.sleep(2);
} catch (InterruptedException ie) {
// no problem
}
}
}
}
/** /**
* The reader encapsulates the authentication and message reading process. It calls back to the * The reader encapsulates the authentication and message reading process. It calls back to the
* {@link Communicator} class to do things, but the general flow of the reader thread is * {@link Communicator} class to do things, but the general flow of the reader thread is
@@ -681,13 +699,7 @@ public class BlockingCommunicator extends Communicator
} }
// make sure we're not exceeding our outgoing throttle rate // make sure we're not exceeding our outgoing throttle rate
while (_client.getOutgoingMessageThrottle().throttleOp()) { throttleOutgoingMessage();
try {
Thread.sleep(2);
} catch (InterruptedException ie) {
// no problem
}
}
try { try {
// write the message out the socket // write the message out the socket
@@ -892,6 +904,9 @@ public class BlockingCommunicator extends Communicator
return; return;
} }
// make sure we're not exceeding our outgoing throttle rate
throttleOutgoingMessage();
try { try {
// write the message out the socket // write the message out the socket
sendDatagram(msg); sendDatagram(msg);