-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
27edc07
commit d13bded
Showing
6 changed files
with
151 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |