java.nio.channels.WritePendingException Java Examples

The following examples show how to use java.nio.channels.WritePendingException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: WsSession.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
private void sendCloseMessage(CloseReason closeReason) {
    // 125 is maximum size for the payload of a control message
    ByteBuffer msg = ByteBuffer.allocate(125);
    CloseCode closeCode = closeReason.getCloseCode();
    // CLOSED_ABNORMALLY should not be put on the wire
    if (closeCode == CloseCodes.CLOSED_ABNORMALLY) {
        // PROTOCOL_ERROR is probably better than GOING_AWAY here
        msg.putShort((short) CloseCodes.PROTOCOL_ERROR.getCode());
    } else {
        msg.putShort((short) closeCode.getCode());
    }

    String reason = closeReason.getReasonPhrase();
    if (reason != null && reason.length() > 0) {
        appendCloseReasonWithTruncation(msg, reason);
    }
    msg.flip();
    try {
        wsRemoteEndpoint.sendMessageBlock(Constants.OPCODE_CLOSE, msg, true);
    } catch (IOException | WritePendingException e) {
        // Failed to send close message. Close the socket and let the caller
        // deal with the Exception
        if (log.isDebugEnabled()) {
            log.debug(sm.getString("wsSession.sendCloseFail", id), e);
        }
        wsRemoteEndpoint.close();
        // Failure to send a close message is not unexpected in the case of
        // an abnormal closure (usually triggered by a failure to read/write
        // from/to the client. In this case do not trigger the endpoint's
        // error handling
        if (closeCode != CloseCodes.CLOSED_ABNORMALLY) {
            localEndpoint.onError(this, e);
        }
    } finally {
        webSocketContainer.unregisterSession(getSessionMapKey(), this);
    }
}
 
Example #2
Source File: AsynchronousTlsChannelGroup.java    From pgadba with BSD 2-Clause "Simplified" License 5 votes vote down vote up
WriteOperation startWrite(
    RegisteredSocket socket,
    ByteBufferSet buffer,
    long timeout, TimeUnit unit,
    LongConsumer onSuccess, Consumer<Throwable> onFailure)
    throws WritePendingException {
  checkTerminated();
  Util.assertTrue(buffer.hasRemaining());
  waitForSocketRegistration(socket);
  WriteOperation op;
  socket.writeLock.lock();
  try {
    if (socket.writeOperation != null) {
      throw new WritePendingException();
    }
    op = new WriteOperation(buffer, onSuccess, onFailure);
    /*
     * we do not try to outsmart the TLS state machine and register for both IO operations for each new socket
     * operation
     */
    socket.pendingOps.set(SelectionKey.OP_WRITE | SelectionKey.OP_READ);
    if (timeout != 0) {
      op.timeoutFuture = timeoutExecutor.schedule(() -> {
        boolean success = doCancelWrite(socket, op);
        if (success) {
          op.onFailure.accept(new InterruptedByTimeoutException());
        }
      }, timeout, unit);
    }
    socket.writeOperation = op;
  } finally {
    socket.writeLock.unlock();
  }
  selector.wakeup();
  startedWrites.increment();
  currentWrites.increment();
  return op;
}
 
Example #3
Source File: WsSession.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
private void sendCloseMessage(CloseReason closeReason) {
    // 125 is maximum size for the payload of a control message
    ByteBuffer msg = ByteBuffer.allocate(125);
    CloseCode closeCode = closeReason.getCloseCode();
    // CLOSED_ABNORMALLY should not be put on the wire
    if (closeCode == CloseCodes.CLOSED_ABNORMALLY) {
        // PROTOCOL_ERROR is probably better than GOING_AWAY here
        msg.putShort((short) CloseCodes.PROTOCOL_ERROR.getCode());
    } else {
        msg.putShort((short) closeCode.getCode());
    }

    String reason = closeReason.getReasonPhrase();
    if (reason != null && reason.length() > 0) {
        appendCloseReasonWithTruncation(msg, reason);
    }
    msg.flip();
    try {
        wsRemoteEndpoint.startMessageBlock(Constants.OPCODE_CLOSE, msg, true);
    } catch (IOException ioe) {
        handleCloseException(ioe, closeCode);
    } catch (WritePendingException wpe) {
        handleCloseException(wpe, closeCode);
    } finally {
        webSocketContainer.unregisterSession(localEndpoint, this);
    }
}
 
Example #4
Source File: WritePendingExceptionTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * java.nio.channels.WritePendingException#WritePendingException()
 */
public void test_empty() {
    WritePendingException e = new WritePendingException();
    assertTrue(e instanceof IllegalStateException);
    assertNull(e.getMessage());
    assertNull(e.getLocalizedMessage());
    assertNull(e.getCause());
}
 
Example #5
Source File: MarkableEndPoint.java    From qpid-broker-j with Apache License 2.0 4 votes vote down vote up
@Override
public void write(final Callback callback, final ByteBuffer... buffers) throws WritePendingException
{
    _underlying.write(callback, buffers);
}
 
Example #6
Source File: AsynchronousTlsChannelGroup.java    From tls-channel with MIT License 4 votes vote down vote up
WriteOperation startWrite(
    RegisteredSocket socket,
    ByteBufferSet buffer,
    long timeout,
    TimeUnit unit,
    LongConsumer onSuccess,
    Consumer<Throwable> onFailure)
    throws WritePendingException {
  checkTerminated();
  Util.assertTrue(buffer.hasRemaining());
  waitForSocketRegistration(socket);
  WriteOperation op;
  socket.writeLock.lock();
  try {
    if (socket.writeOperation != null) {
      throw new WritePendingException();
    }
    op = new WriteOperation(buffer, onSuccess, onFailure);
    /*
     * we do not try to outsmart the TLS state machine and register for both IO operations for each new socket
     * operation
     */
    socket.pendingOps.set(SelectionKey.OP_WRITE | SelectionKey.OP_READ);
    if (timeout != 0) {
      op.timeoutFuture =
          timeoutExecutor.schedule(
              () -> {
                boolean success = doCancelWrite(socket, op);
                if (success) {
                  op.onFailure.accept(new InterruptedByTimeoutException());
                }
              },
              timeout,
              unit);
    }
    socket.writeOperation = op;
  } finally {
    socket.writeLock.unlock();
  }
  selector.wakeup();
  startedWrites.increment();
  currentWrites.increment();
  return op;
}