Package org.omegazero.net.socket
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 Summary
FieldsModifier and TypeFieldDescriptionprotected static final int
Event ID of theclose
event.protected static final int
Event ID of theconnect
event.protected static final int
Event ID of thedata
event.protected static final int
Event ID of theerror
event.protected static final int
Event ID of thetimeout
event.protected static final int
Event ID of thewritable
event.protected final org.omegazero.common.event.EventEmitter
The event emitter used for events.protected final Object
Lock for read operations.protected final Object
Lock for write operations.Fields inherited from class org.omegazero.common.util.SimpleAttachmentContainer
attachments
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfinal SocketAddress
Returns the apparent remote address previously set bySocketConnection.setApparentRemoteAddress(SocketAddress)
, or the address returned bySocketConnection.getRemoteAddress()
if none was set.final Object
Returns the read lock.Returns the worker for thisAbstractSocketConnection
.final Object
Returns the write lock.final void
Called by subclasses or classes managing thisSocketConnection
when this connection closed.final void
Called internally, by subclasses or by classes managing thisSocketConnection
when this socket connects.final boolean
handleData
(byte[] data) Called by classes managing thisSocketConnection
when data was received usingSocketConnection.read()
.final void
Called by subclasses or classes managing thisSocketConnection
when an error occurred in a callback.final void
Called internally, by subclasses or by classes managing thisSocketConnection
when a connection attempt times out.final void
Called by subclasses when this socket is writable.boolean
Returnstrue
if theconnect
event has ever executed.protected final void
Called by implementing classes when this connection was closed using any close method (SocketConnection.close()
orSocketConnection.destroy()
).protected final void
Called by implementing classes if this connection was established immediately upon callingSocketConnection.connect(int)
.final SocketConnection
Removes an event listener previously registered usingSocketConnection.on(String, GenericRunnable)
.final SocketConnection
Adds an event listener for the given event.final SocketConnection
Adds a single-event event listener for the given event.protected final void
queueWrite
(byte[] data) Called by implementing classes to queue data for writing before this connection has connected.protected final void
Delegates the givenRunnable
to thisAbstractSocketConnection
's worker.protected final <T> void
Delegates the givenConsumer
to thisAbstractSocketConnection
's worker.final void
setApparentRemoteAddress
(SocketAddress apparentRemoteAddress) Sets a possibly different remote address a client claims to be or act on behalf of.final void
setDefaultErrorListener
(org.omegazero.common.event.runnable.GenericRunnable.A1<Throwable> runnable) Called by classes managing thisSocketConnection
.final void
setOnLocalClose
(Consumer<AbstractSocketConnection> onLocalClose) Called by classes managing thisSocketConnection
.final void
setOnLocalConnect
(Consumer<AbstractSocketConnection> onLocalConnect) Called by classes managing thisSocketConnection
.void
Sets the worker for thisAbstractSocketConnection
.Methods inherited from class org.omegazero.common.util.SimpleAttachmentContainer
getAttachment, hasAttachment, removeAttachment, setAttachment
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.omegazero.common.util.AttachmentContainer
requireAttachment
Methods inherited from interface org.omegazero.net.socket.SocketConnection
close, connect, destroy, flush, getLastIOTime, getLocalAddress, getRemoteAddress, hasDisconnected, isConnected, isWritable, on, on, read, setOnClose, setOnConnect, setOnData, setOnError, setOnTimeout, setOnWritable, setReadBlock, write, write, write, writeQueue, writeQueue
-
Field Details
-
EV_CONNECT
protected static final int EV_CONNECTEvent ID of theconnect
event.- See Also:
-
EV_TIMEOUT
protected static final int EV_TIMEOUTEvent ID of thetimeout
event.- See Also:
-
EV_DATA
protected static final int EV_DATAEvent ID of thedata
event.- See Also:
-
EV_WRITABLE
protected static final int EV_WRITABLEEvent ID of thewritable
event.- See Also:
-
EV_CLOSE
protected static final int EV_CLOSEEvent ID of theclose
event.- See Also:
-
EV_ERROR
protected static final int EV_ERROREvent ID of theerror
event.- See Also:
-
eventEmitter
protected final org.omegazero.common.event.EventEmitter eventEmitterThe event emitter used for events. -
readLock
Lock for read operations. -
writeLock
Lock for write operations.
-
-
Constructor Details
-
AbstractSocketConnection
public AbstractSocketConnection()Creates a newAbstractSocketConnection
.
-
-
Method Details
-
hasConnected
public boolean hasConnected()Description copied from interface:SocketConnection
Returnstrue
if theconnect
event has ever executed. This is alreadytrue
while running the event.- Specified by:
hasConnected
in interfaceSocketConnection
- Returns:
true
if theconnect
event has ever been emitted
-
setApparentRemoteAddress
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 interfaceSocketConnection
- Parameters:
apparentRemoteAddress
- The apparent address of the peer
-
getApparentRemoteAddress
Description copied from interface:SocketConnection
Returns the apparent remote address previously set bySocketConnection.setApparentRemoteAddress(SocketAddress)
, or the address returned bySocketConnection.getRemoteAddress()
if none was set.- Specified by:
getApparentRemoteAddress
in interfaceSocketConnection
- 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 usingSocketConnection.connect(int)
times out. If no listener is registered for this event, and a timeout occurs, anerror
event is emitted instead. This event is followed by aclose
event in both cases.data(byte[])
: Called when data is received on this connection.writable()
: Called when this socket is ready for writing after aSocketConnection.write(byte[])
orSocketConnection.connect(int)
operation. This event is not emitted if the socket was previously already writable. This event is also not emitted during awrite(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 aclose
event.
An example for registering an event handler for the
data
event:connection.on("data", (byte[] data) -> { // .... });
- Specified by:
on
in interfaceSocketConnection
- Parameters:
event
- The event namerunnable
- 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 interfaceSocketConnection
- Parameters:
event
- The event namerunnable
- 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 usingSocketConnection.on(String, GenericRunnable)
.- Specified by:
off
in interfaceSocketConnection
- Parameters:
event
- The event namerunnable
- The callback to remove- Returns:
- This
SocketConnection
-
setOnLocalClose
Called by classes managing thisSocketConnection
.- Parameters:
onLocalClose
- The onLocalClose callback
-
setOnLocalConnect
Called by classes managing thisSocketConnection
.- Parameters:
onLocalConnect
- The onLocalConnect callback
-
setDefaultErrorListener
public final void setDefaultErrorListener(org.omegazero.common.event.runnable.GenericRunnable.A1<Throwable> runnable) Called by classes managing thisSocketConnection
.- Parameters:
runnable
- The callback- Since:
- 2.2.0
-
getWorker
Returns the worker for thisAbstractSocketConnection
.- Returns:
- The worker
- See Also:
-
setWorker
Sets the worker for thisAbstractSocketConnection
. This worker is used to run all events excepterror
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
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
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 callingSocketConnection.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()
orSocketConnection.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 thisSocketConnection
when this socket connects. This method calls theconnect
event. -
handleTimeout
public final void handleTimeout()Called internally, by subclasses or by classes managing thisSocketConnection
when a connection attempt times out. This method calls thetimeout
event. -
handleData
public final boolean handleData(byte[] data) Called by classes managing thisSocketConnection
when data was received usingSocketConnection.read()
. This method calls thedata
event.- Parameters:
data
- The data that was received on this connection- Returns:
false
if nodata
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 thewritable
event. -
handleClose
public final void handleClose()Called by subclasses or classes managing thisSocketConnection
when this connection closed. This method calls theclose
event on the first invocation of this method. -
handleError
Called by subclasses or classes managing thisSocketConnection
when an error occurred in a callback. This method calls theerror
event and forcibly closes this connection.Unlike the other
handle
methods, this method always runs synchronously.- Parameters:
e
- The error- Throws:
UnhandledException
- If noerror
event handler is set
-
runAsync
Delegates the givenRunnable
to thisAbstractSocketConnection
's worker.- Parameters:
runnable
- The runnable
-
runAsync
Delegates the givenConsumer
to thisAbstractSocketConnection
's worker.- Parameters:
runnable
- The consumervalue
- The value to pass to the consumer
-