📄 channelsocket.java
字号:
/* * ==================================================================== * * The Apache Software License, Version 1.1 * * Copyright (c) 1999 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, if * any, must include the following acknowlegement: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowlegement may appear in the software itself, * if and wherever such third-party acknowlegements normally appear. * * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software * Foundation" must not be used to endorse or promote products derived * from this software without prior written permission. For written * permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache" * nor may "Apache" appear in their names without prior written * permission of the Apache Group. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. * * [Additional notices, if required by prior licensing conditions] * */package org.apache.jk.common;import java.io.*;import java.net.*;import java.util.*;import org.apache.tomcat.util.buf.*;import org.apache.tomcat.util.http.*;import org.apache.tomcat.util.threads.*;import org.apache.jk.core.*;/* XXX Make the 'message type' pluggable *//* A lot of the 'original' behavior is hardcoded - this uses Ajp13 wire protocol, TCP, Ajp14 API etc. As we add other protocols/transports/APIs this will change, the current goal is to get the same level of functionality as in the original jk connector.*//** * Jk2 can use multiple protocols/transports. * Various container adapters should load this object ( as a bean ), * set configurations and use it. Note that the connector will handle * all incoming protocols - it's not specific to ajp1x. The protocol * is abstracted by MsgContext/Message/Channel. *//** Accept ( and send ) TCP messages. * * @author Costin Manolache * @jmx:mbean name="jk2:service=ChannelSocket" * description="Accept socket connections" */public class ChannelSocket extends JkHandler { private static org.apache.commons.logging.Log log= org.apache.commons.logging.LogFactory.getLog( ChannelSocket.class ); int startPort=8009; int maxPort=8019; // 0 for backward compat. int port=startPort; InetAddress inet; int serverTimeout; boolean tcpNoDelay=true; // nodelay to true by default int linger=100; int socketTimeout; long requestCount=0; /* Turning this to true will reduce the latency with about 20%. But it requires changes in tomcat to make sure client-requested flush() is honored ( on my test, I got 367->433 RPS and 52->35ms average time with a simple servlet ) */ static final boolean BUFFER_WRITE=false; ThreadPool tp=new ThreadPool(); /* ==================== Tcp socket options ==================== */ /** * @jmx:managed-constructor description="default constructor" */ public ChannelSocket() { } public ThreadPool getThreadPool() { return tp; } public long getRequestCount() { return requestCount; } /** Set the port for the ajp13 channel. * To support seemless load balancing and jni, we treat this * as the 'base' port - we'll try up until we find one that is not * used. We'll also provide the 'difference' to the main coyote * handler - that will be our 'sessionID' and the position in * the scoreboard and the suffix for the unix domain socket. * * @jmx:managed-attribute description="Port to listen" access="READ_WRITE" */ public void setPort( int port ) { this.startPort=port; this.port=port; this.maxPort=port+10; } public int getPort() { return port; } public void setAddress(InetAddress inet) { this.inet=inet; } /** * @jmx:managed-attribute description="Bind on a specified address" access="READ_WRITE" */ public void setAddress(String inet) { try { this.inet= InetAddress.getByName( inet ); } catch( Exception ex ) { ex.printStackTrace(); } } public String getAddress() { if( inet!=null) return inet.toString(); return null; } /** * Sets the timeout in ms of the server sockets created by this * server. This method allows the developer to make servers * more or less responsive to having their server sockets * shut down. * * <p>By default this value is 1000ms. */ public void setServerTimeout(int timeout) { this.serverTimeout = timeout; } public int getServerTimeout() { return serverTimeout; } public void setTcpNoDelay( boolean b ) { tcpNoDelay=b; } public boolean getTcpNoDelay() { return tcpNoDelay; } public void setSoLinger( int i ) { linger=i; } public int getSoLinger() { return linger; } public void setSoTimeout( int i ) { socketTimeout=i; } public int getSoTimeout() { return socketTimeout; } public void setMaxPort( int i ) { maxPort=i; } public int getMaxPort() { return maxPort; } /** At startup we'll look for the first free port in the range. The difference between this port and the beggining of the range is the 'id'. This is usefull for lb cases ( less config ). */ public int getInstanceId() { return port-startPort; } /** If set to false, the thread pool will be created in * non-daemon mode, and will prevent main from exiting */ public void setDaemon( boolean b ) { tp.setDaemon( b ); } public boolean getDaemon() { return tp.getDaemon(); } public void setMaxThreads( int i ) { if( log.isDebugEnabled()) log.debug("Setting maxThreads " + i); tp.setMaxThreads(i); } public int getMaxThreads() { return tp.getMaxThreads(); } public void setBacklog(int i) { } /* ==================== ==================== */ ServerSocket sSocket; int socketNote=1; int isNote=2; int osNote=3; public void accept( MsgContext ep ) throws IOException { if( sSocket==null ) return; Socket s=sSocket.accept(); ep.setNote( socketNote, s ); if(log.isDebugEnabled() ) log.debug("Accepted socket " + s ); if( linger > 0 ) s.setSoLinger( true, linger); if( socketTimeout > 0 ) s.setSoTimeout( socketTimeout ); s.setTcpNoDelay( tcpNoDelay ); // set socket tcpnodelay state requestCount++; InputStream is=new BufferedInputStream(s.getInputStream()); OutputStream os; if( BUFFER_WRITE ) os = new BufferedOutputStream( s.getOutputStream()); else os = s.getOutputStream(); ep.setNote( isNote, is ); ep.setNote( osNote, os ); } public void resetCounters() { requestCount=0; } /** Called after you change some fields at runtime using jmx. Experimental for now. */ public void reinit() throws IOException { destroy(); init(); } /** * @jmx:managed-operation */ public void init() throws IOException { // Find a port. if (startPort == 0) { port = 0; log.info("JK2: ajp13 disabling channelSocket"); running = true; return; } if (maxPort < startPort) maxPort = startPort; if (getAddress() == null) setAddress("0.0.0.0"); for( int i=startPort; i<=maxPort; i++ ) { try { sSocket=new ServerSocket( i, 0, inet ); port=i; break; } catch( IOException ex ) { log.info("Port busy " + i + " " + ex.toString()); continue; } } if( sSocket==null ) { log.error("Can't find free port " + startPort + " " + maxPort ); return; } log.info("JK2: ajp13 listening on " + getAddress() + ":" + port ); // If this is not the base port and we are the 'main' channleSocket and // SHM didn't already set the localId - we'll set the instance id if( "channelSocket".equals( name ) && port != startPort && (wEnv.getLocalId()==0) ) { wEnv.setLocalId( port - startPort ); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -