Class NioConnection

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable

    public class NioConnection
    extends Connection
    An NIO based impl of Connection
    Since:
    3.6.5
    Author:
    Bela Ban
    • Field Detail

      • channel

        protected java.nio.channels.SocketChannel channel
      • key

        protected java.nio.channels.SelectionKey key
      • send_buf

        protected final Buffers send_buf
      • length_buf

        protected final java.nio.ByteBuffer length_buf
      • copy_on_partial_write

        protected boolean copy_on_partial_write
      • partial_writes

        protected int partial_writes
      • send_lock

        protected final java.util.concurrent.locks.Lock send_lock
      • recv_buf

        protected Buffers recv_buf
    • Constructor Detail

      • NioConnection

        public NioConnection​(Address peer_addr,
                             NioBaseServer server)
                      throws java.lang.Exception
        Creates a connection stub and binds it, use connect(Address) to connect
        Throws:
        java.lang.Exception
      • NioConnection

        public NioConnection​(java.nio.channels.SocketChannel channel,
                             NioBaseServer server)
                      throws java.lang.Exception
        Throws:
        java.lang.Exception
    • Method Detail

      • key

        public java.nio.channels.SelectionKey key()
      • key

        public NioConnection key​(java.nio.channels.SelectionKey k)
      • copyOnPartialWrite

        public NioConnection copyOnPartialWrite​(boolean b)
      • copyOnPartialWrite

        public boolean copyOnPartialWrite()
      • numPartialWrites

        public int numPartialWrites()
      • registerSelectionKey

        public void registerSelectionKey​(int interest_ops)
      • clearSelectionKey

        public void clearSelectionKey​(int interest_ops)
      • connect

        public void connect​(Address dest)
                     throws java.lang.Exception
        Specified by:
        connect in class Connection
        Throws:
        java.lang.Exception
      • connect

        protected void connect​(Address dest,
                               boolean send_local_addr)
                        throws java.lang.Exception
        Throws:
        java.lang.Exception
      • start

        public void start()
                   throws java.lang.Exception
        Specified by:
        start in class Connection
        Throws:
        java.lang.Exception
      • send

        public void send​(byte[] buf,
                         int offset,
                         int length)
                  throws java.lang.Exception
        Specified by:
        send in class Connection
        Throws:
        java.lang.Exception
      • send

        public void send​(java.nio.ByteBuffer buf)
                  throws java.lang.Exception
        Sends a message. If the previous write didn't complete, tries to complete it. If this still doesn't complete, the message is dropped (needs to be retransmitted, e.g. by UNICAST3 or NAKACK2).
        Specified by:
        send in class Connection
        Parameters:
        buf -
        Throws:
        java.lang.Exception
      • send

        protected void send​(java.nio.ByteBuffer buf,
                            boolean send_length)
                     throws java.lang.Exception
        Throws:
        java.lang.Exception
      • send

        public void send()
                  throws java.lang.Exception
        Throws:
        java.lang.Exception
      • read

        public void read()
                  throws java.lang.Exception
        Read the length first, then the actual data. This method is not reentrant and access must be synchronized
        Throws:
        java.lang.Exception
      • _read

        protected boolean _read()
                         throws java.lang.Exception
        Throws:
        java.lang.Exception
      • close

        public void close()
                   throws java.io.IOException
        Throws:
        java.io.IOException
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • status

        public java.lang.String status()
        Specified by:
        status in class Connection
      • setSocketParameters

        protected void setSocketParameters​(java.net.Socket client_sock)
                                    throws java.net.SocketException
        Throws:
        java.net.SocketException
      • sendLocalAddress

        protected void sendLocalAddress​(Address local_addr)
                                 throws java.lang.Exception
        Throws:
        java.lang.Exception
      • readPeerAddress

        protected Address readPeerAddress()
                                   throws java.lang.Exception
        Throws:
        java.lang.Exception
      • getBuffer

        protected static byte[] getBuffer​(java.nio.ByteBuffer buf)
      • makeLengthBuffer

        protected java.nio.ByteBuffer makeLengthBuffer​(int length)