public class ArcusSocket extends Object
0x28BAD0100 (32 bits) containing the signature (0x2BAD), major protocol
version (0x01) and minor protocol version (0x00).
There are also two special headers. A keep-alive header (0x00000000) is
being sent every 250 ms to check if the connection is still alive and a close header
(0xF0F0F0F0) is sent when the connection is closing.
addListener(SocketListener) and removed by calling
removeListener(SocketListener). All registered listeners get notified when:
SocketListener.messageReceived(ArcusSocket)SocketListener.stateChanged(ArcusSocket, SocketState)SocketListener.error(ArcusSocket, Error)
registerMessageType(Message) the
message type (more particularly the parser) will be stored to identify and parse
incoming messages. This method returns a boolean value indicating if the type has been
added.
listen(int), it listens on the given port and waits for a client to
connect. If the port is set to 0, a port will automatically be allocated. With
connect(String, int), a connection to the given address will be established.
All other methods affect the socket in both cases in the same way.
Once the socket is connected it will send and receive message on another thread.
Messages can be sent by calling sendMessage(Message). They will be added to an
internal Queue. The separate Thread will check for messages in the Queue and send them
to the other side. After all messages are sent it tries to read incoming messages. If a
valid message has been received and successfully parsed, all registered
SocketListeners will get notified and the message gets added to the internal
Queue. Messages can be chronically obtained by successive calls to
takeNextMessage().
connect(String, int) and is not connecting to the given address
listen(int) and is starting an internal ServerSocket
ErrorCode and a message describing what happened.
reset(). The socket will
be reset to default values and can now be used as if it was newly constructed.
SocketListeners and message types will not be reset and remain the same.| Constructor and Description |
|---|
ArcusSocket() |
| Modifier and Type | Method and Description |
|---|---|
void |
addListener(SocketListener listener)
Adds a listener to the internal List.
|
void |
close()
Closes the socket.
|
void |
connect(String address,
int port)
Connects to a remote address.
|
int |
getPort()
This will return the ports specified by calls to
listen(int) or
connect(String, int). |
int |
getSocketTimeout()
Returns the time until the socket times out while connecting, reading or writing.
|
SocketState |
getState()
Returns the current state the socket is in.
|
void |
listen(int port)
Listens for a new incoming connection on the specified port.
|
boolean |
registerMessageType(com.google.protobuf.Message message)
Registers a new message type to receive and parse messages of the same type later.
|
void |
removeAllListeners()
Removes all listeners from the internal list.
|
void |
removeListener(SocketListener listener)
Removes a listener from the internal list.
|
void |
reset()
Resets the socket and the socket's state to use it again.
|
void |
sendMessage(com.google.protobuf.Message message)
Adds a message to the internal send queue.
|
void |
setSocketTimeout(int socketTimeout)
Sets the time until the socket times out while connecting, reading or writing.
|
com.google.protobuf.Message |
takeNextMessage()
Polls the first message in the receive queue.
|
public boolean registerMessageType(com.google.protobuf.Message message)
Although you can use any instance of a message type, it is recommended for
readability and null-safety purposes to use the default instance
(MessageLiteOrBuilder.getDefaultInstanceForType()) of the message type.
message - the message whose type will be registerednull,
the socket is not in it's initial state or the type has already been registered.public int getSocketTimeout()
public void setSocketTimeout(int socketTimeout)
socketTimeout - time in mspublic void addListener(SocketListener listener)
listener - SocketListener that will be addedpublic void removeListener(SocketListener listener)
listener - SocketListener that will be removedpublic void removeAllListeners()
public void connect(String address, int port)
address - remote addressport - remote portpublic void listen(int port)
Opening state.
If the specified port is 0, the port number is automatically allocated,
typically from an ephemeral port range. This port number can then be retrieved by
calling getPort().
port - port to listen on. If 0, the port is automatically allocated.public void sendMessage(com.google.protobuf.Message message)
message - message to be sentpublic com.google.protobuf.Message takeNextMessage()
Returns null if there are no new messages
public void reset()
public void close()
public SocketState getState()
ArcusSocketpublic int getPort()
listen(int) or
connect(String, int). If none of them have been called, 0 will be
returned.
If listen(int) has been called with the port 0, this will
return the automatically allocated port
Copyright © 2016. All rights reserved.