Package org.java_websocket
Class SSLSocketChannel2
- java.lang.Object
-
- org.java_websocket.SSLSocketChannel2
-
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
,java.nio.channels.ByteChannel
,java.nio.channels.Channel
,java.nio.channels.ReadableByteChannel
,java.nio.channels.WritableByteChannel
,ISSLChannel
,WrappedByteChannel
public class SSLSocketChannel2 extends java.lang.Object implements java.nio.channels.ByteChannel, WrappedByteChannel, ISSLChannel
Implements the relevant portions of the SocketChannel interface with the SSLEngine wrapper.
-
-
Field Summary
Fields Modifier and Type Field Description protected int
bufferallocations
Should be used to count the buffer allocations.protected static java.nio.ByteBuffer
emptybuffer
This object is used to feed theSSLEngine
's wrap and unwrap methods during the handshake phase.protected java.util.concurrent.ExecutorService
exec
protected java.nio.ByteBuffer
inCrypt
encrypted data incomingprotected java.nio.ByteBuffer
inData
raw payload incommingprivate org.slf4j.Logger
log
Logger instanceprotected java.nio.ByteBuffer
outCrypt
encrypted data outgoingprotected javax.net.ssl.SSLEngineResult
readEngineResult
private byte[]
saveCryptData
protected java.nio.channels.SelectionKey
selectionKey
used to set interestOP SelectionKey.OP_WRITE for the underlying channelprotected java.nio.channels.SocketChannel
socketChannel
the underlying channelprotected javax.net.ssl.SSLEngine
sslEngine
protected java.util.List<java.util.concurrent.Future<?>>
tasks
protected javax.net.ssl.SSLEngineResult
writeEngineResult
-
Constructor Summary
Constructors Constructor Description SSLSocketChannel2(java.nio.channels.SocketChannel channel, javax.net.ssl.SSLEngine sslEngine, java.util.concurrent.ExecutorService exec, java.nio.channels.SelectionKey key)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
java.nio.channels.SelectableChannel
configureBlocking(boolean b)
boolean
connect(java.net.SocketAddress remote)
protected void
consumeDelegatedTasks()
private void
consumeFutureUninterruptible(java.util.concurrent.Future<?> f)
protected void
createBuffers(javax.net.ssl.SSLSession session)
boolean
finishConnect()
javax.net.ssl.SSLEngine
getSSLEngine()
Get the ssl engine used for the de- and encryption of the communication.boolean
isBlocking()
This function returns the blocking state of the channelboolean
isConnected()
private boolean
isHandShakeComplete()
boolean
isInboundDone()
boolean
isNeedRead()
returns whether readMore should be called to fetch data which has been decoded but not yet been returned.boolean
isNeedWrite()
returns whether writeMore should be called write additional data.boolean
isOpen()
private void
processHandshake()
This method will do whatever necessary to process the sslengine handshake.int
read(java.nio.ByteBuffer dst)
Blocks when in blocking mode until at least one byte has been decoded.
When not in blocking mode 0 may be returned.int
readMore(java.nio.ByteBuffer dst)
This function does not read data from the underlying channel at all.private int
readRemaining(java.nio.ByteBuffer dst)
read(ByteBuffer)
may not be to leave all buffers(inData, inCrypt)private void
saveCryptedData()
java.net.Socket
socket()
private int
transfereTo(java.nio.ByteBuffer from, java.nio.ByteBuffer to)
private void
tryRestoreCryptedData()
private java.nio.ByteBuffer
unwrap()
private java.nio.ByteBuffer
wrap(java.nio.ByteBuffer b)
int
write(java.nio.ByteBuffer src)
void
writeMore()
Gets called whenWrappedByteChannel.isNeedWrite()
()} requires a additional rite
-
-
-
Field Detail
-
emptybuffer
protected static java.nio.ByteBuffer emptybuffer
This object is used to feed theSSLEngine
's wrap and unwrap methods during the handshake phase.
-
log
private final org.slf4j.Logger log
Logger instance- Since:
- 1.4.0
-
exec
protected java.util.concurrent.ExecutorService exec
-
tasks
protected java.util.List<java.util.concurrent.Future<?>> tasks
-
inData
protected java.nio.ByteBuffer inData
raw payload incomming
-
outCrypt
protected java.nio.ByteBuffer outCrypt
encrypted data outgoing
-
inCrypt
protected java.nio.ByteBuffer inCrypt
encrypted data incoming
-
socketChannel
protected java.nio.channels.SocketChannel socketChannel
the underlying channel
-
selectionKey
protected java.nio.channels.SelectionKey selectionKey
used to set interestOP SelectionKey.OP_WRITE for the underlying channel
-
sslEngine
protected javax.net.ssl.SSLEngine sslEngine
-
readEngineResult
protected javax.net.ssl.SSLEngineResult readEngineResult
-
writeEngineResult
protected javax.net.ssl.SSLEngineResult writeEngineResult
-
bufferallocations
protected int bufferallocations
Should be used to count the buffer allocations. But because of #190 where HandshakeStatus.FINISHED is not properly returned by nio wrap/unwrap this variable is used to check whethercreateBuffers(SSLSession)
needs to be called.
-
saveCryptData
private byte[] saveCryptData
-
-
Method Detail
-
consumeFutureUninterruptible
private void consumeFutureUninterruptible(java.util.concurrent.Future<?> f)
-
processHandshake
private void processHandshake() throws java.io.IOException
This method will do whatever necessary to process the sslengine handshake. Thats why it's called both from theread(ByteBuffer)
andwrite(ByteBuffer)
- Throws:
java.io.IOException
-
wrap
private java.nio.ByteBuffer wrap(java.nio.ByteBuffer b) throws javax.net.ssl.SSLException
- Throws:
javax.net.ssl.SSLException
-
unwrap
private java.nio.ByteBuffer unwrap() throws javax.net.ssl.SSLException
- Throws:
javax.net.ssl.SSLException
-
consumeDelegatedTasks
protected void consumeDelegatedTasks()
-
createBuffers
protected void createBuffers(javax.net.ssl.SSLSession session)
-
write
public int write(java.nio.ByteBuffer src) throws java.io.IOException
- Specified by:
write
in interfacejava.nio.channels.WritableByteChannel
- Throws:
java.io.IOException
-
read
public int read(java.nio.ByteBuffer dst) throws java.io.IOException
Blocks when in blocking mode until at least one byte has been decoded.
When not in blocking mode 0 may be returned.- Specified by:
read
in interfacejava.nio.channels.ReadableByteChannel
- Returns:
- the number of bytes read.
- Throws:
java.io.IOException
-
readRemaining
private int readRemaining(java.nio.ByteBuffer dst) throws javax.net.ssl.SSLException
read(ByteBuffer)
may not be to leave all buffers(inData, inCrypt)- Throws:
javax.net.ssl.SSLException
-
isConnected
public boolean isConnected()
-
close
public void close() throws java.io.IOException
- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.nio.channels.Channel
- Specified by:
close
in interfacejava.io.Closeable
- Throws:
java.io.IOException
-
isHandShakeComplete
private boolean isHandShakeComplete()
-
configureBlocking
public java.nio.channels.SelectableChannel configureBlocking(boolean b) throws java.io.IOException
- Throws:
java.io.IOException
-
connect
public boolean connect(java.net.SocketAddress remote) throws java.io.IOException
- Throws:
java.io.IOException
-
finishConnect
public boolean finishConnect() throws java.io.IOException
- Throws:
java.io.IOException
-
socket
public java.net.Socket socket()
-
isInboundDone
public boolean isInboundDone()
-
isOpen
public boolean isOpen()
- Specified by:
isOpen
in interfacejava.nio.channels.Channel
-
isNeedWrite
public boolean isNeedWrite()
Description copied from interface:WrappedByteChannel
returns whether writeMore should be called write additional data.- Specified by:
isNeedWrite
in interfaceWrappedByteChannel
- Returns:
- is a additional write needed
-
writeMore
public void writeMore() throws java.io.IOException
Description copied from interface:WrappedByteChannel
Gets called whenWrappedByteChannel.isNeedWrite()
()} requires a additional rite- Specified by:
writeMore
in interfaceWrappedByteChannel
- Throws:
java.io.IOException
- may be thrown due to an error while writing
-
isNeedRead
public boolean isNeedRead()
Description copied from interface:WrappedByteChannel
returns whether readMore should be called to fetch data which has been decoded but not yet been returned.- Specified by:
isNeedRead
in interfaceWrappedByteChannel
- Returns:
- is a additional read needed
- See Also:
ReadableByteChannel.read(ByteBuffer)
,WrappedByteChannel.readMore(ByteBuffer)
-
readMore
public int readMore(java.nio.ByteBuffer dst) throws javax.net.ssl.SSLException
Description copied from interface:WrappedByteChannel
This function does not read data from the underlying channel at all. It is just a way to fetch data which has already be received or decoded but was but was not yet returned to the user. This could be the case when the decoded data did not fit into the buffer the user passed toReadableByteChannel.read(ByteBuffer)
.- Specified by:
readMore
in interfaceWrappedByteChannel
- Parameters:
dst
- the destiny of the read- Returns:
- the amount of remaining data
- Throws:
javax.net.ssl.SSLException
-
transfereTo
private int transfereTo(java.nio.ByteBuffer from, java.nio.ByteBuffer to)
-
isBlocking
public boolean isBlocking()
Description copied from interface:WrappedByteChannel
This function returns the blocking state of the channel- Specified by:
isBlocking
in interfaceWrappedByteChannel
- Returns:
- is the channel blocking
-
getSSLEngine
public javax.net.ssl.SSLEngine getSSLEngine()
Description copied from interface:ISSLChannel
Get the ssl engine used for the de- and encryption of the communication.- Specified by:
getSSLEngine
in interfaceISSLChannel
- Returns:
- the ssl engine of this channel
-
saveCryptedData
private void saveCryptedData()
-
tryRestoreCryptedData
private void tryRestoreCryptedData()
-
-