Skip to content

Commit

Permalink
Add enet in server
Browse files Browse the repository at this point in the history
  • Loading branch information
Saverio976 committed Nov 12, 2024
1 parent 27edc07 commit d13bded
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 0 deletions.
2 changes: 2 additions & 0 deletions server/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ target_sources(
${CMAKE_CURRENT_SOURCE_DIR}/main.cpp
${CMAKE_CURRENT_SOURCE_DIR}/Logger.cpp
)

add_subdirectory(utils)
14 changes: 14 additions & 0 deletions server/src/utils/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
cmake_minimum_required(VERSION 3.15)

target_include_directories(
${PROJECT_NAME}
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}
)

target_sources(
${PROJECT_NAME}
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/INetwork.cpp
${CMAKE_CURRENT_SOURCE_DIR}/NetworkInternal.cpp
)
8 changes: 8 additions & 0 deletions server/src/utils/INetwork.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include "NetworkInternal.hpp"
#include "INetwork.hpp"

INetwork &INetwork::GetInstance(std::string url)
{
static Network network(url);
return network;
}
13 changes: 13 additions & 0 deletions server/src/utils/INetwork.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once

#include <string>

class INetwork {
public:
static INetwork &GetInstance(std::string url);
virtual bool init() = 0;
virtual void update() = 0;
virtual bool send(std::string text) = 0;
virtual bool hasPacket() = 0;
virtual std::string receive() = 0;
};
84 changes: 84 additions & 0 deletions server/src/utils/NetworkInternal.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#define ENET_IMPLEMENTATION
#include "enet.h"
#include "Logger.hpp"
#include "NetworkInternal.hpp"

Network::Network(std::string url): _url(url)
{
}

Network::~Network()
{
if (_initialized) {
enet_deinitialize();
}
if (_client != nullptr) {
enet_host_destroy(_client);
}
}

bool Network::init()
{
if (enet_initialize() != 0) {
Logger::fatal("NETWORK: could not initialize the client");
return false;
}
_client = enet_host_create(NULL, 1, 2, 0, 0);
if (_client == nullptr) {
Logger::fatal("NETWORK: could not create the client");
return false;
}
ENetAddress address;
enet_address_set_host(&address, SERVER_URL);
address.port = SERVER_PORT;
enet_host_connect(_client, &address, 2, 0);
_initialized = true;
return true;
}

void Network::update()
{
ENetEvent event;
while (enet_host_service(_client, &event, 0) > 0) {
switch (event.type) {
case ENET_EVENT_TYPE_CONNECT:
Logger::debug("NETWORK: connection succesful");
event.peer->data = nullptr;
_server = event.peer;
break;
case ENET_EVENT_TYPE_RECEIVE:
Logger::debug("NETWORK: receive data");
_packets.push(std::string(reinterpret_cast<char*>(event.packet->data)));
enet_packet_destroy(event.packet);
break;
case ENET_EVENT_TYPE_DISCONNECT:
Logger::warn("NETWORK: connection closed");
break;
default:
Logger::error("NETWORK: unknow event type: " + std::to_string(event.type));
break;
}
}
}

bool Network::send(std::string text)
{
ENetPacket *packet = enet_packet_create(text.data(), text.length() + 1, ENET_PACKET_FLAG_RELIABLE);
if (packet == nullptr) {
return false;
}
enet_peer_send(_server, 0, packet);
return true;
}

bool Network::hasPacket()
{
return !_packets.empty();
}

std::string Network::receive()
{
std::string packet = _packets.front();
_packets.pop();
return packet;
}
30 changes: 30 additions & 0 deletions server/src/utils/NetworkInternal.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#pragma once

#include <string>
#include <vector>
#include <queue>

#include "enet.h"
#include "INetwork.hpp"

class Network : public INetwork {
public:
Network(std::string url);
~Network();

Network &operator=(const Network &other) = delete;
Network &operator=(const Network other) = delete;

bool init() override;
void update() override;
bool send(std::string text) override;
bool hasPacket() override;
std::string receive() override;

private:
bool _initialized = false;
std::string _url;
ENetHost *_client = nullptr;
ENetPeer *_server = nullptr;
std::queue<std::string> _packets;
};

0 comments on commit d13bded

Please sign in to comment.