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.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
-
-
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.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addListener(ReliableSocketListener listener)
Adds the specified listener to this socket.void
addStateListener(ReliableSocketStateListener stateListener)
Adds the specified state listener to this socket.void
bind(java.net.SocketAddress bindpoint)
void
close()
protected void
closeImpl()
Cleans up and closes the socket.protected void
closeSocket()
Closes the underlying UDP socket.void
connect(java.net.SocketAddress endpoint)
void
connect(java.net.SocketAddress endpoint, int timeout)
java.nio.channels.SocketChannel
getChannel()
java.net.InetAddress
getInetAddress()
java.io.InputStream
getInputStream()
boolean
getKeepAlive()
java.net.InetAddress
getLocalAddress()
int
getLocalPort()
java.net.SocketAddress
getLocalSocketAddress()
java.io.OutputStream
getOutputStream()
int
getPort()
int
getReceiveBufferSize()
java.net.SocketAddress
getRemoteSocketAddress()
int
getSendBufferSize()
boolean
getTcpNoDelay()
protected void
init(java.net.DatagramSocket sock, ReliableSocketProfile profile)
Initializes socket and sets it up for receiving incoming traffic.boolean
isBound()
boolean
isClosed()
boolean
isConnected()
boolean
isInputShutdown()
boolean
isOutputShutdown()
protected void
log(java.lang.String msg)
Log routine.protected int
read(byte[] b, int off, int len)
Reads up tolen
bytes of data from the receiver buffer into an array of bytes.protected net.rudp.impl.Segment
receiveSegmentImpl()
Reads in a segment from the underlying UDP socket.void
removeListener(ReliableSocketListener listener)
Removes the specified listener from this socket.void
removeStateListener(ReliableSocketStateListener stateListener)
Removes the specified state listener from this socket.void
reset()
Resets the socket state.void
reset(ReliableSocketProfile profile)
Resets the socket state and profile.protected void
sendSegmentImpl(net.rudp.impl.Segment s)
Writes out a segment to the underlying UDP socket.void
setKeepAlive(boolean on)
void
setReceiveBufferSize(int size)
void
setSendBufferSize(int size)
void
setSoTimeout(int timeout)
void
setTcpNoDelay(boolean on)
void
shutdownInput()
void
shutdownOutput()
protected void
write(byte[] b, int off, int len)
Writeslen
bytes from the specified byte array starting at offsetoff
as data segments and queues them for immediate transmission.
-
-
-
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, ornull
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 thebind
operation.- Parameters:
address
- the remote address.port
- the remote port.localAddr
- the local address the socket is bound to, ornull
for the wildcard address.localPort
- the local port the socket is bound to, orzero
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 thebind
operation.- Parameters:
host
- the name of the remote host, ornull
for the loopback address.port
- the remote port.localAddr
- the local address the socket is bound to, ornull
for the wildcard address.localPort
- the local port the socket is bound to, orzero
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 classjava.net.Socket
- Throws:
java.io.IOException
-
connect
public void connect(java.net.SocketAddress endpoint) throws java.io.IOException
- Overrides:
connect
in classjava.net.Socket
- Throws:
java.io.IOException
-
connect
public void connect(java.net.SocketAddress endpoint, int timeout) throws java.io.IOException
- Overrides:
connect
in classjava.net.Socket
- Throws:
java.io.IOException
-
getChannel
public java.nio.channels.SocketChannel getChannel()
- Overrides:
getChannel
in classjava.net.Socket
-
getInetAddress
public java.net.InetAddress getInetAddress()
- Overrides:
getInetAddress
in classjava.net.Socket
-
getPort
public int getPort()
- Overrides:
getPort
in classjava.net.Socket
-
getRemoteSocketAddress
public java.net.SocketAddress getRemoteSocketAddress()
- Overrides:
getRemoteSocketAddress
in classjava.net.Socket
-
getLocalAddress
public java.net.InetAddress getLocalAddress()
- Overrides:
getLocalAddress
in classjava.net.Socket
-
getLocalPort
public int getLocalPort()
- Overrides:
getLocalPort
in classjava.net.Socket
-
getLocalSocketAddress
public java.net.SocketAddress getLocalSocketAddress()
- Overrides:
getLocalSocketAddress
in classjava.net.Socket
-
getInputStream
public java.io.InputStream getInputStream() throws java.io.IOException
- Overrides:
getInputStream
in classjava.net.Socket
- Throws:
java.io.IOException
-
getOutputStream
public java.io.OutputStream getOutputStream() throws java.io.IOException
- Overrides:
getOutputStream
in classjava.net.Socket
- Throws:
java.io.IOException
-
close
public void close() throws java.io.IOException
- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Overrides:
close
in classjava.net.Socket
- Throws:
java.io.IOException
-
isBound
public boolean isBound()
- Overrides:
isBound
in classjava.net.Socket
-
isConnected
public boolean isConnected()
- Overrides:
isConnected
in classjava.net.Socket
-
isClosed
public boolean isClosed()
- Overrides:
isClosed
in classjava.net.Socket
-
setSoTimeout
public void setSoTimeout(int timeout) throws java.net.SocketException
- Overrides:
setSoTimeout
in classjava.net.Socket
- Throws:
java.net.SocketException
-
setSendBufferSize
public void setSendBufferSize(int size) throws java.net.SocketException
- Overrides:
setSendBufferSize
in classjava.net.Socket
- Throws:
java.net.SocketException
-
getSendBufferSize
public int getSendBufferSize() throws java.net.SocketException
- Overrides:
getSendBufferSize
in classjava.net.Socket
- Throws:
java.net.SocketException
-
setReceiveBufferSize
public void setReceiveBufferSize(int size) throws java.net.SocketException
- Overrides:
setReceiveBufferSize
in classjava.net.Socket
- Throws:
java.net.SocketException
-
getReceiveBufferSize
public int getReceiveBufferSize() throws java.net.SocketException
- Overrides:
getReceiveBufferSize
in classjava.net.Socket
- Throws:
java.net.SocketException
-
setTcpNoDelay
public void setTcpNoDelay(boolean on) throws java.net.SocketException
- Overrides:
setTcpNoDelay
in classjava.net.Socket
- Throws:
java.net.SocketException
-
getTcpNoDelay
public boolean getTcpNoDelay()
- Overrides:
getTcpNoDelay
in classjava.net.Socket
-
setKeepAlive
public void setKeepAlive(boolean on) throws java.net.SocketException
- Overrides:
setKeepAlive
in classjava.net.Socket
- Throws:
java.net.SocketException
-
getKeepAlive
public boolean getKeepAlive() throws java.net.SocketException
- Overrides:
getKeepAlive
in classjava.net.Socket
- Throws:
java.net.SocketException
-
shutdownInput
public void shutdownInput() throws java.io.IOException
- Overrides:
shutdownInput
in classjava.net.Socket
- Throws:
java.io.IOException
-
shutdownOutput
public void shutdownOutput() throws java.io.IOException
- Overrides:
shutdownOutput
in classjava.net.Socket
- Throws:
java.io.IOException
-
isInputShutdown
public boolean isInputShutdown()
- Overrides:
isInputShutdown
in classjava.net.Socket
-
isOutputShutdown
public boolean isOutputShutdown()
- Overrides:
isOutputShutdown
in classjava.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
Writeslen
bytes from the specified byte array starting at offsetoff
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, anIOException
is thrown if the socket is closed.
-
read
protected int read(byte[] b, int off, int len) throws java.io.IOException
Reads up tolen
bytes of data from the receiver buffer into an array of bytes. An attempt is made to read as many aslen
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 arrayb
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, anIOException
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.
-
-