diff --git a/src/java/com/threerings/presents/client/BlockingCommunicator.java b/src/java/com/threerings/presents/client/BlockingCommunicator.java index f29a0e40b..a7da66e72 100644 --- a/src/java/com/threerings/presents/client/BlockingCommunicator.java +++ b/src/java/com/threerings/presents/client/BlockingCommunicator.java @@ -40,6 +40,7 @@ import java.nio.channels.SocketChannel; import com.samskivert.util.LoopingThread; import com.samskivert.util.Queue; import com.samskivert.util.StringUtil; +import com.samskivert.util.Throttle; import com.threerings.io.ByteBufferInputStream; import com.threerings.io.ByteBufferOutputStream; @@ -526,6 +527,23 @@ public class BlockingCommunicator extends Communicator 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 * {@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 - while (_client.getOutgoingMessageThrottle().throttleOp()) { - try { - Thread.sleep(2); - } catch (InterruptedException ie) { - // no problem - } - } + throttleOutgoingMessage(); try { // write the message out the socket @@ -892,6 +904,9 @@ public class BlockingCommunicator extends Communicator return; } + // make sure we're not exceeding our outgoing throttle rate + throttleOutgoingMessage(); + try { // write the message out the socket sendDatagram(msg);