Skip to content

Commit c2ea00a

Browse files
authored
Merge pull request #89 from YGNI-RType/dev
Release v0.6.1
2 parents 56d0103 + 314187f commit c2ea00a

15 files changed

Lines changed: 220 additions & 52 deletions

File tree

include/GEngine/libdev/Systems.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,16 @@
88
#pragma once
99

1010
#include "GEngine/libdev/systems/AutoKiller.hpp"
11-
#include "GEngine/libdev/systems/CLI.hpp"
1211
#include "GEngine/libdev/systems/Collisions.hpp"
1312
#include "GEngine/libdev/systems/Logger.hpp"
1413
#include "GEngine/libdev/systems/MainLoop.hpp"
1514
#include "GEngine/libdev/systems/Motions.hpp"
1615

16+
/* TODO: Thomas: these includes should never exists, it should be the developer to get them individually */
17+
namespace gengine::system {
18+
class CLI;
19+
}
20+
1721
namespace geg::system {
1822
using AutoKiller = gengine::system::AutoKiller;
1923

include/GEngine/libdev/systems/CLI.hpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
#include "GEngine/libdev/systems/events/CLI.hpp"
1616
#include "GEngine/libdev/systems/events/MainLoop.hpp"
1717
#include "GEngine/libdev/systems/events/Native.hpp"
18+
#include "GEngine/net/events/socket_event.hpp"
19+
#include "GEngine/net/net_socket_system.hpp"
20+
#include "GEngine/net/net_wait.hpp"
1821

1922
#include <atomic>
2023
#include <chrono>
@@ -36,10 +39,20 @@ class CLI : public gengine::System<CLI> {
3639
private:
3740
void getInputs(void);
3841
std::vector<std::string> splitInput(const std::string &input);
42+
void processOutput(void);
43+
44+
Network::NetWait m_wait;
45+
Network::Event::SocketEvent m_socketEventStop;
46+
Network::SocketSTD m_socketSTD;
3947

4048
std::thread m_inputThread;
4149
std::atomic_bool m_stopReading;
50+
std::atomic_bool m_stopProgram; /* due to EOF */
4251
std::vector<std::string> m_userInputHistory;
4352
mutable std::mutex m_historyMutex;
53+
54+
#ifdef NET_USE_HANDLE
55+
DWORD m_dwMod;
56+
#endif
4457
};
4558
} // namespace gengine::system

include/GEngine/net/net.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ class NET {
113113
return mg_client.getRecord();
114114
}
115115

