FileDocCategorySizeDatePackage
UDPConnection.javaAPI DocAzureus 3.0.3.45066Fri Jan 05 13:42:36 GMT 2007com.aelitis.azureus.core.networkmanager.impl.udp

UDPConnection

public class UDPConnection extends Object

Fields Summary
private UDPConnectionSet
set
private int
id
private UDPTransportHelper
transport
private List
read_buffers
private org.gudy.azureus2.core3.util.AESemaphore
read_buffer_sem
private volatile boolean
connected
Constructors Summary
protected UDPConnection(UDPConnectionSet _set, int _id, UDPTransportHelper _transport)

	
	
	
			
							
			 
	
		set			= _set;
		id			= _id;
		transport	= _transport;
	
protected UDPConnection(UDPConnectionSet _set, int _id)

		set			= _set;
		id			= _id;
	
Methods Summary
protected booleancanRead()

		synchronized( read_buffers ){

			return( read_buffers.size() > 0 );
		}
	
protected booleancanWrite()

		return( set.canWrite( this ));
	
protected voidclose(java.lang.String reason)

		if ( transport != null ){
			
			transport.close( reason );
			
		}else{
			
			closeSupport( reason );
		}
	
protected voidcloseSupport(java.lang.String reason)

		connected	= false;
		
		read_buffer_sem.releaseForever();
		
		set.close( this, reason );
	
protected voidfailed(java.lang.Throwable reason)

		if ( transport != null ){
			
			transport.failed( reason );
			
		}else{
			
			failedSupport( reason );
		}
	
protected voidfailedSupport(java.lang.Throwable reason)

		connected	= false;
		
		read_buffer_sem.releaseForever();

		set.failed( this, reason );
	
protected intgetID()

		return( id );
	
protected UDPSelectorgetSelector()

		return( set.getSelector());
	
protected UDPTransportHelpergetTransport()

		return( transport );
	
protected booleanisConnected()

		return( connected );
	
public booleanisIncoming()

		return( transport.isIncoming());
	
protected voidpoll()

		if ( transport != null ){
			
			transport.poll();
		}
	
protected intread(java.nio.ByteBuffer buffer)

		int	total = 0;
		
		synchronized( read_buffers ){

			while( read_buffers.size() > 0 ){
				
				int	rem = buffer.remaining();
				
				if ( rem == 0 ){
					
					break;
				}

				ByteBuffer	b = (ByteBuffer)read_buffers.get(0);
								
				int	old_limit = b.limit();
				
				if ( b.remaining() > rem ){
					
					b.limit( b.position() + rem );
				}
				
				buffer.put( b );
				
				b.limit( old_limit );
				
				total += rem - buffer.remaining();
				
				if ( b.hasRemaining()){
					
					break;
					
				}else{
					
					read_buffers.remove(0);
					
					read_buffer_sem.release();
				}
			}
		}
		
		// System.out.println( "Connection(" + getID() + ") - read -> " +total );

		return( total );
	
protected voidreceive(java.nio.ByteBuffer data)

			// packets reach us using 8K space regardless of content - trim this back for small protocol
			// messages to save memory
		
		int	rem = data.remaining();
		
		if ( rem < 256 ){
			
			byte[]	temp = new byte[rem];
			
			data.get( temp );
			
			data = ByteBuffer.wrap( temp );
		}
		
		read_buffer_sem.reserve();
		
		if ( !connected ){
			
			throw( new IOException( "Transport closed" ));
		}
		
		boolean	was_empty = false;
	
		synchronized( read_buffers ){
		
			was_empty = read_buffers.size() == 0;
			
			read_buffers.add( data );
		}
		
		if ( was_empty ){
			
			transport.canRead();
		}
	
protected voidsent()

			// notification that a packet has been sent 
		
		transport.canWrite();
	
protected voidsetID(int _id)

		id	= _id;
	
protected voidsetSecret(byte[] session_secret)

		set.setSecret( this, session_secret );
	
protected voidsetTransport(UDPTransportHelper _transport)

		transport	= _transport;
	
protected intwrite(java.nio.ByteBuffer[] buffers, int offset, int length)

		int	written = set.write( this, buffers, offset, length );
		
		// System.out.println( "Connection(" + getID() + ") - write -> " + written );
		
		return( written );