R-Type
Networked shoot'em up built on Aegis2D
Loading...
Searching...
No Matches
chat.hpp
Go to the documentation of this file.
1
3#pragma once
4#include <cstddef>
5#include <cstdint>
6#include <optional>
7#include <span>
8#include <string>
9#include <string_view>
10#include <vector>
11
12namespace rtype {
13
15inline constexpr std::size_t max_chat_size = 200;
16
18inline constexpr std::uint8_t lobby_player = 255;
19
22inline constexpr std::uint8_t server_player = 254;
23
25inline constexpr std::string_view server_name = "server";
26
28enum class ChatRejection : std::uint8_t {
30 TooLong = 1,
32 BadText = 2,
34 TooFast = 3,
35};
36
40 std::uint8_t player = 0;
42 std::string name;
44 std::string text;
45
48 bool operator==(const ChatMessage&) const = default;
49};
50
54[[nodiscard]] std::string_view to_string(ChatRejection reason) noexcept;
55
60[[nodiscard]] std::optional<ChatRejection> check_chat_text(std::string_view text) noexcept;
61
66[[nodiscard]] std::optional<std::string> read_chat_send(std::span<const std::byte> message);
67
72[[nodiscard]] std::optional<ChatMessage> read_chat_message(std::span<const std::byte> message);
73
78[[nodiscard]] std::optional<std::uint8_t>
79read_chat_rejected(std::span<const std::byte> message) noexcept;
80
84[[nodiscard]] std::vector<std::byte> write_chat_send(std::string_view text);
85
89[[nodiscard]] std::vector<std::byte> write_chat_message(const ChatMessage& message);
90
94[[nodiscard]] std::vector<std::byte> write_chat_rejected(ChatRejection reason);
95
96} // namespace rtype
R-Type game code, built on the Aegis2D engine.
Definition enemy_scripts.hpp:16
constexpr std::uint8_t lobby_player
player of a ChatMessage whose sender is in the lobby, with no player number.
Definition chat.hpp:18
constexpr std::size_t max_chat_size
Longest chat text, in bytes of UTF-8.
Definition chat.hpp:15
std::vector< std::byte > write_chat_message(const ChatMessage &message)
Encodes a ChatMessage.
std::optional< std::uint8_t > read_chat_rejected(std::span< const std::byte > message) noexcept
Reads a ChatRejected.
constexpr std::string_view server_name
name of a ChatMessage of the server itself.
Definition chat.hpp:25
constexpr std::uint8_t server_player
player of a ChatMessage of the server itself, such as its administrator's say: no player has it (RFC ...
Definition chat.hpp:22
std::optional< ChatRejection > check_chat_text(std::string_view text) noexcept
Checks a text against the rules of ChatSend, the length first.
std::vector< std::byte > write_chat_send(std::string_view text)
Encodes a ChatSend.
std::optional< std::string > read_chat_send(std::span< const std::byte > message)
Reads a ChatSend.
std::optional< ChatMessage > read_chat_message(std::span< const std::byte > message)
Reads a ChatMessage.
std::string_view to_string(ChatRejection reason) noexcept
Short name of a rejection, for logs.
std::vector< std::byte > write_chat_rejected(ChatRejection reason)
Encodes a ChatRejected.
ChatRejection
Why a chat message is not relayed, the reason of ChatRejected.
Definition chat.hpp:28
@ TooLong
The text is longer than max_chat_size bytes.
@ BadText
The text is empty, not UTF-8 or has control characters.
@ TooFast
The sender is over the rate: the message is a flood.
Content of a ChatMessage.
Definition chat.hpp:38
std::string name
Name of the sender, as received: see valid_player_name().
Definition chat.hpp:42
bool operator==(const ChatMessage &) const =default
Field-by-field equality.
std::string text
Text, as received: see check_chat_text().
Definition chat.hpp:44
std::uint8_t player
Number of the sender in its room, or lobby_player.
Definition chat.hpp:40