public abstract class SocketWrapperBase<E> extends Object
Modifier and Type | Class and Description |
---|---|
static class |
SocketWrapperBase.BlockingMode |
static interface |
SocketWrapperBase.CompletionCheck |
static class |
SocketWrapperBase.CompletionHandlerCall |
static class |
SocketWrapperBase.CompletionState |
protected class |
SocketWrapperBase.OperationState<A>
Internal state tracker for vectored operations.
|
protected class |
SocketWrapperBase.VectoredIOCompletionHandler<A>
Completion handler for vectored operations.
|
Modifier and Type | Field and Description |
---|---|
protected int |
bufferedWriteSize
The max size of the individual buffered write buffers
|
protected AtomicBoolean |
closed |
static SocketWrapperBase.CompletionCheck |
COMPLETE_READ
This utility CompletionCheck will cause the completion handler
to be called once the given buffers are full.
|
static SocketWrapperBase.CompletionCheck |
COMPLETE_READ_WITH_COMPLETION
This utility CompletionCheck will cause the completion handler
to be called once the given buffers are full.
|
static SocketWrapperBase.CompletionCheck |
COMPLETE_WRITE
This utility CompletionCheck will cause the write to fully write
all remaining data.
|
static SocketWrapperBase.CompletionCheck |
COMPLETE_WRITE_WITH_COMPLETION
This utility CompletionCheck will cause the write to fully write
all remaining data.
|
protected Object |
currentProcessor
The org.apache.coyote.Processor instance currently associated
with the wrapper.
|
protected String |
localAddr |
protected String |
localName |
protected int |
localPort |
protected WriteBuffer |
nonBlockingWriteBuffer
Additional buffer used for non-blocking writes.
|
static SocketWrapperBase.CompletionCheck |
READ_DATA
This utility CompletionCheck will cause the completion handler
to be called once some data has been read.
|
protected SocketWrapperBase.OperationState<?> |
readOperation |
protected Semaphore |
readPending |
protected String |
remoteAddr |
protected String |
remoteHost |
protected int |
remotePort |
protected static StringManager |
sm |
protected SocketBufferHandler |
socketBufferHandler
The buffers used for communicating with the socket.
|
protected SocketWrapperBase.OperationState<?> |
writeOperation |
protected Semaphore |
writePending |
Constructor and Description |
---|
SocketWrapperBase(E socket,
AbstractEndpoint<E,?> endpoint) |
Modifier and Type | Method and Description |
---|---|
boolean |
awaitReadComplete(long timeout,
TimeUnit unit)
Deprecated.
|
boolean |
awaitWriteComplete(long timeout,
TimeUnit unit)
Deprecated.
|
protected static boolean |
buffersArrayHasRemaining(ByteBuffer[] buffers,
int offset,
int length) |
boolean |
canWrite() |
void |
checkError() |
void |
close()
Close the socket wrapper.
|
abstract SendfileDataBase |
createSendfileData(String filename,
long pos,
long length) |
int |
decrementKeepAlive() |
abstract void |
doClientAuth(SSLSupport sslSupport)
Require the client to perform CLIENT-CERT authentication if it hasn't
already done so.
|
protected abstract void |
doClose()
Perform the actual close.
|
protected void |
doWrite(boolean block)
Write the contents of the socketWriteBuffer to the socket.
|
protected abstract void |
doWrite(boolean block,
ByteBuffer from)
Write the contents of the ByteBuffer to the socket.
|
void |
execute(Runnable runnable)
Transfers processing to a container thread.
|
boolean |
flush(boolean block)
Writes as much data as possible from any that remains in the buffers.
|
protected void |
flushBlocking() |
protected boolean |
flushNonBlocking() |
Object |
getCurrentProcessor() |
protected AbstractEndpoint<E,?> |
getEndpoint() |
IOException |
getError() |
String |
getLocalAddr() |
String |
getLocalName() |
int |
getLocalPort() |
String |
getNegotiatedProtocol() |
long |
getReadTimeout() |
String |
getRemoteAddr() |
String |
getRemoteHost() |
int |
getRemotePort() |
E |
getSocket() |
SocketBufferHandler |
getSocketBufferHandler() |
abstract SSLSupport |
getSslSupport(String clientCertProvider) |
long |
getWriteTimeout() |
boolean |
hasAsyncIO()
Allows using NIO2 style read/write.
|
boolean |
hasDataToRead() |
boolean |
hasDataToWrite() |
boolean |
hasPerOperationTimeout()
Allows indicating if the connector supports per operation timeout.
|
boolean |
isClosed() |
boolean |
isReadPending()
Allows checking if an asynchronous read operation is currently pending.
|
abstract boolean |
isReadyForRead() |
boolean |
isReadyForWrite()
Checks to see if there are any writes pending and if there are calls
registerWriteInterest() to trigger a callback once the pending
writes have completed. |
boolean |
isSecure()
Deprecated.
Unused. Will be removed in Tomcat 10.
|
boolean |
isUpgraded()
Deprecated.
Unused. Will be removed in Tomcat 10.
|
boolean |
isWritePending()
Allows checking if an asynchronous write operation is currently pending.
|
boolean |
needSemaphores()
Allows indicating if the connector needs semaphores.
|
protected abstract <A> SocketWrapperBase.OperationState<A> |
newOperationState(boolean read,
ByteBuffer[] buffers,
int offset,
int length,
SocketWrapperBase.BlockingMode block,
long timeout,
TimeUnit unit,
A attachment,
SocketWrapperBase.CompletionCheck check,
CompletionHandler<Long,? super A> handler,
Semaphore semaphore,
SocketWrapperBase.VectoredIOCompletionHandler<A> completion) |
protected abstract void |
populateLocalAddr() |
protected abstract void |
populateLocalName() |
protected abstract void |
populateLocalPort() |
protected int |
populateReadBuffer(byte[] b,
int off,
int len) |
protected int |
populateReadBuffer(ByteBuffer to) |
protected abstract void |
populateRemoteAddr() |
protected abstract void |
populateRemoteHost() |
protected abstract void |
populateRemotePort() |
abstract SendfileState |
processSendfile(SendfileDataBase sendfileData)
Starts the sendfile process.
|
void |
processSocket(SocketEvent socketStatus,
boolean dispatch) |
abstract int |
read(boolean block,
byte[] b,
int off,
int len) |
abstract int |
read(boolean block,
ByteBuffer to) |
<A> SocketWrapperBase.CompletionState |
read(ByteBuffer[] dsts,
int offset,
int length,
SocketWrapperBase.BlockingMode block,
long timeout,
TimeUnit unit,
A attachment,
SocketWrapperBase.CompletionCheck check,
CompletionHandler<Long,? super A> handler)
Scatter read.
|
<A> SocketWrapperBase.CompletionState |
read(long timeout,
TimeUnit unit,
A attachment,
CompletionHandler<Long,? super A> handler,
ByteBuffer... dsts)
Scatter read.
|
<A> SocketWrapperBase.CompletionState |
read(SocketWrapperBase.BlockingMode block,
long timeout,
TimeUnit unit,
A attachment,
SocketWrapperBase.CompletionCheck check,
CompletionHandler<Long,? super A> handler,
ByteBuffer... dsts)
Scatter read.
|
abstract void |
registerReadInterest() |
abstract void |
registerWriteInterest() |
protected void |
reset(E closedSocket) |
abstract void |
setAppReadBufHandler(ApplicationBufferHandler handler) |
void |
setCurrentProcessor(Object currentProcessor) |
void |
setError(IOException error) |
void |
setKeepAliveLeft(int keepAliveLeft) |
void |
setNegotiatedProtocol(String negotiatedProtocol) |
void |
setReadTimeout(long readTimeout)
Set the timeout for reading.
|
void |
setSecure(boolean secure)
Deprecated.
Unused. Will be removed in Tomcat 10.
|
void |
setUpgraded(boolean upgraded)
Deprecated.
Unused. Will be removed in Tomcat 10.
|
void |
setWriteTimeout(long writeTimeout)
Set the timeout for writing.
|
String |
toString()
Overridden for debug purposes.
|
protected static int |
transfer(byte[] from,
int offset,
int length,
ByteBuffer to) |
protected static int |
transfer(ByteBuffer from,
ByteBuffer to) |
void |
unRead(ByteBuffer returnedInput)
Return input that has been read to the input buffer for re-reading by the
correct component.
|
protected <A> SocketWrapperBase.CompletionState |
vectoredOperation(boolean read,
ByteBuffer[] buffers,
int offset,
int length,
SocketWrapperBase.BlockingMode block,
long timeout,
TimeUnit unit,
A attachment,
SocketWrapperBase.CompletionCheck check,
CompletionHandler<Long,? super A> handler)
Vectored operation.
|
void |
write(boolean block,
byte[] buf,
int off,
int len)
Writes the provided data to the socket write buffer.
|
void |
write(boolean block,
ByteBuffer from)
Writes the provided data to the socket write buffer.
|
<A> SocketWrapperBase.CompletionState |
write(ByteBuffer[] srcs,
int offset,
int length,
SocketWrapperBase.BlockingMode block,
long timeout,
TimeUnit unit,
A attachment,
SocketWrapperBase.CompletionCheck check,
CompletionHandler<Long,? super A> handler)
Gather write.
|
<A> SocketWrapperBase.CompletionState |
write(long timeout,
TimeUnit unit,
A attachment,
CompletionHandler<Long,? super A> handler,
ByteBuffer... srcs)
Gather write.
|
<A> SocketWrapperBase.CompletionState |
write(SocketWrapperBase.BlockingMode block,
long timeout,
TimeUnit unit,
A attachment,
SocketWrapperBase.CompletionCheck check,
CompletionHandler<Long,? super A> handler,
ByteBuffer... srcs)
Gather write.
|
protected void |
writeBlocking(byte[] buf,
int off,
int len)
Writes the provided data to the socket write buffer.
|
protected void |
writeBlocking(ByteBuffer from)
Writes the provided data to the socket write buffer.
|
protected void |
writeNonBlocking(byte[] buf,
int off,
int len)
Transfers the data to the socket write buffer (writing that data to the
socket if the buffer fills up using a non-blocking write) until either
all the data has been transferred and space remains in the socket write
buffer or a non-blocking write leaves data in the socket write buffer.
|
protected void |
writeNonBlocking(ByteBuffer from)
Transfers the data to the socket write buffer (writing that data to the
socket if the buffer fills up using a non-blocking write) until either
all the data has been transferred and space remains in the socket write
buffer or a non-blocking write leaves data in the socket write buffer.
|
protected void |
writeNonBlockingInternal(ByteBuffer from)
Separate method so it can be re-used by the socket write buffer to write
data to the network
|
protected static final StringManager sm
protected final AtomicBoolean closed
protected String localAddr
protected String localName
protected int localPort
protected String remoteAddr
protected String remoteHost
protected int remotePort
protected volatile SocketBufferHandler socketBufferHandler
protected int bufferedWriteSize
protected final WriteBuffer nonBlockingWriteBuffer
protected final Semaphore readPending
protected volatile SocketWrapperBase.OperationState<?> readOperation
protected final Semaphore writePending
protected volatile SocketWrapperBase.OperationState<?> writeOperation
protected Object currentProcessor
public static final SocketWrapperBase.CompletionCheck COMPLETE_WRITE
public static final SocketWrapperBase.CompletionCheck COMPLETE_WRITE_WITH_COMPLETION
public static final SocketWrapperBase.CompletionCheck READ_DATA
public static final SocketWrapperBase.CompletionCheck COMPLETE_READ_WITH_COMPLETION
public static final SocketWrapperBase.CompletionCheck COMPLETE_READ
public SocketWrapperBase(E socket, AbstractEndpoint<E,?> endpoint)
public E getSocket()
protected void reset(E closedSocket)
protected AbstractEndpoint<E,?> getEndpoint()
public Object getCurrentProcessor()
public void setCurrentProcessor(Object currentProcessor)
public void execute(Runnable runnable)
runnable
- The actions to process on a container threadRejectedExecutionException
- If the runnable cannot be executedpublic IOException getError()
public void setError(IOException error)
public void checkError() throws IOException
IOException
@Deprecated public boolean isUpgraded()
true
if the connection has been upgraded.@Deprecated public void setUpgraded(boolean upgraded)
upgraded
- true
if the connection has been upgraded.@Deprecated public boolean isSecure()
true
if the connection uses TLS@Deprecated public void setSecure(boolean secure)
secure
- true
if the connection uses TLSpublic String getNegotiatedProtocol()
public void setNegotiatedProtocol(String negotiatedProtocol)
public void setReadTimeout(long readTimeout)
readTimeout
- The timeout in milliseconds. A value of -1 indicates
an infinite timeout.public long getReadTimeout()
public void setWriteTimeout(long writeTimeout)
writeTimeout
- The timeout in milliseconds. A value of zero or less
indicates an infinite timeout.public long getWriteTimeout()
public void setKeepAliveLeft(int keepAliveLeft)
public int decrementKeepAlive()
public String getRemoteHost()
protected abstract void populateRemoteHost()
public String getRemoteAddr()
protected abstract void populateRemoteAddr()
public int getRemotePort()
protected abstract void populateRemotePort()
public String getLocalName()
protected abstract void populateLocalName()
public String getLocalAddr()
protected abstract void populateLocalAddr()
public int getLocalPort()
protected abstract void populateLocalPort()
public SocketBufferHandler getSocketBufferHandler()
public boolean hasDataToRead()
public boolean hasDataToWrite()
public boolean isReadyForWrite()
registerWriteInterest()
to trigger a callback once the pending
writes have completed.
Note: Once this method has returned false
it MUST NOT
be called again until the pending write has completed and the
callback has been fired.
TODO: Modify registerWriteInterest()
so the above
restriction is enforced there rather than relying on the caller.
true
if no writes are pending and data can be
written otherwise false
public boolean canWrite()
public String toString()
public abstract int read(boolean block, byte[] b, int off, int len) throws IOException
IOException
public abstract int read(boolean block, ByteBuffer to) throws IOException
IOException
public abstract boolean isReadyForRead() throws IOException
IOException
public abstract void setAppReadBufHandler(ApplicationBufferHandler handler)
protected int populateReadBuffer(byte[] b, int off, int len)
protected int populateReadBuffer(ByteBuffer to)
public void unRead(ByteBuffer returnedInput)
returnedInput
- The input to return to the input buffer.public void close()
protected abstract void doClose()
public boolean isClosed()
public final void write(boolean block, byte[] buf, int off, int len) throws IOException
Non-blocking writes must return immediately and the byte array holding the data to be written must be immediately available for re-use. It may not be possible to write sufficient data to the network to allow this to happen. In this case data that cannot be written to the network and cannot be held by the socket buffer is stored in the non-blocking write buffer.
Note: There is an implementation assumption that, before switching from non-blocking writes to blocking writes, any data remaining in the non-blocking write buffer will have been written to the network.
block
- true
if a blocking write should be used,
otherwise a non-blocking write will be usedbuf
- The byte array containing the data to be writtenoff
- The offset within the byte array of the data to be writtenlen
- The length of the data to be writtenIOException
- If an IO error occurs during the writepublic final void write(boolean block, ByteBuffer from) throws IOException
Non-blocking writes must return immediately and the ByteBuffer holding the data to be written must be immediately available for re-use. It may not be possible to write sufficient data to the network to allow this to happen. In this case data that cannot be written to the network and cannot be held by the socket buffer is stored in the non-blocking write buffer.
Note: There is an implementation assumption that, before switching from non-blocking writes to blocking writes, any data remaining in the non-blocking write buffer will have been written to the network.
block
- true
if a blocking write should be used,
otherwise a non-blocking write will be usedfrom
- The ByteBuffer containing the data to be writtenIOException
- If an IO error occurs during the writeprotected void writeBlocking(byte[] buf, int off, int len) throws IOException
buf
- The byte array containing the data to be writtenoff
- The offset within the byte array of the data to be writtenlen
- The length of the data to be writtenIOException
- If an IO error occurs during the writeprotected void writeBlocking(ByteBuffer from) throws IOException
from
- The ByteBuffer containing the data to be writtenIOException
- If an IO error occurs during the writeprotected void writeNonBlocking(byte[] buf, int off, int len) throws IOException
buf
- The byte array containing the data to be writtenoff
- The offset within the byte array of the data to be writtenlen
- The length of the data to be writtenIOException
- If an IO error occurs during the writeprotected void writeNonBlocking(ByteBuffer from) throws IOException
from
- The ByteBuffer containing the data to be writtenIOException
- If an IO error occurs during the writeprotected void writeNonBlockingInternal(ByteBuffer from) throws IOException
from
- The ByteBuffer containing the data to be writtenIOException
- If an IO error occurs during the writepublic boolean flush(boolean block) throws IOException
block
- true
if a blocking write should be used,
otherwise a non-blocking write will be usedtrue
if data remains to be flushed after this method
completes, otherwise false
. In blocking mode
therefore, the return value should always be false
IOException
- If an IO error occurs during the writeprotected void flushBlocking() throws IOException
IOException
protected boolean flushNonBlocking() throws IOException
IOException
protected void doWrite(boolean block) throws IOException
block
- Should the write be blocking or not?IOException
- If an I/O error such as a timeout occurs during the
writeprotected abstract void doWrite(boolean block, ByteBuffer from) throws IOException
block
- Should the write be blocking or not?from
- the ByteBuffer containing the data to be writtenIOException
- If an I/O error such as a timeout occurs during the
writepublic void processSocket(SocketEvent socketStatus, boolean dispatch)
public abstract void registerReadInterest()
public abstract void registerWriteInterest()
public abstract SendfileDataBase createSendfileData(String filename, long pos, long length)
public abstract SendfileState processSendfile(SendfileDataBase sendfileData)
sendfileData
- Data representing the file to sendpublic abstract void doClientAuth(SSLSupport sslSupport) throws IOException
sslSupport
- The SSL/TLS support instance currently being used by
the connection that may need updating after the client
authenticationIOException
- If authentication is required then there will be I/O
with the client and this exception will be thrown if
that goes wrongpublic abstract SSLSupport getSslSupport(String clientCertProvider)
public boolean hasAsyncIO()
true
if the connector has the capability enabledpublic boolean needSemaphores()
false
public boolean hasPerOperationTimeout()
false
public boolean isReadPending()
true
if the endpoint supports asynchronous IO and
a read operation is being processed asynchronouslypublic boolean isWritePending()
true
if the endpoint supports asynchronous IO and
a write operation is being processed asynchronously@Deprecated public boolean awaitReadComplete(long timeout, TimeUnit unit)
timeout
- The maximum amount of time to waitunit
- The unit for the timeouttrue
if the read operation is complete,
false
if the operation is still pending and
the specified timeout has passed@Deprecated public boolean awaitWriteComplete(long timeout, TimeUnit unit)
timeout
- The maximum amount of time to waitunit
- The unit for the timeouttrue
if the read operation is complete,
false
if the operation is still pending and
the specified timeout has passedpublic final <A> SocketWrapperBase.CompletionState read(long timeout, TimeUnit unit, A attachment, CompletionHandler<Long,? super A> handler, ByteBuffer... dsts)
A
- The attachment typetimeout
- timeout duration for the readunit
- units for the timeout durationattachment
- an object to attach to the I/O operation that will be
used when calling the completion handlerhandler
- to call when the IO is completedsts
- bufferspublic final <A> SocketWrapperBase.CompletionState read(SocketWrapperBase.BlockingMode block, long timeout, TimeUnit unit, A attachment, SocketWrapperBase.CompletionCheck check, CompletionHandler<Long,? super A> handler, ByteBuffer... dsts)
A
- The attachment typeblock
- is the blocking mode that will be used for this operationtimeout
- timeout duration for the readunit
- units for the timeout durationattachment
- an object to attach to the I/O operation that will be
used when calling the completion handlercheck
- for the IO operation completionhandler
- to call when the IO is completedsts
- bufferspublic final <A> SocketWrapperBase.CompletionState read(ByteBuffer[] dsts, int offset, int length, SocketWrapperBase.BlockingMode block, long timeout, TimeUnit unit, A attachment, SocketWrapperBase.CompletionCheck check, CompletionHandler<Long,? super A> handler)
A
- The attachment typedsts
- buffersoffset
- in the buffer arraylength
- in the buffer arrayblock
- is the blocking mode that will be used for this operationtimeout
- timeout duration for the readunit
- units for the timeout durationattachment
- an object to attach to the I/O operation that will be
used when calling the completion handlercheck
- for the IO operation completionhandler
- to call when the IO is completepublic final <A> SocketWrapperBase.CompletionState write(long timeout, TimeUnit unit, A attachment, CompletionHandler<Long,? super A> handler, ByteBuffer... srcs)
A
- The attachment typetimeout
- timeout duration for the writeunit
- units for the timeout durationattachment
- an object to attach to the I/O operation that will be
used when calling the completion handlerhandler
- to call when the IO is completesrcs
- bufferspublic final <A> SocketWrapperBase.CompletionState write(SocketWrapperBase.BlockingMode block, long timeout, TimeUnit unit, A attachment, SocketWrapperBase.CompletionCheck check, CompletionHandler<Long,? super A> handler, ByteBuffer... srcs)
A
- The attachment typeblock
- is the blocking mode that will be used for this operationtimeout
- timeout duration for the writeunit
- units for the timeout durationattachment
- an object to attach to the I/O operation that will be
used when calling the completion handlercheck
- for the IO operation completionhandler
- to call when the IO is completesrcs
- bufferspublic final <A> SocketWrapperBase.CompletionState write(ByteBuffer[] srcs, int offset, int length, SocketWrapperBase.BlockingMode block, long timeout, TimeUnit unit, A attachment, SocketWrapperBase.CompletionCheck check, CompletionHandler<Long,? super A> handler)
A
- The attachment typesrcs
- buffersoffset
- in the buffer arraylength
- in the buffer arrayblock
- is the blocking mode that will be used for this operationtimeout
- timeout duration for the writeunit
- units for the timeout durationattachment
- an object to attach to the I/O operation that will be
used when calling the completion handlercheck
- for the IO operation completionhandler
- to call when the IO is completeprotected final <A> SocketWrapperBase.CompletionState vectoredOperation(boolean read, ByteBuffer[] buffers, int offset, int length, SocketWrapperBase.BlockingMode block, long timeout, TimeUnit unit, A attachment, SocketWrapperBase.CompletionCheck check, CompletionHandler<Long,? super A> handler)
A
- The attachment typeread
- true if the operation is a read, false if it is a writebuffers
- buffersoffset
- in the buffer arraylength
- in the buffer arrayblock
- is the blocking mode that will be used for this operationtimeout
- timeout duration for the writeunit
- units for the timeout durationattachment
- an object to attach to the I/O operation that will be
used when calling the completion handlercheck
- for the IO operation completionhandler
- to call when the IO is completeprotected abstract <A> SocketWrapperBase.OperationState<A> newOperationState(boolean read, ByteBuffer[] buffers, int offset, int length, SocketWrapperBase.BlockingMode block, long timeout, TimeUnit unit, A attachment, SocketWrapperBase.CompletionCheck check, CompletionHandler<Long,? super A> handler, Semaphore semaphore, SocketWrapperBase.VectoredIOCompletionHandler<A> completion)
protected static int transfer(byte[] from, int offset, int length, ByteBuffer to)
protected static int transfer(ByteBuffer from, ByteBuffer to)
protected static boolean buffersArrayHasRemaining(ByteBuffer[] buffers, int offset, int length)
Copyright © 2000-2020 Apache Software Foundation. All Rights Reserved.