NioSender (Apache Tomcat 10.1.57 API Documentation)
Description: declaration: package: org.apache.catalina.tribes.transport.nio, class: NioSender

# Class NioSender

java.lang.Object
org.apache.catalina.tribes.transport.AbstractSender
org.apache.catalina.tribes.transport.nio.NioSender

All Implemented Interfaces:
  `DataSender`

public class NioSender extends AbstractSender
This class is NOT thread safe and should never be used with more than one thread at a time. This is a state machine, handled by the process method. States are:

- NOT_CONNECTED -> connect() -> CONNECTED
- CONNECTED -> setMessage() -> READY TO WRITE
- READY_TO_WRITE -> write() -> READY TO WRITE | READY TO READ
- READY_TO_READ -> read() -> READY_TO_READ | TRANSFER_COMPLETE
- TRANSFER_COMPLETE -> CONNECTED

Thread-safety / synchronisation is managed by ParallelNioSender

- ## Field Summary

  Fields
  Modifier and Type
  Field
  Description
  `protected final XByteBuffer`
  `ackbuf`
  Acknowledgment buffer.
  `protected boolean`
  `complete`
  Flag indicating send completion.
  `protected boolean`
  `connecting`
  Flag indicating connection in progress.
  `protected byte[]`
  `current`
  Current message being processed.
  `protected DatagramChannel`
  `dataChannel`
  The UDP datagram channel.
  `protected ByteBuffer`
  `readbuf`
  Buffer for reading from the channel.
  `protected int`
  `remaining`
  Number of remaining bytes to send.
  `protected Selector`
  `selector`
  The NIO selector for multiplexing channels.
  `protected static final StringManager`
  `sm`
  String manager for this class.
  `protected SocketChannel`
  `socketChannel`
  The TCP socket channel.
  `protected ByteBuffer`
  `writebuf`
  Buffer for writing to the channel.

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `NioSender()`
  Default constructor.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `connect()`
  Connect.
  `void`
  `disconnect()`
  Disconnect.
  `byte[]`
  `getMessage()`
  Returns the current message being sent.
  `Selector`
  `getSelector()`
  Returns the NIO selector.
  `boolean`
  `isComplete()`
  Checks if the send operation is complete.
  `boolean`
  `process(SelectionKey key, boolean waitForAck)`
  State machine to send data.
  `protected boolean`
  `read()`
  Reads acknowledgment data from the channel.
  `void`
  `reset()`
  Resets the sender state for reuse.
  `void`
  `setComplete(boolean complete)`
  Sets the completion flag.
  `void`
  `setMessage(byte[] data)`
  Send message.
  `void`
  `setMessage(byte[] data, int offset, int length)`
  Sets the message to be sent.
  `void`
  `setSelector(Selector selector)`
  Sets the NIO selector.
  `protected boolean`
  `write()`
  Writes the current message to the channel.

  ### Methods inherited from class AbstractSender

  `getAddress, getAttempt, getConnectTime, getDestination, getDirectBuffer, getKeepAliveCount, getKeepAliveTime, getMaxRetryAttempts, getOoBInline, getPort, getRequestCount, getRxBufSize, getSoKeepAlive, getSoLingerOn, getSoLingerTime, getSoReuseAddress, getSoTrafficClass, getTcpNoDelay, getThrowOnFailedAck, getTimeout, getTxBufSize, getUdpPort, getUdpRxBufSize, getUdpTxBufSize, isConnected, isUdpBased, keepalive, setAddress, setAttempt, setConnected, setConnectTime, setDestination, setDirectBuffer, setKeepAliveCount, setKeepAliveTime, setMaxRetryAttempts, setOoBInline, setPort, setRequestCount, setRxBufSize, setSoKeepAlive, setSoLingerOn, setSoLingerTime, setSoReuseAddress, setSoTrafficClass, setTcpNoDelay, setThrowOnFailedAck, setTimeout, setTxBufSize, setUdpBased, setUdpPort, setUdpRxBufSize, setUdpTxBufSize, transferProperties`

  ### Methods inherited from class Object

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

- ## Field Details

  - ### sm

    protected static final StringManager sm
    String manager for this class.

  - ### selector

    protected Selector selector
    The NIO selector for multiplexing channels.

  - ### socketChannel

    protected SocketChannel socketChannel
    The TCP socket channel.

  - ### dataChannel

    protected DatagramChannel dataChannel
    The UDP datagram channel.

  - ### readbuf

    protected ByteBuffer readbuf
    Buffer for reading from the channel.

  - ### writebuf

    protected ByteBuffer writebuf
    Buffer for writing to the channel.

  - ### current

    protected volatile byte[] current
    Current message being processed.

  - ### ackbuf

    protected final XByteBuffer ackbuf
    Acknowledgment buffer.

  - ### remaining

    protected int remaining
    Number of remaining bytes to send.

  - ### complete

    protected boolean complete
    Flag indicating send completion.

  - ### connecting

    protected boolean connecting
    Flag indicating connection in progress.

- ## Constructor Details

  - ### NioSender

    public NioSender()
    Default constructor.

- ## Method Details

  - ### process

    public boolean process(SelectionKey key, boolean waitForAck) throws IOException
    State machine to send data.
    Parameters:
      `key` - The key to use
      `waitForAck` - Wait for an ack
    Returns:
      `true` if the processing was successful
    Throws:
      `IOException` - An IO error occurred

  - ### read

    protected boolean read() throws IOException
    Reads acknowledgment data from the channel.
    Returns:
      `true` if there is more data to read
    Throws:
      `IOException` - If an I/O error occurs

  - ### write

    protected boolean write() throws IOException
    Writes the current message to the channel.
    Returns:
      `true` if the message was fully written
    Throws:
      `IOException` - If an I/O error occurs

  - ### connect

    public void connect() throws IOException
    Description copied from interface: `DataSender`
    Connect.
    Throws:
      `IOException` - when an error occurs

  - ### disconnect

    public void disconnect()
    Description copied from interface: `DataSender`
    Disconnect.

  - ### reset

    public void reset()
    Resets the sender state for reuse.

  - ### setMessage

    public void setMessage(byte[] data) throws IOException
    Send message.
    Parameters:
      `data` - ChannelMessage
    Throws:
      `IOException` - if an error occurs

  - ### setMessage

    public void setMessage(byte[] data, int offset, int length) throws IOException
    Sets the message to be sent.
    Parameters:
      `data` - The message data
      `offset` - The offset in the data array
      `length` - The length of the data to send
    Throws:
      `IOException` - If an I/O error occurs

  - ### getMessage

    public byte[] getMessage()
    Returns the current message being sent.
    Returns:
      the current message byte array

  - ### isComplete

    public boolean isComplete()
    Checks if the send operation is complete.
    Returns:
      `true` if the send is complete

  - ### getSelector

    public Selector getSelector()
    Returns the NIO selector.
    Returns:
      the selector

  - ### setSelector

    public void setSelector(Selector selector)
    Sets the NIO selector.
    Parameters:
      `selector` - the selector

  - ### setComplete

    public void setComplete(boolean complete)
    Sets the completion flag.
    Parameters:
      `complete` - the completion status