Skip to content

Commit

Permalink
client data!
Browse files Browse the repository at this point in the history
  • Loading branch information
BurntRanch committed Jan 6, 2025
1 parent 4450733 commit 5dcdce8
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 48 deletions.
10 changes: 2 additions & 8 deletions include/engine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,8 @@ class Engine {

void ProcessNetworkEvents(std::vector<Networking_Event> *networkingEvents);

bool IsConnectedToGameServer();

/* Sends a packet to the server that gets handled at the program level, this could include inputs n such. */
void SendRequestToServer(std::vector<std::byte> &data);

Expand Down Expand Up @@ -628,14 +630,6 @@ class Engine {

void DeserializeClientRequest(std::vector<std::byte> &serializedClientRequest, Networking_ClientRequest &dest);

/* Deserialize to dest */
template<typename T>
void Deserialize(std::vector<std::byte> &object, T &dest);

/* Serialize and append to dest. */
template<typename T>
void Serialize(T object, std::vector<std::byte> &dest);

void CheckButtonClicks(SDL_Event *event);

void InitNetworkingThread(NetworkingThreadStatus status);
Expand Down
40 changes: 40 additions & 0 deletions include/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,44 @@ glm::vec2 getScale(rapidxml::xml_node<char> *propertiesNode);
float getZDepth(rapidxml::xml_node<char> *propertiesNode, float depthDefault = 1.0f);
bool getVisible(rapidxml::xml_node<char> *propertiesNode);

template<typename T>
void Deserialize(std::vector<std::byte> &object, T &dest) {
if constexpr (std::is_same<T, std::string>::value) {
UTILASSERT(object.size() >= sizeof(size_t)); /* minimum size */

size_t stringSize = *reinterpret_cast<size_t *>(object.data());
object.erase(object.begin(), object.begin() + sizeof(size_t));

UTILASSERT(object.size() >= stringSize); /* Each char is 1 byte, this is valid. */

char *string = reinterpret_cast<char *>(object.data());

dest = std::string(string, stringSize);

object.erase(object.begin(), object.begin() + stringSize);
} else {
UTILASSERT(object.size() >= sizeof(T));

dest = *reinterpret_cast<T *>(object.data());

object.erase(object.begin(), object.begin() + sizeof(T));
}
}

template<typename T>
void Serialize(T object, std::vector<std::byte> &dest) {
if constexpr (std::is_same<T, std::string>::value) {
Serialize(object.size(), dest);

for (char &c : object) {
Serialize(c, dest);
}
} else {
for (size_t i = 0; i < sizeof(T); i++) {
std::byte *byte = reinterpret_cast<std::byte *>(&object) + i;
dest.push_back(*byte);
}
}
}

#endif
44 changes: 4 additions & 40 deletions src/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3552,6 +3552,10 @@ void Engine::ProcessNetworkEvents(std::vector<Networking_Event> *networkingEvent
}
}

bool Engine::IsConnectedToGameServer() {
return m_NetworkingThreadStates[0].netConnections.size() == 1;
}

void Engine::SendRequestToServer(std::vector<std::byte> &data) {
NetworkingThreadState &state = m_NetworkingThreadStates[0];

Expand Down Expand Up @@ -3912,44 +3916,4 @@ void Engine::DeserializeClientRequest(std::vector<std::byte> &serializedClientRe
Deserialize(serializedClientRequest, dest.requestType);
}

template<typename T>
void Engine::Deserialize(std::vector<std::byte> &object, T &dest) {
if constexpr (std::is_same<T, std::string>::value) {
UTILASSERT(object.size() >= sizeof(size_t)); /* minimum size */

size_t stringSize = *reinterpret_cast<size_t *>(object.data());
object.erase(object.begin(), object.begin() + sizeof(size_t));

UTILASSERT(object.size() >= stringSize); /* Each char is 1 byte, this is valid. */

char *string = reinterpret_cast<char *>(object.data());

dest = std::string(string, stringSize);

object.erase(object.begin(), object.begin() + stringSize);
} else {
UTILASSERT(object.size() >= sizeof(T));

dest = *reinterpret_cast<T *>(object.data());

object.erase(object.begin(), object.begin() + sizeof(T));
}
}

template<typename T>
void Engine::Serialize(T object, std::vector<std::byte> &dest) {
if constexpr (std::is_same<T, std::string>::value) {
Serialize(object.size(), dest);

for (char &c : object) {
Serialize(c, dest);
}
} else {
for (size_t i = 0; i < sizeof(T); i++) {
std::byte *byte = reinterpret_cast<std::byte *>(&object) + i;
dest.push_back(*byte);
}
}
}

Engine *Engine::m_CallbackInstance = nullptr;

0 comments on commit 5dcdce8

Please sign in to comment.