Class SecureNioChannel

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable, java.nio.channels.ByteChannel, java.nio.channels.Channel, java.nio.channels.GatheringByteChannel, java.nio.channels.ReadableByteChannel, java.nio.channels.ScatteringByteChannel, java.nio.channels.WritableByteChannel

    public class SecureNioChannel
    extends NioChannel
    Implementation of a secure socket channel
    • Field Detail

      • netInBuffer

        protected java.nio.ByteBuffer netInBuffer
      • netOutBuffer

        protected java.nio.ByteBuffer netOutBuffer
      • sslEngine

        protected javax.net.ssl.SSLEngine sslEngine
      • sniComplete

        protected boolean sniComplete
      • handshakeComplete

        protected boolean handshakeComplete
      • handshakeStatus

        protected javax.net.ssl.SSLEngineResult.HandshakeStatus handshakeStatus
      • closed

        protected boolean closed
      • closing

        protected boolean closing
    • Method Detail

      • reset

        public void reset​(java.nio.channels.SocketChannel channel,
                          NioEndpoint.NioSocketWrapper socketWrapper)
                   throws java.io.IOException
        Description copied from class: NioChannel
        Reset the channel
        Overrides:
        reset in class NioChannel
        Parameters:
        channel - the socket channel
        socketWrapper - the socket wrapper
        Throws:
        java.io.IOException - If a problem was encountered resetting the channel
      • free

        public void free()
        Description copied from class: NioChannel
        Free the channel memory
        Overrides:
        free in class NioChannel
      • flush

        protected boolean flush​(java.nio.ByteBuffer buf)
                         throws java.io.IOException
        Flushes the buffer to the network, non blocking
        Parameters:
        buf - ByteBuffer
        Returns:
        boolean true if the buffer has been emptied out, false otherwise
        Throws:
        java.io.IOException - An IO error occurred writing data
      • handshake

        public int handshake​(boolean read,
                             boolean write)
                      throws java.io.IOException
        Performs SSL handshake, non blocking, but performs NEED_TASK on the same thread. Hence, you should never call this method using your Acceptor thread, as you would slow down your system significantly. If the return value from this method is positive, the selection key should be registered interestOps given by the return value.
        Overrides:
        handshake in class NioChannel
        Parameters:
        read - boolean - true if the underlying channel is readable
        write - boolean - true if the underlying channel is writable
        Returns:
        0 if hand shake is complete, -1 if an error (other than an IOException) occurred, otherwise it returns a SelectionKey interestOps value
        Throws:
        java.io.IOException - If an I/O error occurs during the handshake or if the handshake fails during wrapping or unwrapping
      • rehandshake

        public void rehandshake​(long timeout)
                         throws java.io.IOException
        Force a blocking handshake to take place for this key. This requires that both network and application buffers have been emptied out prior to this call taking place, or a IOException will be thrown.
        Parameters:
        timeout - - timeout in milliseconds for each socket operation
        Throws:
        java.io.IOException - - if an IO exception occurs or if application or network buffers contain data
        java.net.SocketTimeoutException - - if a socket operation timed out
      • tasks

        protected javax.net.ssl.SSLEngineResult.HandshakeStatus tasks()
        Executes all the tasks needed on the same thread.
        Returns:
        the status
      • handshakeWrap

        protected javax.net.ssl.SSLEngineResult handshakeWrap​(boolean doWrite)
                                                       throws java.io.IOException
        Performs the WRAP function
        Parameters:
        doWrite - boolean
        Returns:
        the result
        Throws:
        java.io.IOException - An IO error occurred
      • handshakeUnwrap

        protected javax.net.ssl.SSLEngineResult handshakeUnwrap​(boolean doread)
                                                         throws java.io.IOException
        Perform handshake unwrap
        Parameters:
        doread - boolean
        Returns:
        the result
        Throws:
        java.io.IOException - An IO error occurred
      • getSSLSupport

        public SSLSupport getSSLSupport()
      • close

        public void close()
                   throws java.io.IOException
        Sends an SSL close message, will not physically close the connection here.
        To close the connection, you could do something like
        
           close();
           while (isOpen() && !myTimeoutFunction()) Thread.sleep(25);
           if ( isOpen() ) close(true); //forces a close if you timed out
         
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.nio.channels.Channel
        Specified by:
        close in interface java.io.Closeable
        Overrides:
        close in class NioChannel
        Throws:
        java.io.IOException - if an I/O error occurs
        java.io.IOException - if there is data on the outgoing network buffer and we are unable to flush it
      • close

        public void close​(boolean force)
                   throws java.io.IOException
        Description copied from class: NioChannel
        Close the connection.
        Overrides:
        close in class NioChannel
        Parameters:
        force - Should the underlying socket be forcibly closed?
        Throws:
        java.io.IOException - If closing the secure channel fails.
      • read

        public int read​(java.nio.ByteBuffer dst)
                 throws java.io.IOException
        Reads a sequence of bytes from this channel into the given buffer.
        Specified by:
        read in interface java.nio.channels.ReadableByteChannel
        Overrides:
        read in class NioChannel
        Parameters:
        dst - The buffer into which bytes are to be transferred
        Returns:
        The number of bytes read, possibly zero, or -1 if the channel has reached end-of-stream
        Throws:
        java.io.IOException - If some other I/O error occurs
        java.lang.IllegalArgumentException - if the destination buffer is different than getBufHandler().getReadBuffer()
      • read

        public long read​(java.nio.ByteBuffer[] dsts,
                         int offset,
                         int length)
                  throws java.io.IOException
        Specified by:
        read in interface java.nio.channels.ScatteringByteChannel
        Overrides:
        read in class NioChannel
        Throws:
        java.io.IOException
      • write

        public int write​(java.nio.ByteBuffer src)
                  throws java.io.IOException
        Writes a sequence of bytes to this channel from the given buffer.
        Specified by:
        write in interface java.nio.channels.WritableByteChannel
        Overrides:
        write in class NioChannel
        Parameters:
        src - The buffer from which bytes are to be retrieved
        Returns:
        The number of bytes written, possibly zero
        Throws:
        java.io.IOException - If some other I/O error occurs
      • write

        public long write​(java.nio.ByteBuffer[] srcs,
                          int offset,
                          int length)
                   throws java.io.IOException
        Specified by:
        write in interface java.nio.channels.GatheringByteChannel
        Overrides:
        write in class NioChannel
        Throws:
        java.io.IOException
      • flushOutbound

        public boolean flushOutbound()
                              throws java.io.IOException
        Description copied from class: NioChannel
        Return true if the buffer wrote data. NO-OP for non-secure channel.
        Overrides:
        flushOutbound in class NioChannel
        Returns:
        Always returns false for non-secure channel
        Throws:
        java.io.IOException - Never for non-secure channel
      • getSslEngine

        public javax.net.ssl.SSLEngine getSslEngine()
      • getEmptyBuf

        public java.nio.ByteBuffer getEmptyBuf()