io.undertow.server.protocol.framed

## 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:
  Closeable, AutoCloseable, Channel, 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 Object
    implements org.xnio.channels.ConnectedChannel

A `ConnectedChannel` which can be used to send and receive Frames.

This provides a common base for framed protocols such as websockets and SPDY

Author:
  Stuart Douglas

### Field Summary

### Fields inherited from interface org.xnio.channels.Configurable

`EMPTY`

### Constructor Summary

Constructors
`protected` `AbstractFramedChannel(org.xnio.StreamConnection connectedStreamChannel, ByteBufferPool bufferPool, FramePriority<C,R,S> framePriority, PooledByteBuffer readData, org.xnio.OptionMap settings)` Create a new `AbstractFramedChannel` 8

### Method Summary

All Methods Instance Methods Abstract Methods Concrete Methods
`void` `addCloseTask(org.xnio.ChannelListener<C> task)`
`void` `close()` Forcibly closes the `AbstractFramedChannel`.
`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.
`protected abstract R` `createChannel(FrameHeaderData frameHeaderData, PooledByteBuffer frameData)` Method that creates the actual stream source channel implementation that is in use.
`protected IdleTimeoutConduit` `createIdleTimeoutChannel(org.xnio.StreamConnection connectedStreamChannel)`
`void` `flush()`
`protected void` `flushSenders()` Flushes all ready stream sink conduits to the channel.
`ByteBufferPool` `getBufferPool()` Get the buffer pool for this connection.
`org.xnio.ChannelListener.Setter<? extends AbstractFramedChannel>` `getCloseSetter()`
`InetSocketAddress` `getDestinationAddress()` Get the destination address of the Channel.
`protected FramePriority<C,R,S>` `getFramePriority()`
`long` `getIdleTimeout()`
`org.xnio.XnioIoThread` `getIoThread()`
`SocketAddress` `getLocalAddress()`
`<A extends SocketAddress> A` `getLocalAddress(Class<A> type)`
`<T> T` `getOption(org.xnio.Option<T> option)`
`SocketAddress` `getPeerAddress()`
`<A extends SocketAddress> A` `getPeerAddress(Class<A> type)`
`protected abstract Collection<AbstractFramedStreamSourceChannel<C,R,S>>` `getReceivers()`
`org.xnio.ChannelListener.Setter<C>` `getReceiveSetter()` Return the `ChannelListener.Setter` which will holds the `ChannelListener` that gets notified once a frame was received.
`protected org.xnio.OptionMap` `getSettings()`
`InetSocketAddress` `getSourceAddress()` Get the source address of the Channel.
`protected org.xnio.StreamConnection` `getUnderlyingConnection()`
`org.xnio.XnioWorker` `getWorker()`
`protected abstract void` `handleBrokenSinkChannel(Throwable e)` Method that is invoked when then write side of a channel is broken.
`protected abstract void` `handleBrokenSourceChannel(Throwable e)` Method that is invoked when the read side of the channel is broken.
`protected abstract boolean` `isLastFrameReceived()` Returns true if the protocol specific final frame has been received.
`protected abstract boolean` `isLastFrameSent()`
`boolean` `isOpen()`
`protected boolean` `isReadsBroken()`
`boolean` `isReceivesResumed()`
`boolean` `isRequireExplicitFlush()`
`protected boolean` `isWritesBroken()`
`protected void` `lastDataRead()` Method than is invoked when read() returns -1.
`protected void` `markReadsBroken(Throwable cause)` Called when a source sub channel fails to fulfil its contract, and leaves the channel in an inconsistent state.
`protected void` `markWritesBroken(Throwable cause)` Called when a sub channel fails to fulfil its contract, and leaves the channel in an inconsistent state.
`protected abstract FrameHeaderData` `parseFrame(ByteBuffer data)` Attempts to parse an incoming frame header from the data in the buffer.
`protected void` `queueFrame(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 void` `recalculateHeldFrames()`
`R` `receive()` Receive method, returns null if no frame is ready.
`void` `resumeReceives()` Resume the receive of new frames via `receive()`
`void` `setIdleTimeout(long timeout)`
`<T> T` `setOption(org.xnio.Option<T> option, T value)`
`void` `setRequireExplicitFlush(boolean requireExplicitFlush)`
`boolean` `supportsOption(org.xnio.Option<?> option)`
`void` `suspendReceives()` Suspend the receive of new frames via `receive()`
`String` `toString()`
`protected org.xnio.ChannelExceptionHandler<org.xnio.channels.SuspendableWriteChannel>` `writeExceptionHandler()`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

### Constructor Detail

#### AbstractFramedChannel

    protected AbstractFramedChannel(org.xnio.StreamConnection connectedStreamChannel,
                                    ByteBufferPool bufferPool,
                                    FramePriority<C,R,S> framePriority,
                                    PooledByteBuffer readData,
                                    org.xnio.OptionMap settings)

Create a new `AbstractFramedChannel` 8

Parameters:
  `connectedStreamChannel` - The `ConnectedStreamChannel` over which the Frames should get send and received. Be aware that it already must be "upgraded".
  `bufferPool` - The `ByteBufferPool` which will be used to acquire `ByteBuffer`'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 SocketAddress getLocalAddress()

Specified by:
  `getLocalAddress` in interface `org.xnio.channels.BoundChannel`

#### getLocalAddress

    public <A extends SocketAddress> A getLocalAddress(Class<A> type)

Specified by:
  `getLocalAddress` in interface `org.xnio.channels.BoundChannel`

#### getWorker

    public org.xnio.XnioWorker getWorker()

Specified by:
  `getWorker` in interface `org.xnio.channels.CloseableChannel`

#### getIoThread

    public org.xnio.XnioIoThread getIoThread()

Specified by:
  `getIoThread` in interface `org.xnio.channels.CloseableChannel`

#### supportsOption

    public boolean supportsOption(org.xnio.Option<?> option)

Specified by:
  `supportsOption` in interface `org.xnio.channels.Configurable`

#### getOption

    public <T> T getOption(org.xnio.Option<T> option)
                    throws IOException

Specified by:
  `getOption` in interface `org.xnio.channels.Configurable`
Throws:
  `IOException`

#### setOption

    public <T> T setOption(org.xnio.Option<T> option,
                           T value)
                    throws IOException

Specified by:
  `setOption` in interface `org.xnio.channels.Configurable`
Throws:
  `IOException`

#### isOpen

    public boolean isOpen()

Specified by:
  `isOpen` in interface `Channel`

#### getPeerAddress

    public SocketAddress getPeerAddress()

Specified by:
  `getPeerAddress` in interface `org.xnio.channels.ConnectedChannel`

#### getPeerAddress

    public <A extends SocketAddress> A getPeerAddress(Class<A> type)

Specified by:
  `getPeerAddress` in interface `org.xnio.channels.ConnectedChannel`

#### getSourceAddress

    public InetSocketAddress getSourceAddress()

Get the source address of the Channel.

Returns:
  the source address of the Channel

#### getDestinationAddress

    public InetSocketAddress getDestinationAddress()

Get the destination address of the Channel.

Returns:
  the destination address of the Channel

#### receive

    public R receive()
              throws 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:
  `IOException`

#### lastDataRead

    protected void lastDataRead()

Method than is invoked when read() returns -1.

#### createChannel

    protected abstract R createChannel(FrameHeaderData frameHeaderData,
                                       PooledByteBuffer frameData)
                                throws IOException

Method that creates the actual stream source channel implementation that is in use.

Parameters:
  `frameHeaderData` - The header data, as returned by `parseFrame(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:
  `IOException`

#### parseFrame

    protected abstract FrameHeaderData parseFrame(ByteBuffer data)
                                           throws 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 `null` if the data was incomplete
Throws:
  `IOException` - If the data could not be parsed.

#### recalculateHeldFrames

    protected void recalculateHeldFrames()
                                  throws IOException

Throws:
  `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 `framePriority` implementation will be invoked to decide which frames are eligible for sending and in what order.

#### queueFrame

    protected void queueFrame(S channel)
                       throws 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 `FramePriority` implementation in use the channel may or may not be added to the actual pending queue

Parameters:
  `channel` - The channel
Throws:
  `IOException`

#### flush

    public void flush()

#### isLastFrameReceived

    protected abstract boolean isLastFrameReceived()

Returns true if the protocol specific final frame has been received.

Returns:
  `true` If the last frame has been received

#### isLastFrameSent

    protected abstract boolean isLastFrameSent()

Returns:
  `true` If the last frame has been sent

#### handleBrokenSourceChannel

    protected abstract void handleBrokenSourceChannel(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(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 the `ChannelListener.Setter` which will holds the `ChannelListener` that gets notified once a frame was received.

#### suspendReceives

    public void suspendReceives()

Suspend the receive of new frames via `receive()`

#### resumeReceives

    public void resumeReceives()

Resume the receive of new frames via `receive()`

#### isReceivesResumed

    public boolean isReceivesResumed()

#### close

    public void close()
               throws IOException

Forcibly closes the `AbstractFramedChannel`.

Specified by:
  `close` in interface `Closeable`
Specified by:
  `close` in interface `AutoCloseable`
Specified by:
  `close` in interface `Channel`
Specified by:
  `close` in interface `InterruptibleChannel`
Specified by:
  `close` in interface `org.xnio.channels.CloseableChannel`
Throws:
  `IOException`

#### getCloseSetter

    public org.xnio.ChannelListener.Setter<? extends AbstractFramedChannel> getCloseSetter()

Specified by:
  `getCloseSetter` in interface `org.xnio.channels.BoundChannel`
Specified by:
  `getCloseSetter` in interface `org.xnio.channels.CloseableChannel`
Specified by:
  `getCloseSetter` in interface `org.xnio.channels.ConnectedChannel`

#### markReadsBroken

    protected void markReadsBroken(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(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 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 String toString()

Overrides:
  `toString` in class `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()