|
From: <ian...@us...> - 2008-03-17 20:41:14
|
Revision: 792
http://ogoglio.svn.sourceforge.net/ogoglio/?rev=792&view=rev
Author: iansmith
Date: 2008-03-17 13:41:15 -0700 (Mon, 17 Mar 2008)
Log Message:
-----------
Fixed the read of packets from network streams to read in a loop in case the whole
message doesn't come in one fell swoop.
Modified Paths:
--------------
maven/trunk/ogoglio-common/src/main/java/com/ogoglio/message/plugin/ChunkedStreamNetworkTransporter.java
maven/trunk/ogoglio-common/src/main/java/com/ogoglio/message/plugin/MessageStack.java
maven/trunk/ogoglio-common/src/main/java/com/ogoglio/message/plugin/StreamNetworkTransporter.java
Modified: maven/trunk/ogoglio-common/src/main/java/com/ogoglio/message/plugin/ChunkedStreamNetworkTransporter.java
===================================================================
--- maven/trunk/ogoglio-common/src/main/java/com/ogoglio/message/plugin/ChunkedStreamNetworkTransporter.java 2008-03-14 21:43:02 UTC (rev 791)
+++ maven/trunk/ogoglio-common/src/main/java/com/ogoglio/message/plugin/ChunkedStreamNetworkTransporter.java 2008-03-17 20:41:15 UTC (rev 792)
@@ -110,17 +110,29 @@
// Now allocate a buffer of the proper size and read the data into it,
// blocking if necessary.
byte[] buffer = new byte[messageLength];
+ int totalBytes=0;
if (messageLength > 0) {
- int bytesRead = inputStream.read(buffer);
- if (bytesRead != messageLength) {
- throw new MessagingException("Amount of data read does not match message length");
+ do {
+ int bytesRead = inputStream.read(buffer,totalBytes,messageLength-totalBytes);
+ if (bytesRead<=0) {
+ throw new MessagingException("Unexpected value from read:" + bytesRead);
+ } else {
+ if (bytesRead!=messageLength) {
+ Log.info("Partial read:"+bytesRead+" (total size="+messageLength+")");
+ }
+ totalBytes+=bytesRead;
+ }
+ } while (totalBytes<messageLength);
+ if (totalBytes!= messageLength) { //testing for too big
+ throw new MessagingException("Amount of data read does not match message length:" +
+ totalBytes + " vs "+messageLength);
}
+
+ // Eat the closing \r\n
+ inputStream.read();
+ inputStream.read();
}
- // Eat the closing \r\n
- b = inputStream.read();
- b = inputStream.read();
-
// If the message length is 0, we've been disconnected. Throw a
// NegativeReadValueException.
if (messageLength == 0) {
Modified: maven/trunk/ogoglio-common/src/main/java/com/ogoglio/message/plugin/MessageStack.java
===================================================================
--- maven/trunk/ogoglio-common/src/main/java/com/ogoglio/message/plugin/MessageStack.java 2008-03-14 21:43:02 UTC (rev 791)
+++ maven/trunk/ogoglio-common/src/main/java/com/ogoglio/message/plugin/MessageStack.java 2008-03-17 20:41:15 UTC (rev 792)
@@ -15,6 +15,7 @@
import com.ogoglio.message.Message;
import com.ogoglio.message.proto.Locator;
+import com.ogoglio.util.Log;
import java.io.IOException;
@@ -138,6 +139,7 @@
// Decode the message
return messageEncoder.decodeMessage(encodedMessage);
} catch (MessagingException e) {
+ Log.error("Underlying messaging exception!",e);
// TODO: Widen interface to throw this directly
throw new IOException("MessagingException encountered in readMessage: " + e.toString());
}
Modified: maven/trunk/ogoglio-common/src/main/java/com/ogoglio/message/plugin/StreamNetworkTransporter.java
===================================================================
--- maven/trunk/ogoglio-common/src/main/java/com/ogoglio/message/plugin/StreamNetworkTransporter.java 2008-03-14 21:43:02 UTC (rev 791)
+++ maven/trunk/ogoglio-common/src/main/java/com/ogoglio/message/plugin/StreamNetworkTransporter.java 2008-03-17 20:41:15 UTC (rev 792)
@@ -99,11 +99,24 @@
// Now we know the message length. Allocate an appropriately-sized buffer and
// read the message (again, blocking if necessary).
byte[] buffer = new byte[messageLength];
- int bytesRead = inputStream.read(buffer);
+ int bytesRead=0,newBytes;
+
+ do {
+ newBytes = inputStream.read(buffer,bytesRead,messageLength-bytesRead);
+ bytesRead+=newBytes;
+ if (newBytes<=0){
+ throw new MessagingException("Unexpected return value from inputStream read():"+newBytes);
+ }
+ if (bytesRead!=messageLength) {
+ Log.info("Partial read of stream data:"+newBytes+ " bytes!");
+ }
+ } while (bytesRead<messageLength);
+ // note: this is ALSO a check for too many bytes, so we have kept it
if (bytesRead != messageLength) {
- throw new MessagingException("Incorrect number of bytes read");
+ throw new MessagingException("Incorrect number of bytes read " +
+ bytesRead +" vs expected "+messageLength);
}
-
+
return buffer;
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|