Package com.rcll.llsf_comm
Class ProtobufTcpConnection
- java.lang.Object
-
- com.rcll.llsf_comm.ProtobufTcpConnection
-
- All Implemented Interfaces:
ProtobufConnection
public class ProtobufTcpConnection extends java.lang.Object implements ProtobufConnection
The ProtobufClient is a client to communicate with a refbox via protobuf messages. You can send stream messages (TCP) by calling the enqueue method. To receive messages, register a ProtobufMessageHandler, incoming messages will be passed to your handler. To send and receive broadcast messages (UDP), use the ProtobufBroadcastPeer.
-
-
Constructor Summary
Constructors Constructor Description ProtobufTcpConnection(java.lang.String hostname, int port)Instantiates a new ProtobufClient.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description <T extends com.google.protobuf.GeneratedMessageV3>
voidadd_message(java.lang.Class<T> c)Adds and registers a new protobuf message type.voidconnect()Tries to connect to the refbox at the IP address and port given in the contructor.voiddisconnect(boolean wait)Disconnects from the refbox.voidenqueue(com.google.protobuf.GeneratedMessageV3 msg)<T> voidenqueue(com.google.protobuf.GeneratedMessageV3 msg, java.lang.Class<T> c)voidenqueue(ProtobufMessage msg)Puts a ProtobufMessage into the send queue in order to be sent out to the refbox.booleanis_connected()Checks if a connection to the refbox is established.voidregister_handler(ProtobufMessageHandler handler)Registers a new ProtobufMessageHandler responsible for handling and deserializing incoming protobuf messages.
-
-
-
Constructor Detail
-
ProtobufTcpConnection
public ProtobufTcpConnection(java.lang.String hostname, int port)Instantiates a new ProtobufClient. This method does not connect (see connect).- Parameters:
hostname- the IP address of the refboxport- the port to which to connect- See Also:
connect()
-
-
Method Detail
-
connect
public void connect() throws java.lang.InterruptedException, java.io.IOException, java.nio.channels.UnresolvedAddressExceptionTries to connect to the refbox at the IP address and port given in the contructor. Throws an UnknownHostException if the hostname defined in the contructor is not known. Throws an IOException if the connection cannot be established.- Throws:
java.io.IOException- Signals that the connection to the refbox cannot be established.java.lang.InterruptedExceptionjava.nio.channels.UnresolvedAddressException
-
disconnect
public void disconnect(boolean wait)
Disconnects from the refbox.
-
is_connected
public boolean is_connected()
Checks if a connection to the refbox is established.- Returns:
- true, if connection is established.
-
register_handler
public void register_handler(ProtobufMessageHandler handler)
Registers a new ProtobufMessageHandler responsible for handling and deserializing incoming protobuf messages. Required if you want to access received messages. Only allows one registered handler at the same time.- Parameters:
handler- the ProtobufMessageHandler- See Also:
ProtobufMessageHandler
-
add_message
public <T extends com.google.protobuf.GeneratedMessageV3> void add_message(java.lang.Class<T> c)
Adds and registers a new protobuf message type. This is required to instantiate the correct protobuf message object when a message is received from the refbox. For example, if you want the client to be able to receive and process a GameState message, call client.<GameState>add_message(GameState.class).- Type Parameters:
T- the type of the protobuf message to register, has to extend from GeneratedMessage- Parameters:
c- the class object of the same protobuf message
-
enqueue
public void enqueue(ProtobufMessage msg)
Puts a ProtobufMessage into the send queue in order to be sent out to the refbox.- Specified by:
enqueuein interfaceProtobufConnection- Parameters:
msg- the ProtobufMessage to send- See Also:
ProtobufMessage
-
enqueue
public void enqueue(com.google.protobuf.GeneratedMessageV3 msg)
- Specified by:
enqueuein interfaceProtobufConnection
-
enqueue
public <T> void enqueue(com.google.protobuf.GeneratedMessageV3 msg, java.lang.Class<T> c)
-
-