Class LoopbackConnection

java.lang.Object
org.omegazero.common.util.SimpleAttachmentContainer
org.omegazero.net.socket.AbstractSocketConnection
org.omegazero.net.util.LoopbackConnection
All Implemented Interfaces:
Closeable, AutoCloseable, org.omegazero.common.util.AttachmentContainer, SocketConnection

public final class LoopbackConnection extends AbstractSocketConnection
invalid reference
SocketConnection
implementation for loopback sockets.
Since:
2.3.1
See Also:
  • Field Details

    • LOCALHOSTADDR

      public static final InetAddress LOCALHOSTADDR
    • lastIOTime

      protected long lastIOTime
    • acceptwrites

      protected boolean acceptwrites
  • Constructor Details

    • LoopbackConnection

      public LoopbackConnection(int number, LoopbackConnection other)
    • LoopbackConnection

      public LoopbackConnection(int number, InetSocketAddress connectaddr)
  • Method Details

    • getNumber

      public int getNumber()
    • setOther

      protected void setOther(LoopbackConnection other)
    • getOther

      public LoopbackConnection getOther()
    • connect

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

      Whether this method is blocking is implementation-defined.

      A connection timeout in milliseconds may be specified in the timeout parameter. If the connection has not been established within this timeout, the timeout event is emitted and the connection is closed, and if this method is blocking, it will return. Depending on the implementation and underlying protocol, a timeout may occur earlier or never and may instead cause the error event to be emitted.

      Parameters:
      timeout - The connection timeout in milliseconds. Disabled if 0
    • flush

      public boolean flush()
      Description copied from interface: SocketConnection
      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.

      Whether this method is blocking is implementation-defined.

      Returns:
      true if all data could be written to the socket
      See Also:
    • close

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

      public void destroy()
      Description copied from interface: 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.

    • isConnected

      public boolean isConnected()
      Description copied from interface: SocketConnection
      Returns true if this socket is connected.
      Returns:
      true if this socket is connected
    • getRemoteAddress

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

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

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

      public boolean isWritable()
      Description copied from interface: 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.
      Returns:
      true if this socket is writable
    • setReadBlock

      public void setReadBlock(boolean block)
      Description copied from interface: 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 emit data events while this option is set to true.
      Parameters:
      block - Whether to attempt to block incoming data
    • read

      public byte[] read()
      Description copied from interface: SocketConnection
      Reads data received from the peer host on this connection.

      Whether this method is blocking is implementation-defined. If no data was available, null is returned, or the method blocks until data is available.

      Returns:
      The read data or null if no data is available.
    • write

      public void write(byte[] data, int offset, int length)
      Description copied from interface: SocketConnection
      Writes data to this connection for delivery to the peer host.

      Whether this method is blocking is implementation-defined. A call to this method may store data in a temporary write buffer if the underlying socket is busy. An application should try to respect the value of SocketConnection.isWritable() to reduce memory consumption by such write buffer if a lot of data is being written (see also: writable event).

      If this method is called before the connect event, the data is queued in a temporary buffer and written out when the socket connects.

      Parameters:
      data - The data to write
      offset - The start index of the data to write in the data byte array
      length - The total number of bytes to write from the data byte array, starting at offset
      See Also:
    • writeQueue

      public void writeQueue(byte[] data, int offset, int length)
      Description copied from interface: SocketConnection
      Similar to SocketConnection.write(byte[], int, int), except that no attempt will be made to immediately flush the data to the socket, if supported by the implementation.
      Parameters:
      data - The data to write
      offset - The start index of the data to write in the data byte array
      length - The total number of bytes to write from the data byte array, starting at offset
      See Also: