/** * Process the given socket. Typically keep alive or upgraded protocol. * * @param socket The socket to process * @param event The event to process * * @return <code>true</code> if the processing completed normally otherwise * <code>false</code> which indicates an error occurred and that the * socket should be closed */ protected boolean processSocket(long socket, SocketEvent event) { SocketWrapperBase<Long> socketWrapper = connections.get(Long.valueOf(socket)); return processSocket(socketWrapper, event, true); }
/** * 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). * * @param socket to add to the poller */ public void add(long socket) { synchronized (this) { // Add socket to the list. Newly added sockets will wait // at most for pollTime before being polled if (addCount >= addS.length) { // Can't do anything: close the socket right away if (comet) { processSocket(socket, SocketStatus.ERROR); } else { destroySocket(socket); } return; } addS[addCount] = socket; addCount++; this.notify(); } }
/** * 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). * * @param socket to add to the poller */ public void add(long socket, boolean keepAlive) { synchronized (this) { // Add socket to the list. Newly added sockets will wait // at most for pollTime before being polled if (addCount >= addSocket.length) { // Can't do anything: close the socket right away if (comet) { processSocket(socket, SocketStatus.ERROR); } else { destroySocket(socket); } return; } addSocket[addCount] = socket; addSocketKeepAlive[addCount] = keepAlive; addCount++; this.notify(); } }
/** * 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). * * @param socket to add to the poller */ public void add(long socket) { synchronized (this) { // Add socket to the list. Newly added sockets will wait // at most for pollTime before being polled if (addCount >= addS.length) { // Can't do anything: close the socket right away if (comet) { processSocket(socket, SocketStatus.ERROR); } else { destroySocket(socket); } return; } addS[addCount] = socket; addCount++; this.notify(); } }
private void closePollset(long pollset) { int rv = Poll.pollset(pollset, desc); if (rv > 0) { for (int n = 0; n < rv; n++) { if (comet) { processSocket(desc[n*2+1], SocketStatus.STOP); } else { destroySocket(desc[n*2+1], true); } } } }
processSocket(socket, SocketStatus.ERROR); } else { destroySocket(socket);
private void closePollset(long pollset) { int rv = Poll.pollset(pollset, desc); if (rv > 0) { for (int n = 0; n < rv; n++) { if (comet) { processSocket(desc[n*2+1], SocketStatus.STOP); } else { destroySocket(desc[n*2+1]); } } } }
private void doTimeout(long pollset) { int rv = Poll.maintain(pollset, desc, true); if (rv > 0) { keepAliveCount -= rv; for (int n = 0; n < rv; n++) { // Close socket and clear pool if (comet) { processSocket(desc[n], SocketStatus.TIMEOUT); } else { destroySocket(desc[n]); } } } } }
private void doTimeout(long pollset) { int rv = Poll.maintain(pollset, desc, true); if (rv > 0) { keepAliveCount -= rv; for (int n = 0; n < rv; n++) { // Close socket and clear pool if (comet) { processSocket(desc[n], SocketStatus.TIMEOUT); } else { destroySocket(desc[n]); } } } } }
if (!event || (event && !processSocket(socket, SocketStatus.ERROR))) { Socket.destroy(socket);
/** * Timeout checks. Must only be called from {@link Poller#run()}. */ private synchronized void maintain() { long date = System.currentTimeMillis(); // Maintain runs at most once every 1s, although it will likely get // called more if ((date - lastMaintain) < 1000L) { return; } else { lastMaintain = date; } long socket = timeouts.check(date); while (socket != 0) { if (log.isDebugEnabled()) { log.debug(sm.getString("endpoint.debug.socketTimeout", Long.valueOf(socket))); } SocketWrapperBase<Long> socketWrapper = connections.get(Long.valueOf(socket)); socketWrapper.setError(new SocketTimeoutException()); processSocket(socketWrapper, SocketEvent.ERROR, true); socket = timeouts.check(date); } }
if (!comet || (comet && !processSocket( socket, SocketStatus.ERROR))) { closeSocket(socket);
/** * 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 - lastMaintain) < 5000L) { return; } else { lastMaintain = date; } long socket = timeouts.check(date); while (socket != 0) { removeFromPoller(socket); if (!event || (event && !processSocket(socket, SocketStatus.TIMEOUT))) { Socket.destroy(socket); } socket = timeouts.check(date); } }
/** * 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 - lastMaintain) < 5000L) { return; } else { lastMaintain = date; } long socket = timeouts.check(date); while (socket != 0) { removeFromPoller(socket); if (!event || (event && !processSocket(socket, SocketStatus.TIMEOUT))) { Socket.destroy(socket); } socket = timeouts.check(date); } }
/** * Timeout checks. Must only be called from {@link Poller#run()}. */ private void maintain() { long date = System.currentTimeMillis(); // Maintain runs at most once every 1s, although it will likely get // called more if ((date - lastMaintain) < 1000L) { return; } else { lastMaintain = date; } long socket = timeouts.check(date); while (socket != 0) { if (log.isDebugEnabled()) { log.debug(sm.getString("endpoint.debug.socketTimeout", Long.valueOf(socket))); } removeFromPoller(socket); boolean comet = connections.get( Long.valueOf(socket)).isComet(); if (!comet || (comet && !processSocket( socket, SocketStatus.TIMEOUT))) { destroySocket(socket); } socket = timeouts.check(date); } }
if (!event || (event && !processSocket(info.socket, SocketStatus.STOP))) { Socket.destroy(info.socket); if (rv > 0) { for (int n = 0; n < rv; n++) { if (!event || (event && !processSocket(desc[n*2+1], SocketStatus.STOP))) { Socket.destroy(desc[n*2+1]);
/** * Destroy the poller. */ @Override public void destroy() { // Close all sockets in the add queue for (int i = 0; i < addCount; i++) { if (comet) { processSocket(addSocket[i], SocketStatus.STOP); } else { destroySocket(addSocket[i]); } } // Close all sockets still in the poller closePollset(connectionPollset); Pool.destroy(pool); keepAliveCount = 0; addCount = 0; try { while (this.isAlive()) { this.interrupt(); this.join(1000); } } catch (InterruptedException e) { // Ignore } }
@Override protected void longPoll(SocketWrapper<Long> socket, Processor<Long> processor) { connections.put(socket.getSocket(), processor); if (processor.isAsync()) { // Async socket.setAsync(true); } else if (processor.isComet()) { // Comet if (proto.endpoint.isRunning()) { ((AprEndpoint) proto.endpoint).getCometPoller().add( socket.getSocket().longValue(), proto.endpoint.getSoTimeout(), AprEndpoint.Poller.FLAGS_READ); } else { // Process a STOP directly ((AprEndpoint) proto.endpoint).processSocket( socket.getSocket().longValue(), SocketStatus.STOP); } } else { // Upgraded ((AprEndpoint) proto.endpoint).getPoller().add( socket.getSocket().longValue(), processor.getUpgradeInbound().getReadTimeout(), AprEndpoint.Poller.FLAGS_READ); } }
@Override protected void longPoll(SocketWrapper<Long> socket, Processor<Long> processor) { if (processor.isAsync()) { // Async socket.setAsync(true); } else if (processor.isComet()) { // Comet if (proto.endpoint.isRunning()) { socket.setComet(true); ((AprEndpoint) proto.endpoint).getPoller().add( socket.getSocket().longValue(), proto.endpoint.getSoTimeout(), true, false); } else { // Process a STOP directly ((AprEndpoint) proto.endpoint).processSocket( socket.getSocket().longValue(), SocketStatus.STOP); } } else { // Upgraded Poller p = ((AprEndpoint) proto.endpoint).getPoller(); if (p == null) { // Connector has been stopped release(socket, processor, true, false); } else { p.add(socket.getSocket().longValue(), -1, true, false); } } }
/** * Destroy the poller. */ @Override public void destroy() { // Close all sockets in the add queue for (int i = 0; i < addCount; i++) { if (comet) { processSocket(addSocket[i], SocketStatus.STOP); } else { destroySocket(addSocket[i]); } } // Close all sockets still in the pollers closePollset(connectionPollset); if (separateKeepAlive) { closePollset(keepAlivePollset); } Pool.destroy(pool); keepAliveCount = 0; addCount = 0; try { while (this.isAlive()) { this.interrupt(); this.join(1000); } } catch (InterruptedException e) { // Ignore } }