Package org.omegazero.net.socket
Class ChannelConnection
java.lang.Object
org.omegazero.net.socket.SocketConnection
org.omegazero.net.socket.ChannelConnection
- All Implemented Interfaces:
AutoCloseable
- Direct Known Subclasses:
PlainConnection
,TLSConnection
-
Field Summary
FieldsFields inherited from class org.omegazero.net.socket.SocketConnection
readLock, writeLock
-
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, ThrowingRunnable writeOut) protected final int
Methods inherited from class org.omegazero.net.socket.SocketConnection
getApparentRemoteAddress, getAttachment, getReadLock, getWriteLock, handleClose, handleConnect, handleData, handleError, handleTimeout, handleWritable, hasConnected, hasDisconnected, localClose, localConnect, queueWrite, read, setApparentRemoteAddress, setAttachment, setOnClose, setOnConnect, setOnData, setOnError, setOnLocalClose, setOnLocalConnect, 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 class:SocketConnection
Connects thisSocketConnection
to the previously specified remote address in the constructor. If no address was specified, this method will throw anUnsupportedOperationException
.This function is non-blocking.
A connection timeout in milliseconds may be specified in the timeout parameter. If the connection has not been established within this timeout, the handler set using
#setOnTimeout(Runnable)
is called and the connection is closed. Depending on the implementation and underlying protocol, a timeout may occur earlier or never and may instead cause theonError
callback to be called.- Specified by:
connect
in classSocketConnection
- 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.- Specified by:
flush
in classSocketConnection
- 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 class:SocketConnection
Closes this connection after all remaining data has been flushed to the socket, which may not be immediately.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in classSocketConnection
-
destroy
public final void destroy()Description copied from class: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.- Specified by:
destroy
in classSocketConnection
-
isConnected
public boolean isConnected()Returnstrue
if this socket is connected.- Specified by:
isConnected
in classSocketConnection
- Returns:
true
if this socket is open and available for reading or writing
-
getRemoteAddress
Description copied from class:SocketConnection
Returns the address of the remote host.- Specified by:
getRemoteAddress
in classSocketConnection
- Returns:
- The address of the peer host
-
getLocalAddress
Description copied from class:SocketConnection
Returns the local address of this connection.- Specified by:
getLocalAddress
in classSocketConnection
- Returns:
- The local address of this connection
-
getLastIOTime
public long getLastIOTime()Description copied from class:SocketConnection
Returns the last time any data was sent over this connection, either incoming or outgoing, as returned bySystem.currentTimeMillis()
.- Specified by:
getLastIOTime
in classSocketConnection
- Returns:
- The last time any data was sent over this connection in milliseconds
-
isWritable
public boolean isWritable()Description copied from class:SocketConnection
Returnstrue
if this socket is writable, meaning data passed toSocketConnection.write(byte[])
will not be buffered but written to the socket directly.- Specified by:
isWritable
in classSocketConnection
- Returns:
true
if this socket is writable
-
setReadBlock
public void setReadBlock(boolean block) Description copied from class: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 fireonData
events while this option is set totrue
.- Specified by:
setReadBlock
in classSocketConnection
- 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, ThrowingRunnable writeOut)
-