public final class AsyncSecureSocket extends AbstractAsyncSocket
AsyncSocket
using
java.security
and javax.net.ssl
packages.
This class behaves exactly like AsyncSocket
. The only
difference is that a SSLContext
must be provided to
AsyncSecureSocket.SecureSocketBuilder
.
AsyncSecureSocket
uses this SSL context to create an
SSLEngine
. SSLEngine
is used to perform the
SSL handshake, wrap plain text, and unwrap encrypted data.
This document does not attempt to explain how to create an
SSLContext
since that is a detailed and lengthy
subject. Developers wanting to use AsyncSecureSocket
are encouraged to see
Oracle Java security documentation
to learn more creating and using SSLContext
.
AsyncSecureSocket
does not support SSL/TLS
handshake renegotiation. If the far-end signals a
re-negotiation, AsyncSecureSocket
will not respond
correctly and the TCP connection will be lost and
SocketListener.handleClose(Throwable, AbstractAsyncSocket)
called. The listener will then have the option of re-opening
the socket connection.
Note:eBus does not contain any security, cipher, or encryption code itself. eBus uses the security features provided by the Java platform to implement a secure TCP connection.
AsyncSecureSocket.SecureSocketBuilder
,
AsyncSocket
,
SocketListener
Modifier and Type | Class and Description |
---|---|
static class |
AsyncSecureSocket.SecureSocketBuilder
Constructs an
AsyncSecureSocket instance based on
the parameters set via this builder API. |
AsyncChannel.BufferMode, AsyncChannel.ChannelBuilder<B extends AsyncChannel.ChannelBuilder<?>>, AsyncChannel.ConnectState
mListener, mOutputPending, mOverflowFlag, mState
LOCALHOST, mChannel, mInputBuffer, mLocalAddress, mOutputBuffer, mOutputLock, mRemoteAddress, mSelectorThread
Modifier and Type | Method and Description |
---|---|
static AsyncSecureSocket.SecureSocketBuilder |
builder()
Returns the builder used to create a new
AsyncSecureSocket instance. |
protected void |
close(boolean nowFlag)
Does the actual work to close this async connection.
|
protected void |
handleRead()
Passes
AsyncChannel.mInputBuffer to the listener and the
number of valid bytes. |
void |
open(java.nio.channels.SelectableChannel channel)
"Opens" the socket connection by storing an already
open socket channel and beginning the secure handshake.
|
boolean |
open(java.net.SocketAddress address)
Opens a secure TCP socket connection to the specified
socket address.
|
boolean |
open(java.net.SocketAddress address,
int bindPort)
Opens a secure TCP socket connection to the specified
socket address after binding the socket to the specified
port.
|
boolean |
open(java.net.SocketAddress address,
java.net.SocketAddress bindAddress)
Opens a socket connection to the specified socket address.
|
void |
send(BufferWriter writer)
Synchronizes the output buffer and then calls
BufferWriter.fill(java.nio.ByteBuffer) to write the output to the
buffer. |
void |
send(byte[] b)
Writes
b.length bytes starting at offset zero from
byte array b to the socket. |
void |
send(byte[] b,
int off,
int len)
Writes
len bytes from byte array b
starting at offset off . |
void |
send(int b)
Writes a single byte to the socket.
|
close, closeNow, closeSocket, connectSocket, isOpen, openSocket, write
createSelector, defaultSelectorName, getOption, inputBufferSize, isSelector, localSocketAddress, outputBufferSize, remoteSocketAddress, selectorNames, setOption, supportedOptions
protected void handleRead()
AsyncChannel.mInputBuffer
to the listener and the
number of valid bytes.handleRead
in class AbstractAsyncSocket
public boolean open(java.net.SocketAddress address) throws java.io.IOException
true
if the connection
and secure handshake completes successfully;
false
if the connection process will
asynchronously complete later. If false
is
returned, then either
SocketListener.handleOpen
or
SocketListener.handleClose
is called depending on whether the connection was
successfully or unsuccessfully completed.
Note: it is highly unlikely that a TCP connection and secure handshaking completes synchronously.
open
in class AbstractAsyncSocket
address
- connect to this remote address.true
if the socket synchronously connected
or false
if the connection is continuing.java.lang.IllegalArgumentException
- if address
is null
.java.lang.IllegalStateException
- if the selector thread is not running or if the socket is
not disconnected.java.io.IOException
- if an I/O exception occurs when connecting or if this
socket is not closed.open(SocketAddress, int)
,
open(SelectableChannel)
public boolean open(java.net.SocketAddress address, int bindPort) throws java.io.IOException
true
if the connection and
secure handshake successfully completes and false
if the connection process will asynchronously complete
later. If false
is returned, then either
SocketListener.handleOpen
or
SocketListener.handleClose
is called depending on whether the connection was
successfully or unsuccessfully completed.
Note: it is highly unlikely that a TCP connection and secure handshaking completes synchronously.
open
in class AbstractAsyncSocket
address
- Connect to this remote address.bindPort
- Bind the local port to this value if
> zero; if ≤ zero then bind to any available port.true
if the socket synchronously connected
or false
if the connection is continuing.java.lang.IllegalArgumentException
- if address
is null
.java.lang.IllegalStateException
- if the selector thread is not running or if the socket is
not disconnected.java.io.IOException
- if an I/O exception occurs when connecting or if this
socket is not closed.open(SocketAddress)
,
open(SelectableChannel)
public boolean open(java.net.SocketAddress address, @Nullable java.net.SocketAddress bindAddress) throws java.io.IOException
true
if the connection completes
synchronously; false
if the connection process
will asynchronously complete later. If false
is
returned, then either
SocketListener.handleOpen
or
SocketListener.handleClose
is called depending on whether the connection
successfully or unsuccessfully completes.
Binds channel's local address to the given
bindAddress
. This binding remains in place until
channel is closed. If bind address is null
then
channel is bound to an automatically assigned address.
open
in class AbstractAsyncSocket
address
- connect to this remote address.bindAddress
- bind local address to this value if not
null
. May be null
which means channel's
local address and port are automatically assigned.true
if the socket synchronously connected
or false
if the connection process continues.java.lang.IllegalArgumentException
- if address
is null
.java.lang.IllegalStateException
- if the selector thread is not running or if the socket is
not disconnected.java.io.IOException
- if an I/O exception occurs when connecting or if this
socket is not closed.public void open(java.nio.channels.SelectableChannel channel) throws java.io.IOException
open
in class AbstractAsyncSocket
channel
- server-accepted client connection.java.lang.NullPointerException
- if socket
is null
.java.io.IOException
- if socket
is closed or this AsyncSocket
is
already open.open(SocketAddress)
,
open(SocketAddress, int)
,
AbstractAsyncSocket.close()
,
AbstractAsyncSocket.closeNow()
protected void close(boolean nowFlag)
nowFlag
is true
, then closes the
connection immediately. Any pending outbound data is lost
in this case. If nowFlag
is false
, then
the connection is closed once all pending outbound data
is transmitted.close
in class AbstractAsyncSocket
nowFlag
- if true
, close the connection
immediately.public void send(int b) throws java.io.IOException
b
.
b
's 24 high-order bits are ignored.send
in class AbstractAsyncSocket
b
- the byte to be sent.java.io.IOException
- if the socket connection is closed.java.nio.BufferOverflowException
- if the output buffer size has been exceeded.send(byte[])
,
send(byte[], int, int)
,
send(BufferWriter)
public void send(byte[] b) throws java.io.IOException
b.length
bytes starting at offset zero from
byte array b
to the socket.send
in class AbstractAsyncSocket
b
- the data to be sent.java.lang.IllegalArgumentException
- if b
is null
.java.io.IOException
- if the socket connection is closed.java.nio.BufferOverflowException
- if the output buffer size has been exceeded.send(int)
,
send(byte[], int, int)
,
send(BufferWriter)
public void send(byte[] b, int off, int len) throws java.io.IOException
len
bytes from byte array b
starting at offset off
. The b
's bytes are
written to the output stream in order: byte b[off]
is written first and byte b[off+len-1]
is written
last.
If b
is null
, an
IllegalArgumentException
is thrown. If off
is negative, or len
is negative, or
off+len
is greater than b
's length, then
an IndexOutOfBoundsException
is thrown.
If the output buffer is overflowed, then none of
b
's bytes are added to the output buffer. This
allows the caller to catch the exception and implement an
error recovery strategy.
SocketListener.handleOutputAvailable(AbstractAsyncSocket)
is called when the output buffer is no longer full.
send
in class AbstractAsyncSocket
b
- data to be sent.off
- offset in b
.len
- number of bytes to be sent.java.lang.IllegalArgumentException
- if b
is null
.java.lang.IndexOutOfBoundsException
- if off
or len
is <
zero, or
off+len
is > b.length
.java.io.IOException
- if the socket is closed.java.nio.BufferOverflowException
- if the output buffer size has been exceeded.send(int)
,
send(byte[])
,
send(BufferWriter)
public void send(BufferWriter writer) throws java.io.IOException
BufferWriter.fill(java.nio.ByteBuffer)
to write the output to the
buffer.
If the output buffer is overflowed, then none
of writer
's output are added to the output buffer.
This allows the caller to catch the exception and
implement an error recovery strategy.
SocketListener.handleOutputAvailable(AbstractAsyncSocket)
is called when the output buffer is no longer full.
send
in class AbstractAsyncSocket
writer
- writes to the output buffer.java.lang.NullPointerException
- if writer
is null
.java.io.IOException
- if the socket is closed.java.nio.BufferOverflowException
- if the output buffer size has been exceeded.send(int)
,
send(byte[])
,
send(byte[], int, int)
public static AsyncSecureSocket.SecureSocketBuilder builder()
AsyncSecureSocket
instance.AsyncSecureSocket
builder.Copyright © 2001 - 2024. Charles W. Rapp. All rights reserved.