generated from AfonsoCMSousa/CPP-Template
feat: New Sessions
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
#ifndef MAPPER_HPP
|
||||
#define MAPPER_HPP
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstddef>
|
||||
|
||||
#include "parcer.h"
|
||||
#include "server_structs.h"
|
||||
|
||||
// INFO: This is basically a identity mapper between raw byte buffer and structs defined in server_structs.h
|
||||
class Mapper {
|
||||
private:
|
||||
const uint_least8_t *buffer;
|
||||
size_t buffer_size;
|
||||
size_t offset;
|
||||
bool ok;
|
||||
|
||||
public:
|
||||
Mapper(const uint_least8_t *buf, size_t buf_size);
|
||||
|
||||
uint8_t get_message_type();
|
||||
bool is_ok() const { return ok; }
|
||||
|
||||
void parse_new_session(trackAtributes &track);
|
||||
void parse_new_connection(carAtributes &car);
|
||||
void parse_connection_closed(carAtributes &car);
|
||||
void parse_car_update(carAtributes &car);
|
||||
void parse_car_info(carAtributes &car);
|
||||
void parse_lap_completed(uint8_t &car_id, uint32_t &lap_time, uint32_t &cuts);
|
||||
void parse_collision_event(uint8_t &car1, uint8_t &car2, uint8_t &event_type);
|
||||
void parse_chat(uint8_t &car_id, char *message, size_t max_len);
|
||||
void parse_client_loaded(uint8_t &car_id);
|
||||
|
||||
void set_size(size_t size) { this->buffer_size = size; }
|
||||
void update_buffer(const uint8_t *buf, size_t size) {
|
||||
this->buffer = buf;
|
||||
this->buffer_size = size;
|
||||
reset();
|
||||
}
|
||||
private:
|
||||
// INFO: Reset the offset to 1 because the first byte is message type
|
||||
void reset() { offset = 1; ok = true; }
|
||||
};
|
||||
|
||||
#endif // MAPPER_HPP
|
||||
@@ -27,6 +27,10 @@ u_int8_t read_uint8(const u_int8_t *buf, size_t recv_len, size_t *offset, int *o
|
||||
// Advances the offset by 2 bytes.
|
||||
u_int16_t read_uint16(const u_int8_t *buf, size_t recv_len, size_t *offset, int *ok);
|
||||
|
||||
// Reads a 16-bit unsigned integer from the buffer in little-endian byte order.
|
||||
// Advances the offset by 2 bytes.
|
||||
u_int16_t read_uint16_le(const u_int8_t *buf, size_t recv_len, size_t *offset, int *ok);
|
||||
|
||||
// Reads a 32-bit unsigned integer from the buffer in network byte order.
|
||||
// Advances the offset by 4 bytes.
|
||||
u_int32_t read_uint32(const u_int8_t *buf, size_t recv_len, size_t *offset, int *ok);
|
||||
|
||||
Reference in New Issue
Block a user