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, writeLockFields 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 voidclose()Closes this connection after all remaining data has been flushed to the socket, which may not be immediately.protected voidclose0()Forcibly closes this connection.voidconnect(int timeout) Connects thisSocketConnectionto the previously specified remote address in the constructor.protected abstract voidMust be called by subclass constructors.final voiddestroy()Similar toSocketConnection.close(), except that the connection is closed immediately, without waiting for data to be flushed to the socket.booleanflush()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.booleanAttempts to write any remaining data in the write buffer and write backlog to the socket and returnstrueif all data could be written.
This method is equivalent toflush().longReturns 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.booleanReturnstrueif this socket is connected.booleanReturnstrueif this socket is writable, meaning data passed toSocketConnection.write(byte[])will not be buffered but written to the socket directly.protected final intvoidsetReadBlock(boolean block) Enables or disables read blocking.protected final voidwriteBuffered(byte[] data, int offset, int length, boolean flush, ByteBuffer targetBuffer, org.omegazero.common.util.function.ThrowingRunnable writeOut) protected final intMethods 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, setWorkerMethods 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
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:SocketConnectionConnects thisSocketConnectionto 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
timeoutevent 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 theerrorevent 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:
trueif 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:SocketConnectionCloses 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:SocketConnectionSimilar toSocketConnection.close(), except that the connection is closed immediately, without waiting for data to be flushed to the socket.SocketConnection.isConnected()should returnfalseimmediately after calling this method. -
isConnected
public boolean isConnected()Returnstrueif this socket is connected.- Returns:
trueif this socket is open and available for reading or writing
-
getRemoteAddress
Description copied from interface:SocketConnectionReturns the address of the remote host.- Returns:
- The address of the peer host
-
getLocalAddress
Description copied from interface:SocketConnectionReturns the local address of this connection.- Returns:
- The local address of this connection
-
getLastIOTime
public long getLastIOTime()Description copied from interface:SocketConnectionReturns 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:SocketConnectionReturnstrueif this socket is writable, meaning data passed toSocketConnection.write(byte[])will not be buffered but written to the socket directly.- Returns:
trueif this socket is writable
-
setReadBlock
public void setReadBlock(boolean block) Description copied from interface:SocketConnectionEnables 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 tofalseagain. Note that the implementation may still emitdataevents 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 returnstrueif all data could be written.
This method is equivalent toflush().- Returns:
trueif 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)
-