Class TcpConnection

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable
    Direct Known Subclasses:
    TcpConnectionNonBlocking

    public class TcpConnection
    extends Connection
    Blocking IO (BIO) connection. Starts 1 reader thread for the peer socket and blocks until data is available. Calls BaseServer.receive(Address,byte[],int,int) when data has been received.
    Since:
    3.6.5
    Author:
    Bela Ban
    • Field Detail

      • sock

        protected final java.net.Socket sock
      • send_lock

        protected final java.util.concurrent.locks.ReentrantLock send_lock
      • out

        protected java.io.OutputStream out
      • in

        protected java.io.DataInputStream in
      • writers

        protected final java.util.concurrent.atomic.AtomicInteger writers
      • connected

        protected volatile boolean connected
      • length_buf

        protected final byte[] length_buf
    • Method Detail

      • getSockAddress

        protected java.lang.String getSockAddress()
      • 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,
                               boolean use_acks)
                        throws java.lang.Exception
        Throws:
        java.lang.Exception
      • send

        public void send​(byte[] data,
                         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
        Specified by:
        send in class Connection
        Throws:
        java.lang.Exception
      • doSend

        protected void doSend​(byte[] data,
                              int offset,
                              int length)
                       throws java.lang.Exception
        Throws:
        java.lang.Exception
      • doSend

        protected void doSend​(byte[] data,
                              int offset,
                              int length,
                              boolean flush)
                       throws java.lang.Exception
        Throws:
        java.lang.Exception
      • createDataOutputStream

        protected java.io.OutputStream createDataOutputStream​(java.io.OutputStream out)
      • createDataInputStream

        protected java.io.DataInputStream createDataInputStream​(java.io.InputStream in)
      • 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
        Send the cookie first, then our port number. If the cookie doesn't match the receiver's cookie, the receiver will reject the connection and close it.
        Throws:
        java.lang.Exception
      • readPeerAddress

        protected Address readPeerAddress​(java.net.Socket client_sock)
                                   throws java.lang.Exception
        Reads the peer's address. First a cookie has to be sent which has to match my own cookie, otherwise the connection will be refused
        Throws:
        java.lang.Exception
      • 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
      • close

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