116+
static NetWait &getWaitHandler(void) {
117+
return mg_wait;
118+
}
119+
116120
public:
117121
/* todo : temp*/
118122
static SocketUDP &getSocketUdp(void) {

include/GEngine/net/net_event.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ struct Info : public InfoHeader {
5353
*/
5454
class Manager {
5555
public:
56-
Manager() = default;
56+
Manager();
5757
~Manager() = default;
5858

5959
template <typename T>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
** EPITECH PROJECT, 2024
3+
** GameEngine
4+
** File description:
5+
** net_system_socket
6+
*/
7+
8+
#pragma once
9+
10+
#include "net_socket.hpp"
11+
12+
#include <string>
13+
14+
namespace Network {
15+
16+
#ifdef _WIN32
17+
#define STD_IN STD_INPUT_HANDLE
18+
#define STD_OUT STD_OUTPUT_HANDLE
19+
#define STD_ERR STD_ERROR_HANDLE
20+
#else
21+
#define STD_IN STDIN_FILENO
22+
#define STD_OUT STDOUT_FILENO
23+
#define STD_ERR STDERR_FILENO
24+
#endif
25+
26+
class SocketSTD : public ASocket {
27+
public:
28+
SocketSTD() = default;
29+
SocketSTD(int stdNumber);
30+
SocketSTD(const SocketSTD &other) = delete;
31+
SocketSTD &operator=(const SocketSTD &) = delete;
32+
SocketSTD(SocketSTD &&other);
33+
SocketSTD &operator=(SocketSTD &&other);
34+
~SocketSTD() = default;
35+
36+
int read(std::string &buffer) const;
37+
int write(const std::string &buffer) const;
38+
39+
int socketClose(void) override final {
40+
return socketCloseAdv(false);
41+
}
42+
43+
void setStd(int stdNumber);
44+
};
45+
} // namespace Network

include/GEngine/net/net_wait.hpp

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class NetWaitSet {
5353
m_resIndex = res;
5454
}
5555

56-
bool applyCallback(void) const;
56+
bool applyCallback(bool shouldReset = true) const;
5757

5858
private:
5959
static constexpr size_t MAX_SOCKETS = MAXIMUM_WAIT_OBJECTS;
@@ -90,24 +90,27 @@ class NetWait {
9090
NetWait();
9191
virtual ~NetWait() = default;
9292

93+
/**
94+
* @brief Waits for a specified amount of time or until an event occurs in the NetWaitSet.
95+
*
96+
* @param ms The maximum number of milliseconds to wait.
97+
* @param set The NetWaitSet to monitor for events.
98+
* @return true if an event occurred within the specified time, false if the timeout was reached.
99+
*/
93100
bool wait(uint32_t ms, NetWaitSet &set);
94101

95102
public:
96-
static void addSocketPool(ASocket &socket);
97-
static void removeSocketPool(const ASocket &socket);
103+
void addSocketPool(ASocket &socket);
104+
void removeSocketPool(const ASocket &socket);
98105

99-
#ifdef NET_USE_HANDLE
100-
101-
#else
102106
public:
103-
static SOCKET getHighestSocket(void) {
107+
SOCKET getHighestSocket(void) {
104108
return m_highFd;
105109
}
106110

107111
private:
108-
static fd_set m_fdSet;
109-
static SOCKET m_highFd;
110-
#endif
112+
fd_set m_fdSet;
113+
SOCKET m_highFd = -1;
111114
};
112115

113116
} // namespace Network

source/GEngine/libdev/systems/CLI.cpp

Lines changed: 74 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@
77

88
#include "GEngine/libdev/systems/CLI.hpp"
99

10+
#include "GEngine/net/events/socket_event.hpp"
11+
#include "GEngine/net/net_socket_system.hpp"
12+
#include "GEngine/net/net_wait.hpp"
13+
1014
namespace gengine::system {
15+
1116
CLI::CLI()
1217
: m_stopReading(false) {
1318
}
@@ -19,32 +24,94 @@ void CLI::init(void) {
1924
}
2025

2126
void CLI::onStartEngine(gengine::system::event::StartEngine &e [[maybe_unused]]) {
27+
m_socketSTD.setStd(STD_IN);
28+
#ifndef NET_USE_HANDLE
29+
m_wait.addSocketPool(m_socketSTD);
30+
m_wait.addSocketPool(m_socketEventStop);
31+
#endif
2232
m_inputThread = std::thread(&CLI::getInputs, this);
23-
m_inputThread.detach();
2433
}
2534

2635
void CLI::onStopEngine(gengine::system::event::StopEngine &e [[maybe_unused]]) {
2736
m_stopReading = true;
37+
38+
m_socketEventStop.signal();
39+
40+
#ifdef NET_USE_HANDLE
41+
SetConsoleMode(m_socketSTD.getHandle(), m_dwMod);
42+
#endif
43+
44+
if (m_inputThread.joinable())
45+
m_inputThread.join();
2846
}
2947

3048
void CLI::onMainLoop(gengine::system::event::MainLoop &e [[maybe_unused]]) {
3149
std::lock_guard<std::mutex> lock(m_historyMutex);
50+
if (m_stopProgram) {
51+
publishEvent(gengine::system::event::StopMainLoop());
52+
return;
53+
}
54+
3255
for (const auto &entry : m_userInputHistory)
3356
publishEvent(gengine::system::event::CLINewInput(splitInput(entry)));
3457
m_userInputHistory.clear();
3558
}
3659

37-
void CLI::getInputs(void) {
60+
void CLI::processOutput(void) {
3861
std::string input;
3962

63+
auto &res = std::getline(std::cin, input);
64+
if (res.eof()) {
65+
m_stopProgram = true;
66+
m_stopReading = true;
67+
return;
68+
}
69+
70+
if (!input.empty()) {
71+
std::lock_guard<std::mutex> lock(m_historyMutex);
72+
m_userInputHistory.push_back(input);
73+
}
74+
75+
/* ugly way of output a > once it's printed */
76+
std::this_thread::sleep_for(std::chrono::milliseconds(100));
77+
}
78+
79+
void CLI::getInputs(void) {
80+
#ifdef NET_USE_HANDLE
81+
GetConsoleMode(m_socketSTD.getHandle(), &m_dwMod);
82+
83+
DWORD fdwMode = m_dwMod & ~(ENABLE_MOUSE_INPUT | ENABLE_WINDOW_INPUT);
84+
SetConsoleMode(m_socketSTD.getHandle(), fdwMode);
85+
86+
/* flush to remove existing events */
87+
FlushConsoleInputBuffer(m_socketSTD.getHandle());
88+
#endif
4089
while (!m_stopReading) {
90+
Network::NetWaitSet set;
91+
92+
set.setAlert(m_socketEventStop, [this]() {
93+
/* do nothing, just loopback since the thread must be killed */
94+
return true;
95+
});
96+
97+
set.setAlert(m_socketSTD, [this]() {
98+
processOutput();
99+
return true;
100+
});
101+
41102
std::cout << "> " << std::flush;
42-
if (std::getline(std::cin, input)) {
43-
std::lock_guard<std::mutex> lock(m_historyMutex);
44-
m_userInputHistory.push_back(input);
45-
} else
103+
bool hasActivity = m_wait.wait(1000000000, set);
104+
if (!hasActivity)
105+
continue;
106+
107+
#ifdef NET_USE_HANDLE
108+
set.applyCallback(false);
109+
#else
110+
if (set.isSignaled(m_socketEventStop)) /**/
46111
break;
47-
std::this_thread::sleep_for(std::chrono::milliseconds(100));
112+
if (set.isSignaled(m_socketSTD))
113+
processOutput();
114+
#endif
48115
}
49116
}
50117

source/GEngine/libdev/systems/driver/output/VoIPAudio.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,8 @@ void VoIPAudio::processSoundInput(void) {
221221

222222
VoIPAudio::~VoIPAudio() {
223223
m_running = false;
224-
m_soundThread.join();
224+
if (m_soundThread.joinable())
225+
m_soundThread.join();
225226

226227
auto err = Pa_StopStream(playbackStream);
227228
err = Pa_CloseStream(playbackStream);

source/GEngine/net/cl_net_client.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -211,17 +211,17 @@ void CLNetClient::getPingResponse(const UDPMessage &msg, const Address &addr) {
211211
msg.readData<UDPSV_PingResponse>(data);
212212

213213
std::unique_ptr<Address> addrPtr;
214-
if (addr.getType() == AT_IPV4)
214+
uint16_t port = -1;
215+
if (addr.getType() == AT_IPV4) {
215216
addrPtr = std::make_unique<AddressV4>(static_cast<const AddressV4 &>(addr));
216-
else if (addr.getType() == AT_IPV6)
217+
port = data.tcpv4Port;
218+
} else if (addr.getType() == AT_IPV6) {
217219
addrPtr = std::make_unique<AddressV6>(static_cast<const AddressV6 &>(addr));
220+
port = data.tcpv6Port;
221+
}
218222

219-
Event::PingInfo pinginfo = {addrPtr->toString(),
220-
addrPtr->getPort(),
221-
data.currentPlayers,
222-
data.maxPlayers,
223-
data.os,
224-
Time::Clock::milliseconds() - m_pingSendTime};
223+
Event::PingInfo pinginfo = {addrPtr->toString(), port, data.currentPlayers,
224+
data.maxPlayers, data.os, Time::Clock::milliseconds() - m_pingSendTime};
225225
NET::getEventManager().invokeCallbacks(Event::CT_OnPingResult, pinginfo);
226226

227227
m_pingedServers.push_back({data, std::move(addrPtr)});

source/GEngine/net/events/net_socket_event.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
#include "GEngine/net/events/socket_event.hpp"
99
#include "GEngine/net/net_exception.hpp"
10-
#include "GEngine/net/net_wait.hpp"
1110

1211
#include <stdexcept>
1312

@@ -44,7 +43,7 @@ SocketEvent::SocketEvent() {
4443
if (m_sock == -1)
4544
throw NetException("Failed to create eventfd", EL_ERR_SOCKET);
4645
#endif
47-
NetWait::addSocketPool(*this);
46+
4847
#else
4948
m_handle = CreateEvent(NULL, TRUE, FALSE, NULL);
5049
#endif

0 commit comments

Comments
 (0)