java.lang.Object com.realtime.crossfire.jxclient.server.socket.ClientSocket
public class ClientSocket
A socket that processes incoming data.
final ClientSocket clientSocket = new ClientSocket(...); clientSocket.writePacket(...); clientSocket.disconnect();
Field Summary | |
---|---|
private java.util.Collection<ClientSocketListener> |
clientSocketListeners
The ClientSocketListener s to notify. |
private DebugWriter |
debugProtocol
The appender to write state changes to. |
private boolean |
disconnectPending
If set, notify listeners. |
private java.lang.String |
host
The host to connect to. |
private byte[] |
inputBuf
The receive buffer. |
private java.nio.ByteBuffer |
inputBuffer
The receive buffer. |
private int |
inputLen
If set to -1 , a two-byte packet header is read next from
inputBuffer . |
private int |
interestOps
The currently set interest ops for selectionKey . |
private boolean |
isConnected
Whether socketChannel is connected. |
private static int |
MAXIMUM_PACKET_SIZE
The maximum payload size of a Crossfire protocol packet. |
private java.nio.ByteBuffer |
outputBuffer
The output buffer. |
private byte[] |
packetHeader
A buffer for sending packets. |
private int |
port
The port to connect to. |
private boolean |
reconnect
Set if host or port has changed and thus a reconnect is
needed. |
private boolean |
reconnectIsError
Only valid if reconnect is set. |
private java.lang.String |
reconnectReason
Only valid if reconnect is set. |
private java.nio.channels.SelectableChannel |
selectableChannel
The SelectableChannel of socketChannel . |
private java.nio.channels.SelectionKey |
selectionKey
The SelectionKey registered to selectableChannel . |
private java.nio.channels.Selector |
selector
The Selector used for waiting. |
private java.nio.channels.SocketChannel |
socketChannel
The SocketChannel when connected. |
private java.lang.Object |
syncConnect
Synchronization object for reconnect , host , port , and disconnectPending . |
private java.lang.Object |
syncOutput
Synchronization object for outputBuffer , selectionKey ,
interestOps , and socketChannel . |
private java.lang.Thread |
thread
The Thread used to operate the socket. |
Constructor Summary | |
---|---|
ClientSocket(DebugWriter debugProtocol)
Creates a new instance. |
Method Summary | |
---|---|
void |
addClientSocketListener(ClientSocketListener clientSocketListener)
Adds a ClientSocketListener to be notified. |
void |
connect(java.lang.String host,
int port)
Connects to a server. |
void |
disconnect(java.lang.String reason,
boolean isError)
Terminates the connection. |
private void |
doConnect()
Processes pending connect requests. |
private void |
doReconnect()
Processes pending re- or disconnect requests. |
private void |
doTransceive()
Processes pending data to receive of transmit. |
private void |
process()
Reads/writes data from/to the socket. |
private void |
processConnect(java.lang.String host,
int port)
Connects the socket. |
private void |
processDisconnect(java.lang.String reason,
boolean isError)
Disconnects the socket. |
private void |
processRead()
Reads data from the socket and parses the data into commands. |
private void |
processReadCommand()
Parses data from inputBuffer into commands. |
private void |
processWrite()
Writes some pending data to the socket. |
void |
removeClientSocketListener(ClientSocketListener clientSocketListener)
Removes a ClientSocketListener to be notified. |
void |
start()
Starts operation. |
void |
stop()
Stops operation. |
private void |
updateInterestOps()
Updates selectionKey 's interest ops to match interestOps . |
private void |
updateWriteInterestOps()
Updates interestOps 's OP_WRITE
according to whether outputBuffer has pending data. |
void |
writePacket(byte[] buf,
int len)
Writes a packet. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
@NotNull private final java.util.Collection<ClientSocketListener> clientSocketListeners
ClientSocketListener
s to notify.
@Nullable private final DebugWriter debugProtocol
null
to not
write anything.
private boolean disconnectPending
@Nullable private java.lang.String host
null
for disconnect.
@NotNull private final byte[] inputBuf
inputBuffer
.
@NotNull private final java.nio.ByteBuffer inputBuffer
private int inputLen
-1
, a two-byte packet header is read next from
inputBuffer
. Otherwise it is set to the packet length which will
be read from inputBuffer
.
private int interestOps
selectionKey
.
private boolean isConnected
socketChannel
is connected.
private static final int MAXIMUM_PACKET_SIZE
@NotNull private final java.nio.ByteBuffer outputBuffer
@NotNull private final byte[] packetHeader
private int port
private boolean reconnect
host
or port
has changed and thus a reconnect is
needed.
private boolean reconnectIsError
reconnect
is set.
@NotNull private java.lang.String reconnectReason
reconnect
is set.
@Nullable private java.nio.channels.SelectableChannel selectableChannel
SelectableChannel
of socketChannel
.
@Nullable private java.nio.channels.SelectionKey selectionKey
SelectionKey
registered to selectableChannel
. It's
interesting ops are interestOps
.
@NotNull private final java.nio.channels.Selector selector
Selector
used for waiting.
@Nullable private java.nio.channels.SocketChannel socketChannel
SocketChannel
when connected. Set to null
when
not connected.
@NotNull private final java.lang.Object syncConnect
reconnect
, host
, port
, and disconnectPending
.
@NotNull private final java.lang.Object syncOutput
outputBuffer
, selectionKey
,
interestOps
, and socketChannel
.
@NotNull private final java.lang.Thread thread
Thread
used to operate the socket.
Constructor Detail |
---|
public ClientSocket(@Nullable DebugWriter debugProtocol) throws java.io.IOException
debugProtocol
- tf non-null
, write all protocol
commands to this writer
java.io.IOException
- if the socket cannot be createdMethod Detail |
---|
public void addClientSocketListener(@NotNull ClientSocketListener clientSocketListener)
ClientSocketListener
to be notified.
clientSocketListener
- the client socket listener to addpublic void connect(@NotNull java.lang.String host, int port)
host
- the host to connect toport
- the port to connect topublic void disconnect(@NotNull java.lang.String reason, boolean isError)
reason
- the reason for the disconnectisError
- whether the disconnect is unexpectedprivate void doConnect() throws java.io.IOException
java.io.IOException
- if an I/O error occursprivate void doReconnect() throws java.io.IOException
java.io.IOException
- if an I/O error occursprivate void doTransceive() throws java.io.IOException
java.io.IOException
- if an I/O error occursprivate void process()
thread
has
been interrupted.
private void processConnect(@NotNull java.lang.String host, int port) throws java.io.IOException
host
- the host to connect toport
- the port to connect to
java.io.IOException
- if an I/O error occursprivate void processDisconnect(@NotNull java.lang.String reason, boolean isError)
reason
- the reason for disconnectionisError
- whether the disconnect is unexpectedprivate void processRead() throws java.io.IOException
java.io.IOException
- if an I/O error occursprivate void processReadCommand()
inputBuffer
into commands.
private void processWrite() throws java.io.IOException
java.io.IOException
- if an I/O error occurspublic void removeClientSocketListener(@NotNull ClientSocketListener clientSocketListener)
ClientSocketListener
to be notified.
clientSocketListener
- the client socket listener to removepublic void start()
public void stop() throws java.lang.InterruptedException
java.lang.InterruptedException
- if stopping was interruptedprivate void updateInterestOps()
selectionKey
's interest ops to match interestOps
. Does nothing if selectionKey
is
null
.
private void updateWriteInterestOps()
interestOps
's OP_WRITE
according to whether outputBuffer
has pending data.
public void writePacket(@NotNull byte[] buf, int len)
This function may be called even if the socket has been closed. In this case he packet is discarded.
buf
- the packet to sendlen
- the number of bytes to send