Skip to content

Commit

Permalink
Merge pull request #45 from MasterLaplace/44-bug-constant-connection-…
Browse files Browse the repository at this point in the history
…using-the-protocol

44 bug constant connection using the protocol
  • Loading branch information
MasterLaplace authored Nov 11, 2024
2 parents 6375b59 + b373a95 commit 3e89d33
Show file tree
Hide file tree
Showing 37 changed files with 266 additions and 283 deletions.
2 changes: 1 addition & 1 deletion .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Language: Cpp
BasedOnStyle: LLVM
Standard: c++20
AccessModifierOffset: 0
AccessModifierOffset: -4
AlignAfterOpenBracket: Align
AlignArrayOfStructures: Left
AlignConsecutiveMacros: Consecutive
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/create_release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ jobs:
uses: mathieudutour/[email protected]
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
default_bump: patch
dry_run: true

- name: Run version bump script
Expand Down
4 changes: 2 additions & 2 deletions Flakkari/Engine/EntityComponentSystem/Entity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace Flakkari::Engine::ECS {
class Registry;

class Entity {
public:
public:
friend class Registry;

explicit Entity(std::size_t id) : _id(id) {}
Expand All @@ -43,7 +43,7 @@ class Entity {
return *this;
}

private:
private:
std::size_t _id;
};

Expand Down
4 changes: 2 additions & 2 deletions Flakkari/Engine/EntityComponentSystem/EntityFactory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
namespace Flakkari::Engine::ECS {

class EntityFactory {
public:
public:
using nl_template = nlohmann::json;

public:
public:
/**
* @brief Create a Entity From Template object based on a template JSON
*
Expand Down
4 changes: 2 additions & 2 deletions Flakkari/Engine/EntityComponentSystem/Registry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
namespace Flakkari::Engine::ECS {

class Registry {
public:
public:
using entity_type = Entity;
using EraseFn = std::function<void(Registry &, const entity_type &)>;
using SystemFn = std::function<void(Registry &)>;
Expand Down Expand Up @@ -214,7 +214,7 @@ class Registry {
*/
void run_systems();

private:
private:
std::unordered_map<std::type_index, std::any> _components;
std::unordered_map<std::type_index, EraseFn> _eraseFunctions;
std::vector<SystemFn> _systems;
Expand Down
6 changes: 3 additions & 3 deletions Flakkari/Engine/EntityComponentSystem/SparseArrays.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
namespace Flakkari::Engine::ECS {

template <typename Component> class SparseArrays {
public:
public:
using value_type = std::optional<Component>;
using reference_type = value_type &;
using const_reference_type = const value_type &;
Expand All @@ -32,7 +32,7 @@ template <typename Component> class SparseArrays {
using iterator = typename container_type::iterator;
using const_iterator = typename container_type::const_iterator;

public:
public:
SparseArrays() = default;
SparseArrays(const SparseArrays &other) : _data(other._data){};
SparseArrays(SparseArrays &&other) noexcept : _data(std::move(other._data)){};
Expand Down Expand Up @@ -185,7 +185,7 @@ template <typename Component> class SparseArrays {
return std::distance(_data.begin(), it);
}

private:
private:
container_type _data;
};

Expand Down
4 changes: 2 additions & 2 deletions Flakkari/Logger/Logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,14 @@
namespace Flakkari {

class Logger {
public:
public:
enum class Mode {
SILENT,
NORMAL,
DEBUG
};

public:
public:
static void setMode(Mode mode) noexcept;
static const std::string get_current_time() noexcept;
static const std::string fatal_error_message() noexcept;
Expand Down
4 changes: 1 addition & 3 deletions Flakkari/Network/Address.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ constexpr const char *Address::socketTypeToString(SocketType socket_type)
Address::operator std::string() const
{
return std::string(toString().value_or("null") + " (" + socketTypeToString(_socket_type) + ", " +
ipTypeToString(_ip_type) + ", " + std::to_string(getId()) + ")");
ipTypeToString(_ip_type) + ")");
}

std::ostream &operator<<(std::ostream &os, const Address &addr)
Expand All @@ -199,8 +199,6 @@ std::ostream &operator<<(std::ostream &os, const Address &addr)
os << Address::socketTypeToString(addr.getSocketType());
os << ", ";
os << Address::ipTypeToString(addr.getIpType());
os << ", ";
os << addr.getId();
os << ")";
return os;
}
Expand Down
27 changes: 6 additions & 21 deletions Flakkari/Network/Address.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
namespace Flakkari::Network {

class Address {
public:
public:
enum class IpType {
None,
IPv4, // Internet Protocol version 4
Expand All @@ -51,7 +51,7 @@ class Address {
UDP, // User Datagram Protocol
};

public:
public:
/**
* @brief Convert IpType to string
*
Expand All @@ -68,12 +68,12 @@ class Address {
*/
static constexpr const char *socketTypeToString(SocketType socket_type);

public:
public:
using address_t = const std::string;
using port_t = unsigned short;
using id_t = short;

public:
public:
Address(address_t &address, port_t port, SocketType socket_type, IpType ip_type);
Address(port_t port, SocketType socket_type, IpType ip_type);
Address(const sockaddr_in &clientAddr, SocketType socket_type, IpType ip_type);
Expand Down Expand Up @@ -150,33 +150,18 @@ class Address {
*/
[[nodiscard]] IpType getIpType() const { return _ip_type; }

/**
* @brief Get the Id object
*
* @return id_t Id
*/
[[nodiscard]] id_t getId() const { return _id; }

/**
* @brief Set the Id object
*
* @param id Id
*/
void setId(id_t id) { _id = id; }

/**
* @brief Convert Address to string (std::string)
*
* @return std::string String representation of Address
*/
operator std::string() const;

protected:
private:
protected:
private:
std::shared_ptr<addrinfo> _addrInfo = nullptr;
SocketType _socket_type = SocketType::None;
IpType _ip_type = IpType::None;
id_t _id = -1;
};

/**
Expand Down
10 changes: 5 additions & 5 deletions Flakkari/Network/Buffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ namespace Flakkari::Network {
* @see Flakkari::Network::Socket
*/
class Buffer : public std::vector<byte> {
public:
public:
using std::vector<byte>::operator=;
using std::vector<byte>::operator[];
using std::vector<byte>::at;
Expand All @@ -72,10 +72,10 @@ class Buffer : public std::vector<byte> {
using std::vector<byte>::resize;
using std::vector<byte>::swap;

public:
public:
using std::vector<byte>::vector;

public:
public:
/**
* @brief Construct a new Buffer object
*
Expand Down Expand Up @@ -238,8 +238,8 @@ class Buffer : public std::vector<byte> {
*/
Buffer operator-=(const Buffer &otherBuffer);

protected:
private:
protected:
private:
};

/**
Expand Down
18 changes: 11 additions & 7 deletions Flakkari/Network/IOMultiplexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ namespace Flakkari::Network {
PSELECT::PSELECT(FileDescriptor fileDescriptor, long int seconds, long int microseconds)
{
if (fileDescriptor == -1)
{
FLAKKARI_LOG_ERROR("Socket is -1");
throw std::runtime_error("Socket is -1");
}

FD_ZERO(&_fds);
_maxFd = fileDescriptor;
Expand All @@ -43,6 +40,7 @@ void PSELECT::addSocket(FileDescriptor socket)
throw std::runtime_error("Socket is -1");
if (socket > FD_SETSIZE)
throw std::runtime_error("Index out of range");

FD_SET(socket, &_fds);
if (socket > _maxFd)
_maxFd = socket;
Expand All @@ -55,6 +53,7 @@ void PSELECT::removeSocket(FileDescriptor socket)
throw std::runtime_error("Socket is -1");
if (socket > _maxFd)
throw std::runtime_error("Index out of range");

FD_CLR(socket, &_fds);
_sockets.erase(std::remove(_sockets.begin(), _sockets.end(), socket), _sockets.end());
_maxFd = *std::max_element(_sockets.begin(), _sockets.end());
Expand All @@ -78,6 +77,7 @@ bool PSELECT::isReady(FileDescriptor socket)
throw std::runtime_error("Socket is -1");
if (socket > _maxFd)
throw std::runtime_error("Index out of range");

return FD_ISSET(socket, &_fds);
}

Expand All @@ -90,13 +90,11 @@ bool PSELECT::skipableError() { return errno == EINTR || errno == EAGAIN || errn
PPOLL::PPOLL(FileDescriptor fileDescriptor, event_t events, long int seconds, long int microseconds)
{
if (fileDescriptor == -1)
{
FLAKKARI_LOG_ERROR("Socket is -1");
throw std::runtime_error("Socket is -1");
}

if (_pollfds.size() < std::size_t(fileDescriptor))
_pollfds.resize(fileDescriptor + 1);

_pollfds[fileDescriptor] = pollfd{fileDescriptor, events, 0};

_timeout.tv_sec = seconds;
Expand All @@ -115,6 +113,7 @@ void PPOLL::addSocket(FileDescriptor socket)
throw std::runtime_error("Socket is -1");
if (_pollfds.size() < std::size_t(socket))
_pollfds.resize(socket + 1);

_pollfds[socket] = pollfd{socket, POLLIN | POLLPRI, 0};
}

Expand All @@ -124,6 +123,7 @@ void PPOLL::addSocket(FileDescriptor socket, event_t events)
throw std::runtime_error("Socket is -1");
if (_pollfds.size() < std::size_t(socket))
_pollfds.resize(socket + 1);

_pollfds[socket] = pollfd{socket, events, 0};
}

Expand Down Expand Up @@ -152,6 +152,7 @@ pollfd &PPOLL::operator[](std::size_t index)
{
if (_pollfds.size() < index)
throw std::runtime_error("Index out of range");

return _pollfds[index];
}

Expand All @@ -161,13 +162,15 @@ pollfd &PPOLL::operator[](FileDescriptor socket)
throw std::runtime_error("Socket is -1");
if (_pollfds.size() < std::size_t(socket))
throw std::runtime_error("Index out of range");

return _pollfds[socket];
}

bool PPOLL::isReady(pollfd fd)
{
if (fd.fd == -1)
throw std::runtime_error("Socket is -1");

return fd.revents & (POLLIN | POLLPRI);
}

Expand All @@ -179,6 +182,7 @@ bool PPOLL::isReady(FileDescriptor socket)
throw std::runtime_error("Index out of range");
if (_pollfds[socket].revents & (POLLIN | POLLPRI))
return true;

return false;
}

Expand All @@ -191,7 +195,7 @@ bool PPOLL::skipableError() { return errno == EINTR || errno == EAGAIN || errno
WSA::WSA(FileDescriptor socket, int seconds, int microseconds)
{
if (socket == -1)
FLAKKARI_LOG_FATAL("Socket is -1");
throw std::runtime_error("Socket is -1");

_sockets.reserve(MAX_POLLFD);
_fdArray.reserve(MAX_POLLFD);
Expand Down
Loading

0 comments on commit 3e89d33

Please sign in to comment.