14#ifndef RTYPE_NETWORK_PACKET_HPP_
15 #define RTYPE_NETWORK_PACKET_HPP_
26 #include <string_view>
29 #include <asio/buffer.hpp>
31 #include <type_traits>
124 #pragma pack(push, 1)
504 template <
typename T>
507 if constexpr (std::is_enum_v<T>) {
508 using U = std::underlying_type_t<T>;
509 return static_cast<T
>(
to_network(
static_cast<U
>(v)));
510 }
else if constexpr (std::is_integral_v<T>) {
511 if constexpr (
sizeof(T) > 1 &&
NATIVE_ENDIAN == std::endian::little)
512 return std::byteswap(v);
514 }
else if constexpr (std::is_same_v<T, float>) {
516 uint32_t u = std::bit_cast<uint32_t>(v);
517 u = std::byteswap(u);
518 return std::bit_cast<float>(u);
531 template <
typename T>
542 template <
typename T>
551 template <
typename T>
567 template <
typename T>
576 template <
typename T>
Concepts for validating component types in the ECS.
Declaration of the 2-dimensional vector class.
Network packet with header and serializable body.
static T to_network(T v)
Converts a primitive type (integer, float) from machine endianness to Big-Endian (network).
size_t _readPos
Current read position in body.
auto operator<<(std::string_view str) -> Packet &
Serialize string into packet body.
void _bumpBodySizeOrThrow()
Increment body size and check for overflow.
void resetRead(void)
Reset read position to beginning of body.
auto operator>>(T &data) -> Packet &
Deserialize data from packet body.
Header _cacheHeader
Cached header with network endianness.
auto operator<<(const std::vector< T > &vec) -> Packet &
Serialize vector into packet body.
auto operator>>(std::vector< T > &vec) -> Packet &
Deserialize vector from packet body.
BufferSequence getBufferSequence(void) const
Get buffer sequence for network transmission.
std::vector< uint8_t > body
Packet body/payload.
auto operator>>(std::string &str) -> Packet &
Deserialize string from packet body.
Packet()=default
Default constructor.
auto operator<<(T data) -> Packet &
Serialize data into packet body.
Header header
Packet header.
static T from_network(T value)
Converts a primitive type (integer, float) from Big-Endian (network) to machine endianness.
File : IEventPublisher.hpp License: MIT Author : Elias Josué HAJJAR LLAUQUEN elias-josue....
@ Lobby
Lobby room (system)
constexpr std::endian NATIVE_ENDIAN
Native endianness of the machine.
constexpr uint32_t MAX_VECTOR_SIZE
Maximum size for serialized vectors.
constexpr uint16_t MAGIC_NUMBER
Magic number for packet validation.
constexpr uint32_t MAX_STRING_SIZE
Maximum sizes for strings, vectors, and packet bodies.
EntityType
Types of entities in the game.
OpCode
Operation codes for different packet types.
@ DebugModeUpdate
Debug mode toggle.
@ SetReady
Set player readiness status.
@ LoginRequest
Client login request.
@ ListRooms
Request for room list.
@ BeamState
Beam start/stop notification.
@ Disconnect
Disconnect notification.
@ RoomChatReceived
Chat message received in room.
@ LeaveRoom
Request to leave a room.
@ RegisterResponse
Incorrect password notification.
@ LoginResponse
Successful connection notification.
@ UpdateSelectedWeapon
Client selected weapon changed.
@ JoinRoom
Request to join a room.
@ RoomList
Response with room list.
@ RegisterRequest
Server login response.
@ AmmoUpdate
Ammo update notification.
@ RoomChatSended
Chat message in room.
@ Welcome
Server welcome response.
@ StartGame
Notification to start the game.
@ Hello
Client hello packet.
@ GameOver
Game over summary.
@ CreateRoom
Request to create a room.
@ HealthUpdate
Player health update.
@ RoomUpdate
Notification of room update.
@ EntitySpawn
Entity spawn notification.
@ ScoreUpdate
Player score update.
@ Kicked
Player kicked notification.
@ InputTick
Client input state.
@ EntityDeath
Entity death notification.
constexpr size_t MTU_SIZE
Safe UDP payload size to avoid fragmentation.
std::array< asio::const_buffer, 2 > BufferSequence
@using BufferSequence
constexpr uint32_t MAX_BODY_SIZE
Maximum size for packet body.
Ammo update notification data Server OpCode.
uint16_t current
Current ammo.
uint8_t isReloading
1 if reloading
float cooldownRemaining
Remaining reload time.
Notify clients that an entity's beam started or stopped.
float length
Visual length of the beam in pixels.
uint32_t ownerNetId
Network id of the player owning the beam.
float timeRemaining
Remaining beam active time (seconds)
uint8_t active
1 if beam active, 0 if stopped
float offsetY
Vertical offset relative to owner (px)
Generic boolean response payload.
uint8_t status
Status code.
Player connection data sended Client OpCode.
uint32_t sessionId
Session identifier.
Data for creating a new room Client OpCode.
uint32_t seed
Seed for random generation.
uint32_t duration
Duration of the game session.
float difficulty
Difficulty level.
uint32_t maxPlayers
Maximum number of players.
uint8_t roomType
Room type (public/private)
char roomName[64]
Desired room name.
float speed
Speed multiplier.
uint32_t levelId
Level identifier.
Debug mode toggle data Server OpCode.
uint8_t enabled
1 if debug mode enabled
Entity death notification data.
uint32_t netId
Network entity identifier.
Vec2f position
Death position.
Entity state snapshot data.
uint32_t netId
Network entity identifier.
Vec2f position
Entity position.
Vec2f velocity
Entity velocity.
float rotation
Entity rotation.
Entity spawn notification data.
uint8_t type
Entity type from EntityType.
uint32_t netId
Network entity identifier.
uint8_t weaponKind
Optional weapon kind for player entities.
float posX
Spawn X position.
float posY
Spawn Y position.
float sizeY
Optional height for static entities.
float sizeX
Optional width for static entities.
int32_t playerScore
Receiver player score.
char bestPlayer[32]
Best player username.
int32_t bestScore
Best score.
Player health update data.
int32_t current
Current health.
Data for joining an existing room Client OpCode.
uint8_t isSpectator
1 if joining as spectator
uint32_t roomId
Room identifier to join.
char password[32]
Player password.
uint8_t weaponKind
Selected weapon kind.
char username[32]
Player username.
Login response data sent by server for LoginResponse Server OpCode.
uint8_t success
Login success flag.
char username[32]
Player username.
Ping request/response payload Client/Server /Pong OpCodes.
uint64_t clientTimeMs
Client timestamp in ms.
Player registration data sended by client Client OpCode.
char password[32]
Player password.
char username[32]
Player username.
Registration response data sent by server for RegisterResponse Server OpCode.
uint8_t success
Registration success flag.
char username[32]
Player username.
Data for sending chat messages in a room Client OpCode.
char message[256]
Chat message content.
Data for receiving chat messages in a room Server OpCode.
char username[32]
Sender's username.
char message[256]
Chat message content.
uint32_t sessionId
Sender's session identifier.
Information about a game room Server OpCode.
uint32_t seed
Seed for random generation.
uint32_t duration
Duration of the game session.
uint8_t inGame
Is the game in progress.
uint8_t roomType
Room type (public/private)
float difficulty
Difficulty level.
uint32_t roomId
Room identifier.
uint32_t maxPlayers
Maximum number of players.
float speed
Speed multiplier.
uint32_t levelId
Level identifier.
char roomName[64]
Room name.
uint32_t currentPlayers
Current number of players.
uint32_t roomId
Room identifier.
uint8_t inGame
Is the game in progress.
uint32_t currentPlayers
Current number of players.
uint16_t entityCount
Entity position.
uint32_t serverTick
Network entity identifier.
Player score update notification data.
int32_t score
Player score.
Data for setting player readiness status Client OpCode.
uint8_t isReady
Player readiness status.