From 82b6612418908654a61b7947550b796ce4ed57d6 Mon Sep 17 00:00:00 2001 From: littleblack111 Date: Sun, 15 Jun 2025 14:19:35 +0800 Subject: fix(cmake): linking --- CMakeLists.txt | 24 ++++++++++++++++-------- include/libsock/server/client.hpp | 5 ++--- include/libsock/server/server.hpp | 7 +++---- include/libsock/types.hpp | 16 ++++++++++++++++ src/misc/FileDescriptor.cpp | 2 ++ src/misc/FileDescriptor.hpp | 5 +++++ src/server/client.cpp | 5 +++-- src/server/clients.cpp | 4 ++++ src/server/server.cpp | 7 ++++--- 9 files changed, 55 insertions(+), 20 deletions(-) create mode 100644 include/libsock/types.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 727108f..5937aa0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,7 @@ cmake_minimum_required(VERSION 3.19) project( - libsock + sock VERSION 1.0.0 DESCRIPTION "Minimal C++ socket library") @@ -36,17 +36,23 @@ else() message(STATUS "Configuring libsock in Release") endif() -file(GLOB_RECURSE SRCFILES CONFIGURE_DEPENDS "src/*.cpp" "include/*.hpp") +# Separate source files and headers +file(GLOB_RECURSE PRIVATE_SOURCES CONFIGURE_DEPENDS "src/*.cpp") +file(GLOB_RECURSE PRIVATE_HEADERS CONFIGURE_DEPENDS "src/*.hpp") file(GLOB_RECURSE PUBLIC_HEADERS CONFIGURE_DEPENDS "include/*.hpp") +# Create list of all source files (private sources + public headers) +set(ALL_SOURCES ${PRIVATE_SOURCES} ${PUBLIC_HEADERS}) + find_package(PkgConfig REQUIRED) -add_library(libsock SHARED ${SRCFILES}) +add_library(sock SHARED ${ALL_SOURCES}) target_include_directories( - libsock + sock PUBLIC "./include" - PRIVATE "./src") -set_target_properties(libsock PROPERTIES VERSION ${PROJECT_VERSION} SOVERSION 1) + PRIVATE "./src" + PRIVATE "./src/misc") # Add this so FileDescriptor.hpp can be found +set_target_properties(sock PROPERTIES VERSION ${PROJECT_VERSION} SOVERSION 1) # tests add_custom_target(tests) # @@ -61,7 +67,9 @@ set_target_properties(libsock PROPERTIES VERSION ${PROJECT_VERSION} SOVERSION 1) # libsock_connection "connection") add_dependencies(tests libsock_connection) # # Installation -install(TARGETS libsock) -install(DIRECTORY "include/libsock" DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) +install(TARGETS sock) +install(DIRECTORY "include/libsock" + DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} + PATTERN "misc" EXCLUDE) install(FILES ${CMAKE_BINARY_DIR}/libsock.pc DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig) diff --git a/include/libsock/server/client.hpp b/include/libsock/server/client.hpp index b86b440..ec5c6bc 100644 --- a/include/libsock/server/client.hpp +++ b/include/libsock/server/client.hpp @@ -1,7 +1,6 @@ #pragma once -#include "misc/FileDescriptor.hpp" -#include "misc/memory.hpp" +#include "../types.hpp" #include #include #include @@ -51,7 +50,7 @@ class Client { private: WP self; - SP m_sockfd; + SP m_sockfd; sockaddr_in m_addr; socklen_t m_addrLen = sizeof(m_addr); std::string m_name; diff --git a/include/libsock/server/server.hpp b/include/libsock/server/server.hpp index c97c945..d831fd8 100644 --- a/include/libsock/server/server.hpp +++ b/include/libsock/server/server.hpp @@ -1,6 +1,5 @@ #pragma once -#include "misc/FileDescriptor.hpp" -#include "misc/memory.hpp" +#include "../types.hpp" #include #include @@ -11,10 +10,10 @@ class Server { public: Server(uint16_t port, bool reuseaddr = true, bool keepalive = false); ~Server(); - SP getSocket() const; + SP getSocket() const; private: - SP m_sockfd; + SP m_sockfd; sockaddr_in m_addr; uint16_t m_port; diff --git a/include/libsock/types.hpp b/include/libsock/types.hpp new file mode 100644 index 0000000..f8116ab --- /dev/null +++ b/include/libsock/types.hpp @@ -0,0 +1,16 @@ +#pragma once + +#include + +template +using SP = std::shared_ptr; + +template +using WP = std::weak_ptr; + +template +using UP = std::unique_ptr; + +namespace LibSock { +class CFileDescriptor; +} diff --git a/src/misc/FileDescriptor.cpp b/src/misc/FileDescriptor.cpp index 92505f5..59fa688 100644 --- a/src/misc/FileDescriptor.cpp +++ b/src/misc/FileDescriptor.cpp @@ -5,6 +5,8 @@ #include #include +using namespace LibSock; + CFileDescriptor::CFileDescriptor(int const fd) : m_fd(fd) {} diff --git a/src/misc/FileDescriptor.hpp b/src/misc/FileDescriptor.hpp index 75aeb73..dd9f8fe 100644 --- a/src/misc/FileDescriptor.hpp +++ b/src/misc/FileDescriptor.hpp @@ -1,6 +1,9 @@ #pragma once #include + +namespace LibSock { + class CFileDescriptor { public: CFileDescriptor() = default; @@ -33,3 +36,5 @@ class CFileDescriptor { private: int m_fd = -1; }; + +} // namespace LibSock diff --git a/src/server/client.cpp b/src/server/client.cpp index f99cb97..9efad18 100644 --- a/src/server/client.cpp +++ b/src/server/client.cpp @@ -1,5 +1,6 @@ #include "libsock/server/client.hpp" -#include "../misc/memory.hpp" +#include "libsock/types.hpp" +#include "misc/FileDescriptor.hpp" // #include "chatManager.hpp" #include "libsock/server/server.hpp" // #include "sessionManager.hpp" @@ -14,7 +15,7 @@ using namespace LibSock::Client; Client::Client(bool track, bool oneShot) : m_track(track), m_oneShot(oneShot) { - m_sockfd = std::make_shared( + m_sockfd = std::make_shared( accept(LibSock::Server::pServer->getSocket()->get(), reinterpret_cast(&m_addr), &m_addrLen)); // if this is in the init list, it will run before diff --git a/src/server/clients.cpp b/src/server/clients.cpp index 0327ea3..e42579c 100644 --- a/src/server/clients.cpp +++ b/src/server/clients.cpp @@ -99,6 +99,10 @@ std::vector> Clients::getClients() const { return m_vClients | std::views::transform([](const auto &s) { return s.second; }) | std::ranges::to(); } +std::vector Clients::getDatas() const { + return m_vDatas; +} + void Clients::kick(WP clientWeak, const bool kill, const std::string &reason) { auto client = clientWeak.lock(); if (!client) diff --git a/src/server/server.cpp b/src/server/server.cpp index 0692a74..2e07809 100644 --- a/src/server/server.cpp +++ b/src/server/server.cpp @@ -1,5 +1,6 @@ #include "libsock/server/server.hpp" -#include "misc/memory.hpp" +#include "libsock/types.hpp" +#include "misc/FileDescriptor.hpp" #include #include #include @@ -8,7 +9,7 @@ using namespace LibSock::Server; Server::Server(uint16_t port, bool reuseaddr, bool keepalive) : m_sockfd( - std::make_shared(socket(AF_INET, SOCK_STREAM, 0))) { + std::make_shared(socket(AF_INET, SOCK_STREAM, 0))) { std::lock_guard lk(m_mutex); if (!m_sockfd->isValid() || m_sockfd->get() < 0) throw std::runtime_error("Failed to create socket"); @@ -35,4 +36,4 @@ Server::Server(uint16_t port, bool reuseaddr, bool keepalive) Server::~Server() { m_sockfd.reset(); } -std::shared_ptr Server::getSocket() const { return m_sockfd; } +std::shared_ptr Server::getSocket() const { return m_sockfd; } -- cgit v1.2.3