Package org.omegazero.net.nio.socket
Class ChannelConnection
java.lang.Object
org.omegazero.common.util.SimpleAttachmentContainer
org.omegazero.net.socket.AbstractSocketConnection
org.omegazero.net.nio.socket.ChannelConnection
- All Implemented Interfaces:
Closeable
,AutoCloseable
,org.omegazero.common.util.AttachmentContainer
,SocketConnection
- Direct Known Subclasses:
NioPlaintextConnection
,NioTLSConnection
- API Note:
- Before version 2.1.0, this class was in package
org.omegazero.net.socket
.
-
Field Summary
FieldsFields inherited from class org.omegazero.net.socket.AbstractSocketConnection
EV_CLOSE, EV_CONNECT, EV_DATA, EV_ERROR, EV_TIMEOUT, EV_WRITABLE, eventEmitter, readLock, writeLock
Fields inherited from class org.omegazero.common.util.SimpleAttachmentContainer
attachments
-
Constructor Summary
ConstructorsConstructorDescriptionChannelConnection
(SelectionKey selectionKey, ChannelProvider provider) ChannelConnection
(SelectionKey selectionKey, ChannelProvider provider, SocketAddress remote) -
Method Summary
Modifier and TypeMethodDescriptionfinal void
close()
Closes this connection after all remaining data has been flushed to the socket, which may not be immediately.protected void
close0()
Forcibly closes this connection.void
connect
(int timeout) Connects thisSocketConnection
to the previously specified remote address in the constructor.protected abstract void
Must be called by subclass constructors.final void
destroy()
Similar toSocketConnection.close()
, except that the connection is closed immediately, without waiting for data to be flushed to the socket.boolean
flush()
Attempts to flush any queued data after a call toSocketConnection.writeQueue(byte[])
or data that could not be written previously because the socket was busy.boolean
Attempts to write any remaining data in the write buffer and write backlog to the socket and returnstrue
if all data could be written.
This method is equivalent toflush()
.long
Returns the last time any data was sent over this connection, either incoming or outgoing, as returned bySystem.currentTimeMillis()
.Returns the local address of this connection.Returns the address of the remote host.boolean
Returnstrue
if this socket is connected.boolean
Returnstrue
if this socket is writable, meaning data passed toSocketConnection.write(byte[])
will not be buffered but written to the socket directly.protected final int
void
setReadBlock
(boolean block) Enables or disables read blocking.protected final void
writeBuffered
(byte[] data, int offset, int length, boolean flush, ByteBuffer targetBuffer, org.omegazero.common.util.function.ThrowingRunnable writeOut) protected final int
Methods inherited from class org.omegazero.net.socket.AbstractSocketConnection
getApparentRemoteAddress, getReadLock, getWorker, getWriteLock, handleClose, handleConnect, handleData, handleError, handleTimeout, handleWritable, hasConnected, localClose, localConnect, off, on, once, queueWrite, runAsync, runAsync, setApparentRemoteAddress, setDefaultErrorListener, setOnLocalClose, setOnLocalConnect, setWorker
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
hasDisconnected, on, on, read, setOnClose, setOnConnect, setOnData, setOnError, setOnTimeout, setOnWritable, write, write, write, writeQueue, writeQueue
-
Field Details
-
readBuf
-
writeBuf
-
-
Constructor Details
-
ChannelConnection
- Throws:
IOException
-
ChannelConnection
public ChannelConnection(SelectionKey selectionKey, ChannelProvider provider, SocketAddress remote) throws IOException - Throws:
IOException
-
-
Method Details
-
createBuffers
protected abstract void createBuffers() -
close0
protected void close0()Forcibly closes this connection. -
connect
public void connect(int timeout) Description copied from interface:SocketConnection
Connects thisSocketConnection
to the previously specified remote address in the constructor. If no address was specified, this method will throw anUnsupportedOperationException
.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 theerror
event to be emitted.- Parameters:
timeout
- The connection timeout in milliseconds. Disabled if 0
-
flush
public boolean flush()Attempts to flush any queued data after a call toSocketConnection.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:
- Implementation Requirements:
- This method does not write remaining data in this
ChannelConnection
'swriteBuf
. Subclasses must override this method to implement this behavior.
-
close
public final 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 final void destroy()Description copied from interface:SocketConnection
Similar toSocketConnection.close()
, except that the connection is closed immediately, without waiting for data to be flushed to the socket.SocketConnection.isConnected()
should returnfalse
immediately after calling this method. -
isConnected
public boolean isConnected()Returnstrue
if this socket is connected.- Returns:
true
if this socket is open and available for reading or writing
-
getRemoteAddress
Description copied from interface:SocketConnection
Returns the address of the remote host.- Returns:
- The address of the peer host
-
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 bySystem.currentTimeMillis()
.- Returns:
- The last time any data was sent over this connection in milliseconds
-
isWritable
public boolean isWritable()Description copied from interface:SocketConnection
Returnstrue
if this socket is writable, meaning data passed toSocketConnection.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 totrue
, the implementation will attempt to block incoming data from being processed and delay it until this is set tofalse
again. Note that the implementation may still emitdata
events while this option is set totrue
.- Parameters:
block
- Whether to attempt to block incoming data
-
getProvider
-
flushWriteBacklog
public boolean flushWriteBacklog()Attempts to write any remaining data in the write buffer and write backlog to the socket and returnstrue
if all data could be written.
This method is equivalent toflush()
.- Returns:
true
if all pending data was written to the socket
-
readFromSocket
- Returns:
- The number of bytes read
- Throws:
IOException
- If an IO error occurs
-
writeToSocket
- 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, org.omegazero.common.util.function.ThrowingRunnable writeOut)
-