7#include <unordered_map>
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"
17namespace rtype::server {
36 explicit Server(std::uint16_t port);
90 void send(std::uint32_t connectionId,
91 const std::vector<std::uint8_t> &payload);
142 void handleMessage(std::uint32_t connectionId,
MessageId type,
143 const std::vector<std::uint8_t> &payload);
145 const std::vector<std::uint8_t> &payload);
148 std::mutex _inputMutex;
149 std::queue<InputEvent> _inputQueue;
150 std::unordered_map<std::uint32_t, std::uint32_t> _lastInputSequence;
152 std::mutex _connectionMutex;
153 std::queue<ConnectionEvent> _connectionQueue;
155 std::mutex _queryMutex;
156 std::queue<QueryEntityEvent> _queryQueue;
158 std::function<void(std::uint32_t connectionId)> _onClientConnected;
159 std::function<void(std::uint32_t connectionId)> _onClientDisconnected;
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
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
@ Connected
Handshake completed.
@ Disconnected
Client sent Disconnect.
std::uint32_t connectionId
Affected connection.
Definition Server.hpp:64
Type type
Kind of change.
Definition Server.hpp:63
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