15#include "Protocol/EntityPayload.hpp"
16#include "Protocol/InputPayload.hpp"
17#include "Protocol/MessageType.hpp"
18#include "Protocol/SnapshotPayload.hpp"
19#include "protocol/MessageHeader.hpp"
20#include "tracking/PacketTracker.hpp"
22namespace rtype::client {
73 asio::io_context _ioContext;
74 asio::ip::udp::socket _socket;
75 asio::ip::udp::endpoint _serverEndpoint;
76 asio::ip::udp::endpoint _receiveEndpoint;
77 std::thread _networkThread;
79 std::array<std::uint8_t, 2048> _receiveBuffer{};
80 std::atomic_bool _running{
false};
81 std::atomic_bool _connected{
false};
82 std::atomic<std::uint32_t> _connectionId{0};
83 std::uint32_t _nextInputSequence = 1;
84 std::mutex _eventMutex;
85 std::queue<NetworkEvent> _events;
86 std::queue<ReplicationEvent> _replicationEvents;
89 void handleDatagram(std::size_t size);
90 void handleHandshake(
const std::uint8_t *data, std::size_t size);
91 void handleConnectedPackets(
const std::uint8_t *data, std::size_t size);
92 void handleReplicationMessage(std::uint32_t connectionId,
94 const std::vector<std::uint8_t> &payload);
95 void doSend(std::vector<std::uint8_t> data);
118 bool connect(
const std::string &host,
const std::string &port);
143 void send(
const std::vector<std::uint8_t> &data);
Generates outgoing sequence numbers and tracks received ones.
Definition PacketTracker.hpp:23
UDP client that performs the handshake, sends inputs and decodes server messages.
Definition NetworkClient.hpp:71
void send(const std::vector< std::uint8_t > &data)
Sends raw bytes to the server.
Definition NetworkClient.cpp:158
bool isConnected() const
Tells whether the server accepted the handshake.
Definition NetworkClient.cpp:332
NetworkClient()
Creates a client with a closed socket.
Definition NetworkClient.cpp:28
void sendHello()
Sends a Hello handshake if not connected yet.
Definition NetworkClient.cpp:62
bool isRunning() const
Tells whether the network thread is running.
Definition NetworkClient.cpp:334
bool pollReplicationEvent(ReplicationEvent &event)
Pops the oldest replication message.
Definition NetworkClient.cpp:365
~NetworkClient()
Stops the client and joins the network thread.
Definition NetworkClient.cpp:30
bool connect(const std::string &host, const std::string &port)
Resolves the server address, opens the socket and starts the network thread.
Definition NetworkClient.cpp:32
void sendInput(std::uint32_t clientTick, rtype::protocol::InputAction actions)
Sends an Input message with the next input sequence number.
Definition NetworkClient.cpp:79
bool pollEvent(NetworkEvent &event)
Pops the oldest connection event or raw message.
Definition NetworkClient.cpp:320
void sendQueryEntity(std::uint32_t networkId)
Asks the server to describe an unknown entity.
Definition NetworkClient.cpp:121
void stop()
Closes the socket and joins the network thread.
Definition NetworkClient.cpp:340
std::uint32_t getConnectionId() const
Returns the id given by the server.
Definition NetworkClient.cpp:336
NetworkEventType
Kind of event produced by the network client.
Definition NetworkClient.hpp:31
std::variant< protocol::CreateEntityPayload, protocol::DestroyEntityPayload, protocol::SnapshotPayload > ReplicationPayload
Decoded replication message.
Definition NetworkClient.hpp:51
@ Connected
The server sent Welcome.
@ Message
A message that is not a replication message.
@ Rejected
The server sent Rejected.
@ Disconnected
The connection was closed.
MessageType
Type identifier of every message exchanged on a connection.
Definition MessageType.hpp:16
InputAction
Bit flags of the actions held by a player.
Definition InputPayload.hpp:16
@ Invalid
Never sent, marks an unset type.
Connection event or raw message received from the server.
Definition NetworkClient.hpp:41
NetworkEventType type
Event kind.
Definition NetworkClient.hpp:42
std::uint32_t connectionId
Definition NetworkClient.hpp:43
std::vector< std::uint8_t > payload
Message payload, for Message events.
Definition NetworkClient.hpp:47
rtype::protocol::MessageType messageType
Message type, for Message events.
Definition NetworkClient.hpp:45
Validated replication message received from the server.
Definition NetworkClient.hpp:58
std::uint32_t connectionId
Connection id given by the server.
Definition NetworkClient.hpp:59
ReplicationPayload payload
Decoded message.
Definition NetworkClient.hpp:60
Payload of a CreateEntity message, 16 bytes, sent by the server to spawn an entity on a client.
Definition EntityPayload.hpp:41
Payload of a DestroyEntity message, 9 bytes, sent by the server to remove an entity from a client.
Definition EntityPayload.hpp:60
Decoded Snapshot message: a SnapshotHeader followed by entityCount SnapshotEntityState records.
Definition SnapshotPayload.hpp:71