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 intbufferallocationsShould be used to count the buffer allocations.protected static java.nio.ByteBufferemptybufferThis object is used to feed theSSLEngine's wrap and unwrap methods during the handshake phase.protected java.util.concurrent.ExecutorServiceexecprotected java.nio.ByteBufferinCryptencrypted data incomingprotected java.nio.ByteBufferinDataraw payload incomingprivate org.slf4j.LoggerlogLogger instanceprotected java.nio.ByteBufferoutCryptencrypted data outgoingprotected javax.net.ssl.SSLEngineResultreadEngineResultprivate byte[]saveCryptDataprotected java.nio.channels.SelectionKeyselectionKeyused to set interestOP SelectionKey.OP_WRITE for the underlying channelprotected java.nio.channels.SocketChannelsocketChannelthe underlying channelprotected javax.net.ssl.SSLEnginesslEngineprotected java.util.List<java.util.concurrent.Future<?>>tasksprotected javax.net.ssl.SSLEngineResultwriteEngineResult
-
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 voidclose()java.nio.channels.SelectableChannelconfigureBlocking(boolean b)booleanconnect(java.net.SocketAddress remote)protected voidconsumeDelegatedTasks()private voidconsumeFutureUninterruptible(java.util.concurrent.Future<?> f)protected voidcreateBuffers(javax.net.ssl.SSLSession session)booleanfinishConnect()javax.net.ssl.SSLEnginegetSSLEngine()Get the ssl engine used for the de- and encryption of the communication.booleanisBlocking()This function returns the blocking state of the channelbooleanisConnected()private booleanisHandShakeComplete()booleanisInboundDone()booleanisNeedRead()returns whether readMore should be called to fetch data which has been decoded but not yet been returned.booleanisNeedWrite()returns whether writeMore should be called write additional data.booleanisOpen()private voidprocessHandshake()This method will do whatever necessary to process the sslEngine handshake.intread(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.intreadMore(java.nio.ByteBuffer dst)This function does not read data from the underlying channel at all.private intreadRemaining(java.nio.ByteBuffer dst)read(ByteBuffer)may not be to leave all buffers(inData, inCrypt)private voidsaveCryptedData()java.net.Socketsocket()private inttransfereTo(java.nio.ByteBuffer from, java.nio.ByteBuffer to)private voidtryRestoreCryptedData()private java.nio.ByteBufferunwrap()private java.nio.ByteBufferwrap(java.nio.ByteBuffer b)intwrite(java.nio.ByteBuffer src)voidwriteMore()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 incoming
-
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.IOExceptionThis 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:
writein interfacejava.nio.channels.WritableByteChannel- Throws:
java.io.IOException
-
read
public int read(java.nio.ByteBuffer dst) throws java.io.IOExceptionBlocks when in blocking mode until at least one byte has been decoded.
When not in blocking mode 0 may be returned.- Specified by:
readin 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.SSLExceptionread(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:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.nio.channels.Channel- Specified by:
closein 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:
isOpenin interfacejava.nio.channels.Channel
-
isNeedWrite
public boolean isNeedWrite()
Description copied from interface:WrappedByteChannelreturns whether writeMore should be called write additional data.- Specified by:
isNeedWritein interfaceWrappedByteChannel- Returns:
- is a additional write needed
-
writeMore
public void writeMore() throws java.io.IOExceptionDescription copied from interface:WrappedByteChannelGets called whenWrappedByteChannel.isNeedWrite()()} requires a additional rite- Specified by:
writeMorein interfaceWrappedByteChannel- Throws:
java.io.IOException- may be thrown due to an error while writing
-
isNeedRead
public boolean isNeedRead()
Description copied from interface:WrappedByteChannelreturns whether readMore should be called to fetch data which has been decoded but not yet been returned.- Specified by:
isNeedReadin 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.SSLExceptionDescription copied from interface:WrappedByteChannelThis 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:
readMorein 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:WrappedByteChannelThis function returns the blocking state of the channel- Specified by:
isBlockingin interfaceWrappedByteChannel- Returns:
- is the channel blocking
-
getSSLEngine
public javax.net.ssl.SSLEngine getSSLEngine()
Description copied from interface:ISSLChannelGet the ssl engine used for the de- and encryption of the communication.- Specified by:
getSSLEnginein interfaceISSLChannel- Returns:
- the ssl engine of this channel
-
saveCryptedData
private void saveCryptedData()
-
tryRestoreCryptedData
private void tryRestoreCryptedData()
-
-