R-Type 0.1
Loading...
Searching...
No Matches
Server.hpp
1#pragma once
2
3#include <cstdint>
4#include <functional>
5#include <mutex>
6#include <queue>
7#include <unordered_map>
8#include <vector>
9
10#include "Protocol/EntityPayload.hpp"
11#include "Protocol/InputPayload.hpp"
12#include "Protocol/MessageType.hpp"
13#include "Protocol/SnapshotPayload.hpp"
14#include "protocol/MessageHeader.hpp"
15#include "server/NetworkServer.hpp"
16
17namespace rtype::server {
30class Server {
31public:
36 explicit Server(std::uint16_t port);
37
41 ~Server();
42
46 struct InputEvent {
47 std::uint32_t connectionId;
49 };
50
58 enum class Type {
59 Connected,
61 };
62
64 std::uint32_t connectionId;
65 };
66
71 std::uint32_t connectionId;
72 std::uint32_t networkId;
73 };
74
78 void start();
79
83 void stop();
84
90 void send(std::uint32_t connectionId,
91 const std::vector<std::uint8_t> &payload);
92
98 bool pollInput(InputEvent &event);
99
106
113
120 bool sendCreateEntity(std::uint32_t connectionId,
121 const protocol::CreateEntityPayload &payload);
122
129 bool sendDestroyEntity(std::uint32_t connectionId,
130 const protocol::DestroyEntityPayload &payload);
131
138 bool sendSnapshot(std::uint32_t connectionId,
139 const protocol::SnapshotPayload &payload);
140
141private:
142 void handleMessage(std::uint32_t connectionId, MessageId type,
143 const std::vector<std::uint8_t> &payload);
144 bool sendMessage(std::uint32_t connectionId, protocol::MessageType type,
145 const std::vector<std::uint8_t> &payload);
146
147 NetworkServer _network;
148 std::mutex _inputMutex;
149 std::queue<InputEvent> _inputQueue;
150 std::unordered_map<std::uint32_t, std::uint32_t> _lastInputSequence;
151
152 std::mutex _connectionMutex;
153 std::queue<ConnectionEvent> _connectionQueue;
154
155 std::mutex _queryMutex;
156 std::queue<QueryEntityEvent> _queryQueue;
157
158 std::function<void(std::uint32_t connectionId)> _onClientConnected;
159 std::function<void(std::uint32_t connectionId)> _onClientDisconnected;
160};
161
163} // namespace rtype::server
UDP server that runs the handshake, tracks connections and dispatches messages.
Definition NetworkServer.hpp:30
Game level layer over NetworkServer that decodes client messages and encodes server messages.
Definition Server.hpp:30
bool pollInput(InputEvent &event)
Pops the oldest Input message.
Definition Server.cpp:104
bool sendSnapshot(std::uint32_t connectionId, const protocol::SnapshotPayload &payload)
Sends a Snapshot message.
Definition Server.cpp:181
void stop()
Stops the network thread.
Definition Server.cpp:49
void start()
Starts the network thread.
Definition Server.cpp:47
void send(std::uint32_t connectionId, const std::vector< std::uint8_t > &payload)
Sends already encoded messages to a client.
Definition Server.cpp:51
~Server()
Stops the network thread.
Definition Server.cpp:45
Server(std::uint16_t port)
Creates the server on a UDP port, without starting it.
Definition Server.cpp:20
bool sendCreateEntity(std::uint32_t connectionId, const protocol::CreateEntityPayload &payload)
Sends a CreateEntity message.
Definition Server.cpp:159
bool pollConnectionEvent(ConnectionEvent &event)
Pops the oldest connection change.
Definition Server.cpp:115
bool pollQueryEntity(QueryEntityEvent &event)
Pops the oldest QueryEntity message.
Definition Server.cpp:126
bool sendDestroyEntity(std::uint32_t connectionId, const protocol::DestroyEntityPayload &payload)
Sends a DestroyEntity message.
Definition Server.cpp:170
std::uint16_t MessageId
Numeric identifier of a message type.
Definition MessageHeader.hpp:14
MessageType
Type identifier of every message exchanged on a connection.
Definition MessageType.hpp:16
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
Payload of an Input message, 9 bytes, sent by a client every frame with the actions currently held.
Definition InputPayload.hpp:56
Decoded Snapshot message: a SnapshotHeader followed by entityCount SnapshotEntityState records.
Definition SnapshotPayload.hpp:71
Client connection or disconnection.
Definition Server.hpp:54
Type
Kind of connection change.
Definition Server.hpp:58
std::uint32_t connectionId
Affected connection.
Definition Server.hpp:64
Type type
Kind of change.
Definition Server.hpp:63
Input message received from a client.
Definition Server.hpp:46
std::uint32_t connectionId
Sender connection.
Definition Server.hpp:47
protocol::InputPayload input
Decoded payload.
Definition Server.hpp:48
QueryEntity message received from a client.
Definition Server.hpp:70
std::uint32_t connectionId
Sender connection.
Definition Server.hpp:71
std::uint32_t networkId
Entity the client asks about.
Definition Server.hpp:72