split into src and lib and socket changes

* libanthracite files are now all in lib/
* anthracite-bin + anthracite-api-bin files are now all in src/
* socket split into header and source properly
This commit is contained in:
Nicholas Orlowsky 2025-02-04 11:37:46 -05:00
parent fba87f3fbb
commit 71be773d49
Signed by: nickorlow
GPG key ID: 838827D8C4611687
24 changed files with 49 additions and 121 deletions

View file

@ -37,9 +37,8 @@ add_custom_target(run
WORKING_DIRECTORY ${CMAKE_PROJECT_DIR}
)
FILE(GLOB SOURCES src/**/*.cpp build_supp/version.cpp)
add_library(anthracite src/anthracite.cpp ${SOURCES})
FILE(GLOB LIB_SOURCES lib/*.cpp lib/**/*.cpp build_supp/version.cpp)
add_library(anthracite ${LIB_SOURCES})
add_dependencies(anthracite build-version)
add_executable(anthracite-bin src/file_main.cpp)

View file

@ -1,4 +1,4 @@
echo "#include <string>" > version.cpp
echo "#include \"../src/version.hpp\"" >> version.cpp
echo "#include \"../lib/version.hpp\"" >> version.cpp
echo "const std::string ANTHRACITE_VERSION_STRING = \"$(cat version.txt)\";" >> version.cpp
echo "const std::string ANTHRACITE_FULL_VERSION_STRING = \"Anthracite/$(cat version.txt)\";" >> version.cpp

View file

View file

@ -1,5 +1,3 @@
#pragma once
#include "./log.hpp"
namespace anthracite::log {

View file

@ -7,24 +7,12 @@
#include <sys/time.h>
#include <unistd.h>
#include <vector>
#include "./socket.hpp"
namespace anthracite::socket {
constexpr int MAX_QUEUE_LENGTH = 100;
class anthracite_socket {
private:
int server_socket;
int client_socket {};
std::string client_ip;
struct sockaddr_in client_addr {};
socklen_t client_addr_len {};
static constexpr struct timeval timeout_tv {
.tv_sec = 5, .tv_usec = 0
};
public:
anthracite_socket(int port, int max_queue = MAX_QUEUE_LENGTH)
anthracite_socket::anthracite_socket(int port, int max_queue)
: server_socket(::socket(AF_INET, SOCK_STREAM, 0))
, client_ip("")
{
@ -40,7 +28,7 @@ public:
listen(server_socket, max_queue);
}
void wait_for_conn()
void anthracite_socket::wait_for_conn()
{
client_ip = "";
client_socket = accept(server_socket, reinterpret_cast<struct sockaddr*>(&client_addr), &client_addr_len);
@ -49,18 +37,18 @@ public:
client_ip = std::string(ip_str.data());
}
const std::string& get_client_ip()
const std::string& anthracite_socket::get_client_ip()
{
return client_ip;
}
void close_conn()
void anthracite_socket::close_conn()
{
close(client_socket);
client_socket = -1;
}
void send_message(std::string& msg)
void anthracite_socket::send_message(std::string& msg)
{
if (client_socket == -1) {
return;
@ -68,7 +56,7 @@ public:
send(client_socket, &msg[0], msg.length(), 0);
}
std::string recv_message(int buffer_size)
std::string anthracite_socket::recv_message(int buffer_size)
{
if (client_socket == -1) {
return "";
@ -85,6 +73,5 @@ public:
response[buffer_size] = '\0';
return { response.data() };
}
};
};

34
lib/socket/socket.hpp Normal file
View file

@ -0,0 +1,34 @@
#include <arpa/inet.h>
#include <malloc.h>
#include <netinet/in.h>
#include <string>
#include <sys/socket.h>
#include <sys/time.h>
#include <unistd.h>
namespace anthracite::socket {
class anthracite_socket {
static const int MAX_QUEUE_LENGTH = 100;
private:
int server_socket;
int client_socket {};
std::string client_ip;
struct sockaddr_in client_addr {};
socklen_t client_addr_len {};
static constexpr struct timeval timeout_tv {
.tv_sec = 5, .tv_usec = 0
};
public:
anthracite_socket(int port, int max_queue = MAX_QUEUE_LENGTH);
void wait_for_conn();
const std::string& get_client_ip();
void close_conn();
void send_message(std::string& msg);
std::string recv_message(int buffer_size);
};
};

View file

@ -1,6 +1,6 @@
#include "./anthracite.hpp"
#include "backends/backend.hpp"
#include "http/constants.hpp"
#include "../lib/anthracite.hpp"
#include "../lib/backends/backend.hpp"
#include "../lib/http/constants.hpp"
#include <memory>
#include <optional>
#include <sstream>

View file

@ -1,5 +1,5 @@
#include "./anthracite.hpp"
#include "backends/file_backend.hpp"
#include "../lib/anthracite.hpp"
#include "../lib/backends/file_backend.hpp"
using namespace anthracite;

View file

@ -1,90 +0,0 @@
#include <arpa/inet.h>
#include <array>
#include <malloc.h>
#include <netinet/in.h>
#include <string>
#include <sys/socket.h>
#include <sys/time.h>
#include <unistd.h>
#include <vector>
namespace anthracite::socket {
constexpr int MAX_QUEUE_LENGTH = 100;
class anthracite_socket {
private:
int server_socket;
int client_socket {};
std::string client_ip;
struct sockaddr_in client_addr {};
socklen_t client_addr_len {};
static constexpr struct timeval timeout_tv {
.tv_sec = 5, .tv_usec = 0
};
public:
anthracite_socket(int port, int max_queue = MAX_QUEUE_LENGTH)
: server_socket(::socket(AF_INET, SOCK_STREAM, 0))
, client_ip("")
{
struct sockaddr_in address {};
address.sin_family = AF_INET;
address.sin_port = htons(port);
address.sin_addr.s_addr = INADDR_ANY;
int reuse_opt = 1;
setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR, &reuse_opt, sizeof(reuse_opt));
bind(server_socket, reinterpret_cast<struct sockaddr*>(&address), sizeof(address));
listen(server_socket, max_queue);
}
void wait_for_conn()
{
client_ip = "";
client_socket = accept(server_socket, reinterpret_cast<struct sockaddr*>(&client_addr), &client_addr_len);
std::array<char, INET_ADDRSTRLEN> ip_str { 0 };
inet_ntop(AF_INET, &client_addr.sin_addr, ip_str.data(), INET_ADDRSTRLEN);
client_ip = std::string(ip_str.data());
}
const std::string& get_client_ip()
{
return client_ip;
}
void close_conn()
{
close(client_socket);
client_socket = -1;
}
void send_message(std::string& msg)
{
if (client_socket == -1) {
return;
}
send(client_socket, &msg[0], msg.length(), 0);
}
std::string recv_message(int buffer_size)
{
if (client_socket == -1) {
return "";
}
setsockopt(client_socket, SOL_SOCKET, SO_RCVTIMEO, &timeout_tv, sizeof timeout_tv);
std::vector<char> response(buffer_size + 1);
ssize_t result = recv(client_socket, response.data(), buffer_size + 1, 0);
if (result < 1) {
return "";
}
response[buffer_size] = '\0';
return { response.data() };
}
};
};