Class LoopbackConnection
- All Implemented Interfaces:
Closeable,AutoCloseable,org.omegazero.common.util.AttachmentContainer,SocketConnection
invalid reference
SocketConnection
- Since:
- 2.3.1
- See Also:
-
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
ConstructorsConstructorDescriptionLoopbackConnection(int number, InetSocketAddress connectaddr) LoopbackConnection(int number, LoopbackConnection other) -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes this connection after all remaining data has been flushed to the socket, which may not be immediately.voidconnect(int timeout) Connects thisSocketConnectionto the previously specified remote address in the constructor.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.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.intgetOther()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.byte[]read()Reads data received from the peer host on this connection.protected voidsetOther(LoopbackConnection other) voidsetReadBlock(boolean block) Enables or disables read blocking.voidwrite(byte[] data, int offset, int length) Writes data to this connection for delivery to the peer host.voidwriteQueue(byte[] data, int offset, int length) Similar toSocketConnection.write(byte[], int, int), except that no attempt will be made to immediately flush the data to the socket, if supported by the implementation.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, 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, setOnClose, setOnConnect, setOnData, setOnError, setOnTimeout, setOnWritable, write, write, writeQueue
-
Field Details
-
LOCALHOSTADDR
-
lastIOTime
protected long lastIOTime -
acceptwrites
protected boolean acceptwrites
-
-
Constructor Details
-
LoopbackConnection
-
LoopbackConnection
-
-
Method Details
-
getNumber
public int getNumber() -
setOther
-
getOther
-
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()Description copied from interface:SocketConnectionAttempts 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:
-
close
public 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 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()Description copied from interface:SocketConnectionReturnstrueif this socket is connected.- Returns:
trueif this socket is connected
-
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
-
read
public byte[] read()Description copied from interface:SocketConnectionReads data received from the peer host on this connection.Whether this method is blocking is implementation-defined. If no data was available,
nullis returned, or the method blocks until data is available.- Returns:
- The read data or
nullif no data is available.
-
write
public void write(byte[] data, int offset, int length) Description copied from interface:SocketConnectionWrites data to this connection for delivery to the peer host.Whether this method is blocking is implementation-defined. A call to this method may store data in a temporary write buffer if the underlying socket is busy. An application should try to respect the value of
SocketConnection.isWritable()to reduce memory consumption by such write buffer if a lot of data is being written (see also:writableevent).If this method is called before the
connectevent, the data is queued in a temporary buffer and written out when the socket connects.- Parameters:
data- The data to writeoffset- The start index of the data to write in the data byte arraylength- The total number of bytes to write from the data byte array, starting at offset- See Also:
-
writeQueue
public void writeQueue(byte[] data, int offset, int length) Description copied from interface:SocketConnectionSimilar toSocketConnection.write(byte[], int, int), except that no attempt will be made to immediately flush the data to the socket, if supported by the implementation.- Parameters:
data- The data to writeoffset- The start index of the data to write in the data byte arraylength- The total number of bytes to write from the data byte array, starting at offset- See Also:
-