R-Type
Distributed multiplayer game engine in C++
Loading...
Searching...
No Matches
LoginResponse.hpp
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2026
3** r-type
4** File description:
5** LoginResponse.hpp
6*/
7
8#pragma once
9
10#include <capnp/message.h>
11#include <capnp/serialize.h>
12#include <kj/array.h>
13#include <schemas/s2c_messages.capnp.h>
14#include <string>
15#include <vector>
16
17namespace RType {
18 namespace Messages {
19 namespace S2C {
20
26 public:
27 bool success;
28 std::string message;
29 std::string sessionToken;
31
33 LoginResponse(bool s, const std::string &msg, const std::string &token = "",
34 bool autoMM = false)
35 : success(s), message(msg), sessionToken(token), autoMatchmaking(autoMM) {}
36
40 std::vector<uint8_t> serialize() const {
41 ::capnp::MallocMessageBuilder msgBuilder;
42 auto response = msgBuilder.initRoot<::LoginResponse>();
43
44 response.setSuccess(success);
45 response.setMessage(message);
46 response.setSessionToken(sessionToken);
47 response.setAutoMatchmaking(autoMatchmaking);
48
49 kj::Array<capnp::word> words = messageToFlatArray(msgBuilder);
50 auto bytes = words.asBytes();
51 return std::vector<uint8_t>(bytes.begin(), bytes.end());
52 }
53
57 static LoginResponse deserialize(const std::vector<uint8_t> &data) {
58 kj::ArrayPtr<const capnp::word> words(reinterpret_cast<const capnp::word *>(data.data()),
59 data.size() / sizeof(capnp::word));
60
61 ::capnp::FlatArrayMessageReader msgReader(words);
62 auto response = msgReader.getRoot<::LoginResponse>();
63
64 LoginResponse result;
65 result.success = response.getSuccess();
66 result.message = response.getMessage().cStr();
67 result.sessionToken = response.getSessionToken().cStr();
68 result.autoMatchmaking = response.getAutoMatchmaking();
69
70 return result;
71 }
72 };
73
74 } // namespace S2C
75 } // namespace Messages
76} // namespace RType
Server response to login request.
std::vector< uint8_t > serialize() const
Serialize to Cap'n Proto binary format.
LoginResponse(bool s, const std::string &msg, const std::string &token="", bool autoMM=false)
static LoginResponse deserialize(const std::vector< uint8_t > &data)
Deserialize from Cap'n Proto binary format.