R-Type  2
Doom but in better
Loading...
Searching...
No Matches
ThreadCapsule.hpp
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2024
3** rtype (Workspace)
4** File description:
5** ThreadCapsule.hpp
6*/
7
8#pragma once
9
10#include <string>
11#include <vector>
12#include <memory>
13#include <atomic>
14#include <thread>
15#include <cstdint>
16
18
19#define ENSURE_THREAD_ALIVE(method) \
20 if (!isThreadAlive()) { \
21 PRETTY_ERROR << "No active thread for " << method << std::endl; \
22 throw CustomExceptions::NoActiveNetworkThread("<No running thread for " + std::string(method) + ">"); \
23 }
24
25namespace GUI
26{
27 namespace Network
28 {
30 public:
31 ThreadCapsule(const std::uint32_t nodeId = 0);
33
34 void initialize();
35
36 void startThread();
37 void stopThread();
38
39 void startGame();
40
41 void sendMessage(const MessageNode &message);
42 const std::vector<GUI::Network::MessageNode> getReceivedMessages();
43
44 void setPort(const unsigned int port);
45 void setIp(const std::string &ip);
46 void setPlayerName(const std::string &name);
47 void setAddress(const std::string &ip, const unsigned int port);
48
49 const bool isConnected() const;
50 const bool isThreadAlive() const;
51
52 const float bytesToFloat(const uint8_t *bytes);
53 const std::string bytesToHex(const std::vector<uint8_t> &bytes);
54 const GUI::Network::MessageNode translateMessage(const std::vector<uint8_t> &message);
55 void receiveMessage();
56
57 private:
58
59 void _spawnChild();
60
61 void _killChild();
62
63 std::thread _childThread;
64 unsigned int _port = 9000;
65 mutable std::mutex _mutex;
66 std::string _ip = "127.0.0.1";
67 std::string _playerName = "Player";
68 std::atomic<bool> _childAlive = false;
69 std::shared_ptr<NetworkManager> _childNode;
70 };
71 }
72}
This file defines the NetworkManager class, which handles UDP socket communication for sending and re...
void sendMessage(const MessageNode &message)
void setPort(const unsigned int port)
const bool isConnected() const
const std::vector< GUI::Network::MessageNode > getReceivedMessages()
const float bytesToFloat(const uint8_t *bytes)
ThreadCapsule(const std::uint32_t nodeId=0)
void setPlayerName(const std::string &name)
void setAddress(const std::string &ip, const unsigned int port)
const std::string bytesToHex(const std::vector< uint8_t > &bytes)
const bool isThreadAlive() const
const GUI::Network::MessageNode translateMessage(const std::vector< uint8_t > &message)
void setIp(const std::string &ip)