Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   /*
    * JBoss, Home of Professional Open Source.
    * Copyright 2012 Red Hat, Inc., and individual contributors
    * as indicated by the @author tags.
    *
    * Licensed under the Apache License, Version 2.0 (the "License");
    * you may not use this file except in compliance with the License.
    * You may obtain a copy of the License at
    *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  package org.apache.tomcat.util.net;
  
  import java.net.Socket;
  
Handle incoming TCP connections. This class implement a simple server model: one listener thread accepts on a socket and creates a new worker thread for each incoming connection. More advanced Endpoints will reuse the threads, use queues, etc.

Author(s):
James Duncan Davidson
Jason Hunter
James Todd
Costin Manolache
Gal Shachor
Yoav Shapira
Remy Maucherat
  
  public class JIoEndpoint {
  
  
      // ----------------------------------------------------------------- Fields
  

    
Available workers.
  
      protected WorkerStack workers = null;


    
Running state of the endpoint.
  
      protected volatile boolean running = false;


    
Will be set to true whenever the endpoint is paused.
  
      protected volatile boolean paused = false;


    
Track the initialization state of the endpoint.
  
      protected boolean initialized = false;


    
Current worker threads busy count.
  
      protected int curThreadsBusy = 0;


    
Current worker threads count.
  
      protected int curThreads = 0;


    
Sequence number used to generate thread names.
  
      protected int sequence = 0;


    
Associated server socket.
  
      protected ServerSocket serverSocket = null;
 
 
     // ------------------------------------------------------------- Properties
 

    
Acceptor thread count.
 
     protected int acceptorThreadCount = 0;
     public void setAcceptorThreadCount(int acceptorThreadCount) { this. = acceptorThreadCount; }
     public int getAcceptorThreadCount() { return ; }


    
External Executor based thread pool.
 
     protected Executor executor = null;
     public void setExecutor(Executor executor) { this. = executor; }
     public Executor getExecutor() { return ; }


    
Maximum amount of worker threads.
 
     protected int maxThreads = (.....) ? 64 : ((. == -1) ? 512 * Runtime.getRuntime().availableProcessors() : .);
     public void setMaxThreads(int maxThreads) { this. = maxThreads; }
     public int getMaxThreads() { return ; }


    
Priority of the acceptor and poller threads.
 
     protected int threadPriority = .;
     public void setThreadPriority(int threadPriority) { this. = threadPriority; }
     public int getThreadPriority() { return ; }

    
    
Size of the socket poller.
 
     protected int pollerSize = (.....) ? 128 : (32 * 1024);
     public void setPollerSize(int pollerSize) { this. = pollerSize; }
     public int getPollerSize() { return ; }


    
Keep-Alive timeout.
 
     protected int keepAliveTimeout = -1;
     public int getKeepAliveTimeout() { return ; }
     public void setKeepAliveTimeout(int keepAliveTimeout) { this. = keepAliveTimeout; }


    
Server socket port.
 
     protected int port;
     public int getPort() { return ; }
     public void setPort(int port ) { this.=port; }


    
Address for the server socket.
 
     protected InetAddress address;
     public InetAddress getAddress() { return ; }
     public void setAddress(InetAddress address) { this. = address; }


    
Handling of accepted sockets.
 
     protected Handler handler = null;
     public void setHandler(Handler handler ) { this. = handler; }
     public Handler getHandler() { return ; }


    
Allows the server developer to specify the backlog that should be used for server sockets. By default, this value is 100.
 
     protected int backlog = 100;
     public void setBacklog(int backlog) { if (backlog > 0) this. = backlog; }
     public int getBacklog() { return ; }


    
Socket TCP no delay.
 
     protected boolean tcpNoDelay = false;
     public boolean getTcpNoDelay() { return ; }
     public void setTcpNoDelay(boolean tcpNoDelay) { this. = tcpNoDelay; }


    
Socket linger.
 
     protected int soLinger = 100;
     public int getSoLinger() { return ; }
     public void setSoLinger(int soLinger) { this. = soLinger; }


    
Socket timeout.
 
     protected int soTimeout = -1;
     public int getSoTimeout() { return ; }
     public void setSoTimeout(int soTimeout) { this. = soTimeout; }


    
The default is true - the created threads will be in daemon mode. If set to false, the control thread will not be daemon - and will keep the process alive.
 
     protected boolean daemon = true;
     public void setDaemon(boolean b) {  = b; }
     public boolean getDaemon() { return ; }


    
Name of the thread pool, which will be used for naming child threads.
 
     protected String name = "TP";
     public void setName(String name) { this. = name; }
     public String getName() { return ; }


    
Server socket factory.
 
     protected ServerSocketFactory serverSocketFactory = null;
     public void setServerSocketFactory(ServerSocketFactory factory) { this. = factory; }
     public ServerSocketFactory getServerSocketFactory() { return ; }


    
The socket poller used for event support.
 
     protected Poller eventPoller = null;
     public Poller getEventPoller() {
         return ;
     }
 
 
     public boolean isRunning() {
         return ;
     }
     
     public boolean isPaused() {
         return ;
     }
     
     public int getCurrentThreadCount() {
         return ;
     }
     
     public int getCurrentThreadsBusy() {
         return !=null? - .size():0;
     }
     
 
     // ------------------------------------------------ Handler Inner Interface
 

    
Bare bones interface used for socket processing. Per thread data is to be stored in the ThreadWithAttributes extra folders, or alternately in thread local fields.
 
     public interface Handler {
         public enum SocketState {
             OPEN, CLOSED, LONG
         }
         public SocketState process(Socket socket);
         public SocketState event(Socket socketSocketStatus status);
     }
 
 
     // --------------------------------------------------- Acceptor Inner Class
 

    
Server socket acceptor thread.
 
     protected class Acceptor implements Runnable {


        
The background thread that listens for incoming TCP/IP connections and hands them off to an appropriate processor.
 
         public void run() {
 
             // Loop until we receive a shutdown command
             while () {
 
                 // Loop if endpoint is paused
                 while ( && ) {
                     try {
                         Thread.sleep(1000);
                     } catch (InterruptedException e) {
                         // Ignore
                     }
                 }
                 if (!) {
                     break;
                 }
 
                 // Accept the next incoming connection from the server socket
                 try {
                     Socket socket = .acceptSocket();
                     .initSocket(socket);
                     // Hand this socket off to an appropriate processor
                     if (!processSocket(socket)) {
                         // Close socket right away
                         try { socket.close(); } catch (IOException e) { }
                     }
                 } catch (IOException x) {
                     if () {
                         ..errorAcceptingSocket(x);
                     }
                 } catch (Throwable t) {
                     ..errorAcceptingSocket(t);
                 }
 
                 // The processor will recycle itself when it finishes
 
             }
 
         }
 
     }
 
 
     // ------------------------------------------------- SocketInfo Inner Class
 

    
Socket list class, used to avoid using a possibly large amount of objects with very little actual use.
 
     public static class SocketInfo {
         public static final int RESUME = 4;
         public static final int WAKEUP = 8;
         public Socket socket;
         public int timeout;
         public int flags;
         public boolean resume() {
             return ( & ) == ;
         }
         public boolean wakeup() {
             return ( & ) == ;
         }
         public static int merge(int flag1int flag2) {
             return ((flag1 & ) | (flag2 & )) 
                 | ((flag1 & ) & (flag2 & ));
         }
     }
     
     
     // --------------------------------------------- SocketTimeouts Inner Class
 

    
Socket list class, used to avoid using a possibly large amount of objects with very little actual use.
 
     public class SocketTimeouts {
         protected int size;
 
         protected Socket[] sockets;
         protected long[] timeouts;
         protected int pos = 0;
 
         public SocketTimeouts(int size) {
             this. = 0;
              = new Socket[size];
              = new long[size];
         }
 
         public void add(Socket socketlong timeout) {
             [] = socket;
             [] = timeout;
             ++;
         }
         
         public boolean remove(Socket socket) {
             for (int i = 0; i < i++) {
                 if ([i] == socket) {
                     [i] = [ - 1];
                     [i] = [ - 1];
                     --;
                     return true;
                 }
             }
             return false;
         }
         
         public Socket check(long date) {
             while ( < ) {
                 if (date >= []) {
                     Socket result = [];
                     [] = [ - 1];
                     [] = [ - 1];
                     --;
                     return result;
                 }
                 ++;
             }
              = 0;
             return null;
         }
         
     }
     
     
     // ------------------------------------------------- SocketList Inner Class
 

    
Socket list class, used to avoid using a possibly large amount of objects with very little actual use.
 
     public class SocketList {
         protected int size;
         protected int pos;
 
         protected Socket[] sockets;
         protected int[] timeouts;
         protected int[] flags;
         
         protected SocketInfo info = new SocketInfo();
         
         public SocketList(int size) {
             this. = 0;
              = 0;
              = new Socket[size];
              = new int[size];
              = new int[size];
         }
         
         public int size() {
             return this.;
         }
         
         public SocketInfo get() {
             if ( == ) {
                 return null;
             } else {
                 . = [];
                 . = [];
                 . = [];
                 ++;
                 return ;
             }
         }
         
         public void clear() {
              = 0;
              = 0;
         }
         
         public boolean add(Socket socketint timeoutint flag) {
             if ( == .) {
                 return false;
             } else {
                 for (int i = 0; i < i++) {
                     if ([i] == socket) {
                         [i] = SocketInfo.merge([i], flag);
                         return true;
                     }
                 }
                 [] = socket;
                 [] = timeout;
                 [] = flag;
                 ++;
                 return true;
             }
         }
         
         public void duplicate(SocketList copy) {
             copy.size = ;
             copy.pos = ;
             System.arraycopy(, 0, copy.sockets, 0, );
             System.arraycopy(, 0, copy.timeouts, 0, );
             System.arraycopy(, 0, copy.flags, 0, );
         }
         
     }
     
     
     // ------------------------------------------- SocketProcessor Inner Class
 

    
This class is the equivalent of the Worker, but will simply use in an external Executor thread pool.
 
     protected class SocketProcessor implements Runnable {
         
         protected Socket socket = null;
         
         public SocketProcessor(Socket socket) {
             this. = socket;
         }
 
         public void run() {
 
             // Process the request from this socket
             if (!setSocketOptions() || (.process() == ..)) {
                 // Close socket
                 try { .close(); } catch (IOException e) { }
             }
 
             // Finish up this request
              = null;
 
         }
         
     }
     
     
     // --------------------------------------- SocketEventProcessor Inner Class
 

    
This class is the equivalent of the Worker, but will simply use in an external Executor thread pool.
 
     protected class SocketEventProcessor implements Runnable {
         
         protected Socket socket = null;
         protected SocketStatus status = null
         
         public SocketEventProcessor(Socket socketSocketStatus status) {
             this. = socket;
             this. = status;
         }
 
         public void run() {
 
             Handler.SocketState socketState = .event();
             if (socketState == ..) {
                 // Close socket
                 try { .close(); } catch (IOException e) { }
             } else if (socketState == ..) {
                 // Process the keepalive after the event processing
                 // This is the main behavior difference with endpoint with pollers, which
                 // will add the socket to the poller
                 if (.process() == ..) {
                     // Close socket
                     try { .close(); } catch (IOException e) { }
                 }
             }
              = null;
 
         }
         
     }
     
     
     // ----------------------------------------------------- Poller Inner Class
 

    
Poller class.
 
     public class Poller implements Runnable {

        
List of sockets to be added to the poller.
 
         protected SocketList addList = null;

        
List of sockets to be added to the poller.
 
         protected SocketList localAddList = null;

        
Structure used for storing timeouts.
 
         protected SocketTimeouts timeouts = null;
        
        
        
Last run of maintain. Maintain will run usually every 5s.
 
         protected long lastMaintain = System.currentTimeMillis();
        
        
        
Amount of connections inside this poller.
 
         protected int connectionCount = 0;
         public int getConnectionCount() { return ; }
 
         public Poller() {
         }
        
        
Create the poller. The java.io poller only deals with timeouts.
 
         protected void init() {
 
              = new SocketTimeouts();
             
              = 0;
              = new SocketList();
              = new SocketList();
 
         }

        
Destroy the poller.
 
         protected void destroy() {
             // Wait for pollerTime before doing anything, so that the poller threads
             // exit, otherwise parallel destruction of sockets which are still
             // in the poller can cause problems
             try {
                 synchronized (this) {
                     this.wait(2);
                 }
             } catch (InterruptedException e) {
                 // Ignore
             }
             // Close all sockets in the add queue
             SocketInfo info = .get();
             while (info != null) {
                 if (!processSocket(info.socket.)) {
                     try { info.socket.close(); } catch (IOException e) { }
                 }
                 info = .get();
             }
             .clear();
             // Close all sockets still in the poller
             long future = System.currentTimeMillis() + .;
             Socket socket = .check(future);
             while (socket != null) {
                 if (!processSocket(socket.)) {
                     try { socket.close(); } catch (IOException e) { }
                 }
                 socket = .check(future);
             }
              = 0;
         }

        
Add specified socket and associated pool to the poller. The socket will be added to a temporary array, and polled first after a maximum amount of time equal to pollTime (in most cases, latency will be much lower, however).

Parameters:
socket to add to the poller
 
         public void add(Socket socketint timeoutboolean resumeboolean wakeup) {
             if (timeout < 0) {
                 timeout = ;
             }
             if (timeout < 0) {
                 timeout = ;
             }
             if (timeout <= 0) {
                 // Always put a timeout in
                 timeout = .;
             }
             boolean ok = false;
             synchronized (this) {
                 // Add socket to the list. Newly added sockets will wait
                 // at most for pollTime before being polled
                 if (.add(sockettimeout, (resume ? . : 0)
                         | (wakeup ? . : 0))) {
                     ok = true;
                     this.notify();
                 }
             }
             if (!ok) {
                 // Can't do anything: close the socket right away
                 if (!processSocket(socket.)) {
                     try { socket.close(); } catch (IOException e) { }
                 }
             }
         }

        
Timeout checks.
 
         protected void maintain() {
 
             long date = System.currentTimeMillis();
             // Maintain runs at most once every 5s, although it will likely get called more
             if ((date - ) < 5000L) {
                 return;
             } else {
                  = date;
             }
             Socket socket = .check(date);
             while (socket != null) {
                 if (!processSocket(socket.)) {
                     try { socket.close(); } catch (IOException e) { }
                 }
                 socket = .check(date);
             }
 
         }
        
        
The background thread that listens for incoming TCP/IP connections and hands them off to an appropriate processor.
 
         public void run() {
 
             int maintain = 0;
             // Loop until we receive a shutdown command
             while () {
 
                 // Loop if endpoint is paused
                 while ( && ) {
                     try {
                         Thread.sleep(1000);
                     } catch (InterruptedException e) {
                         // Ignore
                     }
                 }
                 // Check timeouts for suspended connections if the poller is empty
                 while ( &&  < 1 && .size() < 1) {
                     // Reset maintain time.
                     try {
                         if ( > 0 && ) {
                             maintain();
                         }
                         synchronized (this) {
                             this.wait(10000);
                         }
                     } catch (InterruptedException e) {
                         // Ignore
                     } catch (Throwable t) {
                         ..errorProcessingSocketTimeout(t);
                     }
                 }
                 if (!) {
                     break;
                 }
 
                 try {
                     
                     // Add sockets which are waiting to the poller
                     if (.size() > 0) {
                         synchronized (this) {
                             // Duplicate to another list, so that the syncing is minimal
                             .duplicate();
                             .clear();
                         }
                         SocketInfo info = .get();
                         while (info != null) {
                             if (info.wakeup()) {
                                 // Resume event if socket is present in the poller
                                 if (.remove(info.socket)) {
                                     if (info.resume()) {
                                         if (!processSocket(info.socket.)) {
                                             try { info.socket.close(); } catch (IOException e) { }
                                         }
                                     } else {
                                         .add(info.socket, System.currentTimeMillis() + info.timeout);
                                     }
                                 }
                             } else {
                                 if (info.resume()) {
                                     .remove(info.socket);
                                     if (!processSocket(info.socket.)) {
                                         try { info.socket.close(); } catch (IOException e) { }
                                     }
                                 } else {
                                     .add(info.socket, System.currentTimeMillis() + info.timeout);
                                 }
                             }
                             info = .get();
                         }
                     }
 
                     try {
                         Thread.sleep(2);
                     } catch (InterruptedException e) {
                         // Ignore
                     }
 
                     // Process socket timeouts
                     if ( > 0 && maintain++ > 1000 && ) {
                         maintain = 0;
                         maintain();
                     }
 
                 } catch (Throwable t) {
                     if (maintain == 0) {
                         ..errorProcessingSocketTimeout(t);
                     } else {
                         ..errorPollingSocketWithException(t);
                     }
                 }
 
             }
 
             synchronized (this) {
                 this.notifyAll();
             }
 
         }
         
     }
 
 
     // ----------------------------------------------------- Worker Inner Class
 
 
     protected class Worker implements Runnable {
 
         protected Thread thread = null;
         protected boolean available = false;
         protected Socket socket = null;
         protected SocketStatus status = null;

        
        
Process an incoming TCP/IP connection on the specified socket. Any exception that occurs during processing must be logged and swallowed. NOTE: This method is called from our Connector's thread. We must assign it to our own thread so that multiple simultaneous requests can be handled.

Parameters:
socket TCP socket to process
 
         protected synchronized void assign(Socket socket) {
 
             // Wait for the Processor to get the previous Socket
             while () {
                 try {
                     wait();
                 } catch (InterruptedException e) {
                 }
             }
 
             // Store the newly available Socket and notify our thread
             this. = socket;
             this. = null;
              = true;
             notifyAll();
 
         }
 
         
         protected synchronized void assign(Socket socketSocketStatus status) {
 
             // Wait for the Processor to get the previous Socket
             while () {
                 try {
                     wait();
                 } catch (InterruptedException e) {
                 }
             }
 
             // Store the newly available Socket and notify our thread
             this. = socket;
             this. = status;
              = true;
             notifyAll();
 
         }


        
Await a newly assigned Socket from our Connector, or null if we are supposed to shut down.
 
         private synchronized Socket await() {
 
             // Wait for the Connector to provide a new Socket
             while (!) {
                 try {
                     wait();
                 } catch (InterruptedException e) {
                 }
             }
 
             // Notify the Connector that we have received this Socket
             Socket socket = this.;
              = false;
             notifyAll();
 
             return (socket);
 
         }



        
The background thread that listens for incoming TCP/IP connections and hands them off to an appropriate processor.
 
         public void run() {
 
             // Process requests until we receive a shutdown signal
             while () {
 
                 // Wait for the next socket to be assigned
                 Socket socket = await();
                 if (socket == null)
                     continue;
 
                 // Process the request from this socket
                 if ( != null){
                     Handler.SocketState socketState = .event(socket);
                     if (socketState == ..) {
                         // Close socket
                         try { socket.close(); } catch (IOException e) { }
                     } else if (socketState == ..) {
                         // Process the keepalive after the event processing
                         // This is the main behavior difference with endpoint with pollers, which
                         // will add the socket to the poller
                         if (.process(socket) == ..) {
                             // Close socket
                             try { socket.close(); } catch (IOException e) { }
                         }
                     }
                 } else if (( == null) && (!setSocketOptions(socket) || (.process(socket) == ..))) {
                     // Close socket
                     try { socket.close(); } catch (IOException e) { }
                 }
 
                 // Finish up this request
                 recycleWorkerThread(this);
 
             }
 
         }


        
Start the background processing thread.
 
         public void start() {
              = new Thread(this);
             .setName(getName() + "-" + (++));
             .setDaemon(true);
             .start();
         }
 
 
     }
 
 
     // -------------------- Public methods --------------------
 
     public void init()
         throws Exception {
 
         if ()
             return;
         
         // Initialize thread count defaults for acceptor
         if ( == 0) {
              = 1;
         }
         if ( == null) {
              = ServerSocketFactory.getDefault();
         }
         if ( == null) {
             try {
                 if ( == null) {
                      = .createSocket();
                 } else {
                      = .createSocket();
                 }
             } catch (BindException be) {
                 if ( == null) {
                     throw new BindException(be.getMessage() + "<null>:" + );
                 } else {
                     throw new BindException(be.getMessage() + " " +
                             .toString() + ":" + );
                 }
             }
         }
         //if( serverTimeout >= 0 )
         //    serverSocket.setSoTimeout( serverTimeout );
         
          = true;
         
     }
     
     public void start()
         throws Exception {
         // Initialize socket if not done before
         if (!) {
             init();
         }
         if (!) {
              = true;
              = false;
            // Create worker collection
            if ( == null) {
                 = new WorkerStack();
            }
            // Start event poller thread
             = new Poller();
            .init();
            Thread pollerThread = new Thread(getName() + "-Poller");
            pollerThread.setPriority();
            pollerThread.setDaemon(true);
            pollerThread.start();
            // Start acceptor threads
            for (int i = 0; i < i++) {
                Thread acceptorThread = new Thread(new Acceptor(), getName() + "-Acceptor-" + i);
                acceptorThread.setPriority();
                acceptorThread.setDaemon();
                acceptorThread.start();
            }
        }
    }
    public void pause() {
        if ( && !) {
             = true;
            unlockAccept();
        }
    }
    public void resume() {
        if () {
             = false;
        }
    }
    public void stop() {
        if () {
             = false;
            unlockAccept();
            .destroy();
             = null;
        }
    }

    
Deallocate APR memory pools, and close server socket.
    public void destroy() throws Exception {
        if () {
            stop();
        }
        if ( != null) {
            try {
                if ( != null)
                    .close();
            } catch (Exception e) {
                ..errorClosingSocket(e);
            }
             = null;
        }
         = false ;
    }
    
    protected void unlockAccept() {
        java.net.Socket s = null;
        InetSocketAddress saddr = null;
        try {
            // Need to create a connection to unlock the accept();
            if ( == null) {
                saddr = new InetSocketAddress("localhost");
            } else {
                saddr = new InetSocketAddress();
            }
            s = new java.net.Socket();
            s.setSoLinger(true, 0);
            s.connect(saddr, 2000);
        } catch (Exception e) {
            // Ignore
        } finally {
            if (s != null) {
                try {
                    s.close();
                } catch (Exception e) {
                    // Ignore
                }
            }
        }
    }


    
Set the options for the current socket.
    protected boolean setSocketOptions(Socket socket) {
        // Process the connection
        int step = 1;
        try {
            // 1: Set socket options: timeout, linger, etc
            if ( >= 0) { 
                socket.setSoLinger(true);
            }
            if () {
                socket.setTcpNoDelay();
            }
            if ( > 0) {
                socket.setSoTimeout();
            }
            // 2: SSL handshake
            step = 2;
            .handshake(socket);
        } catch (Throwable t) {
            if (step == 2) {
                ..handshakeFailed(t);
            } else {
                ..unexpectedError(t);
            }
            // Tell to close the socket
            return false;
        }
        return true;
    }

    
    
Create (or allocate) and return an available processor for use in processing a specific HTTP request, if possible. If the maximum allowed processors have already been created and are in use, return null instead.
    protected Worker createWorkerThread() {
        synchronized () {
            if (.size() > 0) {
                ++;
                return .pop();
            }
            if (( > 0) && ( < )) {
                ++;
                if ( == ) {
                    ..maxThreadsReached();
                }
                return (newWorkerThread());
            } else {
                if ( < 0) {
                    ++;
                    return (newWorkerThread());
                } else {
                    return (null);
                }
            }
        }
    }


    
Create and return a new processor suitable for processing HTTP requests and returning the corresponding responses.
    protected Worker newWorkerThread() {
        Worker workerThread = new Worker();
        workerThread.start();
        return (workerThread);
    }


    
Return a new worker thread, and block while to worker is available.
    protected Worker getWorkerThread() {
        // Allocate a new worker thread
        Worker workerThread = createWorkerThread();
                || .....) {
            while (workerThread == null) {
                try {
                    synchronized () {
                        .wait();
                    }
                } catch (InterruptedException e) {
                    // Ignore
                }
                workerThread = createWorkerThread();
            }
        }
        return workerThread;
    }


    
Recycle the specified Processor so that it can be used again.

Parameters:
workerThread The processor to be recycled
    protected void recycleWorkerThread(Worker workerThread) {
        synchronized () {
            .push(workerThread);
            --;
            .notify();
        }
    }


    
Process given socket.
    protected boolean processSocket(Socket socket) {
        try {
            if ( == null) {
                Worker worker = getWorkerThread();
                if (worker != null) {
                    worker.assign(socket);
                } else {
                    return false;
                }
            } else {
                .execute(new SocketProcessor(socket));
            }
        } catch (Throwable t) {
            // This means we got an OOM or similar creating a thread, or that
            // the pool and its queue are full
            return false;
        }
        return true;
    }
    

    
Process given socket for an event.
    protected boolean processSocket(Socket socketSocketStatus status) {
        try {
            if ( == null) {
                Worker worker = getWorkerThread();
                if (worker != null) {
                    worker.assign(socketstatus);
                } else {
                    return false;
                }
            } else {
                .execute(new SocketEventProcessor(socketstatus));
            }
        } catch (Throwable t) {
            // This means we got an OOM or similar creating a thread, or that
            // the pool and its queue are full
            return false;
        }
        return true;
    }
    
    // ------------------------------------------------- WorkerStack Inner Class
    public class WorkerStack {
        
        protected Worker[] workers = null;
        protected int end = 0;
        
        public WorkerStack(int size) {
             = new