guy_breaking_chains_watching_sunrise.png
This commit is contained in:
parent
376378ee2b
commit
b4c7068c30
3 changed files with 96 additions and 87 deletions
2
.clangd
Normal file
2
.clangd
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
CompileFlags:
|
||||||
|
Add: [-std=c++23]
|
||||||
|
|
@ -11,7 +11,7 @@ And also my first serious C++ project.
|
||||||
- [X] Serialisation and saving
|
- [X] Serialisation and saving
|
||||||
- [X] FUCKING NETWORKING (probably minimal RESP)
|
- [X] FUCKING NETWORKING (probably minimal RESP)
|
||||||
- [X] Proper startup/shutdown w/ load & save
|
- [X] Proper startup/shutdown w/ load & save
|
||||||
- [ ] Multithreading
|
- [X] Multithreading
|
||||||
- [ ] More types
|
- [ ] More types
|
||||||
- [ ] Async
|
- [X] Async
|
||||||
- [ ] Backflip off the Rio-Antirrio bridge
|
- [ ] Backflip off the Rio-Antirrio bridge
|
||||||
|
|
|
||||||
177
main.cpp
177
main.cpp
|
|
@ -1,4 +1,7 @@
|
||||||
#include <asm-generic/socket.h>
|
#include <boost/asio/detached.hpp>
|
||||||
|
#include <boost/asio/executor_work_guard.hpp>
|
||||||
|
#include <boost/asio/io_context.hpp>
|
||||||
|
#include <boost/asio/use_awaitable.hpp>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
@ -12,6 +15,7 @@
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
#include <boost/asio.hpp>
|
||||||
|
|
||||||
using Db = std::vector<std::vector<std::string>>;
|
using Db = std::vector<std::vector<std::string>>;
|
||||||
Db database;
|
Db database;
|
||||||
|
|
@ -63,18 +67,18 @@ std::vector<std::string> parse_resp_request(char* request) {
|
||||||
return split_request;
|
return split_request;
|
||||||
}
|
}
|
||||||
|
|
||||||
void safe_exit(int signum) {
|
namespace asio = boost::asio;
|
||||||
std::cout << "[W] Exiting..." << std::endl;
|
using asio::ip::tcp;
|
||||||
save_database("db.json");
|
using asio::use_awaitable;
|
||||||
exit(signum);
|
boost::asio::io_context io_context;
|
||||||
}
|
|
||||||
|
|
||||||
void handle_client(int client_socket) {
|
asio::awaitable<void> handle_client(tcp::socket client_socket) {
|
||||||
while (true) {
|
char buffer[4096];
|
||||||
char buffer[4096];
|
|
||||||
int bytes_received = recv(client_socket, buffer, 4096, 0);
|
try {
|
||||||
|
while (true) {
|
||||||
|
std::size_t bytes_received = co_await client_socket.async_read_some(asio::buffer(buffer), use_awaitable);
|
||||||
|
|
||||||
if (bytes_received > 0) {
|
|
||||||
buffer[bytes_received] = '\0';
|
buffer[bytes_received] = '\0';
|
||||||
std::vector<std::string> parsed = parse_resp_request(buffer);
|
std::vector<std::string> parsed = parse_resp_request(buffer);
|
||||||
|
|
||||||
|
|
@ -84,97 +88,100 @@ void handle_client(int client_socket) {
|
||||||
for (const auto& inner_vec: database) { // Iterate through DB and find the required item
|
for (const auto& inner_vec: database) { // Iterate through DB and find the required item
|
||||||
if (!inner_vec.empty() && inner_vec[0] == parsed[1]) {
|
if (!inner_vec.empty() && inner_vec[0] == parsed[1]) {
|
||||||
int length = inner_vec[1].length();
|
int length = inner_vec[1].length();
|
||||||
std::string return_string = std::format("${}\r\n{}\r\n", length, inner_vec[1]); // Serialise to RESP
|
std::string return_string = std::format("${}\r\n{}\r\n", length, inner_vec[1]); // Serialise to RESP
|
||||||
std::cout << "[d] Sending: " << return_string << std::endl;
|
std::cout << "[d] Sending: " << return_string << std::endl;
|
||||||
send(client_socket, return_string.data(), return_string.length(), 0);
|
co_await asio::async_write(client_socket, asio::buffer(return_string.data(), return_string.length()), use_awaitable);
|
||||||
found = true;
|
found = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (!found) {
|
|
||||||
send(client_socket, "$-1\r\n", strlen("$-1\r\n"), 0); // Not found
|
|
||||||
}
|
|
||||||
} else if (parsed[0] == "SET") {
|
|
||||||
bool found = false; // Avoids SEGFAULT
|
|
||||||
|
|
||||||
for (auto& inner_vec: database) {
|
|
||||||
if (!inner_vec.empty() && inner_vec[0] == parsed[1]) {
|
|
||||||
inner_vec[1] = parsed[2];
|
|
||||||
send(client_socket, "+OK\r\n", strlen("+OK\r\n"), 0);
|
|
||||||
found = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!found) {
|
if (!found) {
|
||||||
std::vector<std::string> new_kv = { parsed[1], parsed[2] };
|
co_await asio::async_write(client_socket, asio::buffer("$-1\r\n", strlen("$-1\r\n")), use_awaitable); // Not found
|
||||||
database.push_back(new_kv);
|
|
||||||
send(client_socket, "+OK\r\n", strlen("+OK\r\n"), 0);
|
|
||||||
found = true;
|
|
||||||
}
|
}
|
||||||
|
} else if (parsed[0] == "SET") {
|
||||||
|
bool found = false; // Avoids SEGFAULT
|
||||||
|
|
||||||
|
for (auto& inner_vec: database) {
|
||||||
|
if (!inner_vec.empty() && inner_vec[0] == parsed[1]) {
|
||||||
|
inner_vec[1] = parsed[2];
|
||||||
|
co_await asio::async_write(client_socket, asio::buffer("+OK\r\n", strlen("+OK\r\n")), use_awaitable);
|
||||||
|
found = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!found) {
|
||||||
|
std::vector<std::string> new_kv = { parsed[1], parsed[2] };
|
||||||
|
database.push_back(new_kv);
|
||||||
|
co_await asio::async_write(client_socket, asio::buffer("+OK\r\n", strlen("+OK\r\n")), use_awaitable);
|
||||||
|
found = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
co_await asio::async_write(client_socket, asio::buffer("+OK\r\n", strlen("+OK\r\n")), use_awaitable); // Temporary catch-all (for more advanced handshake)
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
send(client_socket, "+OK\r\n", strlen("+OK\r\n"), 0); // Temporary catch-all (for more advanced handshake)
|
|
||||||
}
|
}
|
||||||
} else {
|
} catch (const std::exception& e) {
|
||||||
close(client_socket);
|
std::cout << "[i] Client session ened/error" << e.what() << std::endl;
|
||||||
std::cout << "[i] Client disconnected." << std::endl;
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
asio::awaitable<void> tcp_server_task(tcp::acceptor& acceptor) {
|
||||||
|
std::cout << "[i] Server listening on port 6379" << std::endl;
|
||||||
|
while (true) {
|
||||||
|
tcp::socket socket = co_await acceptor.async_accept(use_awaitable);
|
||||||
|
|
||||||
|
asio::co_spawn(
|
||||||
|
io_context,
|
||||||
|
handle_client(std::move(socket)),
|
||||||
|
asio::detached
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
asio::awaitable<void> shutdown_handler(asio::io_context& io_context, tcp::acceptor& acceptor, std::shared_ptr<asio::executor_work_guard<asio::io_context::executor_type>> work_guard) {
|
||||||
|
asio::signal_set signals(io_context, SIGINT, SIGTERM);
|
||||||
|
co_await signals.async_wait(use_awaitable);
|
||||||
|
std::cout << "[i] Stopping blueis server..." << std::endl;
|
||||||
|
acceptor.cancel();
|
||||||
|
acceptor.close();
|
||||||
|
|
||||||
|
work_guard->reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
std::cout << "[i] Starting blueis server..." << std::endl;
|
|
||||||
|
|
||||||
signal(SIGINT, safe_exit);
|
|
||||||
|
|
||||||
load_database("db.json");
|
load_database("db.json");
|
||||||
int listening_socket = socket(AF_INET, SOCK_STREAM, 0 );
|
const int N_THREADS = std::thread::hardware_concurrency();
|
||||||
if (listening_socket < 0) {
|
std::vector<std::thread> threads;
|
||||||
std::cerr << "[E] Failed to create socket." << std::endl;
|
tcp::endpoint endpoint(tcp::v4(), 6379);
|
||||||
return 1;
|
tcp::acceptor acceptor(io_context, endpoint);
|
||||||
}
|
auto work_guard = std::make_shared<asio::executor_work_guard<asio::io_context::executor_type>>(asio::make_work_guard(io_context));
|
||||||
|
|
||||||
int optval = 1;
|
|
||||||
if (setsockopt(listening_socket, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)) < 0) {
|
|
||||||
std::cerr << "[W] Failed to use SO_REUSEADDR. Continuing." << std::endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
sockaddr_in server_address;
|
asio::co_spawn(
|
||||||
server_address.sin_family = AF_INET;
|
io_context,
|
||||||
server_address.sin_port = htons(6379);
|
tcp_server_task(acceptor),
|
||||||
server_address.sin_addr.s_addr = INADDR_ANY;
|
asio::detached
|
||||||
|
);
|
||||||
|
|
||||||
if (bind(listening_socket, (sockaddr*)&server_address, sizeof(server_address)) < 0) {
|
asio::co_spawn(
|
||||||
std::cerr << "[E] Failed to bind to port 6379. Check for any other running redis or blueis instances." << std::endl;
|
io_context,
|
||||||
close(listening_socket);
|
shutdown_handler(io_context, acceptor, work_guard),
|
||||||
return 1;
|
asio::detached
|
||||||
|
);
|
||||||
|
|
||||||
|
for (int i = 0; i < N_THREADS - 1; i++) {
|
||||||
|
threads.emplace_back([&io_context] {
|
||||||
|
io_context.run();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (listen(listening_socket, 5) < 0) {
|
std::cout << "[i] Starting blueis server on 6379..." << std::endl;
|
||||||
std::cerr << "[E] Failed to listen on socket." << std::endl;
|
io_context.run();
|
||||||
close(listening_socket);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::cout << "[i] Server listening on port 6379..." << std::endl;
|
for (auto& t: threads) {
|
||||||
|
|
||||||
sockaddr_in client_address;
|
|
||||||
socklen_t client_size = sizeof(client_address);
|
|
||||||
|
|
||||||
while (true) {
|
|
||||||
int client_socket = accept(listening_socket, (sockaddr*)&client_address, &client_size);
|
|
||||||
if (client_socket < 0) {
|
|
||||||
std::cerr << "[E] Failed to accept connection." << std::endl;
|
|
||||||
close(listening_socket);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
std::thread t(handle_client, client_socket); // Temporarily using the shit method of one thread per client despite the DoS potential until I can get async down.
|
|
||||||
if (t.joinable()) {
|
if (t.joinable()) {
|
||||||
t.detach();
|
t.join();
|
||||||
}
|
}
|
||||||
std::cout << "[i] Client connected." << std::endl;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue