init
This commit is contained in:
commit
5962730120
11 changed files with 354 additions and 0 deletions
34
src/main.cpp
Normal file
34
src/main.cpp
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
#include "network.h"
|
||||
#include "riot.h"
|
||||
#include <boost/asio.hpp>
|
||||
#include <boost/asio/awaitable.hpp>
|
||||
#include <boost/asio/co_spawn.hpp>
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
|
||||
int main() {
|
||||
uint32_t thread_cnt = std::thread::hardware_concurrency();
|
||||
if (thread_cnt == 0) {
|
||||
thread_cnt = 1;
|
||||
}
|
||||
|
||||
boost::asio::io_context ctx;
|
||||
|
||||
const std::string address = "0.0.0.0";
|
||||
const uint16_t port = 8080;
|
||||
|
||||
try {
|
||||
|
||||
auto service = std::make_shared<NetworkService>(
|
||||
ctx, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port),
|
||||
thread_cnt);
|
||||
service->start();
|
||||
|
||||
ctx.run();
|
||||
|
||||
} catch (const std::exception &e) {
|
||||
std::cerr << "Error: " << e.what() << "\n";
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
68
src/network.cpp
Normal file
68
src/network.cpp
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
#include "network.h"
|
||||
#include <boost/asio.hpp>
|
||||
#include <boost/asio/strand.hpp>
|
||||
#include <boost/beast.hpp>
|
||||
#include <boost/beast/core.hpp>
|
||||
#include <boost/beast/http.hpp>
|
||||
#include <iostream>
|
||||
|
||||
namespace beast = boost::beast;
|
||||
namespace asio = boost::asio;
|
||||
|
||||
NetworkService::NetworkService(asio::io_context &ctx,
|
||||
asio::ip::tcp::endpoint endpoint, int thread_cnt)
|
||||
: ctx_(ctx), endpoint_(endpoint), work_guard_(ctx.get_executor()) {
|
||||
threads_.reserve(thread_cnt);
|
||||
for (int i = 0; i < thread_cnt; ++i) {
|
||||
threads_.emplace_back([ctx = &ctx_]() { ctx->run(); });
|
||||
}
|
||||
}
|
||||
|
||||
void NetworkService::start() {
|
||||
asio::co_spawn(
|
||||
asio::make_strand(ctx_),
|
||||
[self = shared_from_this()]() { return self->listen(); }, asio::detached);
|
||||
}
|
||||
|
||||
asio::awaitable<void> NetworkService::listen() {
|
||||
auto executor = co_await asio::this_coro::executor;
|
||||
asio::ip::tcp::acceptor acceptor(executor, endpoint_);
|
||||
|
||||
std::cout << "Server listening on port: " << endpoint_.port() << std::endl;
|
||||
|
||||
while (true) {
|
||||
asio::ip::tcp::socket socket =
|
||||
co_await acceptor.async_accept(asio::use_awaitable);
|
||||
|
||||
asio::co_spawn(
|
||||
asio::make_strand(ctx_),
|
||||
[self = shared_from_this(),
|
||||
socket = std::move(socket)]() mutable -> asio::awaitable<void> {
|
||||
co_await self->session(std::move(socket));
|
||||
},
|
||||
asio::detached);
|
||||
}
|
||||
}
|
||||
|
||||
asio::awaitable<void> NetworkService::session(asio::ip::tcp::socket socket) {
|
||||
try {
|
||||
beast::flat_buffer buffer;
|
||||
|
||||
beast::http::request<beast::http::string_body> req;
|
||||
co_await beast::http::async_read(socket, buffer, req, asio::use_awaitable);
|
||||
|
||||
beast::http::response<beast::http::string_body> res{beast::http::status::ok,
|
||||
req.version()};
|
||||
res.set(beast::http::field::content_type, "text/plain");
|
||||
res.keep_alive(req.keep_alive());
|
||||
res.body() = "Bye, World!";
|
||||
res.prepare_payload();
|
||||
|
||||
co_await beast::http::async_write(socket, res, asio::use_awaitable);
|
||||
|
||||
} catch (std::exception &e) {
|
||||
std::cerr << "Session error: " << e.what() << "\n";
|
||||
}
|
||||
|
||||
co_return;
|
||||
}
|
||||
76
src/riot.cpp
Normal file
76
src/riot.cpp
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
#include "riot.h"
|
||||
#include <boost/beast/core.hpp>
|
||||
#include <boost/beast/http.hpp>
|
||||
#include <boost/beast/ssl.hpp>
|
||||
#include <boost/beast/version.hpp>
|
||||
#include <format>
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
|
||||
namespace beast = boost::beast;
|
||||
namespace http = beast::http;
|
||||
|
||||
namespace riot {
|
||||
|
||||
std::string Client::divisionToString(Division division) const {
|
||||
switch (division) {
|
||||
case Division::I:
|
||||
return "I";
|
||||
case Division::II:
|
||||
return "II";
|
||||
case Division::III:
|
||||
return "III";
|
||||
case Division::IV:
|
||||
return "IV";
|
||||
default:
|
||||
throw std::invalid_argument("invalid division");
|
||||
}
|
||||
}
|
||||
|
||||
std::string Client::tierToString(Tier tier) const {
|
||||
switch (tier) {
|
||||
case Tier::BRONZE:
|
||||
return "BRONZE";
|
||||
case Tier::SILVER:
|
||||
return "SILVER";
|
||||
case Tier::GOLD:
|
||||
return "GOLD";
|
||||
case Tier::PLATINUM:
|
||||
return "PLATINUM";
|
||||
case Tier::DIAMOND:
|
||||
return "DIAMOND";
|
||||
default:
|
||||
throw std::invalid_argument("invalid tier");
|
||||
}
|
||||
}
|
||||
|
||||
std::string Client::regionToString(Region region) const {
|
||||
switch (region) {
|
||||
case Region::KR:
|
||||
return "kr";
|
||||
default:
|
||||
throw std::invalid_argument("invalid region");
|
||||
}
|
||||
}
|
||||
|
||||
Client::Client(boost::asio::io_context &ctx, std::string key, Region region)
|
||||
: ctx_(ctx), api_key_(std::move(key)), region_(region) {}
|
||||
|
||||
asio::awaitable<std::vector<LeagueEntry>>
|
||||
Client::GetIdsInTier(Division division, Tier tier, uint8_t page) const {
|
||||
auto div_str = divisionToString(division);
|
||||
auto tier_str = tierToString(tier);
|
||||
auto region_str = regionToString(region_);
|
||||
|
||||
auto request_url =
|
||||
std::format("https://{}.{}/lol/league/v4/entries/RANKED_SOLO_5x5/{}/"
|
||||
"{}?api_key={}&page={}",
|
||||
region_str, BASE_URL, tier_str, div_str, api_key_, page);
|
||||
|
||||
std::cout << "[DEBUG] Fetching from: " << request_url << std::endl;
|
||||
|
||||
// TODO: Implement JSON parsing
|
||||
co_return std::vector<LeagueEntry>{};
|
||||
}
|
||||
|
||||
} // namespace riot
|
||||
Loading…
Add table
Add a link
Reference in a new issue