R-Type 0.1
Loading...
Searching...
No Matches
networkConnectionScene.hpp
1#pragma once
2
3#include <iostream>
4#include <optional>
5
6#include "EngineContext.hpp"
7#include "events/NetworkEvent.hpp"
8#include "protocol/HandshakeHeader.hpp"
9#include "protocol/PacketWriter.hpp"
10
11#include "scene/sceneType.hpp"
12
13namespace rtype::client {
24private:
25 float _timeoutTimer = 0.0f;
26 float _retryTimer = 0.0f;
27 bool _isConnected = false;
28
29public:
32
36 NetworkConnectionScene(rtk::RenderWindow &) {}
37
42 void onEnter(rtk::EngineContext &context) {
45 LOG_INFO("Handshake success connection");
46 _isConnected = true;
47 });
48
51 LOG_ERROR("Connection refused by the server");
52 });
53 }
54
61 std::optional<SceneType> onUpdate(rtk::EngineContext &context,
62 const rtk::InputState &, float dt) {
63 if (_isConnected) {
64 return SceneType::Game;
65 }
66
67 _retryTimer += dt;
68 if (_retryTimer >= 1.0f) {
69 _retryTimer = 0.0f;
70
71 PacketWriter writer;
72 HandshakeHeader helloHeader;
73 helloHeader.type = HandshakeType::Hello;
74
75 if (helloHeader.serialize(writer)) {
76 LOG_INFO("Send handshake hello");
77
78 context.eventBus.publish(
80 }
81 }
82
83 _timeoutTimer += dt;
84 if (_timeoutTimer >= 10.0f) {
85 LOG_ERROR("Timeout: cannot join the server.");
86 return SceneType::Menu;
87 }
88
89 return std::nullopt;
90 }
91
96};
97
99} // namespace rtype::client
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
Player input sampled during the current frame.
Definition InputAction.hpp:14
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