Class AbstractSocketConnection

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

public abstract class AbstractSocketConnection extends org.omegazero.common.util.SimpleAttachmentContainer implements SocketConnection
A SocketConnection containing implementations for several interface methods and utility methods.
Since:
2.1.0
API Note:
Before version 2.1.0, parts of this class were in SocketConnection.
  • Field Details

    • EV_CONNECT

      protected static final int EV_CONNECT
      Event ID of the connect event.
      See Also:
    • EV_TIMEOUT

      protected static final int EV_TIMEOUT
      Event ID of the timeout event.
      See Also:
    • EV_DATA

      protected static final int EV_DATA
      Event ID of the data event.
      See Also:
    • EV_WRITABLE

      protected static final int EV_WRITABLE
      Event ID of the writable event.
      See Also:
    • EV_CLOSE

      protected static final int EV_CLOSE
      Event ID of the close event.
      See Also:
    • EV_ERROR

      protected static final int EV_ERROR
      Event ID of the error event.
      See Also:
    • eventEmitter

      protected final org.omegazero.common.event.EventEmitter eventEmitter
      The event emitter used for events.
    • readLock

      protected final Object readLock
      Lock for read operations.
    • writeLock

      protected final Object writeLock
      Lock for write operations.
  • Constructor Details

    • AbstractSocketConnection

      public AbstractSocketConnection()
      Creates a new AbstractSocketConnection.
  • Method Details

    • hasConnected

      public boolean hasConnected()
      Description copied from interface: SocketConnection
      Returns true if the connect event has ever executed. This is already true while running the event.
      Specified by:
      hasConnected in interface SocketConnection
      Returns:
      true if the connect event has ever been emitted
    • setApparentRemoteAddress

      public final void setApparentRemoteAddress(SocketAddress apparentRemoteAddress)
      Description copied from interface: SocketConnection
      Sets a possibly different remote address a client claims to be or act on behalf of.

      For example, if a connection received by a server was proxied through a proxy, this should be set to the actual client address.

      Specified by:
      setApparentRemoteAddress in interface SocketConnection
      Parameters:
      apparentRemoteAddress - The apparent address of the peer
    • getApparentRemoteAddress

      public final SocketAddress getApparentRemoteAddress()
      Description copied from interface: SocketConnection
      Returns the apparent remote address previously set by SocketConnection.setApparentRemoteAddress(SocketAddress), or the address returned by SocketConnection.getRemoteAddress() if none was set.
      Specified by:
      getApparentRemoteAddress in interface SocketConnection
      Returns:
      The apparent remote address
    • on

      public final SocketConnection on(String event, org.omegazero.common.event.runnable.GenericRunnable runnable)
      Description copied from interface: SocketConnection
      Adds an event listener for the given event.

      The following events exist:

      • connect(): Called when this socket is connected and ready to receive or send data.
      • timeout(): Called when the connect operation started using SocketConnection.connect(int) times out. If no listener is registered for this event, and a timeout occurs, an error event is emitted instead. This event is followed by a close event in both cases.
      • data(byte[]): Called when data is received on this connection.
      • writable(): Called when this socket is ready for writing after a SocketConnection.write(byte[]) or SocketConnection.connect(int) operation. This event is not emitted if the socket was previously already writable. This event is also not emitted during a write(byte[]) call to allow the event handler to safely call that method without being called again synchronously.
      • close(): Called when this connection closes and can no longer receive or send data.
      • error(Throwable): Called when an error occurs on this connection. This event is usually followed by a close event.

      An example for registering an event handler for the data event:

      
                      connection.on("data", (byte[] data) -> {
                              // ....
                      });
       
      Specified by:
      on in interface SocketConnection
      Parameters:
      event - The event name
      runnable - The callback
      Returns:
      This SocketConnection
    • once

      public final SocketConnection once(String event, org.omegazero.common.event.runnable.GenericRunnable runnable)
      Description copied from interface: SocketConnection
      Adds a single-event event listener for the given event. Event listeners registered using this method are only called once, the next time the event is emitted, and are then unregistered.
      Specified by:
      once in interface SocketConnection
      Parameters:
      event - The event name
      runnable - The callback
      Returns:
      This SocketConnection
    • off

      public final SocketConnection off(String event, org.omegazero.common.event.runnable.GenericRunnable runnable)
      Description copied from interface: SocketConnection
      Removes an event listener previously registered using SocketConnection.on(String, GenericRunnable).
      Specified by:
      off in interface SocketConnection
      Parameters:
      event - The event name
      runnable - The callback to remove
      Returns:
      This SocketConnection
    • setOnLocalClose

      public final void setOnLocalClose(Consumer<AbstractSocketConnection> onLocalClose)
      Called by classes managing this SocketConnection.
      Parameters:
      onLocalClose - The onLocalClose callback
    • setOnLocalConnect

      public final void setOnLocalConnect(Consumer<AbstractSocketConnection> onLocalConnect)
      Called by classes managing this SocketConnection.
      Parameters:
      onLocalConnect - The onLocalConnect callback
    • setDefaultErrorListener

      public final void setDefaultErrorListener(org.omegazero.common.event.runnable.GenericRunnable.A1<Throwable> runnable)
      Called by classes managing this SocketConnection.
      Parameters:
      runnable - The callback
      Since:
      2.2.0
    • getWorker

      public Consumer<Runnable> getWorker()
      Returns the worker for this AbstractSocketConnection.
      Returns:
      The worker
      See Also:
    • setWorker

      public void setWorker(Consumer<Runnable> worker)
      Sets the worker for this AbstractSocketConnection. This worker is used to run all events except error and may be used to run the callbacks in a different thread.

      The worker must execute all tasks in the order they were given and should not execute tasks concurrently.

      The default worker is SyncWorker.

      Parameters:
      worker - The worker
    • getReadLock

      public final Object getReadLock()
      Returns the read lock. This object is locked every time a read operation that changes the internal state of the connection is performed.
      Returns:
      The read lock
      Since:
      1.4
    • getWriteLock

      public final Object getWriteLock()
      Returns the write lock. This object is locked every time a write operation that changes the internal state of the connection is performed.
      Returns:
      The write lock
      Since:
      1.4
    • localConnect

      protected final void localConnect()
      Called by implementing classes if this connection was established immediately upon calling SocketConnection.connect(int). May be used internally by the connection manager.
    • localClose

      protected final void localClose()
      Called by implementing classes when this connection was closed using any close method (SocketConnection.close() or SocketConnection.destroy()).
    • queueWrite

      protected final void queueWrite(byte[] data)
      Called by implementing classes to queue data for writing before this connection has connected.
      Parameters:
      data - The data
    • handleConnect

      public final void handleConnect()
      Called internally, by subclasses or by classes managing this SocketConnection when this socket connects. This method calls the connect event.
    • handleTimeout

      public final void handleTimeout()
      Called internally, by subclasses or by classes managing this SocketConnection when a connection attempt times out. This method calls the timeout event.
    • handleData

      public final boolean handleData(byte[] data)
      Called by classes managing this SocketConnection when data was received using SocketConnection.read(). This method calls the data event.
      Parameters:
      data - The data that was received on this connection
      Returns:
      false if no data event handler was set upon entry of this method
    • handleWritable

      public final void handleWritable()
      Called by subclasses when this socket is writable. This method calls the writable event.
    • handleClose

      public final void handleClose()
      Called by subclasses or classes managing this SocketConnection when this connection closed. This method calls the close event on the first invocation of this method.
    • handleError

      public final void handleError(Throwable e)
      Called by subclasses or classes managing this SocketConnection when an error occurred in a callback. This method calls the error event and forcibly closes this connection.

      Unlike the other handle methods, this method always runs synchronously.

      Parameters:
      e - The error
      Throws:
      UnhandledException - If no error event handler is set
    • runAsync

      protected final void runAsync(Runnable runnable)
      Delegates the given Runnable to this AbstractSocketConnection's worker.
      Parameters:
      runnable - The runnable
    • runAsync

      protected final <T> void runAsync(Consumer<T> runnable, T value)
      Delegates the given Consumer to this AbstractSocketConnection's worker.
      Parameters:
      runnable - The consumer
      value - The value to pass to the consumer