[Spread-cvs] cvs commit: spread/javalib SpreadConnection.java

jonathan at spread.org jonathan at spread.org
Sat Oct 2 21:24:33 EDT 2004


jonathan    04/10/02 21:24:33

  Modified:    javalib  SpreadConnection.java
  Log:
  Probable fix for spurious failed connections in Java clients when multiple
  connections are initiated from same process. Bug report by Brian Moseley,
  idea of how to fix by Ryan Caudy, patch by Jonathan Stanton.
  
  Note this has not been verified to completely fix the reported problem,
  but definitely fixes a bug with reading.
  
  Revision  Changes    Path
  1.8       +35 -33    spread/javalib/SpreadConnection.java
  
  Index: SpreadConnection.java
  ===================================================================
  RCS file: /storage/cvsroot/spread/javalib/SpreadConnection.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- SpreadConnection.java	5 Mar 2004 00:37:12 -0000	1.7
  +++ SpreadConnection.java	3 Oct 2004 01:24:33 -0000	1.8
  @@ -290,6 +290,34 @@
   		
   		return ((i0 << 24) | (i1 << 16) | (i2 << 8) | (i3));
   	}	
  +
  +    // Reads from inputsocket until all bytes read or exception raised
  +    //////////////////////////////////////////////////////////////////
  +    private void readBytesFromSocket(byte buffer[], String bufferTypeString) throws SpreadException
  +    {
  +	int byteIndex;
  +	int rcode;
  +	try
  +	{
  +	    for(byteIndex = 0 ; byteIndex < buffer.length ; byteIndex += rcode)
  +	    {
  +		rcode = socketInput.read(buffer, byteIndex, buffer.length - byteIndex);
  +		if(rcode == -1)
  +		{
  +		    throw new SpreadException("Connection closed while reading " + bufferTypeString);
  +		}
  +	    }
  +	}
  +	catch(InterruptedIOException e) {
  +	    throw new SpreadException("readBytesFromSocket(): InterruptedIOException " + e);
  +	}
  +	catch(IOException e)
  +	{
  +	    throw new SpreadException("readBytesFromSocket(): read() " + e);
  +	}
  +
  +    }
  +
   	
   	// Gets a string from an array of bytes.
   	////////////////////////////////////////
  @@ -454,24 +482,13 @@
   		// Read the name.
   		/////////////////
   		byte buffer[] = new byte[len];
  -		int numRead;
  -		try
  -		{
  -			numRead = socketInput.read(buffer);
  -		}
  -		catch(IOException e)
  -		{
  -			throw new SpreadException("read(): " + e);
  -		}
  -		
  +		readBytesFromSocket(buffer, "authname");
   		// System.out.println("readAuthMethods: string is " + new String(buffer));
   
  -		// Check for not enough data.
  -		/////////////////////////////
  -		if(numRead != len)
  -		{
  -			throw new SpreadException("Connection clsoed during connect attempt to read authnames");
  -		}
  +		//		if(numRead != len)
  +		//{
  +		//	throw new SpreadException("Connection closed during connect attempt to read authnames");
  +		//}
   		
   		// for now we ignore the list.
   		//////////////////////////////
  @@ -658,23 +675,8 @@
   		// Read the name.
   		/////////////////
   		byte buffer[] = new byte[len];
  -		int numRead;
  -		try
  -		{
  -			numRead = socketInput.read(buffer);
  -		}
  -		catch(IOException e)
  -		{
  -			throw new SpreadException("read(): " + e);
  -		}
  -		
  -		// Check for not enough data.
  -		/////////////////////////////
  -		if(numRead != len)
  -		{
  -			throw new SpreadException("Connection clsoed during connect attempt");
  -		}
  -		
  +		readBytesFromSocket(buffer, "group name");
  +
   		// Store the group.
   		///////////////////
   		group = new SpreadGroup(this, new String(buffer));
  
  
  




More information about the Spread-cvs mailing list