Class ReliableSocket
- java.lang.Object
-
- java.net.Socket
-
- net.rudp.ReliableSocket
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable
- Direct Known Subclasses:
ReliableServerSocket.ReliableClientSocket
public class ReliableSocket extends java.net.SocketThis 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
-
-
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.protectedReliableSocket(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.protectedReliableSocket(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.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddListener(ReliableSocketListener listener)Adds the specified listener to this socket.voidaddStateListener(ReliableSocketStateListener stateListener)Adds the specified state listener to this socket.voidbind(java.net.SocketAddress bindpoint)voidclose()protected voidcloseImpl()Cleans up and closes the socket.protected voidcloseSocket()Closes the underlying UDP socket.voidconnect(java.net.SocketAddress endpoint)voidconnect(java.net.SocketAddress endpoint, int timeout)java.nio.channels.SocketChannelgetChannel()java.net.InetAddressgetInetAddress()java.io.InputStreamgetInputStream()booleangetKeepAlive()java.net.InetAddressgetLocalAddress()intgetLocalPort()java.net.SocketAddressgetLocalSocketAddress()java.io.OutputStreamgetOutputStream()intgetPort()intgetReceiveBufferSize()java.net.SocketAddressgetRemoteSocketAddress()intgetSendBufferSize()booleangetTcpNoDelay()protected voidinit(java.net.DatagramSocket sock, ReliableSocketProfile profile)Initializes socket and sets it up for receiving incoming traffic.booleanisBound()booleanisClosed()booleanisConnected()booleanisInputShutdown()booleanisOutputShutdown()protected voidlog(java.lang.String msg)Log routine.protected intread(byte[] b, int off, int len)Reads up tolenbytes of data from the receiver buffer into an array of bytes.protected net.rudp.impl.SegmentreceiveSegmentImpl()Reads in a segment from the underlying UDP socket.voidremoveListener(ReliableSocketListener listener)Removes the specified listener from this socket.voidremoveStateListener(ReliableSocketStateListener stateListener)Removes the specified state listener from this socket.voidreset()Resets the socket state.voidreset(ReliableSocketProfile profile)Resets the socket state and profile.protected voidsendSegmentImpl(net.rudp.impl.Segment s)Writes out a segment to the underlying UDP socket.voidsetKeepAlive(boolean on)voidsetReceiveBufferSize(int size)voidsetSendBufferSize(int size)voidsetSoTimeout(int timeout)voidsetTcpNoDelay(boolean on)voidshutdownInput()voidshutdownOutput()protected voidwrite(byte[] b, int off, int len)Writeslenbytes from the specified byte array starting at offsetoffas data segments and queues them for immediate transmission.
-
-
-
Constructor Detail
-
ReliableSocket
public ReliableSocket() throws java.io.IOExceptionCreates 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.IOExceptionCreates 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, ornullfor 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.IOExceptionCreates 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
zerowill let the system pick up a free port in thebindoperation.- Parameters:
address- the remote address.port- the remote port.localAddr- the local address the socket is bound to, ornullfor the wildcard address.localPort- the local port the socket is bound to, orzerofor 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.IOExceptionCreates 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
zerowill let the system pick up a free port in thebindoperation.- Parameters:
host- the name of the remote host, ornullfor the loopback address.port- the remote port.localAddr- the local address the socket is bound to, ornullfor the wildcard address.localPort- the local port the socket is bound to, orzerofor 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.IOExceptionCreates 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:
bindin classjava.net.Socket- Throws:
java.io.IOException
-
connect
public void connect(java.net.SocketAddress endpoint) throws java.io.IOException- Overrides:
connectin classjava.net.Socket- Throws:
java.io.IOException
-
connect
public void connect(java.net.SocketAddress endpoint, int timeout) throws java.io.IOException- Overrides:
connectin classjava.net.Socket- Throws:
java.io.IOException
-
getChannel
public java.nio.channels.SocketChannel getChannel()
- Overrides:
getChannelin classjava.net.Socket
-
getInetAddress
public java.net.InetAddress getInetAddress()
- Overrides:
getInetAddressin classjava.net.Socket
-
getPort
public int getPort()
- Overrides:
getPortin classjava.net.Socket
-
getRemoteSocketAddress
public java.net.SocketAddress getRemoteSocketAddress()
- Overrides:
getRemoteSocketAddressin classjava.net.Socket
-
getLocalAddress
public java.net.InetAddress getLocalAddress()
- Overrides:
getLocalAddressin classjava.net.Socket
-
getLocalPort
public int getLocalPort()
- Overrides:
getLocalPortin classjava.net.Socket
-
getLocalSocketAddress
public java.net.SocketAddress getLocalSocketAddress()
- Overrides:
getLocalSocketAddressin classjava.net.Socket
-
getInputStream
public java.io.InputStream getInputStream() throws java.io.IOException- Overrides:
getInputStreamin classjava.net.Socket- Throws:
java.io.IOException
-
getOutputStream
public java.io.OutputStream getOutputStream() throws java.io.IOException- Overrides:
getOutputStreamin classjava.net.Socket- Throws:
java.io.IOException
-
close
public void close() throws java.io.IOException- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Overrides:
closein classjava.net.Socket- Throws:
java.io.IOException
-
isBound
public boolean isBound()
- Overrides:
isBoundin classjava.net.Socket
-
isConnected
public boolean isConnected()
- Overrides:
isConnectedin classjava.net.Socket
-
isClosed
public boolean isClosed()
- Overrides:
isClosedin classjava.net.Socket
-
setSoTimeout
public void setSoTimeout(int timeout) throws java.net.SocketException- Overrides:
setSoTimeoutin classjava.net.Socket- Throws:
java.net.SocketException
-
setSendBufferSize
public void setSendBufferSize(int size) throws java.net.SocketException- Overrides:
setSendBufferSizein classjava.net.Socket- Throws:
java.net.SocketException
-
getSendBufferSize
public int getSendBufferSize() throws java.net.SocketException- Overrides:
getSendBufferSizein classjava.net.Socket- Throws:
java.net.SocketException
-
setReceiveBufferSize
public void setReceiveBufferSize(int size) throws java.net.SocketException- Overrides:
setReceiveBufferSizein classjava.net.Socket- Throws:
java.net.SocketException
-
getReceiveBufferSize
public int getReceiveBufferSize() throws java.net.SocketException- Overrides:
getReceiveBufferSizein classjava.net.Socket- Throws:
java.net.SocketException
-
setTcpNoDelay
public void setTcpNoDelay(boolean on) throws java.net.SocketException- Overrides:
setTcpNoDelayin classjava.net.Socket- Throws:
java.net.SocketException
-
getTcpNoDelay
public boolean getTcpNoDelay()
- Overrides:
getTcpNoDelayin classjava.net.Socket
-
setKeepAlive
public void setKeepAlive(boolean on) throws java.net.SocketException- Overrides:
setKeepAlivein classjava.net.Socket- Throws:
java.net.SocketException
-
getKeepAlive
public boolean getKeepAlive() throws java.net.SocketException- Overrides:
getKeepAlivein classjava.net.Socket- Throws:
java.net.SocketException
-
shutdownInput
public void shutdownInput() throws java.io.IOException- Overrides:
shutdownInputin classjava.net.Socket- Throws:
java.io.IOException
-
shutdownOutput
public void shutdownOutput() throws java.io.IOException- Overrides:
shutdownOutputin classjava.net.Socket- Throws:
java.io.IOException
-
isInputShutdown
public boolean isInputShutdown()
- Overrides:
isInputShutdownin classjava.net.Socket
-
isOutputShutdown
public boolean isOutputShutdown()
- Overrides:
isOutputShutdownin classjava.net.Socket
-
reset
public void reset() throws java.io.IOExceptionResets 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.IOExceptionWriteslenbytes from the specified byte array starting at offsetoffas 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, anIOExceptionis thrown if the socket is closed.
-
read
protected int read(byte[] b, int off, int len) throws java.io.IOExceptionReads up tolenbytes of data from the receiver buffer into an array of bytes. An attempt is made to read as many aslenbytes, 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 arraybat which the data is written.len- the maximum number of bytes to read.- Returns:
- the total number of bytes read into the buffer,
or
-1if 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, anIOExceptionis 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.IOExceptionWrites 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.IOExceptionReads 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.
-
-