Make readAvailable() non-recursive.

Thane apparently has a maximum stack depth of 64, and this is quickly getting eaten up by readAvailable() when lots of frames are sent down the tubes simultaneously. (mxmlc doesn't seem to optimize tail-recursive functions.)


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@5360 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Tom Conkling
2008-09-05 00:30:19 +00:00
parent d5546a37fb
commit ca8abbaf62
+3 -13
View File
@@ -62,13 +62,9 @@ public class FrameReader extends EventDispatcher
Log.getLog(this).debug( Log.getLog(this).debug(
"socketHasData(" + _socket.bytesAvailable + ")"); "socketHasData(" + _socket.bytesAvailable + ")");
} }
while (_socket.bytesAvailable >= HEADER_SIZE) {
if (_curData == null) { if (_curData == null) {
if (_socket.bytesAvailable < HEADER_SIZE) {
// if there are less bytes available than a header, let's
// just leave them on the socket until we can read the length
// all at once
return;
}
// the length specified is the length of the entire frame, // the length specified is the length of the entire frame,
// including the length of the bytes used to encode the length. // including the length of the bytes used to encode the length.
// (I think this is pretty silly). // (I think this is pretty silly).
@@ -80,8 +76,7 @@ public class FrameReader extends EventDispatcher
} }
// read bytes: either as much as possible or up to the end of the frame // read bytes: either as much as possible or up to the end of the frame
var toRead :int = Math.min(_length - _curData.length, var toRead :int = Math.min(_length - _curData.length, _socket.bytesAvailable);
_socket.bytesAvailable);
// Just in case, if the amount needed is 0, don't do anything! // Just in case, if the amount needed is 0, don't do anything!
// Passing 0 causes it to read *all available bytes*. // Passing 0 causes it to read *all available bytes*.
if (toRead != 0) { if (toRead != 0) {
@@ -96,11 +91,6 @@ public class FrameReader extends EventDispatcher
} }
dispatchEvent(new FrameAvailableEvent(_curData)); dispatchEvent(new FrameAvailableEvent(_curData));
_curData = null; // clear, so we know we need to first read length _curData = null; // clear, so we know we need to first read length
// there's a good chance there's more on the socket, recurse
// now to read it
if (_socket.bytesAvailable >= HEADER_SIZE) {
readAvailable();
} }
} }
} }