Class AbstractFramedChannel<C extends AbstractFramedChannel<C,R,S>,R extends AbstractFramedStreamSourceChannel<C,R,S>,S extends AbstractFramedStreamSinkChannel<C,R,S>>
- java.lang.Object
-
- io.undertow.server.protocol.framed.AbstractFramedChannel<C,R,S>
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable,java.nio.channels.Channel,java.nio.channels.InterruptibleChannel,org.xnio.channels.BoundChannel,org.xnio.channels.CloseableChannel,org.xnio.channels.Configurable,org.xnio.channels.ConnectedChannel
- Direct Known Subclasses:
AjpClientChannel,Http2Channel,WebSocketChannel
public abstract class AbstractFramedChannel<C extends AbstractFramedChannel<C,R,S>,R extends AbstractFramedStreamSourceChannel<C,R,S>,S extends AbstractFramedStreamSinkChannel<C,R,S>> extends java.lang.Object implements org.xnio.channels.ConnectedChannelAConnectedChannelwhich can be used to send and receive Frames.This provides a common base for framed protocols such as websockets and SPDY
- Author:
- Stuart Douglas
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedAbstractFramedChannel(org.xnio.StreamConnection connectedStreamChannel, ByteBufferPool bufferPool, FramePriority<C,R,S> framePriority, PooledByteBuffer readData, org.xnio.OptionMap settings)Create a newAbstractFramedChannel8
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidaddCloseTask(org.xnio.ChannelListener<C> task)voidclose()Forcibly closes theAbstractFramedChannel.protected abstract voidcloseSubChannels()Method that is called when the channel is being forcibly closed, and all sub stream sink/source channels should also be forcibly closed.protected abstract RcreateChannel(FrameHeaderData frameHeaderData, PooledByteBuffer frameData)Method that creates the actual stream source channel implementation that is in use.protected IdleTimeoutConduitcreateIdleTimeoutChannel(org.xnio.StreamConnection connectedStreamChannel)voidflush()protected voidflushSenders()Flushes all ready stream sink conduits to the channel.ByteBufferPoolgetBufferPool()Get the buffer pool for this connection.org.xnio.ChannelListener.Setter<? extends AbstractFramedChannel>getCloseSetter()java.net.InetSocketAddressgetDestinationAddress()Get the destination address of the Channel.protected FramePriority<C,R,S>getFramePriority()longgetIdleTimeout()org.xnio.XnioIoThreadgetIoThread()java.net.SocketAddressgetLocalAddress()<A extends java.net.SocketAddress>
AgetLocalAddress(java.lang.Class<A> type)<T> TgetOption(org.xnio.Option<T> option)java.net.SocketAddressgetPeerAddress()<A extends java.net.SocketAddress>
AgetPeerAddress(java.lang.Class<A> type)protected abstract java.util.Collection<AbstractFramedStreamSourceChannel<C,R,S>>getReceivers()org.xnio.ChannelListener.Setter<C>getReceiveSetter()Return theChannelListener.Setterwhich will holds theChannelListenerthat gets notified once a frame was received.protected org.xnio.OptionMapgetSettings()java.net.InetSocketAddressgetSourceAddress()Get the source address of the Channel.protected org.xnio.StreamConnectiongetUnderlyingConnection()org.xnio.XnioWorkergetWorker()protected abstract voidhandleBrokenSinkChannel(java.lang.Throwable e)Method that is invoked when then write side of a channel is broken.protected abstract voidhandleBrokenSourceChannel(java.lang.Throwable e)Method that is invoked when the read side of the channel is broken.protected abstract booleanisLastFrameReceived()Returns true if the protocol specific final frame has been received.protected abstract booleanisLastFrameSent()booleanisOpen()protected booleanisReadsBroken()booleanisReceivesResumed()booleanisRequireExplicitFlush()protected booleanisWritesBroken()protected voidlastDataRead()Method than is invoked when read() returns -1.protected voidmarkReadsBroken(java.lang.Throwable cause)Called when a source sub channel fails to fulfil its contract, and leaves the channel in an inconsistent state.protected voidmarkWritesBroken(java.lang.Throwable cause)Called when a sub channel fails to fulfil its contract, and leaves the channel in an inconsistent state.protected abstract FrameHeaderDataparseFrame(java.nio.ByteBuffer data)Attempts to parse an incoming frame header from the data in the buffer.protected voidqueueFrame(S channel)Queues a new frame to be sent, and attempts a flush if this is the first frame in the new frame queue.protected voidrecalculateHeldFrames()Rreceive()Receive method, returns null if no frame is ready.voidresumeReceives()Resume the receive of new frames viareceive()voidsetIdleTimeout(long timeout)<T> TsetOption(org.xnio.Option<T> option, T value)voidsetRequireExplicitFlush(boolean requireExplicitFlush)booleansupportsOption(org.xnio.Option<?> option)voidsuspendReceives()Suspend the receive of new frames viareceive()java.lang.StringtoString()protected org.xnio.ChannelExceptionHandler<org.xnio.channels.SuspendableWriteChannel>writeExceptionHandler()
-
-
-
Constructor Detail
-
AbstractFramedChannel
protected AbstractFramedChannel(org.xnio.StreamConnection connectedStreamChannel, ByteBufferPool bufferPool, FramePriority<C,R,S> framePriority, PooledByteBuffer readData, org.xnio.OptionMap settings)Create a newAbstractFramedChannel8- Parameters:
connectedStreamChannel- TheConnectedStreamChannelover which the Frames should get send and received. Be aware that it already must be "upgraded".bufferPool- TheByteBufferPoolwhich will be used to acquireByteBuffer's from.framePriority-settings- The settings
-
-
Method Detail
-
createIdleTimeoutChannel
protected IdleTimeoutConduit createIdleTimeoutChannel(org.xnio.StreamConnection connectedStreamChannel)
-
getBufferPool
public ByteBufferPool getBufferPool()
Get the buffer pool for this connection.- Returns:
- the buffer pool for this connection
-
getLocalAddress
public java.net.SocketAddress getLocalAddress()
- Specified by:
getLocalAddressin interfaceorg.xnio.channels.BoundChannel
-
getLocalAddress
public <A extends java.net.SocketAddress> A getLocalAddress(java.lang.Class<A> type)
- Specified by:
getLocalAddressin interfaceorg.xnio.channels.BoundChannel
-
getWorker
public org.xnio.XnioWorker getWorker()
- Specified by:
getWorkerin interfaceorg.xnio.channels.CloseableChannel
-
getIoThread
public org.xnio.XnioIoThread getIoThread()
- Specified by:
getIoThreadin interfaceorg.xnio.channels.CloseableChannel
-
supportsOption
public boolean supportsOption(org.xnio.Option<?> option)
- Specified by:
supportsOptionin interfaceorg.xnio.channels.Configurable
-
getOption
public <T> T getOption(org.xnio.Option<T> option) throws java.io.IOException- Specified by:
getOptionin interfaceorg.xnio.channels.Configurable- Throws:
java.io.IOException
-
setOption
public <T> T setOption(org.xnio.Option<T> option, T value) throws java.io.IOException- Specified by:
setOptionin interfaceorg.xnio.channels.Configurable- Throws:
java.io.IOException
-
isOpen
public boolean isOpen()
- Specified by:
isOpenin interfacejava.nio.channels.Channel
-
getPeerAddress
public java.net.SocketAddress getPeerAddress()
- Specified by:
getPeerAddressin interfaceorg.xnio.channels.ConnectedChannel
-
getPeerAddress
public <A extends java.net.SocketAddress> A getPeerAddress(java.lang.Class<A> type)
- Specified by:
getPeerAddressin interfaceorg.xnio.channels.ConnectedChannel
-
getSourceAddress
public java.net.InetSocketAddress getSourceAddress()
Get the source address of the Channel.- Returns:
- the source address of the Channel
-
getDestinationAddress
public java.net.InetSocketAddress getDestinationAddress()
Get the destination address of the Channel.- Returns:
- the destination address of the Channel
-
receive
public R receive() throws java.io.IOException
Receive method, returns null if no frame is ready. Otherwise returns a channel that can be used to read the frame contents.Calling this method can also have the side effect of making additional data available to existing source channels. In general if you suspend receives or don't have some other way of calling this method then it can prevent frame channels for being fully consumed.
- Throws:
java.io.IOException
-
lastDataRead
protected void lastDataRead()
Method than is invoked when read() returns -1.
-
createChannel
protected abstract R createChannel(FrameHeaderData frameHeaderData, PooledByteBuffer frameData) throws java.io.IOException
Method that creates the actual stream source channel implementation that is in use.- Parameters:
frameHeaderData- The header data, as returned byparseFrame(java.nio.ByteBuffer)frameData- Any additional data for the frame that has already been read. This may not be the complete frame contents- Returns:
- A new stream source channel
- Throws:
java.io.IOException
-
parseFrame
protected abstract FrameHeaderData parseFrame(java.nio.ByteBuffer data) throws java.io.IOException
Attempts to parse an incoming frame header from the data in the buffer.- Parameters:
data- The data that has been read from the channel- Returns:
- The frame header data, or
nullif the data was incomplete - Throws:
java.io.IOException- If the data could not be parsed.
-
recalculateHeldFrames
protected void recalculateHeldFrames() throws java.io.IOException- Throws:
java.io.IOException
-
flushSenders
protected void flushSenders()
Flushes all ready stream sink conduits to the channel.Frames will be batched up, to allow them all to be written out via a gathering write. The
framePriorityimplementation will be invoked to decide which frames are eligible for sending and in what order.
-
queueFrame
protected void queueFrame(S channel) throws java.io.IOException
Queues a new frame to be sent, and attempts a flush if this is the first frame in the new frame queue.Depending on the
FramePriorityimplementation in use the channel may or may not be added to the actual pending queue- Parameters:
channel- The channel- Throws:
java.io.IOException
-
flush
public void flush()
-
isLastFrameReceived
protected abstract boolean isLastFrameReceived()
Returns true if the protocol specific final frame has been received.- Returns:
trueIf the last frame has been received
-
isLastFrameSent
protected abstract boolean isLastFrameSent()
- Returns:
trueIf the last frame has been sent
-
handleBrokenSourceChannel
protected abstract void handleBrokenSourceChannel(java.lang.Throwable e)
Method that is invoked when the read side of the channel is broken. This generally happens on a protocol error.
-
handleBrokenSinkChannel
protected abstract void handleBrokenSinkChannel(java.lang.Throwable e)
Method that is invoked when then write side of a channel is broken. This generally happens on a protocol error.
-
getReceiveSetter
public org.xnio.ChannelListener.Setter<C> getReceiveSetter()
Return theChannelListener.Setterwhich will holds theChannelListenerthat gets notified once a frame was received.
-
suspendReceives
public void suspendReceives()
Suspend the receive of new frames viareceive()
-
resumeReceives
public void resumeReceives()
Resume the receive of new frames viareceive()
-
isReceivesResumed
public boolean isReceivesResumed()
-
close
public void close() throws java.io.IOExceptionForcibly closes theAbstractFramedChannel.- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.nio.channels.Channel- Specified by:
closein interfacejava.io.Closeable- Specified by:
closein interfaceorg.xnio.channels.CloseableChannel- Specified by:
closein interfacejava.nio.channels.InterruptibleChannel- Throws:
java.io.IOException
-
getCloseSetter
public org.xnio.ChannelListener.Setter<? extends AbstractFramedChannel> getCloseSetter()
- Specified by:
getCloseSetterin interfaceorg.xnio.channels.BoundChannel- Specified by:
getCloseSetterin interfaceorg.xnio.channels.CloseableChannel- Specified by:
getCloseSetterin interfaceorg.xnio.channels.ConnectedChannel
-
markReadsBroken
protected void markReadsBroken(java.lang.Throwable cause)
Called when a source sub channel fails to fulfil its contract, and leaves the channel in an inconsistent state.The underlying read side will be forcibly closed.
- Parameters:
cause- The possibly null cause
-
closeSubChannels
protected abstract void closeSubChannels()
Method that is called when the channel is being forcibly closed, and all sub stream sink/source channels should also be forcibly closed.
-
markWritesBroken
protected void markWritesBroken(java.lang.Throwable cause)
Called when a sub channel fails to fulfil its contract, and leaves the channel in an inconsistent state.The underlying channel will be closed, and any sub channels that have writes resumed will have their listeners notified. It is expected that these listeners will then attempt to use the channel, and their standard error handling logic will take over.
- Parameters:
cause- The possibly null cause
-
isWritesBroken
protected boolean isWritesBroken()
-
isReadsBroken
protected boolean isReadsBroken()
-
getReceivers
protected abstract java.util.Collection<AbstractFramedStreamSourceChannel<C,R,S>> getReceivers()
-
setIdleTimeout
public void setIdleTimeout(long timeout)
-
getIdleTimeout
public long getIdleTimeout()
-
getFramePriority
protected FramePriority<C,R,S> getFramePriority()
-
addCloseTask
public void addCloseTask(org.xnio.ChannelListener<C> task)
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
getUnderlyingConnection
protected org.xnio.StreamConnection getUnderlyingConnection()
-
writeExceptionHandler
protected org.xnio.ChannelExceptionHandler<org.xnio.channels.SuspendableWriteChannel> writeExceptionHandler()
-
isRequireExplicitFlush
public boolean isRequireExplicitFlush()
-
setRequireExplicitFlush
public void setRequireExplicitFlush(boolean requireExplicitFlush)
-
getSettings
protected org.xnio.OptionMap getSettings()
-
-