⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 incomingsocketchannelmanager.java

📁 基于JXTA开发平台的下载软件开发源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * Created on Jan 18, 2005
 * Created by Alon Rohter
 * Copyright (C) 2004-2005 Aelitis, All Rights Reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 * 
 * AELITIS, SAS au capital de 46,603.30 euros
 * 8 Allee Lenotre, La Grille Royale, 78600 Le Mesnil le Roi, France.
 *
 */

package com.aelitis.azureus.core.networkmanager.impl;

import java.io.IOException;
import java.net.*;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.util.*;

import org.gudy.azureus2.core3.config.*;
import org.gudy.azureus2.core3.logging.*;
import org.gudy.azureus2.core3.util.*;

import com.aelitis.azureus.core.networkmanager.*;


/**
 * Accepts new incoming socket connections and manages routing of them
 * to registered handlers.
 */
public class IncomingSocketChannelManager
	implements VirtualChannelSelector.VirtualSelectorListener
{
  private static final LogIDs LOGID = LogIDs.NWMAN;

  public static final int READ_TIMEOUT		= 10*1000;
  public static final int CONNECT_TIMEOUT	= 60*1000;
  
  private final ArrayList connections = new ArrayList();
  private final AEMonitor connections_mon = new AEMonitor( "IncomingConnectionManager:conns" );
  
  private volatile Map match_buffers_cow = new HashMap();	// copy-on-write
  private final AEMonitor match_buffers_mon = new AEMonitor( "IncomingConnectionManager:match" );
  private int max_match_buffer_size = 0;
  private int max_min_match_buffer_size = 0;
   
  private int listen_port = COConfigurationManager.getIntParameter( "TCP.Listen.Port" );
  private int so_rcvbuf_size = COConfigurationManager.getIntParameter( "network.tcp.socket.SO_RCVBUF" );
  private String bind_address = COConfigurationManager.getStringParameter( "Bind IP" );
  
  private long last_timeout_check_time = SystemTime.getCurrentTime();
  
  private VirtualServerChannelSelector server_selector = null;
  
  protected AEMonitor	this_mon	= new AEMonitor( "IncomingSocketChannelManager" );

    
  
  /**
   * Create manager and begin accepting and routing new connections.
   */
  public IncomingSocketChannelManager() {    
    //allow dynamic port number changes
    COConfigurationManager.addParameterListener( "TCP.Listen.Port", new ParameterListener() {
      public void parameterChanged(String parameterName) {
        int port = COConfigurationManager.getIntParameter( "TCP.Listen.Port" );
        if( port != listen_port ) {
          listen_port = port;
          restart();
        }
      }
    });
    
    //allow dynamic receive buffer size changes
    COConfigurationManager.addParameterListener( "network.tcp.socket.SO_RCVBUF", new ParameterListener() {
      public void parameterChanged(String parameterName) {
        int size = COConfigurationManager.getIntParameter( "network.tcp.socket.SO_RCVBUF" );
        if( size != so_rcvbuf_size ) {
          so_rcvbuf_size = size;
          restart();
        }
      }
    });
    
    //allow dynamic bind address changes
    COConfigurationManager.addListener(
    	new COConfigurationListener()
    	{
    		public void configurationSaved() {
	        String address = COConfigurationManager.getStringParameter( "Bind IP" );
	        if( !address.equals( bind_address ) ) {
	          bind_address = address;
	          restart();
	        }
      }
    });
    
    //start processing
    start();
    
     
    	//run a daemon thread to poll listen port for connectivity
    	//it seems that sometimes under OSX that listen server sockets sometimes stop accepting incoming connections for some unknown reason
    	//this checker tests to make sure the listen socket is still accepting connections, and if not, recreates the socket
    	AEThread checker = new AEThread( "ServerSocketChecker" ){
    		public void runSupport() {
    			try{  Thread.sleep( 60*1000 );  }catch( Throwable t){ t.printStackTrace(); }
    		
    			int fail_count = 0;
    		
    			while( true ) {			
    				if( server_selector != null && server_selector.isRunning() ) { //ensure it's actually running
    					
    					long accept_idle = SystemTime.getCurrentTime() - server_selector.getTimeOfLastAccept();
 
    					if( accept_idle > 10*60*1000 ) {  //the socket server hasn't accepted any new connections in the last 10min
  						
    						//so manually test the listen port for connectivity
    						
    						InetAddress inet_address = server_selector.getBoundToAddress();
        				
      					try{   					
      						if( inet_address == null )  inet_address = InetAddress.getByName( "127.0.0.1" );  //failback
      					
      						Socket sock = new Socket( inet_address, listen_port, inet_address, 0 );

      						sock.close();
      						fail_count = 0;
      					}
      					catch( Throwable t ) {
      						
      						//ok, let's try again without the explicit local bind
      						try {
      							Socket sock = new Socket( InetAddress.getByName( "127.0.0.1" ), listen_port );      							
      							sock.close();
      							fail_count = 0;
      						}
      						catch( Throwable x ) {
      							fail_count++;
        						Debug.out( new Date()+ ": listen port on [" +inet_address+ ": " +listen_port+ "] seems CLOSED [" +fail_count+ "x]" );
        				
        						if( fail_count > 4 ) {
        							String error = t.getMessage() == null ? "<null>" : t.getMessage();
        							String msg = "Listen server socket on [" +inet_address+ ": " +listen_port+ "] does not appear to be accepting inbound connections.\n[" +error+ "]\nAuto-repairing listen service....\n";
        							Logger.log(new LogAlert(LogAlert.UNREPEATABLE, LogAlert.AT_WARNING, msg));
        							restart();
        							fail_count = 0;
        						}
      						}
      					}
    					}
    					else {  //it's recently accepted an inbound connection
    						fail_count = 0;
    					}
    				}
    			
    				try{  Thread.sleep( 60*1000 );  }catch( Throwable t){ t.printStackTrace(); }
    			}
    		}
    	};
    	checker.setDaemon( true );
    	checker.start(); 
    
  }
  
  
  
  /**
   * Get port that the TCP server socket is listening for incoming connections on.
   * @return port number
   */
  public int getTCPListeningPortNumber() {  return listen_port;  }
  
  

  /**
   * Register the given byte sequence matcher to handle matching against new incoming connection
   * initial data; i.e. the first bytes read from a connection must match in order for the given
   * listener to be invoked.
   * @param matcher byte filter sequence
   * @param listener to call upon match
   */
  public void registerMatchBytes( NetworkManager.ByteMatcher matcher, MatchListener listener ) {
    try {  match_buffers_mon.enter();
    
      if( matcher.size() > max_match_buffer_size ) {
        max_match_buffer_size = matcher.size();
      }

      if ( matcher.minSize() > max_min_match_buffer_size ){
    	  max_min_match_buffer_size = matcher.minSize();
      }
      
      Map	new_match_buffers = new HashMap( match_buffers_cow );
      
      new_match_buffers.put( matcher, listener );
      
      match_buffers_cow = new_match_buffers;
    
      byte[]	secret = matcher.getSharedSecret();
      
      if ( secret != null ){
    	  
	     TCPProtocolDecoder.addSecret( secret );
      }
    } finally {  match_buffers_mon.exit();  }
    
  }
  
  
  /**
   * Remove the given byte sequence match from the registration list.
   * @param to_remove byte sequence originally used to register
   */
  public void deregisterMatchBytes( NetworkManager.ByteMatcher to_remove ) {
    try {  match_buffers_mon.enter();
      Map	new_match_buffers = new HashMap( match_buffers_cow );
    
      new_match_buffers.remove( to_remove );
    
      if( to_remove.size() == max_match_buffer_size ) { //recalc longest buffer if necessary
        max_match_buffer_size = 0;
        for( Iterator i = new_match_buffers.keySet().iterator(); i.hasNext(); ) {
          NetworkManager.ByteMatcher bm = (NetworkManager.ByteMatcher)i.next();
          if( bm.size() > max_match_buffer_size ) {
            max_match_buffer_size = bm.size();
          }
        }
      }
    
      match_buffers_cow = new_match_buffers;
      
      byte[]	secret = to_remove.getSharedSecret();
      
      if ( secret != null ){
    	  
	      TCPProtocolDecoder.removeSecret( secret );
      }
    } finally {  match_buffers_mon.exit();  }  
  } 
  
    
  
  
  private void start() {
  	try{
  		this_mon.enter();
      
        if( listen_port < 0 || listen_port > 65535 || listen_port == 6880 ) {
          String msg = "Invalid incoming listen port configured, " +listen_port+ ". Port reset to default. Please check your config!";
          Debug.out( msg );
          Logger.log(new LogAlert(LogAlert.UNREPEATABLE, LogAlert.AT_ERROR, msg));
          listen_port = RandomUtils.generateRandomNetworkListenPort();
          COConfigurationManager.setParameter( "TCP.Listen.Port", listen_port );
        }
  	
	    if( server_selector == null ) {
	      InetSocketAddress address;
	      try{
	        if( bind_address.length() > 0 ) {
	          address = new InetSocketAddress( InetAddress.getByName( bind_address ), listen_port );
	        }
	        else {
	          address = new InetSocketAddress( listen_port );
	        }
	      }
	      catch( UnknownHostException e ) {
	        Debug.out( e );
	        address = new InetSocketAddress( listen_port );
	      }
	      
	      server_selector = new VirtualServerChannelSelector( address, so_rcvbuf_size, new VirtualServerChannelSelector.SelectListener() {
	        public void newConnectionAccepted( final SocketChannel channel ) {
	        	
	        	//check for encrypted transport
	        	TransportCryptoManager.getSingleton().manageCrypto( channel, null, true, new TransportCryptoManager.HandshakeListener() {
	        		public void handshakeSuccess( TCPTransportHelperFilter filter ) {
	        			process( filter );
	        		}

	            public void 
	            handshakeFailure( 
	            	Throwable failure_msg ) 
	            {
	            	
	            	if (Logger.isEnabled()) 	Logger.log(new LogEvent(LOGID, "incoming crypto handshake failure: " + Debug.getNestedExceptionMessage( failure_msg )));
	            	
	            	/*
	            		// we can have problems with sockets stuck in a TIME_WAIT state if we just
	            		// close an incoming channel - to clear things down properly the client needs
	            		// to initiate the close. So what we do is send some random bytes to the client
	            		// under the assumption this will cause them to close, and we delay our socket close
	            		// for 10 seconds to give them a chance to do so.	            	
	            		try{
	            			Random	random = new Random();
	            		
	            			byte[]	random_bytes = new byte[68+random.nextInt(128-68)];
	            		
	            			random.nextBytes( random_bytes );
	            		
	            			channel.write( ByteBuffer.wrap( random_bytes ));
	            		
	            		}catch( Throwable e ){
	            			// ignore anything here
	            		}
	            		NetworkManager.getSingleton().closeSocketChannel( channel, 10*1000 );
	            	*/
	            
	            	NetworkManager.getSingleton().closeSocketChannel( channel );
	            }
	            
	    		public int
	    		getMaximumPlainHeaderLength()
	    		{
	    			return( max_min_match_buffer_size );
	    		}
	    		
	    		public int
	    		matchPlainHeader(
	    			ByteBuffer			buffer )
	    		{
	    			MatchListener	match = checkForMatch( buffer, true );
	    			
	    			if ( match == null ){
	    				
	    				return( TransportCryptoManager.HandshakeListener.MATCH_NONE );
	    				
	    			}else{
	    				
	    				if ( match.autoCryptoFallback()){

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -