Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🌍 Wrapping asio lib to create a custom network library (EPI-40) #10

Open
wants to merge 14 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
cmake_minimum_required(VERSION 3.5)
set(CMAKE_CXX_STANDARD 20)

project(R-Type-Reborn
LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)


include_directories(includes)
link_directories(libs)
link_libraries(StellarForge)

add_executable(R-Type-Reborn ${R-Type-Reborn_SOURCES})
find_package(asio REQUIRED)

add_subdirectory(src/network)

add_executable(R-Type-Reborn-server tests/server/main.cpp)
target_link_libraries(R-Type-Reborn-server PRIVATE rtype::network)

add_executable(R-Type-Reborn-client tests/client/main.cpp)
target_link_libraries(R-Type-Reborn-client PRIVATE rtype::network)


add_subdirectory(src)
include_directories(src/network)
#add_subdirectory(tests)
6 changes: 6 additions & 0 deletions conanfile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[requires]
asio/1.31.0

[generators]
CMakeDeps
CMakeToolchain
80 changes: 80 additions & 0 deletions documentation/conan_installation_guide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@

# Installing `Conan`

This guide provides step-by-step instructions to install `Conan` on Windows, Linux, and macOS, including how to configure your system for use.

## Windows / Windows server

1. **Open PowerShell as Administrator**.
2. **Install Conan using `pip`**:
- First, ensure that you have Python and `pip` installed. If not, download and install Python from the [official website](https://www.python.org/downloads/).
- Run the following command to install Conan:
```bash
pip install conan
```

3. **Verify installation**:
After installing, verify Conan is installed by running:
```bash
conan --version
```

4. **Add Conan to the system path** (optional):
If you encounter issues with the `conan` command, you may need to add Python's Scripts folder to your system path:
- Open **Start** and search for **Environment Variables**.
- In the System Properties window, click on **Environment Variables**.
- Under **System variables**, find and select the **Path** variable, then click **Edit**.
- Click **New** and add the path to Python’s Scripts folder, typically `C:\Users\YourUsername\AppData\Local\Programs\Python\PythonXX\Scripts`.
- Click **OK** to close all windows.

## Linux

1. **Open a terminal**.
2. **Install Conan using `pip`**:
First, make sure you have Python and `pip` installed. Then, install Conan:
```bash
sudo apt update
sudo apt install python3-pip
pip3 install conan
```

3. **Add Conan to the system path** (if necessary):
- If `conan` is not recognized, ensure `~/.local/bin` is in your PATH by adding the following line to your `.bashrc` or `.zshrc` file:
```bash
export PATH="$HOME/.local/bin:$PATH"
```
- Apply the changes:
```bash
source ~/.bashrc
```

4. **Verify installation**:
Restart your terminal and run:
```bash
conan --version
```

## macOS

1. **Open a terminal**.
2. **Install Homebrew** (if not already installed):
- Homebrew is a package manager for macOS. You can install it with:
```bash
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
```

3. **Install Conan using Homebrew**:
- Once Homebrew is installed, install Conan with:
```bash
brew install conan
```

4. **Verify installation**:
Restart your terminal and run:
```bash
conan --version
```

---

Follow these instructions to ensure a smooth installation of `Conan` on your system.
106 changes: 106 additions & 0 deletions documentation/vcpkg_installation_guide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@

# Installing `vcpkg` (outdated guide conan is used now)

This guide provides step-by-step instructions to install `vcpkg` on Windows, Linux, and macOS, including how to add it to your system path.

## Windows / Windows server

1. **Open PowerShell as Administrator**.
2. **Clone the `vcpkg` repository**:
```bash
git clone https://github.com/microsoft/vcpkg.git
cd vcpkg
.\bootstrap-vcpkg.bat
```

3. **Add `vcpkg` to the system path**:
- Open **Start** and search for **Environment Variables**.
- In the System Properties window, click on **Environment Variables**.
- Under **System variables**, find and select the **Path** variable, then click **Edit**.
- Click **New** and add the full path to the `vcpkg` executable, e.g., `C:\path\to\vcpkg`.
- Click **OK** to close all windows.

<img src="https://cdn.discordapp.com/attachments/539130489798393858/1289630502382342174/image.png?ex=66f9857f&is=66f833ff&hm=024db2fbffb9cfa002e2b95080f0e224ba9e029ef974dbbac3313d3fb7a31962&" width="800px" />
<br>
<img src="https://cdn.discordapp.com/attachments/539130489798393858/1289630725884084324/image.png?ex=66f985b5&is=66f83435&hm=b35ad6528588045461e476441abfd3bb57750922471fd3ff6d347e01de6adf08&" width="400px" />

4. **Verify installation**:
After adding `vcpkg` to the path, restart your terminal or PowerShell and run:
```bash
vcpkg --version
```

## Linux

1. **Open a terminal**.
2. **Install essential build tools**:
```bash
sudo apt-get update
sudo apt-get install build-essential curl zip unzip
```

3. **Clone the `vcpkg` repository**:
```bash
git clone https://github.com/microsoft/vcpkg.git
cd vcpkg
./bootstrap-vcpkg.sh
```

4. **Add `vcpkg` to the system path**:
- Open your `.bashrc` or `.zshrc` file:
```bash
nano ~/.bashrc
```
- Add the following line at the end:
```bash
export PATH="$HOME/path/to/vcpkg:$PATH"
```
- Save and exit the editor, then run:
```bash
source ~/.bashrc
```

5. **Verify installation**:
Restart your terminal and run:
```bash
vcpkg --version
```

## macOS

1. **Open a terminal**.
2. **Install Xcode Command Line Tools**:
```bash
xcode-select --install
```

3. **Clone the `vcpkg` repository**:
```bash
git clone https://github.com/microsoft/vcpkg.git
cd vcpkg
./bootstrap-vcpkg.sh
```

4. **Add `vcpkg` to the system path**:
- Open your `.bash_profile` or `.zshrc` file:
```bash
nano ~/.bash_profile
```
- Add the following line:
```bash
export PATH="$HOME/path/to/vcpkg:$PATH"
```
- Save and exit, then run:
```bash
source ~/.bash_profile
```

5. **Verify installation**:
Restart your terminal and run:
```bash
vcpkg --version
```

---

Follow these instructions to ensure a smooth installation of `vcpkg` on your system.
5 changes: 0 additions & 5 deletions src/CMakeLists.txt

This file was deleted.

14 changes: 0 additions & 14 deletions src/main.cpp

This file was deleted.

16 changes: 16 additions & 0 deletions src/network/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
cmake_minimum_required(VERSION 3.5)
add_library(Rtype-network STATIC)
add_library(rtype::network ALIAS Rtype-network)
target_link_libraries(Rtype-network
PUBLIC
asio::asio
)

target_sources(Rtype-network
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/server/Server.hpp
${CMAKE_CURRENT_SOURCE_DIR}/client/Client.hpp
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/client/Client.cpp
${CMAKE_CURRENT_SOURCE_DIR}/server/Server.cpp
)
83 changes: 83 additions & 0 deletions src/network/client/Client.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
** EPITECH PROJECT, 2024
** R-Type-Reborn
** File description:
** No file there , just an epitech header example .
** You can even have multiple lines if you want !
*/
#include "Client.hpp"
#include <iostream>
#include <utility>
#include <asio/error_code.hpp>
#include <asio/ip/udp.hpp>

Network::Client::Client(std::string host, const unsigned short udp_port, unsigned short tcp_port)
: _host(std::move(host)), _UDP_PORT(udp_port), _TCP_PORT(tcp_port), _udp_socket(_io_context), _tcp_socket(_io_context)
{
_id = -1;
}

Network::Client::~Client()
{
_io_context.stop();
}

void Network::Client::connect(callback function)
{
// TCP connection init
asio::ip::tcp::resolver resolver(_io_context);
asio::ip::tcp::resolver::results_type endpoints = resolver.resolve(_host, std::to_string(_TCP_PORT));

asio::error_code error;
asio::connect(_tcp_socket, endpoints, error);
if (error) {
std::cerr << "Error: " << error.message() << std::endl;
return;
}

asio::read(_tcp_socket, asio::buffer(&_id, sizeof(_id)), error);
if (error) {
std::cerr << "Error: " << error.message() << std::endl;
return;
} else {
std::cout << "Connected to server with id " << static_cast<int>(_id) << std::endl;
}

// UDP connection init
_udp_socket.open(asio::ip::udp::v4());
_endpoint = asio::ip::udp::endpoint(asio::ip::address::from_string(_host), _UDP_PORT);
_callback = std::move(function);
receive();
}

void Network::Client::send(const std::vector<uint8_t> &data)
{
_udp_socket.async_send_to(asio::buffer(data), _endpoint,
[](const asio::error_code &error, std::size_t bytes_transferred) {
if (error) {
std::cerr << "Error: " << error.message() << std::endl;
}
});
std::cout << "Data sent" << std::endl;
}

void Network::Client::receive() {
_udp_socket.async_receive_from(asio::buffer(_recv_buffer), _endpoint,
[this](const asio::error_code &error, std::size_t bytes_read)
{
if (!error) {
if(_callback) {
_callback(_recv_buffer, _endpoint);
}
receive();
} else {
std::cerr << "Error: " << error.message() << std::endl;
}
});
}

void Network::Client::disconnect()
{
_tcp_socket.close();
_udp_socket.close();
}
Loading