r-type  0.0.0
R-Type main
Loading...
Searching...
No Matches
AsioClient.hpp
Go to the documentation of this file.
1///
2/// @file AsioClient.hpp
3/// @brief This file contains the client network implementation for Asio
4/// @namespace eng
5///
6
7#pragma once
8
9#include <functional>
10#include <memory>
11#include <optional>
12#include <string>
13#include <thread>
14#include <vector>
15
16#define ASIO_STANDALONE
17#include "asio.hpp"
18
21
22namespace eng
23{
24
25 ///
26 /// @class AsioClient
27 /// @brief Network implementation with asio for client
28 /// @namespace eng
29 ///
30 class AsioClient final : public INetworkClient
31 {
32 public:
33 using PacketHandler = std::function<void(const rnp::PacketHeader &, const std::vector<uint8_t> &)>;
34
35 AsioClient();
36 ~AsioClient() override = default;
37
38 AsioClient(const AsioClient &) = delete;
39 AsioClient(AsioClient &&) = delete;
40 AsioClient &operator=(const AsioClient &) = delete;
42
43 [[nodiscard]] const std::string getName() const override { return "Network_Asio_Client"; }
44 [[nodiscard]] utl::PluginType getType() const override { return utl::PluginType::NETWORK_CLIENT; }
45
46 void connect(const std::string &host, uint16_t port) override;
47 void disconnect() override;
48
49 void sendConnect(const std::string &playerName);
50 void sendConnectWithCaps(const std::string &playerName, std::uint32_t clientCaps);
51 void sendDisconnect();
53 void sendPlayerInput(uint8_t direction, uint8_t shooting);
54 void sendPlayerInputAsEvent(std::uint16_t playerId, uint8_t direction, uint8_t shooting,
55 uint32_t clientTimeMs);
56 void sendPing();
57 void sendPing(std::uint32_t nonce, std::uint32_t sendTimeMs);
58 void sendAck(std::uint32_t cumulative, std::uint32_t ackBits);
59
61
62 void setEventsHandler(std::function<void(const std::vector<rnp::EventRecord> &)> handler);
63
64 std::uint32_t getSessionId() const { return m_sessionId; }
65 std::uint16_t getServerTickRate() const { return m_serverTickRate; }
66
67 private:
68 void startReceive();
69 void handleReceive(const asio::error_code &error, std::size_t bytesTransferred);
70 void handleSend(const asio::error_code &error, std::size_t bytesTransferred);
71 void processPacket(const std::vector<uint8_t> &data);
72 void processEvents(const std::vector<uint8_t> &payload);
73 void handleConnectAccept(const std::vector<uint8_t> &payload);
74 void handleReliablePacket(const rnp::PacketHeader &header);
75 void processAck(const std::vector<uint8_t> &payload);
76 void processWorldState(const std::vector<uint8_t> &payload);
77 void processEntityEvent(const std::vector<uint8_t> &payload);
78 void retransmitReliable();
79
80 asio::io_context m_ioContext;
81 asio::ip::udp::socket m_socket;
82 asio::ip::udp::endpoint m_serverEndpoint;
83 std::array<uint8_t, rnp::MAX_PAYLOAD + 16> m_recvBuffer;
84
85 std::unique_ptr<asio::executor_work_guard<asio::io_context::executor_type>> m_workGuard;
86 std::thread m_ioThread;
87 std::unordered_map<rnp::PacketType, PacketHandler> m_packetHandlers;
88 std::function<void(const std::vector<rnp::EventRecord> &)> m_eventsHandler;
89 uint32_t m_sequenceNumber = 0;
90 bool m_connected = false;
91 std::uint32_t m_sessionId = 0;
92 std::uint16_t m_serverTickRate = 0;
93 std::uint16_t m_serverMtu = 0;
94 std::uint32_t m_serverCaps = 0;
95 std::uint32_t m_clientCaps = 0;
96 std::unordered_map<std::uint32_t, std::vector<uint8_t>> m_pendingReliable;
97 std::uint32_t m_lastAckSent = 0;
98 }; // class AsioClient
99} // namespace eng
This file contains the client network interface.
This file contains the network protocol.
Network implementation with asio for client.
std::uint32_t m_serverCaps
void processPacket(const std::vector< uint8_t > &data)
void handleReliablePacket(const rnp::PacketHeader &header)
void handleReceive(const asio::error_code &error, std::size_t bytesTransferred)
std::uint16_t m_serverTickRate
void connect(const std::string &host, uint16_t port) override
void sendPlayerInput(uint8_t direction, uint8_t shooting)
std::unordered_map< std::uint32_t, std::vector< uint8_t > > m_pendingReliable
std::uint32_t m_clientCaps
void sendConnect(const std::string &playerName)
void processWorldState(const std::vector< uint8_t > &payload)
void sendPlayerInputAsEvent(std::uint16_t playerId, uint8_t direction, uint8_t shooting, uint32_t clientTimeMs)
std::unordered_map< rnp::PacketType, PacketHandler > m_packetHandlers
std::uint32_t m_sessionId
std::array< uint8_t, rnp::MAX_PAYLOAD+16 > m_recvBuffer
void disconnect() override
void processEvents(const std::vector< uint8_t > &payload)
void handleSend(const asio::error_code &error, std::size_t bytesTransferred)
AsioClient(AsioClient &&)=delete
void processAck(const std::vector< uint8_t > &payload)
std::unique_ptr< asio::executor_work_guard< asio::io_context::executor_type > > m_workGuard
std::thread m_ioThread
std::function< void(const rnp::PacketHeader &, const std::vector< uint8_t > &)> PacketHandler
std::uint32_t m_lastAckSent
void processEntityEvent(const std::vector< uint8_t > &payload)
std::function< void(const std::vector< rnp::EventRecord > &)> m_eventsHandler
std::uint32_t getSessionId() const
uint32_t m_sequenceNumber
AsioClient & operator=(AsioClient &&)=delete
void setEventsHandler(std::function< void(const std::vector< rnp::EventRecord > &)> handler)
void setPacketHandler(rnp::PacketType type, PacketHandler handler)
AsioClient & operator=(const AsioClient &)=delete
void retransmitReliable()
const std::string getName() const override
AsioClient(const AsioClient &)=delete
void sendAck(std::uint32_t cumulative, std::uint32_t ackBits)
asio::ip::udp::endpoint m_serverEndpoint
utl::PluginType getType() const override
std::uint16_t getServerTickRate() const
~AsioClient() override=default
std::uint16_t m_serverMtu
void sendConnectWithCaps(const std::string &playerName, std::uint32_t clientCaps)
asio::ip::udp::socket m_socket
void handleConnectAccept(const std::vector< uint8_t > &payload)
asio::io_context m_ioContext
Interface for the client network.
DisconnectReason
Disconnect reason codes.
Definition Protocol.hpp:53
PacketType
Packet types according to RNP specification.
Definition Protocol.hpp:24
PluginType
Definition IPlugin.hpp:15
Packet header according to RNP specification (Big Endian) Total size: 16 bytes.
Definition Protocol.hpp:115