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 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, ThrowingRunnable writeOut) protected final intMethods 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:SocketConnectionConnects thisSocketConnectionto 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 theonErrorcallback to be called.- Specified by:
connectin 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:
flushin classSocketConnection- 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 class:SocketConnectionCloses this connection after all remaining data has been flushed to the socket, which may not be immediately.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein classSocketConnection
-
destroy
public final void destroy()Description copied from class: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.- Specified by:
destroyin classSocketConnection
-
isConnected
public boolean isConnected()Returnstrueif this socket is connected.- Specified by:
isConnectedin classSocketConnection- Returns:
trueif this socket is open and available for reading or writing
-
getRemoteAddress
Description copied from class:SocketConnectionReturns the address of the remote host.- Specified by:
getRemoteAddressin classSocketConnection- Returns:
- The address of the peer host
-
getLocalAddress
Description copied from class:SocketConnectionReturns the local address of this connection.- Specified by:
getLocalAddressin classSocketConnection- Returns:
- The local address of this connection
-
getLastIOTime
public long getLastIOTime()Description copied from class:SocketConnectionReturns the last time any data was sent over this connection, either incoming or outgoing, as returned bySystem.currentTimeMillis().- Specified by:
getLastIOTimein classSocketConnection- Returns:
- The last time any data was sent over this connection in milliseconds
-
isWritable
public boolean isWritable()Description copied from class:SocketConnectionReturnstrueif this socket is writable, meaning data passed toSocketConnection.write(byte[])will not be buffered but written to the socket directly.- Specified by:
isWritablein classSocketConnection- Returns:
trueif this socket is writable
-
setReadBlock
public void setReadBlock(boolean block) Description copied from class: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 fireonDataevents while this option is set totrue.- Specified by:
setReadBlockin 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 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, ThrowingRunnable writeOut)
-