public class ClientSocket
extends java.lang.Object
final ClientSocket clientSocket = new ClientSocket(...); clientSocket.writePacket(...); clientSocket.disconnect();
Modifier and Type | Field and Description |
---|---|
private EventListenerList2<ClientSocketListener> |
clientSocketListeners
The
ClientSocketListeners 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 Model |
model
The
Model instance that is updated. |
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
|
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
|
private java.lang.Object |
syncOutput
|
private java.lang.Thread |
thread
The
Thread used to operate the socket. |
Constructor and Description |
---|
ClientSocket(Model model,
DebugWriter debugProtocol)
Creates a new instance.
|
Modifier and Type | Method and Description |
---|---|
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()
|
void |
writePacket(byte[] buf,
int len)
Writes a packet.
|
@NotNull private final EventListenerList2<ClientSocketListener> clientSocketListeners
ClientSocketListeners
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
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
@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
@NotNull private final java.lang.Object syncOutput
@NotNull private final java.lang.Thread thread
Thread
used to operate the socket.public ClientSocket(@NotNull Model model, @Nullable DebugWriter debugProtocol) throws java.io.IOException
model
- the model to updatedebugProtocol
- tf non-null
, write all protocol commands to
this writerjava.io.IOException
- if the socket cannot be createdpublic 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 tojava.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()
private void updateWriteInterestOps()
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