Package io.netty.channel
Class ChannelOutboundBuffer
- java.lang.Object
-
- io.netty.channel.ChannelOutboundBuffer
-
public final class ChannelOutboundBuffer extends java.lang.Object(Transport implementors only) an internal data structure used byAbstractChannelto store its pending outbound write requests.All methods must be called by a transport implementation from an I/O thread, except the following ones:
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static classChannelOutboundBuffer.Entrystatic interfaceChannelOutboundBuffer.MessageProcessor
-
Field Summary
Fields Modifier and Type Field Description private Channelchannel(package private) static intCHANNEL_OUTBOUND_BUFFER_ENTRY_OVERHEADprivate java.lang.RunnablefireChannelWritabilityChangedTaskprivate intflushedprivate ChannelOutboundBuffer.EntryflushedEntryprivate booleaninFailprivate static InternalLoggerloggerprivate static FastThreadLocal<java.nio.ByteBuffer[]>NIO_BUFFERSprivate intnioBufferCountprivate longnioBufferSizeprivate ChannelOutboundBuffer.EntrytailEntryprivate static java.util.concurrent.atomic.AtomicLongFieldUpdater<ChannelOutboundBuffer>TOTAL_PENDING_SIZE_UPDATERprivate longtotalPendingSizeprivate ChannelOutboundBuffer.EntryunflushedEntryprivate intunwritableprivate static java.util.concurrent.atomic.AtomicIntegerFieldUpdater<ChannelOutboundBuffer>UNWRITABLE_UPDATER
-
Constructor Summary
Constructors Constructor Description ChannelOutboundBuffer(AbstractChannel channel)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidaddFlush()Add a flush to thisChannelOutboundBuffer.voidaddMessage(java.lang.Object msg, int size, ChannelPromise promise)Add given message to thisChannelOutboundBuffer.longbytesBeforeUnwritable()Get how many bytes can be written untilisWritable()returnsfalse.longbytesBeforeWritable()Get how many bytes must be drained from the underlying buffer untilisWritable()returnstrue.private voidclearNioBuffers()private voidclearUserDefinedWritability(int index)(package private) voidclose(java.lang.Throwable cause, boolean allowChannelOpen)(package private) voidclose(java.nio.channels.ClosedChannelException cause)java.lang.Objectcurrent()Return the current message to write ornullif nothing was flushed before and so is ready to be written.longcurrentProgress()Return the current message flush progress.(package private) voiddecrementPendingOutboundBytes(long size)Decrement the pending bytes which will be written at some point.private voiddecrementPendingOutboundBytes(long size, boolean invokeLater, boolean notifyWritability)private static java.nio.ByteBuffer[]expandNioBufferArray(java.nio.ByteBuffer[] array, int neededSpace, int size)(package private) voidfailFlushed(java.lang.Throwable cause, boolean notify)private voidfireChannelWritabilityChanged(boolean invokeLater)voidforEachFlushedMessage(ChannelOutboundBuffer.MessageProcessor processor)CallChannelOutboundBuffer.MessageProcessor.processMessage(Object)for each flushed message in thisChannelOutboundBufferuntilChannelOutboundBuffer.MessageProcessor.processMessage(Object)returnsfalseor there are no more flushed messages to process.booleangetUserDefinedWritability(int index)Returnstrueif and only if the user-defined writability flag at the specified index is set totrue.(package private) voidincrementPendingOutboundBytes(long size)Increment the pending bytes which will be written at some point.private voidincrementPendingOutboundBytes(long size, boolean invokeLater)booleanisEmpty()private booleanisFlushedEntry(ChannelOutboundBuffer.Entry e)booleanisWritable()Returnstrueif and only if the total number of pending bytes did not exceed the write watermark of theChanneland no user-defined writability flag has been set tofalse.intnioBufferCount()Returns the number ofByteBufferthat can be written out of theByteBufferarray that was obtained vianioBuffers().java.nio.ByteBuffer[]nioBuffers()Returns an array of direct NIO buffers if the currently pending messages are made ofByteBufonly.java.nio.ByteBuffer[]nioBuffers(int maxCount, long maxBytes)Returns an array of direct NIO buffers if the currently pending messages are made ofByteBufonly.private static intnioBuffers(ChannelOutboundBuffer.Entry entry, ByteBuf buf, java.nio.ByteBuffer[] nioBuffers, int nioBufferCount, int maxCount)longnioBufferSize()Returns the number of bytes that can be written out of theByteBufferarray that was obtained vianioBuffers().voidprogress(long amount)Notify theChannelPromiseof the current message about writing progress.voidrecycle()Deprecated.booleanremove()Will remove the current message, mark itsChannelPromiseas success and returntrue.booleanremove(java.lang.Throwable cause)Will remove the current message, mark itsChannelPromiseas failure using the givenThrowableand returntrue.private booleanremove0(java.lang.Throwable cause, boolean notifyWritability)voidremoveBytes(long writtenBytes)Removes the fully written entries and update the reader index of the partially written entry.private voidremoveEntry(ChannelOutboundBuffer.Entry e)private static voidsafeFail(ChannelPromise promise, java.lang.Throwable cause)private static voidsafeSuccess(ChannelPromise promise)private voidsetUnwritable(boolean invokeLater)private voidsetUserDefinedWritability(int index)voidsetUserDefinedWritability(int index, boolean writable)Sets a user-defined writability flag at the specified index.private voidsetWritable(boolean invokeLater)intsize()Returns the number of flushed messages in thisChannelOutboundBuffer.private static longtotal(java.lang.Object msg)longtotalPendingWriteBytes()private static intwritabilityMask(int index)
-
-
-
Field Detail
-
CHANNEL_OUTBOUND_BUFFER_ENTRY_OVERHEAD
static final int CHANNEL_OUTBOUND_BUFFER_ENTRY_OVERHEAD
-
logger
private static final InternalLogger logger
-
NIO_BUFFERS
private static final FastThreadLocal<java.nio.ByteBuffer[]> NIO_BUFFERS
-
channel
private final Channel channel
-
flushedEntry
private ChannelOutboundBuffer.Entry flushedEntry
-
unflushedEntry
private ChannelOutboundBuffer.Entry unflushedEntry
-
tailEntry
private ChannelOutboundBuffer.Entry tailEntry
-
flushed
private int flushed
-
nioBufferCount
private int nioBufferCount
-
nioBufferSize
private long nioBufferSize
-
inFail
private boolean inFail
-
TOTAL_PENDING_SIZE_UPDATER
private static final java.util.concurrent.atomic.AtomicLongFieldUpdater<ChannelOutboundBuffer> TOTAL_PENDING_SIZE_UPDATER
-
totalPendingSize
private volatile long totalPendingSize
-
UNWRITABLE_UPDATER
private static final java.util.concurrent.atomic.AtomicIntegerFieldUpdater<ChannelOutboundBuffer> UNWRITABLE_UPDATER
-
unwritable
private volatile int unwritable
-
fireChannelWritabilityChangedTask
private volatile java.lang.Runnable fireChannelWritabilityChangedTask
-
-
Constructor Detail
-
ChannelOutboundBuffer
ChannelOutboundBuffer(AbstractChannel channel)
-
-
Method Detail
-
addMessage
public void addMessage(java.lang.Object msg, int size, ChannelPromise promise)Add given message to thisChannelOutboundBuffer. The givenChannelPromisewill be notified once the message was written.
-
addFlush
public void addFlush()
Add a flush to thisChannelOutboundBuffer. This means all previous added messages are marked as flushed and so you will be able to handle them.
-
incrementPendingOutboundBytes
void incrementPendingOutboundBytes(long size)
Increment the pending bytes which will be written at some point. This method is thread-safe!
-
incrementPendingOutboundBytes
private void incrementPendingOutboundBytes(long size, boolean invokeLater)
-
decrementPendingOutboundBytes
void decrementPendingOutboundBytes(long size)
Decrement the pending bytes which will be written at some point. This method is thread-safe!
-
decrementPendingOutboundBytes
private void decrementPendingOutboundBytes(long size, boolean invokeLater, boolean notifyWritability)
-
total
private static long total(java.lang.Object msg)
-
current
public java.lang.Object current()
Return the current message to write ornullif nothing was flushed before and so is ready to be written.
-
currentProgress
public long currentProgress()
Return the current message flush progress.- Returns:
0if nothing was flushed before for the current message or there is no current message
-
progress
public void progress(long amount)
Notify theChannelPromiseof the current message about writing progress.
-
remove
public boolean remove()
Will remove the current message, mark itsChannelPromiseas success and returntrue. If no flushed message exists at the time this method is called it will returnfalseto signal that no more messages are ready to be handled.
-
remove
public boolean remove(java.lang.Throwable cause)
Will remove the current message, mark itsChannelPromiseas failure using the givenThrowableand returntrue. If no flushed message exists at the time this method is called it will returnfalseto signal that no more messages are ready to be handled.
-
remove0
private boolean remove0(java.lang.Throwable cause, boolean notifyWritability)
-
removeEntry
private void removeEntry(ChannelOutboundBuffer.Entry e)
-
removeBytes
public void removeBytes(long writtenBytes)
Removes the fully written entries and update the reader index of the partially written entry. This operation assumes all messages in this buffer isByteBuf.
-
clearNioBuffers
private void clearNioBuffers()
-
nioBuffers
public java.nio.ByteBuffer[] nioBuffers()
Returns an array of direct NIO buffers if the currently pending messages are made ofByteBufonly.nioBufferCount()andnioBufferSize()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 toNioSocketChannel.doWrite(ChannelOutboundBuffer)for an example.
-
nioBuffers
public java.nio.ByteBuffer[] nioBuffers(int maxCount, long maxBytes)Returns an array of direct NIO buffers if the currently pending messages are made ofByteBufonly.nioBufferCount()andnioBufferSize()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 toNioSocketChannel.doWrite(ChannelOutboundBuffer)for an example.- Parameters:
maxCount- The maximum amount of buffers that will be added to the return value.maxBytes- A hint toward the maximum number of bytes to include as part of the return value. Note that this value maybe exceeded because we make a best effort to include at least 1ByteBufferin the return value to ensure write progress is made.
-
nioBuffers
private static int nioBuffers(ChannelOutboundBuffer.Entry entry, ByteBuf buf, java.nio.ByteBuffer[] nioBuffers, int nioBufferCount, int maxCount)
-
expandNioBufferArray
private static java.nio.ByteBuffer[] expandNioBufferArray(java.nio.ByteBuffer[] array, int neededSpace, int size)
-
nioBufferCount
public int nioBufferCount()
Returns the number ofByteBufferthat can be written out of theByteBufferarray that was obtained vianioBuffers(). This method MUST be called afternioBuffers()was called.
-
nioBufferSize
public long nioBufferSize()
Returns the number of bytes that can be written out of theByteBufferarray that was obtained vianioBuffers(). This method MUST be called afternioBuffers()was called.
-
isWritable
public boolean isWritable()
Returnstrueif and only if the total number of pending bytes did not exceed the write watermark of theChanneland no user-defined writability flag has been set tofalse.
-
getUserDefinedWritability
public boolean getUserDefinedWritability(int index)
Returnstrueif and only if the user-defined writability flag at the specified index is set totrue.
-
setUserDefinedWritability
public void setUserDefinedWritability(int index, boolean writable)Sets a user-defined writability flag at the specified index.
-
setUserDefinedWritability
private void setUserDefinedWritability(int index)
-
clearUserDefinedWritability
private void clearUserDefinedWritability(int index)
-
writabilityMask
private static int writabilityMask(int index)
-
setWritable
private void setWritable(boolean invokeLater)
-
setUnwritable
private void setUnwritable(boolean invokeLater)
-
fireChannelWritabilityChanged
private void fireChannelWritabilityChanged(boolean invokeLater)
-
size
public int size()
Returns the number of flushed messages in thisChannelOutboundBuffer.
-
isEmpty
public boolean isEmpty()
-
failFlushed
void failFlushed(java.lang.Throwable cause, boolean notify)
-
close
void close(java.lang.Throwable cause, boolean allowChannelOpen)
-
close
void close(java.nio.channels.ClosedChannelException cause)
-
safeSuccess
private static void safeSuccess(ChannelPromise promise)
-
safeFail
private static void safeFail(ChannelPromise promise, java.lang.Throwable cause)
-
recycle
@Deprecated public void recycle()
Deprecated.
-
totalPendingWriteBytes
public long totalPendingWriteBytes()
-
bytesBeforeUnwritable
public long bytesBeforeUnwritable()
Get how many bytes can be written untilisWritable()returnsfalse. This quantity will always be non-negative. IfisWritable()isfalsethen 0.
-
bytesBeforeWritable
public long bytesBeforeWritable()
Get how many bytes must be drained from the underlying buffer untilisWritable()returnstrue. This quantity will always be non-negative. IfisWritable()istruethen 0.
-
forEachFlushedMessage
public void forEachFlushedMessage(ChannelOutboundBuffer.MessageProcessor processor) throws java.lang.Exception
CallChannelOutboundBuffer.MessageProcessor.processMessage(Object)for each flushed message in thisChannelOutboundBufferuntilChannelOutboundBuffer.MessageProcessor.processMessage(Object)returnsfalseor there are no more flushed messages to process.- Throws:
java.lang.Exception
-
isFlushedEntry
private boolean isFlushedEntry(ChannelOutboundBuffer.Entry e)
-
-