public final class ChannelOutboundBuffer
extends java.lang.Object
AbstractChannel
to store its pending
outbound write requests.
All methods must be called by a transport implementation from an I/O thread, except the following ones:
Modifier and Type | Class and Description |
---|---|
(package private) static class |
ChannelOutboundBuffer.Entry |
static interface |
ChannelOutboundBuffer.MessageProcessor |
Modifier and Type | Field and Description |
---|---|
private Channel |
channel |
private java.lang.Runnable |
fireChannelWritabilityChangedTask |
private int |
flushed |
private ChannelOutboundBuffer.Entry |
flushedEntry |
private boolean |
inFail |
private static InternalLogger |
logger |
private static FastThreadLocal<java.nio.ByteBuffer[]> |
NIO_BUFFERS |
private int |
nioBufferCount |
private long |
nioBufferSize |
private ChannelOutboundBuffer.Entry |
tailEntry |
private static java.util.concurrent.atomic.AtomicLongFieldUpdater<ChannelOutboundBuffer> |
TOTAL_PENDING_SIZE_UPDATER |
private long |
totalPendingSize |
private ChannelOutboundBuffer.Entry |
unflushedEntry |
private int |
unwritable |
private static java.util.concurrent.atomic.AtomicIntegerFieldUpdater<ChannelOutboundBuffer> |
UNWRITABLE_UPDATER |
Constructor and Description |
---|
ChannelOutboundBuffer(AbstractChannel channel) |
Modifier and Type | Method and Description |
---|---|
void |
addFlush()
Add a flush to this
ChannelOutboundBuffer . |
void |
addMessage(java.lang.Object msg,
int size,
ChannelPromise promise)
Add given message to this
ChannelOutboundBuffer . |
long |
bytesBeforeUnwritable()
Get how many bytes can be written until
isWritable() returns false . |
long |
bytesBeforeWritable()
Get how many bytes must be drained from the underlying buffer until
isWritable() returns true . |
private void |
clearNioBuffers() |
private void |
clearUserDefinedWritability(int index) |
(package private) void |
close(java.nio.channels.ClosedChannelException cause) |
java.lang.Object |
current()
Return the current message to write or
null if nothing was flushed before and so is ready to be written. |
(package private) void |
decrementPendingOutboundBytes(long size)
Decrement the pending bytes which will be written at some point.
|
private void |
decrementPendingOutboundBytes(long size,
boolean invokeLater,
boolean notifyWritability) |
private static java.nio.ByteBuffer[] |
expandNioBufferArray(java.nio.ByteBuffer[] array,
int neededSpace,
int size) |
(package private) void |
failFlushed(java.lang.Throwable cause,
boolean notify) |
private static int |
fillBufferArray(java.nio.ByteBuffer[] nioBufs,
java.nio.ByteBuffer[] nioBuffers,
int nioBufferCount) |
private void |
fireChannelWritabilityChanged(boolean invokeLater) |
void |
forEachFlushedMessage(ChannelOutboundBuffer.MessageProcessor processor)
Call
ChannelOutboundBuffer.MessageProcessor.processMessage(Object) for each flushed message
in this ChannelOutboundBuffer until ChannelOutboundBuffer.MessageProcessor.processMessage(Object)
returns false or there are no more flushed messages to process. |
boolean |
getUserDefinedWritability(int index)
Returns
true if and only if the user-defined writability flag at the specified index is set to
true . |
(package private) void |
incrementPendingOutboundBytes(long size)
Increment the pending bytes which will be written at some point.
|
private void |
incrementPendingOutboundBytes(long size,
boolean invokeLater) |
boolean |
isEmpty()
|
private boolean |
isFlushedEntry(ChannelOutboundBuffer.Entry e) |
boolean |
isWritable()
Returns
true if and only if the total number of pending bytes did
not exceed the write watermark of the Channel and
no user-defined writability flag has been set to
false . |
int |
nioBufferCount()
Returns the number of
ByteBuffer that can be written out of the ByteBuffer array that was
obtained via nioBuffers() . |
java.nio.ByteBuffer[] |
nioBuffers()
Returns an array of direct NIO buffers if the currently pending messages are made of
ByteBuf only. |
long |
nioBufferSize()
Returns the number of bytes that can be written out of the
ByteBuffer array that was
obtained via nioBuffers() . |
void |
progress(long amount)
Notify the
ChannelPromise of the current message about writing progress. |
void |
recycle()
Deprecated.
|
boolean |
remove()
Will remove the current message, mark its
ChannelPromise as success and return true . |
boolean |
remove(java.lang.Throwable cause)
Will remove the current message, mark its
ChannelPromise as failure using the given Throwable
and return true . |
private boolean |
remove0(java.lang.Throwable cause,
boolean notifyWritability) |
void |
removeBytes(long writtenBytes)
Removes the fully written entries and update the reader index of the partially written entry.
|
private void |
removeEntry(ChannelOutboundBuffer.Entry e) |
private static void |
safeFail(ChannelPromise promise,
java.lang.Throwable cause) |
private static void |
safeSuccess(ChannelPromise promise) |
private void |
setUnwritable(boolean invokeLater) |
private void |
setUserDefinedWritability(int index) |
void |
setUserDefinedWritability(int index,
boolean writable)
Sets a user-defined writability flag at the specified index.
|
private void |
setWritable(boolean invokeLater) |
int |
size()
Returns the number of flushed messages in this
ChannelOutboundBuffer . |
private static long |
total(java.lang.Object msg) |
long |
totalPendingWriteBytes() |
private static int |
writabilityMask(int index) |
private static final InternalLogger logger
private static final FastThreadLocal<java.nio.ByteBuffer[]> NIO_BUFFERS
private final Channel channel
private ChannelOutboundBuffer.Entry flushedEntry
private ChannelOutboundBuffer.Entry unflushedEntry
private ChannelOutboundBuffer.Entry tailEntry
private int flushed
private int nioBufferCount
private long nioBufferSize
private boolean inFail
private static final java.util.concurrent.atomic.AtomicLongFieldUpdater<ChannelOutboundBuffer> TOTAL_PENDING_SIZE_UPDATER
private volatile long totalPendingSize
private static final java.util.concurrent.atomic.AtomicIntegerFieldUpdater<ChannelOutboundBuffer> UNWRITABLE_UPDATER
private volatile int unwritable
private volatile java.lang.Runnable fireChannelWritabilityChangedTask
ChannelOutboundBuffer(AbstractChannel channel)
public void addMessage(java.lang.Object msg, int size, ChannelPromise promise)
ChannelOutboundBuffer
. The given ChannelPromise
will be notified once
the message was written.public void addFlush()
ChannelOutboundBuffer
. This means all previous added messages are marked as flushed
and so you will be able to handle them.void incrementPendingOutboundBytes(long size)
private void incrementPendingOutboundBytes(long size, boolean invokeLater)
void decrementPendingOutboundBytes(long size)
private void decrementPendingOutboundBytes(long size, boolean invokeLater, boolean notifyWritability)
private static long total(java.lang.Object msg)
public java.lang.Object current()
null
if nothing was flushed before and so is ready to be written.public void progress(long amount)
ChannelPromise
of the current message about writing progress.public boolean remove()
ChannelPromise
as success and return true
. If no
flushed message exists at the time this method is called it will return false
to signal that no more
messages are ready to be handled.public boolean remove(java.lang.Throwable cause)
ChannelPromise
as failure using the given Throwable
and return true
. If no flushed message exists at the time this method is called it will return
false
to signal that no more messages are ready to be handled.private boolean remove0(java.lang.Throwable cause, boolean notifyWritability)
private void removeEntry(ChannelOutboundBuffer.Entry e)
public void removeBytes(long writtenBytes)
ByteBuf
.private void clearNioBuffers()
public java.nio.ByteBuffer[] nioBuffers()
ByteBuf
only.
nioBufferCount()
and nioBufferSize()
will return the number of NIO buffers in the returned
array and the total number of readable bytes of the NIO buffers respectively.
Note that the returned array is reused and thus should not escape
AbstractChannel.doWrite(ChannelOutboundBuffer)
.
Refer to NioSocketChannel.doWrite(ChannelOutboundBuffer)
for an example.
private static int fillBufferArray(java.nio.ByteBuffer[] nioBufs, java.nio.ByteBuffer[] nioBuffers, int nioBufferCount)
private static java.nio.ByteBuffer[] expandNioBufferArray(java.nio.ByteBuffer[] array, int neededSpace, int size)
public int nioBufferCount()
ByteBuffer
that can be written out of the ByteBuffer
array that was
obtained via nioBuffers()
. This method MUST be called after nioBuffers()
was called.public long nioBufferSize()
ByteBuffer
array that was
obtained via nioBuffers()
. This method MUST be called after nioBuffers()
was called.public boolean isWritable()
true
if and only if the total number of pending bytes did
not exceed the write watermark of the Channel
and
no user-defined writability flag has been set to
false
.public boolean getUserDefinedWritability(int index)
true
if and only if the user-defined writability flag at the specified index is set to
true
.public void setUserDefinedWritability(int index, boolean writable)
private void setUserDefinedWritability(int index)
private void clearUserDefinedWritability(int index)
private static int writabilityMask(int index)
private void setWritable(boolean invokeLater)
private void setUnwritable(boolean invokeLater)
private void fireChannelWritabilityChanged(boolean invokeLater)
public int size()
ChannelOutboundBuffer
.public boolean isEmpty()
void failFlushed(java.lang.Throwable cause, boolean notify)
void close(java.nio.channels.ClosedChannelException cause)
private static void safeSuccess(ChannelPromise promise)
private static void safeFail(ChannelPromise promise, java.lang.Throwable cause)
@Deprecated public void recycle()
public long totalPendingWriteBytes()
public long bytesBeforeUnwritable()
isWritable()
returns false
.
This quantity will always be non-negative. If isWritable()
is false
then 0.public long bytesBeforeWritable()
isWritable()
returns true
.
This quantity will always be non-negative. If isWritable()
is true
then 0.public void forEachFlushedMessage(ChannelOutboundBuffer.MessageProcessor processor) throws java.lang.Exception
ChannelOutboundBuffer.MessageProcessor.processMessage(Object)
for each flushed message
in this ChannelOutboundBuffer
until ChannelOutboundBuffer.MessageProcessor.processMessage(Object)
returns false
or there are no more flushed messages to process.java.lang.Exception
private boolean isFlushedEntry(ChannelOutboundBuffer.Entry e)