Class NIOConnection

    • Field Detail

      • NOTIFICATION_INITIALIZED

        protected static final java.lang.Object NOTIFICATION_INITIALIZED
      • NOTIFICATION_CLOSED_COMPLETE

        protected static final java.lang.Object NOTIFICATION_CLOSED_COMPLETE
      • maxAsyncWriteQueueSize

        protected volatile int maxAsyncWriteQueueSize
      • readTimeoutMillis

        protected volatile long readTimeoutMillis
      • writeTimeoutMillis

        protected volatile long writeTimeoutMillis
      • channel

        protected volatile java.nio.channels.SelectableChannel channel
      • selectionKey

        protected volatile java.nio.channels.SelectionKey selectionKey
      • processor

        protected volatile Processor processor
      • connectCloseSemaphoreUpdater

        protected static final java.util.concurrent.atomic.AtomicReferenceFieldUpdater<NIOConnection,​java.lang.Object> connectCloseSemaphoreUpdater
        Semaphore responsible for connect/close notification
      • isBlocking

        protected volatile boolean isBlocking
      • isStandalone

        protected volatile boolean isStandalone
      • zeroByteReadCount

        protected short zeroByteReadCount
    • Constructor Detail

      • NIOConnection

        public NIOConnection​(NIOTransport transport)
    • Method Detail

      • getId

        public long getId()
        Description copied from interface: Connection
        Returns id of this instance, unique in the context of the JVM and the implementation class.
        Specified by:
        getId in interface Connection<java.net.SocketAddress>
        Returns:
        id of this instance, never null.
      • configureBlocking

        public void configureBlocking​(boolean isBlocking)
        Description copied from interface: Connection
        Sets the Connection mode.
        Specified by:
        configureBlocking in interface Connection<java.net.SocketAddress>
        Parameters:
        isBlocking - the Connection mode. true, if Connection should operate in blocking mode, or false otherwise.
      • isBlocking

        public boolean isBlocking()
        Specified by:
        isBlocking in interface Connection<java.net.SocketAddress>
        Returns:
        the Connection mode. true, if Connection is operating in blocking mode, or false otherwise.
      • configureStandalone

        public void configureStandalone​(boolean isStandalone)
        Specified by:
        configureStandalone in interface Connection<java.net.SocketAddress>
      • isStandalone

        public boolean isStandalone()
        Specified by:
        isStandalone in interface Connection<java.net.SocketAddress>
      • getMaxAsyncWriteQueueSize

        public int getMaxAsyncWriteQueueSize()
        Get the max size (in bytes) of asynchronous write queue associated with connection.
        Specified by:
        getMaxAsyncWriteQueueSize in interface Connection<java.net.SocketAddress>
        Returns:
        the max size (in bytes) of asynchronous write queue associated with connection.
      • setMaxAsyncWriteQueueSize

        public void setMaxAsyncWriteQueueSize​(int maxAsyncWriteQueueSize)
        Set the max size (in bytes) of asynchronous write queue associated with connection.
        Specified by:
        setMaxAsyncWriteQueueSize in interface Connection<java.net.SocketAddress>
        Parameters:
        maxAsyncWriteQueueSize - the max size (in bytes) of asynchronous write queue associated with connection.
      • getReadTimeout

        public long getReadTimeout​(java.util.concurrent.TimeUnit timeUnit)
        Description copied from interface: Connection
        Returns the current value for the blocking read timeout converted to the provided TimeUnit specification. If this value hasn't been explicitly set, it will default to 30 seconds.
        Specified by:
        getReadTimeout in interface Connection<java.net.SocketAddress>
        Parameters:
        timeUnit - the TimeUnit to convert the returned result to.
        Returns:
        the read timeout value
      • setReadTimeout

        public void setReadTimeout​(long timeout,
                                   java.util.concurrent.TimeUnit timeUnit)
        Description copied from interface: Connection
        Specifies the timeout for the blocking reads. This may be overridden on a per-connection basis. A value of zero or less effectively disables the timeout.
        Specified by:
        setReadTimeout in interface Connection<java.net.SocketAddress>
        Parameters:
        timeout - the new timeout value
        timeUnit - the TimeUnit specification of the provided value.
        See Also:
        Connection.setReadTimeout(long, java.util.concurrent.TimeUnit)
      • getWriteTimeout

        public long getWriteTimeout​(java.util.concurrent.TimeUnit timeUnit)
        Description copied from interface: Connection
        Returns the current value for the blocking write timeout converted to the provided TimeUnit specification. If this value hasn't been explicitly set, it will default to 30 seconds.
        Specified by:
        getWriteTimeout in interface Connection<java.net.SocketAddress>
        Parameters:
        timeUnit - the TimeUnit to convert the returned result to.
        Returns:
        the write timeout value
      • setWriteTimeout

        public void setWriteTimeout​(long timeout,
                                    java.util.concurrent.TimeUnit timeUnit)
        Description copied from interface: Connection
        Specifies the timeout for the blocking writes. This may be overridden on a per-connection basis. A value of zero or less effectively disables the timeout.
        Specified by:
        setWriteTimeout in interface Connection<java.net.SocketAddress>
        Parameters:
        timeout - the new timeout value
        timeUnit - the TimeUnit specification of the provided value.
        See Also:
        Connection.setWriteTimeout(long, java.util.concurrent.TimeUnit)
      • setSelectorRunner

        protected void setSelectorRunner​(SelectorRunner selectorRunner)
      • attachToSelectorRunner

        public void attachToSelectorRunner​(SelectorRunner selectorRunner)
                                    throws java.io.IOException
        Throws:
        java.io.IOException
      • detachSelectorRunner

        public void detachSelectorRunner()
                                  throws java.io.IOException
        Throws:
        java.io.IOException
      • getChannel

        public java.nio.channels.SelectableChannel getChannel()
      • setChannel

        protected void setChannel​(java.nio.channels.SelectableChannel channel)
      • getSelectionKey

        public java.nio.channels.SelectionKey getSelectionKey()
      • setSelectionKey

        protected void setSelectionKey​(java.nio.channels.SelectionKey selectionKey)
      • obtainProcessorState

        public <E> E obtainProcessorState​(Processor processor,
                                          java.util.function.Supplier<E> factory)
        Description copied from interface: Connection
        Returns the Processor state associated with this Connection.
        Specified by:
        obtainProcessorState in interface Connection<java.net.SocketAddress>
        Type Parameters:
        E - state of the Processor
        Parameters:
        processor - Processor
        factory - factory that is used to initialise the state
        Returns:
        the Processor state associated with this Connection.
      • executeInEventThread

        public void executeInEventThread​(IOEvent event,
                                         java.lang.Runnable runnable)
        Description copied from interface: Connection
        Executes the Runnable in the thread, responsible for running the given type of event on this Connection. The thread will be chosen based on Transport settings, especially current I/O strategy.
        Specified by:
        executeInEventThread in interface Connection<java.net.SocketAddress>
        Parameters:
        event - event to get the thread pool from
        runnable - Runnable to run in the thread
      • read

        public <M> GrizzlyFuture<ReadResult<M,​java.net.SocketAddress>> read()
        Description copied from interface: Readable
        Method reads data.
        Specified by:
        read in interface Readable<java.net.SocketAddress>
        Type Parameters:
        M - type of data to read
        Returns:
        Future, using which it's possible to check the result
      • write

        public <M> GrizzlyFuture<WriteResult<M,​java.net.SocketAddress>> write​(M message)
        Description copied from interface: Writeable
        Method writes the buffer.
        Specified by:
        write in interface Writeable<java.net.SocketAddress>
        Type Parameters:
        M - type of data to be written
        Parameters:
        message - the buffer, from which the data will be written
        Returns:
        Future, using which it's possible to check the result
      • write

        public <M> void write​(M message,
                              CompletionHandler<WriteResult<M,​java.net.SocketAddress>> completionHandler)
        Description copied from interface: Writeable
        Method writes the buffer.
        Specified by:
        write in interface Writeable<java.net.SocketAddress>
        Type Parameters:
        M - type of data to be written
        Parameters:
        message - the buffer, from which the data will be written
        completionHandler - CompletionHandler, which will get notified, when write will be completed
      • write

        @Deprecated
        public <M> void write​(M message,
                              CompletionHandler<WriteResult<M,​java.net.SocketAddress>> completionHandler,
                              PushBackHandler pushbackHandler)
        Deprecated.
        Description copied from interface: Writeable
        Method writes the buffer.
        Specified by:
        write in interface Writeable<java.net.SocketAddress>
        Type Parameters:
        M - type of data to be written
        Parameters:
        message - the buffer, from which the data will be written
        completionHandler - CompletionHandler, which will get notified, when write will be completed
        pushbackHandler - PushBackHandler, which will be notified if message was accepted by transport write queue or refused
      • write

        public <M> void write​(java.net.SocketAddress dstAddress,
                              M message,
                              CompletionHandler<WriteResult<M,​java.net.SocketAddress>> completionHandler)
        Description copied from interface: Writeable
        Method writes the buffer to the specific address.
        Specified by:
        write in interface Writeable<java.net.SocketAddress>
        Type Parameters:
        M - type of data to be written
        Parameters:
        dstAddress - the destination address the buffer will be sent to
        message - the buffer, from which the data will be written
        completionHandler - CompletionHandler, which will get notified, when write will be completed
      • write

        @Deprecated
        public <M> void write​(java.net.SocketAddress dstAddress,
                              M message,
                              CompletionHandler<WriteResult<M,​java.net.SocketAddress>> completionHandler,
                              PushBackHandler pushbackHandler)
        Deprecated.
        Description copied from interface: Writeable
        Method writes the buffer to the specific address.
        Specified by:
        write in interface Writeable<java.net.SocketAddress>
        Type Parameters:
        M - type of data to be written
        Parameters:
        dstAddress - the destination address the buffer will be sent to
        message - the buffer, from which the data will be written
        completionHandler - CompletionHandler, which will get notified, when write will be completed
        pushbackHandler - PushBackHandler, which will be notified if message was accepted by transport write queue or refused
      • isOpen

        public boolean isOpen()
        Description copied from interface: Connection
        Is Connection open and ready. Returns true, if connection is open and ready, or false otherwise.
        Specified by:
        isOpen in interface Closeable
        Specified by:
        isOpen in interface Connection<java.net.SocketAddress>
        Returns:
        true, if connection is open and ready, or false otherwise.
      • assertOpen

        public void assertOpen()
                        throws java.io.IOException
        Description copied from interface: Connection
        Checks if this Connection is open and ready to be used. If this Connection is closed - this method throws IOException giving the reason why this Connection was closed.
        Specified by:
        assertOpen in interface Closeable
        Specified by:
        assertOpen in interface Connection<java.net.SocketAddress>
        Throws:
        java.io.IOException - giving the reason why this Closeable was closed.
      • isClosed

        public boolean isClosed()
      • terminateWithReason

        public void terminateWithReason​(java.io.IOException reason)
        Description copied from interface: Connection
        Closes the Connection and provides the reason description. This method is similar to Connection.terminateSilently(), but additionally provides the reason why the Connection will be closed.
        Specified by:
        terminateWithReason in interface Closeable
        Specified by:
        terminateWithReason in interface Connection<java.net.SocketAddress>
        Parameters:
        reason - reason why terminated. This will be thrown is Closeable.isOpen() is called subsequently
      • close

        public GrizzlyFuture<Closeable> close()
        Description copied from interface: Connection
        Gracefully close the Connection
        Specified by:
        close in interface Closeable
        Specified by:
        close in interface Connection<java.net.SocketAddress>
        Returns:
        Future, which could be checked in case, if close operation will be run asynchronously
        See Also:
        which is not asynchronous
      • closeWithReason

        public void closeWithReason​(java.io.IOException reason)
        Description copied from interface: Connection
        Gracefully closes the Connection and provides the reason description. This method is similar to Connection.closeSilently(), but additionally provides the reason why the Connection will be closed.
        Specified by:
        closeWithReason in interface Closeable
        Specified by:
        closeWithReason in interface Connection<java.net.SocketAddress>
        Parameters:
        reason - reason why closed, this will be thrown by Closeable.isOpen() if called subsequently
      • doClose

        protected void doClose()
                        throws java.io.IOException
        Do the actual connection close.
        Throws:
        java.io.IOException
      • notifyConnectionError

        public void notifyConnectionError​(java.lang.Throwable error)
        Method gets invoked, when error occur during the Connection lifecycle.
        Specified by:
        notifyConnectionError in interface Connection<java.net.SocketAddress>
        Parameters:
        error - Throwable.
      • notifyProbesBind

        protected static void notifyProbesBind​(NIOConnection connection)
        Notify registered ConnectionProbes about the bind event.
        Parameters:
        connection - the Connection event occurred on.
      • notifyProbesAccept

        protected static void notifyProbesAccept​(NIOConnection serverConnection,
                                                 NIOConnection clientConnection)
        Notify registered ConnectionProbes about the accept event.
        Parameters:
        serverConnection - the server Connection, which accepted the client connection.
        clientConnection - the client Connection.
      • notifyProbesConnect

        protected static void notifyProbesConnect​(NIOConnection connection)
        Notify registered ConnectionProbes about the connect event.
        Parameters:
        connection - the Connection event occurred on.
      • notifyProbesRead

        protected static void notifyProbesRead​(NIOConnection connection,
                                               Buffer data,
                                               int size)
        Notify registered ConnectionProbes about the read event.
      • notifyProbesWrite

        protected static void notifyProbesWrite​(NIOConnection connection,
                                                Buffer data,
                                                long size)
        Notify registered ConnectionProbes about the write event.
      • notifyIOEventReady

        protected static void notifyIOEventReady​(NIOConnection connection,
                                                 IOEvent ioEvent)
        Notify registered ConnectionProbes about the IO Event ready event.
        Parameters:
        connection - the Connection event occurred on.
        ioEvent - the IOEvent.
      • notifyIOEventEnabled

        protected static void notifyIOEventEnabled​(NIOConnection connection,
                                                   IOEvent ioEvent)
        Notify registered ConnectionProbes about the IO Event enabled event.
        Parameters:
        connection - the Connection event occurred on.
        ioEvent - the IOEvent.
      • notifyIOEventDisabled

        protected static void notifyIOEventDisabled​(NIOConnection connection,
                                                    IOEvent ioEvent)
        Notify registered ConnectionProbes about the IO Event disabled event.
        Parameters:
        connection - the Connection event occurred on.
        ioEvent - the IOEvent.
      • notifyProbesClose

        protected static void notifyProbesClose​(NIOConnection connection)
        Notify registered ConnectionProbes about the close event.
        Parameters:
        connection - the Connection event occurred on.
      • notifyProbesError

        protected static void notifyProbesError​(NIOConnection connection,
                                                java.lang.Throwable error)
        Notify registered ConnectionProbes about the error.
        Parameters:
        connection - the Connection event occurred on.
      • preClose

        protected void preClose()
      • enableInitialOpRead

        protected void enableInitialOpRead()
                                    throws java.io.IOException
        Enables OP_READ if it has never been enabled before.
        Throws:
        java.io.IOException
      • simulateIOEvent

        public void simulateIOEvent​(IOEvent ioEvent)
                             throws java.io.IOException
        Specified by:
        simulateIOEvent in interface Connection<java.net.SocketAddress>
        Throws:
        java.io.IOException
      • enableIOEvent

        public final void enableIOEvent​(IOEvent ioEvent)
                                 throws java.io.IOException
        Specified by:
        enableIOEvent in interface Connection<java.net.SocketAddress>
        Throws:
        java.io.IOException
      • disableIOEvent

        public final void disableIOEvent​(IOEvent ioEvent)
                                  throws java.io.IOException
        Specified by:
        disableIOEvent in interface Connection<java.net.SocketAddress>
        Throws:
        java.io.IOException
      • checkEmptyRead

        protected final void checkEmptyRead​(int size)