From d7e5d3cf858b36c340de599fa2c9c9eeefc53c37 Mon Sep 17 00:00:00 2001 From: Andrzej Kapolka Date: Wed, 9 Jun 2010 03:27:27 +0000 Subject: [PATCH] Broke out the places where we actually read from/write to the channels into separate methods so that we can override them to do crazy stuff like simulate latency/dropped packets for debugging. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@6072 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- .../presents/client/BlockingCommunicator.java | 52 +++++++++++++++++-- 1 file changed, 48 insertions(+), 4 deletions(-) diff --git a/src/java/com/threerings/presents/client/BlockingCommunicator.java b/src/java/com/threerings/presents/client/BlockingCommunicator.java index d37fbc5ca..7450b4627 100644 --- a/src/java/com/threerings/presents/client/BlockingCommunicator.java +++ b/src/java/com/threerings/presents/client/BlockingCommunicator.java @@ -395,7 +395,7 @@ public class BlockingCommunicator extends Communicator String txt = StringUtil.truncate(String.valueOf(msg), 80, "..."); log.info("Whoa, writin' a big one", "msg", txt, "size", buffer.limit()); } - int wrote = _channel.write(buffer); + int wrote = writeMessage(buffer); if (wrote != buffer.limit()) { log.warning("Aiya! Couldn't write entire message", "msg", msg, "size", buffer.limit(), "wrote", wrote); @@ -412,6 +412,17 @@ public class BlockingCommunicator extends Communicator updateWriteStamp(); } + /** + * Writes the message contained in the supplied buffer. + * + * @return the number of bytes written. + */ + protected int writeMessage (ByteBuffer buf) + throws IOException + { + return _channel.write(buf); + } + /** * Sends a datagram over the datagram socket. */ @@ -444,12 +455,23 @@ public class BlockingCommunicator extends Communicator buf.put(hash, 0, 8).rewind(); // send the datagram - _datagramChannel.write(buf); + writeDatagram(buf); // notify the tracker _client.getMessageTracker().messageSent(true, size, msg); } + /** + * Writes the datagram contained in the supplied buffer. + * + * @return the number of bytes written. + */ + protected int writeDatagram (ByteBuffer buf) + throws IOException + { + return _datagramChannel.write(buf); + } + /** * Reads a new message from the socket (blocking until a message has arrived). */ @@ -460,7 +482,7 @@ public class BlockingCommunicator extends Communicator // of the frame from the network, in which case we simply call it again because we can't do // anything until it has a whole frame; it will throw an exception if it hits EOF or if // something goes awry) - while (!_fin.readFrame(_channel)) { + while (!readFrame()) { // noop! } @@ -479,6 +501,17 @@ public class BlockingCommunicator extends Communicator } } + /** + * Reads a frame from the socket. + * + * @return true if a complete frame is available, false if more needs to be read. + */ + protected boolean readFrame () + throws IOException + { + return _fin.readFrame(_channel); + } + /** * Reads a datagram from the socket (blocking until a datagram has arrived). */ @@ -487,7 +520,7 @@ public class BlockingCommunicator extends Communicator { // clear the buffer and read a datagram _buf.clear(); - int size = _datagramChannel.read(_buf); + int size = readDatagram(_buf); if (size <= 0) { throw new IOException("No datagram available to read."); } @@ -514,6 +547,17 @@ public class BlockingCommunicator extends Communicator } } + /** + * Reads a datagram into the supplied buffer. + * + * @return the number of bytes read. + */ + protected int readDatagram (ByteBuffer buf) + throws IOException + { + return _datagramChannel.read(buf); + } + protected void openChannel (InetAddress host) throws IOException {