Class SocketCommNIO
- All Implemented Interfaces:
ISocketComm
,IWorkerTimeout
- Direct Known Subclasses:
SocketComm
For proxy and/or WebSocket, an authenticator must/should be used to support user authentication with the proxy or destination (for WebSocket).
For SSL/TLS, client certificates can be added, discrete or from system, and if so, an password callback might be have to be registered to provide a password for a keystore or a certificate, unless preset in the properties.
For SSL/TLS, server certificates validator can be provided in order to check server certificates and/or the destinations host name.
- Author:
- Christopher Mindus
Field Summary
Constructor Summary
ModifierConstructorDescriptionprotected
SocketCommNIO
(WorkerNIO worker, SocketChannel socketChannel, SocketCommListener listener, boolean isVerbose) Creates a new Socket Client Communication channel from a Server Socket Accept.SocketCommNIO
(String host, int port, String bindAddress, WorkerNIO worker, SocketCommListener listener, boolean isVerbose, SSLEngine engine) Creates a new Socket Client Communication channel as UNINITIALIZED.Method Summary
Modifier and TypeMethodDescriptionvoid
checkTimeout
(long timeNow) Performs a timeout check for the socket channel.boolean
close()
Call this method to close the socket.boolean
close
(boolean rightNow) Call this method to close the socket.void
enterSecureMode
(SSLEngine engine, ByteBuffer remainingBuffer) Enters SSL mode for a plain connection from another protocol, e.g.long
Gets the read time-out value for the connection before the state enters CONNECTED.long
Gets the read time-out value for the connection.long
getRemainingTimeout
(long timeNow) Gets the socket timeout value remaining.Returns the Internet Protocol (IP) address of the client or last proxy that sent the request.Returns the fully qualified name of the client or the last proxy that sent the request.Returns the remote address to which this channel's socket is connected.Gets the SSL communication layer.getState()
Gets the state of the connection.long
Sets the read time-out value for the connection.boolean
Checks if the session has been opened, and if so cannot be reopened but needs to have a new instance.boolean
isClosed()
Checks if the connection is disposed of.boolean
isSecure()
Checks if this is a secure communication link.void
open()
Opens the communication.void
process
(ByteBuffer buffer) Processes the SelectionKey as it has generated an event.void
send
(ByteBuffer sendBuf) Adds the byte buffer to the send queue without blocking.static void
Sets a new logger.void
setNonConnectedReadTimeout
(long timeout) Sets the read time-out value for the connection before the state enters CONNECTED.void
setReadTimeout
(long timeout) Sets the read time-out value for the connection.protected Socket
Sets up the socket.void
setWriteTimeout
(long timeout) Sets the read time-out value for the connection.
Field Details
host
The host name.port
public final int portThe port.bindAddress
The bind address, null for none.doResolveHostNames
public boolean doResolveHostNamesFlag for resolving host names.
Constructor Details
SocketCommNIO
public SocketCommNIO(String host, int port, String bindAddress, WorkerNIO worker, SocketCommListener listener, boolean isVerbose, SSLEngine engine) Creates a new Socket Client Communication channel as UNINITIALIZED.- Parameters:
host
- The destination host.port
- The destination port.bindAddress
- The bind address or null for none.worker
- The worker thread for socket processing.listener
- The listener for socket events.isVerbose
- Flag for logging.
SocketCommNIO
protected SocketCommNIO(WorkerNIO worker, SocketChannel socketChannel, SocketCommListener listener, boolean isVerbose) throws IOException Creates a new Socket Client Communication channel from a Server Socket Accept.- Throws:
IOException
- For I/O errors.
Method Details
setLogger
Sets a new logger.isSecure
public boolean isSecure()Checks if this is a secure communication link.getSSLCommLayer
Gets the SSL communication layer.- Returns:
- The instance of the SSL communication layer, or null if not secure.
setupSocket
Sets up the socket.- Throws:
SocketException
- For socket errors.
hasBeenOpen
public boolean hasBeenOpen()Checks if the session has been opened, and if so cannot be reopened but needs to have a new instance.open
Opens the communication.- Throws:
IOException
enterSecureMode
Enters SSL mode for a plain connection from another protocol, e.g. Telnet.- Parameters:
engine
- The SSL engine.remainingBuffer
- Remaining buffer to process, null for none.- Throws:
IOException
- if an I/O error occurs.
process
Processes the SelectionKey as it has generated an event.- Specified by:
process
in interfaceISocketComm
- Parameters:
buffer
- The direct buffer allocated by the worker.
setNonConnectedReadTimeout
public void setNonConnectedReadTimeout(long timeout) Sets the read time-out value for the connection before the state enters CONNECTED.- Parameters:
timeout
- The timeout value in milliseconds, 0 for indefinite.
getNonConnectedReadTimeout
public long getNonConnectedReadTimeout()Gets the read time-out value for the connection before the state enters CONNECTED.- Returns:
- The timeout value in milliseconds, 0 for indefinite.
setReadTimeout
public void setReadTimeout(long timeout) Sets the read time-out value for the connection.- Parameters:
timeout
- The timeout value in milliseconds, 0 for indefinite.
getReadTimeout
public long getReadTimeout()Gets the read time-out value for the connection.- Returns:
- The timeout value in milliseconds, 0 for indefinite.
setWriteTimeout
public void setWriteTimeout(long timeout) Sets the read time-out value for the connection.- Parameters:
timeout
- The timeout value in milliseconds, 0 for indefinite.
getWriteTimeout
public long getWriteTimeout()Sets the read time-out value for the connection.- Returns:
- The timeout value in milliseconds, 0 for indefinite.
getRemainingTimeout
public long getRemainingTimeout(long timeNow) Gets the socket timeout value remaining.- Specified by:
getRemainingTimeout
in interfaceIWorkerTimeout
- Parameters:
timeNow
- The current time.- Returns:
- Time in milliseconds until timeout, NOTE! -1 for indefinite timeout.
checkTimeout
public void checkTimeout(long timeNow) Performs a timeout check for the socket channel. This is called to all channels, so each channel must check its own state.- Specified by:
checkTimeout
in interfaceIWorkerTimeout
- Parameters:
timeNow
- The current time.
send
Adds the byte buffer to the send queue without blocking.- Parameters:
sendBuf
- The buffer to send.- Throws:
IOException
- for failures, e.g. socket is closed, not open, not connected.
getState
Gets the state of the connection.getRemoteAddr
Returns the Internet Protocol (IP) address of the client or last proxy that sent the request. The IP address format depends if it is an IPv4 or IPv6 address.- Returns:
- a String containing the IP address of the client that sent the request.
getRemoteHost
Returns the fully qualified name of the client or the last proxy that sent the request. If the engine cannot or chooses not to resolve the host name (to improve performance), this method returns the dotted-string form of the IP address (if IPv4, colon'ed-string for IPv6).The resolving is done once only, if the
doResolveHostNames
is true.- Returns:
- a String containing the fully qualified name of the client.
getSocketAddress
Returns the remote address to which this channel's socket is connected.Where the channel is bound and connected to an Internet Protocol socket address then the return value from this method is of type
InetSocketAddress
.- Returns:
- The remote address;
null
if the channel's socket is not connected.
isClosed
public boolean isClosed()Checks if the connection is disposed of.close
public boolean close()Call this method to close the socket.A call to this method will return true while flushing pending send operations and then close the socket when that is completed. If no send operations are pending, the socket is closed immediately.
- Returns:
- true if closed completed successfully, false if already closed. Note that true is returned even if the socket is entering or is in CLOSING state.
close
public boolean close(boolean rightNow) Call this method to close the socket.If the parameter "rightNow" is false, a call to this method will return true while flushing pending send operations and then close the socket when that is completed. If no send operations are pending, the socket is closed immediately.
- Specified by:
close
in interfaceISocketComm
- Parameters:
rightNow
- If false, a flush of pending send operations will be performed.- Returns:
- true if closed, false if already closed.