Class ChannelConnection

java.lang.Object
org.omegazero.net.socket.SocketConnection
org.omegazero.net.socket.ChannelConnection
All Implemented Interfaces:
AutoCloseable
Direct Known Subclasses:
PlainConnection, TLSConnection

public abstract class ChannelConnection extends SocketConnection
Represents a SocketConnection based on a java.nio SocketChannel or DatagramChannel.
  • Field Details

  • Constructor Details

  • Method Details

    • createBuffers

      protected abstract void createBuffers()
      Must be called by subclass constructors. This method creates the readBuf and writeBuf buffers and any additional buffers required.
    • close0

      protected void close0()
      Forcibly closes this connection.
    • connect

      public void connect(int timeout)
      Description copied from class: SocketConnection
      Connects this SocketConnection to the previously specified remote address in the constructor. If no address was specified, this method will throw an UnsupportedOperationException.

      This function is non-blocking.

      A connection timeout in milliseconds may be specified in the timeout parameter. If the connection has not been established within this timeout, the handler set using #setOnTimeout(Runnable) is called and the connection is closed. Depending on the implementation and underlying protocol, a timeout may occur earlier or never and may instead cause the onError callback to be called.

      Specified by:
      connect in class SocketConnection
      Parameters:
      timeout - The connection timeout in milliseconds. Disabled if 0
    • flush

      public boolean flush()
      Attempts to flush any queued data after a call to SocketConnection.writeQueue(byte[]) or data that could not be written previously because the socket was busy.
      Specified by:
      flush in class SocketConnection
      Returns:
      true if all data could be written to the socket
      See Also:
      Implementation Requirements:
      This method does not write remaining data in this ChannelConnection's writeBuf. Subclasses must override this method to implement this behavior.
    • close

      public final void close()
      Description copied from class: SocketConnection
      Closes this connection after all remaining data has been flushed to the socket, which may not be immediately.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in class SocketConnection
    • destroy

      public final void destroy()
      Description copied from class: SocketConnection
      Similar to SocketConnection.close(), except that the connection is closed immediately, without waiting for data to be flushed to the socket.

      SocketConnection.isConnected() should return false immediately after calling this method.

      Specified by:
      destroy in class SocketConnection
    • isConnected

      public boolean isConnected()
      Returns true if this socket is connected.
      Specified by:
      isConnected in class SocketConnection
      Returns:
      true if this socket is open and available for reading or writing
    • getRemoteAddress

      public SocketAddress getRemoteAddress()
      Description copied from class: SocketConnection
      Returns the address of the remote host.
      Specified by:
      getRemoteAddress in class SocketConnection
      Returns:
      The address of the peer host
    • getLocalAddress

      public SocketAddress getLocalAddress()
      Description copied from class: SocketConnection
      Returns the local address of this connection.
      Specified by:
      getLocalAddress in class SocketConnection
      Returns:
      The local address of this connection
    • getLastIOTime

      public long getLastIOTime()
      Description copied from class: SocketConnection
      Returns the last time any data was sent over this connection, either incoming or outgoing, as returned by System.currentTimeMillis().
      Specified by:
      getLastIOTime in class SocketConnection
      Returns:
      The last time any data was sent over this connection in milliseconds
    • isWritable

      public boolean isWritable()
      Description copied from class: SocketConnection
      Returns true if this socket is writable, meaning data passed to SocketConnection.write(byte[]) will not be buffered but written to the socket directly.
      Specified by:
      isWritable in class SocketConnection
      Returns:
      true if this socket is writable
    • setReadBlock

      public void setReadBlock(boolean block)
      Description copied from class: SocketConnection
      Enables or disables read blocking. If set to true, the implementation will attempt to block incoming data from being processed and delay it until this is set to false again. Note that the implementation may still fire onData events while this option is set to true.
      Specified by:
      setReadBlock in class SocketConnection
      Parameters:
      block - Whether to attempt to block incoming data
    • getProvider

      public ChannelProvider getProvider()
    • flushWriteBacklog

      public boolean flushWriteBacklog()
      Attempts to write any remaining data in the write buffer and write backlog to the socket and returns true if all data could be written.

      This method is equivalent to flush().
      Returns:
      true if all pending data was written to the socket
    • readFromSocket

      protected final int readFromSocket() throws IOException
      Reads data into the read buffer using this ChannelConnection's ChannelProvider.
      Returns:
      The number of bytes read
      Throws:
      IOException - If an IO error occurs
    • writeToSocket

      protected final int writeToSocket() throws IOException
      Writes data from the write buffer using this ChannelConnection's ChannelProvider.
      Returns:
      The number of bytes written, including buffered bytes
      Throws:
      IOException - If an IO error occurs
    • writeBuffered

      protected final void writeBuffered(byte[] data, int offset, int length, boolean flush, ByteBuffer targetBuffer, ThrowingRunnable writeOut)