6#include "EngineContext.hpp"
7#include "events/NetworkEvent.hpp"
8#include "protocol/HandshakeHeader.hpp"
9#include "protocol/PacketWriter.hpp"
11#include "scene/sceneType.hpp"
13namespace rtype::client {
25 float _timeoutTimer = 0.0f;
26 float _retryTimer = 0.0f;
27 bool _isConnected =
false;
45 LOG_INFO(
"Handshake success connection");
51 LOG_ERROR(
"Connection refused by the server");
68 if (_retryTimer >= 1.0f) {
76 LOG_INFO(
"Send handshake hello");
84 if (_timeoutTimer >= 10.0f) {
85 LOG_ERROR(
"Timeout: cannot join the server.");
Growable buffer of big-endian values, capped at MAX_PAYLOAD_SIZE.
Definition PacketWriter.hpp:16
const std::vector< std::uint8_t > & getData() const
Returns the written bytes.
Definition PacketWriter.cpp:55
void subscribe(std::function< void(const E &event)> func)
Registers a handler for one event type.
Definition busEvents.hpp:33
void publish(const E &event)
Calls every handler registered for the event type.
Definition busEvents.hpp:52
Connection screen, which sends Hello every second until the server answers or 10 seconds pass.
Definition networkConnectionScene.hpp:23
std::optional< SceneType > onUpdate(rtk::EngineContext &context, const rtk::InputState &, float dt)
Retries the handshake and picks the next scene.
Definition networkConnectionScene.hpp:61
static constexpr SceneType Type
Identifier of this scene.
Definition networkConnectionScene.hpp:31
void onExit(rtk::EngineContext &)
Does nothing.
Definition networkConnectionScene.hpp:95
NetworkConnectionScene(rtk::RenderWindow &)
Creates the scene.
Definition networkConnectionScene.hpp:36
void onEnter(rtk::EngineContext &context)
Subscribes to the connection result events.
Definition networkConnectionScene.hpp:42
SceneType
Identifiers of the client scenes.
Definition sceneType.hpp:12
@ Connection
Handshake with the server.
@ Hello
Client asks the server to open a connection.
Header of the datagrams exchanged before a connection exists.
Definition HandshakeHeader.hpp:38
HandshakeType type
Handshake step.
Definition HandshakeHeader.hpp:41
bool serialize(PacketWriter &writer) const
Writes the header in network byte order.
Definition HandshakeHeader.cpp:4
Engine services handed to the game and its scenes.
Definition EngineContext.hpp:20
busEvent & eventBus
Event bus shared by systems.
Definition EngineContext.hpp:23
Published when the server accepted the handshake.
Definition NetworkEvent.hpp:14
Published when the server refused the handshake.
Definition NetworkEvent.hpp:23
Asks the network client to send raw bytes to the server.
Definition NetworkEvent.hpp:30