R-Type
Networked shoot'em up built on Aegis2D
Loading...
Searching...
No Matches
login.hpp
Go to the documentation of this file.
1
5#pragma once
6#include <cstddef>
7#include <cstdint>
8#include <optional>
9#include <span>
10#include <string>
11#include <string_view>
12#include <vector>
13
14namespace rtype {
15
17inline constexpr std::uint8_t protocol_version = 1;
18
20inline constexpr std::size_t max_name_size = 16;
21
24enum class TcpKind : std::uint8_t {
26 LoginRequest = 1,
28 LoginAccepted = 2,
30 LoginRejected = 3,
32 Logout = 4,
34 ListRooms = 5,
36 RoomList = 6,
38 CreateRoom = 7,
40 RoomCreated = 8,
42 RoomRejected = 9,
44 JoinRoom = 10,
46 RoomJoined = 11,
48 JoinRejected = 12,
50 LeaveRoom = 13,
52 SetReady = 14,
54 RoomPlayers = 15,
56 GameStart = 16,
58 ChatSend = 17,
60 ChatMessage = 18,
62 ChatRejected = 19,
64 ListScores = 20,
66 ScoreList = 21,
67};
68
70enum class LoginRejection : std::uint8_t {
72 ServerFull = 1,
74 BadVersion = 2,
76 BadName = 3,
78 NameTaken = 4,
80 GameStarted = 5,
82 Banned = 6,
83};
84
86enum class LogoutReason : std::uint8_t {
88 Left = 0,
90 Shutdown = 1,
92 TimedOut = 2,
94 ProtocolError = 3,
96 RoomClosed = 4,
98 Kicked = 5,
100 Banned = 6,
101};
102
106 std::uint8_t version = protocol_version;
108 std::string name;
109
112 bool operator==(const LoginRequest&) const = default;
113};
114
118 std::uint32_t session = 0;
120 std::uint8_t player = 0;
122 std::uint16_t udp_port = 0;
123
126 bool operator==(const LoginAccepted&) const = default;
127};
128
132[[nodiscard]] std::string_view to_string(LoginRejection reason) noexcept;
133
141[[nodiscard]] bool valid_text(std::string_view text, std::size_t max_size) noexcept;
142
148[[nodiscard]] bool valid_player_name(std::string_view name) noexcept;
149
153[[nodiscard]] std::optional<TcpKind> kind_of(std::span<const std::byte> message) noexcept;
154
159[[nodiscard]] std::optional<LoginRequest> read_login_request(std::span<const std::byte> message);
160
164[[nodiscard]] std::optional<LoginAccepted>
165read_login_accepted(std::span<const std::byte> message) noexcept;
166
171[[nodiscard]] std::optional<std::uint8_t>
172read_login_rejected(std::span<const std::byte> message) noexcept;
173
178[[nodiscard]] std::optional<std::uint8_t> read_logout(std::span<const std::byte> message) noexcept;
179
183[[nodiscard]] std::vector<std::byte> write_login_request(const LoginRequest& request);
184
188[[nodiscard]] std::vector<std::byte> write_login_accepted(const LoginAccepted& accepted);
189
193[[nodiscard]] std::vector<std::byte> write_login_rejected(LoginRejection reason);
194
198[[nodiscard]] std::vector<std::byte> write_logout(LogoutReason reason);
199
200} // namespace rtype
R-Type game code, built on the Aegis2D engine.
Definition enemy_scripts.hpp:16
@ Left
It quit: Logout, Disconnect, or its TCP connection closed.
@ TimedOut
No valid datagram for too long, or no event acknowledged (RFC section 5.5).
@ Kicked
A protocol error ended its session (RFC section 3.2).
constexpr std::size_t max_name_size
Longest player name, in bytes of UTF-8.
Definition login.hpp:20
LoginRejection
Why a login is refused, the reason of LoginRejected.
Definition login.hpp:70
@ BadName
The name is empty, too long, not UTF-8 or has control characters.
@ ServerFull
Every room of the server is full.
@ GameStarted
The game is running and does not take new players.
@ NameTaken
Another player of the server has this name.
@ BadVersion
The server does not speak the requested version.
@ Banned
The administrator banned the address the client connects from.
std::vector< std::byte > write_logout(LogoutReason reason)
Encodes a Logout.
LogoutReason
Why a session ends, the reason of Logout.
Definition login.hpp:86
@ Shutdown
The server is stopping.
@ RoomClosed
The administrator of the server closed the player's room.
@ ProtocolError
The client sent something invalid over UDP.
std::vector< std::byte > write_login_request(const LoginRequest &request)
Encodes a LoginRequest.
bool valid_player_name(std::string_view name) noexcept
Tells whether a name follows the rules of LoginRequest.
std::vector< std::byte > write_login_accepted(const LoginAccepted &accepted)
Encodes a LoginAccepted.
std::vector< std::byte > write_login_rejected(LoginRejection reason)
Encodes a LoginRejected.
TcpKind
First byte of every TCP message: what it is.
Definition login.hpp:24
@ Logout
Both ways: the sender ends the session and closes.
@ ChatRejected
Server to client: why a ChatSend was not relayed.
@ SetReady
Client to server: ready to start, or not.
@ ListRooms
Client to server, after the login: asks for the rooms.
@ GameStart
Server to client: every player of its room is ready, bind and play.
@ LeaveRoom
Client to server: leaves its room for the lobby.
@ JoinRoom
Client to server: the room to join.
@ RoomList
Server to client: the rooms, answering ListRooms.
@ LoginRejected
Server to client: why the login failed; the server closes afterwards.
@ ScoreList
Server to client: the best scores, best first.
@ ChatSend
Client to server: a text for the players of its room (RFC section 3.8).
@ CreateRoom
Client to server, after the login: name and size of a room to create.
@ RoomCreated
Server to client: the number of the room created, answering CreateRoom.
@ JoinRejected
Server to client: why the room was not joined, answering JoinRoom.
@ ListScores
Client to server: the best scores, please (RFC section 3.9).
@ RoomRejected
Server to client: why the room was not created, answering CreateRoom.
@ RoomJoined
Server to client: the room joined and the player's number there.
std::optional< LoginRequest > read_login_request(std::span< const std::byte > message)
Reads a LoginRequest.
std::optional< TcpKind > kind_of(std::span< const std::byte > message) noexcept
Kind of a TCP message.
std::optional< std::uint8_t > read_logout(std::span< const std::byte > message) noexcept
Reads a Logout.
std::optional< LoginAccepted > read_login_accepted(std::span< const std::byte > message) noexcept
Reads a LoginAccepted.
std::string_view to_string(ChatRejection reason) noexcept
Short name of a rejection, for logs.
constexpr std::uint8_t protocol_version
Protocol version the server speaks, in LoginRequest and in the UDP header.
Definition login.hpp:17
bool valid_text(std::string_view text, std::size_t max_size) noexcept
Tells whether a text is 1 to max_size bytes of valid UTF-8 without control characters,...
std::optional< std::uint8_t > read_login_rejected(std::span< const std::byte > message) noexcept
Reads a LoginRejected.
Content of a ChatMessage.
Definition chat.hpp:38
Content of a LoginAccepted.
Definition login.hpp:116
std::uint8_t player
Player number in its room, 0 to 3.
Definition login.hpp:120
std::uint16_t udp_port
UDP port of the server.
Definition login.hpp:122
std::uint32_t session
Session token, never 0.
Definition login.hpp:118
bool operator==(const LoginAccepted &) const =default
Field-by-field equality.
Content of a LoginRequest.
Definition login.hpp:104
std::uint8_t version
Protocol version of the client.
Definition login.hpp:106
std::string name
Player name, not checked yet: see valid_player_name().
Definition login.hpp:108
bool operator==(const LoginRequest &) const =default
Field-by-field equality.
Content of a RoomPlayers.
Definition rooms.hpp:101