Package net.rudp

Class ReliableSocket

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

    public class ReliableSocket
    extends java.net.Socket
    This class implements client sockets that use the Simple Reliable UDP (RUDP) protocol for end-to-end communication between two machines.
    Author:
    Adrian Granados
    See Also:
    Socket
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected java.net.SocketAddress _endpoint  
      protected net.rudp.ReliableSocketInputStream _in  
      protected net.rudp.ReliableSocketOutputStream _out  
      protected java.net.DatagramSocket _sock  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
        ReliableSocket()
      Creates an unconnected RUDP socket with default RUDP parameters.
        ReliableSocket​(java.lang.String host, int port)
      Creates a RUDP socket and connects it to the specified port number on the named host.
        ReliableSocket​(java.lang.String host, int port, java.net.InetAddress localAddr, int localPort)
      Creates a RUDP socket and connects it to the specified remote host on the specified remote port.
        ReliableSocket​(java.net.DatagramSocket sock)
      Creates a RUDP socket and attaches it to the underlying datagram socket.
      protected ReliableSocket​(java.net.DatagramSocket sock, ReliableSocketProfile profile)
      Creates a RUDP socket and attaches it to the underlying datagram socket using the given RUDP parameters.
        ReliableSocket​(java.net.InetAddress address, int port, java.net.InetAddress localAddr, int localPort)
      Creates a RUDP socket and connects it to the specified remote address on the specified remote port.
      protected ReliableSocket​(java.net.InetSocketAddress inetAddr, java.net.InetSocketAddress localAddr)
      Creates a RUDP socket and connects it to the specified remote address.
        ReliableSocket​(ReliableSocketProfile profile)
      Creates an unconnected RUDP socket and uses the given RUDP parameters.
    • Field Detail

      • _sock

        protected java.net.DatagramSocket _sock
      • _endpoint

        protected java.net.SocketAddress _endpoint
      • _in

        protected net.rudp.ReliableSocketInputStream _in
      • _out

        protected net.rudp.ReliableSocketOutputStream _out
    • Constructor Detail

      • ReliableSocket

        public ReliableSocket()
                       throws java.io.IOException
        Creates an unconnected RUDP socket with default RUDP parameters.
        Throws:
        java.io.IOException - if an I/O error occurs when creating the underlying UDP socket.
      • ReliableSocket

        public ReliableSocket​(ReliableSocketProfile profile)
                       throws java.io.IOException
        Creates an unconnected RUDP socket and uses the given RUDP parameters.
        Throws:
        java.io.IOException - if an I/O error occurs when creating the underlying UDP socket.
      • ReliableSocket

        public ReliableSocket​(java.lang.String host,
                              int port)
                       throws java.net.UnknownHostException,
                              java.io.IOException
        Creates a RUDP socket and connects it to the specified port number on the named host.

        If the specified host is null it is the equivalent of specifying the address as InetAddress.getByName(null). In other words, it is equivalent to specifying an address of the loopback interface.

        Parameters:
        host - the host name, or null for the loopback address.
        port - the port number.
        Throws:
        java.net.UnknownHostException - if the IP address of the host could not be determined.
        java.io.IOException - if an I/O error occurs when creating the socket.
        java.lang.IllegalArgumentException - if the port parameter is outside the specified range of valid port values, which is between 0 and 65535, inclusive.
        See Also:
        Socket(String, int)
      • ReliableSocket

        public ReliableSocket​(java.net.InetAddress address,
                              int port,
                              java.net.InetAddress localAddr,
                              int localPort)
                       throws java.io.IOException
        Creates a RUDP socket and connects it to the specified remote address on the specified remote port. The socket will also bind to the local address and port supplied.

        If the specified local address is null it is the equivalent of specifying the address as the wildcard address (see InetAddress.isAnyLocalAddress()).

        A local port number of zero will let the system pick up a free port in the bind operation.

        Parameters:
        address - the remote address.
        port - the remote port.
        localAddr - the local address the socket is bound to, or null for the wildcard address.
        localPort - the local port the socket is bound to, or zero for a system selected free port.
        Throws:
        java.io.IOException - if an I/O error occurs when creating the socket.
        java.lang.IllegalArgumentException - if the port parameter is outside the specified range of valid port values, which is between 0 and 65535, inclusive.
      • ReliableSocket

        public ReliableSocket​(java.lang.String host,
                              int port,
                              java.net.InetAddress localAddr,
                              int localPort)
                       throws java.io.IOException
        Creates a RUDP socket and connects it to the specified remote host on the specified remote port. The socket will also bind to the local address and port supplied.

        If the specified host is null it is the equivalent of specifying the address as InetAddress.getByName(null). In other words, it is equivalent to specifying an address of the loopback interface.

        A local port number of zero will let the system pick up a free port in the bind operation.

        Parameters:
        host - the name of the remote host, or null for the loopback address.
        port - the remote port.
        localAddr - the local address the socket is bound to, or null for the wildcard address.
        localPort - the local port the socket is bound to, or zero for a system selected free port.
        Throws:
        java.io.IOException - if an I/O error occurs when creating the socket.
        java.lang.IllegalArgumentException - if the port parameter is outside the specified range of valid port values, which is between 0 and 65535, inclusive.
      • ReliableSocket

        protected ReliableSocket​(java.net.InetSocketAddress inetAddr,
                                 java.net.InetSocketAddress localAddr)
                          throws java.io.IOException
        Creates a RUDP socket and connects it to the specified remote address. The socket will also bind to the local address supplied.
        Parameters:
        inetAddr - the remote address.
        localAddr - the local address.
        Throws:
        java.io.IOException - if an I/O error occurs when creating the socket.
      • ReliableSocket

        public ReliableSocket​(java.net.DatagramSocket sock)
        Creates a RUDP socket and attaches it to the underlying datagram socket.
        Parameters:
        sock - the datagram socket.
      • ReliableSocket

        protected ReliableSocket​(java.net.DatagramSocket sock,
                                 ReliableSocketProfile profile)
        Creates a RUDP socket and attaches it to the underlying datagram socket using the given RUDP parameters.
        Parameters:
        sock - the datagram socket.
        profile - the socket profile.
    • Method Detail

      • init

        protected void init​(java.net.DatagramSocket sock,
                            ReliableSocketProfile profile)
        Initializes socket and sets it up for receiving incoming traffic.
        Parameters:
        sock - the datagram socket.
        profile - the socket profile.
      • bind

        public void bind​(java.net.SocketAddress bindpoint)
                  throws java.io.IOException
        Overrides:
        bind in class java.net.Socket
        Throws:
        java.io.IOException
      • connect

        public void connect​(java.net.SocketAddress endpoint)
                     throws java.io.IOException
        Overrides:
        connect in class java.net.Socket
        Throws:
        java.io.IOException
      • connect

        public void connect​(java.net.SocketAddress endpoint,
                            int timeout)
                     throws java.io.IOException
        Overrides:
        connect in class java.net.Socket
        Throws:
        java.io.IOException
      • getChannel

        public java.nio.channels.SocketChannel getChannel()
        Overrides:
        getChannel in class java.net.Socket
      • getInetAddress

        public java.net.InetAddress getInetAddress()
        Overrides:
        getInetAddress in class java.net.Socket
      • getPort

        public int getPort()
        Overrides:
        getPort in class java.net.Socket
      • getRemoteSocketAddress

        public java.net.SocketAddress getRemoteSocketAddress()
        Overrides:
        getRemoteSocketAddress in class java.net.Socket
      • getLocalAddress

        public java.net.InetAddress getLocalAddress()
        Overrides:
        getLocalAddress in class java.net.Socket
      • getLocalPort

        public int getLocalPort()
        Overrides:
        getLocalPort in class java.net.Socket
      • getLocalSocketAddress

        public java.net.SocketAddress getLocalSocketAddress()
        Overrides:
        getLocalSocketAddress in class java.net.Socket
      • getInputStream

        public java.io.InputStream getInputStream()
                                           throws java.io.IOException
        Overrides:
        getInputStream in class java.net.Socket
        Throws:
        java.io.IOException
      • getOutputStream

        public java.io.OutputStream getOutputStream()
                                             throws java.io.IOException
        Overrides:
        getOutputStream in class java.net.Socket
        Throws:
        java.io.IOException
      • close

        public void close()
                   throws java.io.IOException
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Overrides:
        close in class java.net.Socket
        Throws:
        java.io.IOException
      • isBound

        public boolean isBound()
        Overrides:
        isBound in class java.net.Socket
      • isConnected

        public boolean isConnected()
        Overrides:
        isConnected in class java.net.Socket
      • isClosed

        public boolean isClosed()
        Overrides:
        isClosed in class java.net.Socket
      • setSoTimeout

        public void setSoTimeout​(int timeout)
                          throws java.net.SocketException
        Overrides:
        setSoTimeout in class java.net.Socket
        Throws:
        java.net.SocketException
      • setSendBufferSize

        public void setSendBufferSize​(int size)
                               throws java.net.SocketException
        Overrides:
        setSendBufferSize in class java.net.Socket
        Throws:
        java.net.SocketException
      • getSendBufferSize

        public int getSendBufferSize()
                              throws java.net.SocketException
        Overrides:
        getSendBufferSize in class java.net.Socket
        Throws:
        java.net.SocketException
      • setReceiveBufferSize

        public void setReceiveBufferSize​(int size)
                                  throws java.net.SocketException
        Overrides:
        setReceiveBufferSize in class java.net.Socket
        Throws:
        java.net.SocketException
      • getReceiveBufferSize

        public int getReceiveBufferSize()
                                 throws java.net.SocketException
        Overrides:
        getReceiveBufferSize in class java.net.Socket
        Throws:
        java.net.SocketException
      • setTcpNoDelay

        public void setTcpNoDelay​(boolean on)
                           throws java.net.SocketException
        Overrides:
        setTcpNoDelay in class java.net.Socket
        Throws:
        java.net.SocketException
      • getTcpNoDelay

        public boolean getTcpNoDelay()
        Overrides:
        getTcpNoDelay in class java.net.Socket
      • setKeepAlive

        public void setKeepAlive​(boolean on)
                          throws java.net.SocketException
        Overrides:
        setKeepAlive in class java.net.Socket
        Throws:
        java.net.SocketException
      • getKeepAlive

        public boolean getKeepAlive()
                             throws java.net.SocketException
        Overrides:
        getKeepAlive in class java.net.Socket
        Throws:
        java.net.SocketException
      • shutdownInput

        public void shutdownInput()
                           throws java.io.IOException
        Overrides:
        shutdownInput in class java.net.Socket
        Throws:
        java.io.IOException
      • shutdownOutput

        public void shutdownOutput()
                            throws java.io.IOException
        Overrides:
        shutdownOutput in class java.net.Socket
        Throws:
        java.io.IOException
      • isInputShutdown

        public boolean isInputShutdown()
        Overrides:
        isInputShutdown in class java.net.Socket
      • isOutputShutdown

        public boolean isOutputShutdown()
        Overrides:
        isOutputShutdown in class java.net.Socket
      • reset

        public void reset()
                   throws java.io.IOException
        Resets the socket state.

        The socket will attempt to deliver all outstanding bytes to the remote endpoint and then it will renegotiate the connection parameters. The transmissions of bytes resumes after the renegotation finishes and the connection is synchronized again.

        Throws:
        java.io.IOException - if an I/O error occurs when resetting the connection.
      • reset

        public void reset​(ReliableSocketProfile profile)
                   throws java.io.IOException
        Resets the socket state and profile.

        The socket will attempt to deliver all outstanding bytes to the remote endpoint and then it will renegotiate the connection parameters specified in the given socket profile. The transmissions of bytes resumes after the renegotation finishes and the connection is synchronized again.

        Parameters:
        profile - the socket profile or null if old profile should be used.
        Throws:
        java.io.IOException - if an I/O error occurs when resetting the connection.
      • write

        protected void write​(byte[] b,
                             int off,
                             int len)
                      throws java.io.IOException
        Writes len bytes from the specified byte array starting at offset off as data segments and queues them for immediate transmission.
        Parameters:
        b - the data.
        off - the start offset in the data.
        len - the number of bytes to write.
        Throws:
        java.io.IOException - if an I/O error occurs. In particular, an IOException is thrown if the socket is closed.
      • read

        protected int read​(byte[] b,
                           int off,
                           int len)
                    throws java.io.IOException
        Reads up to len bytes of data from the receiver buffer into an array of bytes. An attempt is made to read as many as len bytes, but a smaller number may be read. The number of bytes actually read is returned as an integer.

        This method blocks until input data is available, end of file is detected, or an exception is thrown.

        Parameters:
        b - the buffer into which the data is read.
        off - the start offset in array b at which the data is written.
        len - the maximum number of bytes to read.
        Returns:
        the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached.
        Throws:
        java.io.IOException - if an I/O error occurs. In particular, an IOException is thrown if the socket is closed, or if the buffer is not big enough to hold a full data segment.
      • addListener

        public void addListener​(ReliableSocketListener listener)
        Adds the specified listener to this socket. If the listener has already been registered, this method does nothing.
        Parameters:
        listener - the listener to add.
      • removeListener

        public void removeListener​(ReliableSocketListener listener)
        Removes the specified listener from this socket. This is harmless if the listener was not previously registered.
        Parameters:
        listener - the listener to remove.
      • addStateListener

        public void addStateListener​(ReliableSocketStateListener stateListener)
        Adds the specified state listener to this socket. If the listener has already been registered, this method does nothing.
        Parameters:
        stateListener - the listener to add.
      • removeStateListener

        public void removeStateListener​(ReliableSocketStateListener stateListener)
        Removes the specified state listener from this socket. This is harmless if the listener was not previously registered.
        Parameters:
        stateListener - the listener to remove.
      • sendSegmentImpl

        protected void sendSegmentImpl​(net.rudp.impl.Segment s)
                                throws java.io.IOException
        Writes out a segment to the underlying UDP socket.
        Parameters:
        s - the segment.
        Throws:
        java.io.IOException - if an I/O error occurs in the underlying UDP socket.
      • receiveSegmentImpl

        protected net.rudp.impl.Segment receiveSegmentImpl()
                                                    throws java.io.IOException
        Reads in a segment from the underlying UDP socket.
        Returns:
        s the segment.
        Throws:
        java.io.IOException - if an I/O error occurs in the underlying UDP socket.
      • closeSocket

        protected void closeSocket()
        Closes the underlying UDP socket.
      • closeImpl

        protected void closeImpl()
        Cleans up and closes the socket.
      • log

        protected void log​(java.lang.String msg)
        Log routine.