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 intEvent ID of thecloseevent.protected static final intEvent ID of theconnectevent.protected static final intEvent ID of thedataevent.protected static final intEvent ID of theerrorevent.protected static final intEvent ID of thetimeoutevent.protected static final intEvent ID of thewritableevent.protected final org.omegazero.common.event.EventEmitterThe event emitter used for events.protected final ObjectLock for read operations.protected final ObjectLock for write operations.Fields inherited from class org.omegazero.common.util.SimpleAttachmentContainer
attachments -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfinal SocketAddressReturns the apparent remote address previously set bySocketConnection.setApparentRemoteAddress(SocketAddress), or the address returned bySocketConnection.getRemoteAddress()if none was set.final ObjectReturns the read lock.Returns the worker for thisAbstractSocketConnection.final ObjectReturns the write lock.final voidCalled by subclasses or classes managing thisSocketConnectionwhen this connection closed.final voidCalled internally, by subclasses or by classes managing thisSocketConnectionwhen this socket connects.final booleanhandleData(byte[] data) Called by classes managing thisSocketConnectionwhen data was received usingSocketConnection.read().final voidCalled by subclasses or classes managing thisSocketConnectionwhen an error occurred in a callback.final voidCalled internally, by subclasses or by classes managing thisSocketConnectionwhen a connection attempt times out.final voidCalled by subclasses when this socket is writable.booleanReturnstrueif theconnectevent has ever executed.protected final voidCalled by implementing classes when this connection was closed using any close method (SocketConnection.close()orSocketConnection.destroy()).protected final voidCalled by implementing classes if this connection was established immediately upon callingSocketConnection.connect(int).final SocketConnectionRemoves an event listener previously registered usingSocketConnection.on(String, GenericRunnable).final SocketConnectionAdds an event listener for the given event.final SocketConnectionAdds a single-event event listener for the given event.protected final voidqueueWrite(byte[] data) Called by implementing classes to queue data for writing before this connection has connected.protected final voidDelegates the givenRunnableto thisAbstractSocketConnection's worker.protected final <T> voidDelegates the givenConsumerto thisAbstractSocketConnection's worker.final voidsetApparentRemoteAddress(SocketAddress apparentRemoteAddress) Sets a possibly different remote address a client claims to be or act on behalf of.final voidsetDefaultErrorListener(org.omegazero.common.event.runnable.GenericRunnable.A1<Throwable> runnable) Called by classes managing thisSocketConnection.final voidsetOnLocalClose(Consumer<AbstractSocketConnection> onLocalClose) Called by classes managing thisSocketConnection.final voidsetOnLocalConnect(Consumer<AbstractSocketConnection> onLocalConnect) Called by classes managing thisSocketConnection.voidSets the worker for thisAbstractSocketConnection.Methods inherited from class org.omegazero.common.util.SimpleAttachmentContainer
getAttachment, hasAttachment, removeAttachment, setAttachmentMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.omegazero.common.util.AttachmentContainer
requireAttachmentMethods 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 theconnectevent.- See Also:
-
EV_TIMEOUT
protected static final int EV_TIMEOUTEvent ID of thetimeoutevent.- See Also:
-
EV_DATA
protected static final int EV_DATAEvent ID of thedataevent.- See Also:
-
EV_WRITABLE
protected static final int EV_WRITABLEEvent ID of thewritableevent.- See Also:
-
EV_CLOSE
protected static final int EV_CLOSEEvent ID of thecloseevent.- See Also:
-
EV_ERROR
protected static final int EV_ERROREvent ID of theerrorevent.- 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:SocketConnectionReturnstrueif theconnectevent has ever executed. This is alreadytruewhile running the event.- Specified by:
hasConnectedin interfaceSocketConnection- Returns:
trueif theconnectevent has ever been emitted
-
setApparentRemoteAddress
Description copied from interface:SocketConnectionSets 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:
setApparentRemoteAddressin interfaceSocketConnection- Parameters:
apparentRemoteAddress- The apparent address of the peer
-
getApparentRemoteAddress
Description copied from interface:SocketConnectionReturns the apparent remote address previously set bySocketConnection.setApparentRemoteAddress(SocketAddress), or the address returned bySocketConnection.getRemoteAddress()if none was set.- Specified by:
getApparentRemoteAddressin interfaceSocketConnection- Returns:
- The apparent remote address
-
on
public final SocketConnection on(String event, org.omegazero.common.event.runnable.GenericRunnable runnable) Description copied from interface:SocketConnectionAdds 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, anerrorevent is emitted instead. This event is followed by acloseevent 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 acloseevent.
An example for registering an event handler for the
dataevent:connection.on("data", (byte[] data) -> { // .... });- Specified by:
onin 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:SocketConnectionAdds 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:
oncein 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:SocketConnectionRemoves an event listener previously registered usingSocketConnection.on(String, GenericRunnable).- Specified by:
offin 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 excepterrorand 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 thisSocketConnectionwhen this socket connects. This method calls theconnectevent. -
handleTimeout
public final void handleTimeout()Called internally, by subclasses or by classes managing thisSocketConnectionwhen a connection attempt times out. This method calls thetimeoutevent. -
handleData
public final boolean handleData(byte[] data) Called by classes managing thisSocketConnectionwhen data was received usingSocketConnection.read(). This method calls thedataevent.- Parameters:
data- The data that was received on this connection- Returns:
falseif nodataevent handler was set upon entry of this method
-
handleWritable
public final void handleWritable()Called by subclasses when this socket is writable. This method calls thewritableevent. -
handleClose
public final void handleClose()Called by subclasses or classes managing thisSocketConnectionwhen this connection closed. This method calls thecloseevent on the first invocation of this method. -
handleError
Called by subclasses or classes managing thisSocketConnectionwhen an error occurred in a callback. This method calls theerrorevent and forcibly closes this connection.Unlike the other
handlemethods, this method always runs synchronously.- Parameters:
e- The error- Throws:
UnhandledException- If noerrorevent handler is set
-
runAsync
Delegates the givenRunnableto thisAbstractSocketConnection's worker.- Parameters:
runnable- The runnable
-
runAsync
Delegates the givenConsumerto thisAbstractSocketConnection's worker.- Parameters:
runnable- The consumervalue- The value to pass to the consumer
-