public abstract class SocketWrapperBase<E> extends Object
Modifier and Type | Class and Description |
---|---|
static interface |
SocketWrapperBase.CompletionCheck |
static class |
SocketWrapperBase.CompletionHandlerCall |
static class |
SocketWrapperBase.CompletionState |
Modifier and Type | Field and Description |
---|---|
protected LinkedBlockingDeque<ByteBufferHolder> |
bufferedWrites
For "non-blocking" writes use an external set of buffers.
|
protected int |
bufferedWriteSize
The max size of the buffered write buffer
|
static SocketWrapperBase.CompletionCheck |
COMPLETE_WRITE
This utility CompletionCheck will cause the write to fully write
all remaining data.
|
protected String |
localAddr |
protected String |
localName |
protected int |
localPort |
static SocketWrapperBase.CompletionCheck |
READ_DATA
This utility CompletionCheck will cause the completion handler
to be called once some data has been read.
|
protected String |
remoteAddr |
protected String |
remoteHost |
protected int |
remotePort |
protected static StringManager |
sm |
protected SocketBufferHandler |
socketBufferHandler
The buffers used for communicating with the socket.
|
Constructor and Description |
---|
SocketWrapperBase(E socket,
AbstractEndpoint<E> endpoint) |
Modifier and Type | Method and Description |
---|---|
protected void |
addToBuffers(byte[] buf,
int offset,
int length) |
boolean |
awaitReadComplete(long timeout,
TimeUnit unit)
If an asynchronous read operation is pending, this method will block
until the operation completes, or the specified amount of time
has passed.
|
boolean |
awaitWriteComplete(long timeout,
TimeUnit unit)
If an asynchronous write operation is pending, this method will block
until the operation completes, or the specified amount of time
has passed.
|
boolean |
canWrite() |
void |
checkError() |
abstract void |
close() |
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 |
doWrite(boolean block)
Write the contents of the socketWriteBuffer to the socket.
|
void |
executeNonBlockingDispatches(Iterator<DispatchType> dispatches) |
boolean |
flush(boolean block)
Writes as much data as possible from any that remains in the buffers.
|
protected void |
flushBlocking() |
protected boolean |
flushNonBlocking() |
boolean |
getBlockingStatus() |
Lock |
getBlockingStatusReadLock() |
ReentrantReadWriteLock.WriteLock |
getBlockingStatusWriteLock() |
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 only for connectors that can
efficiently support it.
|
boolean |
hasDataToWrite() |
abstract 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() |
boolean |
isUpgraded() |
boolean |
isWritePending()
Allows checking if an asynchronous write operation is currently pending.
|
protected abstract void |
populateLocalAddr() |
protected abstract void |
populateLocalName() |
protected abstract void |
populateLocalPort() |
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) |
<A> SocketWrapperBase.CompletionState |
read(boolean block,
long timeout,
TimeUnit unit,
A attachment,
SocketWrapperBase.CompletionCheck check,
CompletionHandler<Long,? super A> handler,
ByteBuffer... dsts)
Scatter read.
|
<A> SocketWrapperBase.CompletionState |
read(ByteBuffer[] dsts,
int offset,
int length,
boolean block,
long timeout,
TimeUnit unit,
A attachment,
SocketWrapperBase.CompletionCheck check,
CompletionHandler<Long,? super A> handler)
Scatter read.
|
abstract void |
registerReadInterest() |
abstract void |
registerWriteInterest() |
void |
setBlockingStatus(boolean blockingStatus) |
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) |
void |
setUpgraded(boolean upgraded) |
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 void |
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.
|
void |
write(boolean block,
byte[] buf,
int off,
int len)
Writes the provided data to the socket, buffering any remaining data if
used in non-blocking mode.
|
<A> SocketWrapperBase.CompletionState |
write(boolean block,
long timeout,
TimeUnit unit,
A attachment,
SocketWrapperBase.CompletionCheck check,
CompletionHandler<Long,? super A> handler,
ByteBuffer... srcs)
Gather write.
|
<A> SocketWrapperBase.CompletionState |
write(ByteBuffer[] srcs,
int offset,
int length,
boolean block,
long timeout,
TimeUnit unit,
A attachment,
SocketWrapperBase.CompletionCheck check,
CompletionHandler<Long,? super A> handler)
Gather write.
|
protected void |
writeBlocking(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 blocking write) until all the data
has been transferred and space remains in 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 static final StringManager sm
protected String localAddr
protected String localName
protected int localPort
protected String remoteAddr
protected String remoteHost
protected int remotePort
protected volatile SocketBufferHandler socketBufferHandler
protected final LinkedBlockingDeque<ByteBufferHolder> bufferedWrites
protected int bufferedWriteSize
public static final SocketWrapperBase.CompletionCheck COMPLETE_WRITE
public static final SocketWrapperBase.CompletionCheck READ_DATA
public SocketWrapperBase(E socket, AbstractEndpoint<E> endpoint)
public E getSocket()
public AbstractEndpoint<E> getEndpoint()
public IOException getError()
public void setError(IOException error)
public void checkError() throws IOException
IOException
public boolean isUpgraded()
public void setUpgraded(boolean upgraded)
public boolean isSecure()
public void setSecure(boolean secure)
public 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 boolean getBlockingStatus()
public void setBlockingStatus(boolean blockingStatus)
public Lock getBlockingStatusReadLock()
public ReentrantReadWriteLock.WriteLock getBlockingStatusWriteLock()
public SocketBufferHandler getSocketBufferHandler()
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 boolean isReadyForRead() throws IOException
IOException
public void unRead(ByteBuffer returnedInput)
returnedInput
- The input to return to the input buffer.public abstract void close() throws IOException
IOException
public abstract boolean isClosed()
public final void write(boolean block, byte[] buf, int off, int len) throws IOException
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 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 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 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 abstract 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 void addToBuffers(byte[] buf, int offset, int length)
public void processSocket(SocketEvent socketStatus, boolean dispatch)
public void executeNonBlockingDispatches(Iterator<DispatchType> dispatches)
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)
sslSupport
- The SSL/TLS support instance currently being used by
the connection that may need updating after the client
authenticationpublic abstract SSLSupport getSslSupport(String clientCertProvider)
public boolean hasAsyncIO()
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 asynchronouslypublic 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 passedpublic 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(boolean block, long timeout, TimeUnit unit, A attachment, SocketWrapperBase.CompletionCheck check, CompletionHandler<Long,? super A> handler, ByteBuffer... dsts)
A
- The attachment typeblock
- true to block until any pending read is done, if the
timeout occurs and a read is still pending, a
ReadPendingException will be thrown; false to
not block but any pending read operation will cause
a ReadPendingExceptiontimeout
- 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 <A> SocketWrapperBase.CompletionState read(ByteBuffer[] dsts, int offset, int length, boolean 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
- true to block until any pending read is done, if the
timeout occurs and a read is still pending, a
ReadPendingException will be thrown; false to
not block but any pending read operation will cause
a ReadPendingExceptiontimeout
- 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(boolean block, long timeout, TimeUnit unit, A attachment, SocketWrapperBase.CompletionCheck check, CompletionHandler<Long,? super A> handler, ByteBuffer... srcs)
A
- The attachment typeblock
- true to block until any pending write is done, if the
timeout occurs and a write is still pending, a
WritePendingException will be thrown; false to
not block but any pending write operation will cause
a WritePendingExceptiontimeout
- 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 <A> SocketWrapperBase.CompletionState write(ByteBuffer[] srcs, int offset, int length, boolean 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
- true to block until any pending write is done, if the
timeout occurs and a write is still pending, a
WritePendingException will be thrown; false to
not block but any pending write operation will cause
a WritePendingExceptiontimeout
- 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 static int transfer(byte[] from, int offset, int length, ByteBuffer to)
protected static void transfer(ByteBuffer from, ByteBuffer to)
Copyright © 2000-2016 Apache Software Foundation. All Rights Reserved.