From 995a27a15e3c9cd1567492f646f7b087ce4dafc0 Mon Sep 17 00:00:00 2001 From: HappyTanuki Date: Wed, 23 Apr 2025 04:51:28 +0900 Subject: [PATCH] =?UTF-8?q?=EC=95=8C=EB=A0=A4=EC=A7=84=20=EB=B2=84?= =?UTF-8?q?=EA=B7=B8=20=ED=94=BD=EC=8A=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Chattering.log | 4 ++ Client/CMakeLists.txt | 50 +++++++++++++++++-- Client/src/client.cpp | 16 +++++- Server/CMakeLists.txt | 1 + .../include}/Utils/ConfigManager.hpp | 0 Server/src/Chattering.log | 7 --- {impl => Server/src}/Utils/ConfigManager.cpp | 0 Server/src/config.json | 8 --- Server/src/server.cpp | 21 ++++---- impl/Socket/config.json => config.json | 0 impl/Socket/Chattering.log | 1 - impl/Socket/Socket.cpp | 2 +- impl/Socket/TCPSocket.cpp | 13 ++++- include/Socket/Address.hpp | 10 ++-- include/Socket/TCPSocket.hpp | 2 + include/precomp.hpp | 1 + 16 files changed, 98 insertions(+), 38 deletions(-) create mode 100644 Chattering.log rename {include => Server/include}/Utils/ConfigManager.hpp (100%) delete mode 100644 Server/src/Chattering.log rename {impl => Server/src}/Utils/ConfigManager.cpp (100%) delete mode 100644 Server/src/config.json rename impl/Socket/config.json => config.json (100%) delete mode 100644 impl/Socket/Chattering.log diff --git a/Chattering.log b/Chattering.log new file mode 100644 index 0000000..876d024 --- /dev/null +++ b/Chattering.log @@ -0,0 +1,4 @@ +[2025-04-23 02:12:26.679] [Chattering Logger] [info] Waiting for connection... +[2025-04-23 02:12:32.642] [Chattering Logger] [critical] [socket()] No such file or directory +[2025-04-23 02:24:30.033] [Chattering Logger] [info] Waiting for connection... +[2025-04-23 02:58:01.648] [Chattering Logger] [info] Waiting for connection... diff --git a/Client/CMakeLists.txt b/Client/CMakeLists.txt index b9a24fa..ba01770 100644 --- a/Client/CMakeLists.txt +++ b/Client/CMakeLists.txt @@ -2,12 +2,56 @@ cmake_minimum_required(VERSION 3.5) set(PROJECT_NAME "Client") set(CMAKE_BUILD_TYPE Debug) +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) -project(${PROJECT_NAME}) +project(${PROJECT_NAME} CXX) -add_executable(${PROJECT_NAME} src/client.cpp) +include(FetchContent) + +FetchContent_Declare( + spdlog + GIT_REPOSITORY "https://github.com/gabime/spdlog.git" + GIT_TAG "v1.11.0" + GIT_SHALLOW ON +) +FetchContent_MakeAvailable(spdlog) + +FetchContent_Declare( + GSL + GIT_REPOSITORY "https://github.com/microsoft/GSL.git" + GIT_TAG "v4.2.0" + GIT_SHALLOW ON +) +FetchContent_MakeAvailable(GSL) + +FetchContent_Declare( + JSONCPP + GIT_REPOSITORY "https://github.com/open-source-parsers/jsoncpp.git" + GIT_TAG "1.9.6" + GIT_SHALLOW ON +) +FetchContent_MakeAvailable(JSONCPP) + +file(GLOB_RECURSE additional_sources CONFIGURE_DEPENDS + "${CMAKE_CURRENT_SOURCE_DIR}/../impl/*.cpp" +) + +add_executable(${PROJECT_NAME} + src/client.cpp + ${additional_sources} +) +target_link_libraries(${PROJECT_NAME} PRIVATE Microsoft.GSL::GSL) +target_link_libraries(${PROJECT_NAME} PRIVATE spdlog) +target_link_libraries(${PROJECT_NAME} PRIVATE jsoncpp_lib) + +if(WIN32) + target_link_libraries(${PROJECT_NAME} PRIVATE ws2_32) +endif() target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../include ${CMAKE_CURRENT_SOURCE_DIR}/include -) \ No newline at end of file +) +target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_20) + +target_precompile_headers(${PROJECT_NAME} PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/../include/precomp.hpp") \ No newline at end of file diff --git a/Client/src/client.cpp b/Client/src/client.cpp index 10daf1f..ba344fc 100644 --- a/Client/src/client.cpp +++ b/Client/src/client.cpp @@ -1,5 +1,17 @@ -#include +#include "Socket/TCPSocket.hpp" +#include "Socket/Address.hpp" +#include "Socket/Log.hpp" +#include "precomp.hpp" int main() { - std::cout << "Hello, world!" << std::endl; + Chattr::TCPSocket sock; + sock.init(AF_INET6); + + Chattr::Address serveraddr; + serveraddr.addr_in6.sin6_family = AF_INET6; + inet_pton(AF_INET6, "fd8a:5f:3adb:774d:dfae:983f:2e7a:ffba", &serveraddr.addr_in6.sin6_addr); + serveraddr.addr_in6.sin6_port = htons(9010); + serveraddr.length = sizeof(sockaddr_in6); + + sock.connect(serveraddr); } \ No newline at end of file diff --git a/Server/CMakeLists.txt b/Server/CMakeLists.txt index f076a1a..1f0e24b 100644 --- a/Server/CMakeLists.txt +++ b/Server/CMakeLists.txt @@ -34,6 +34,7 @@ FetchContent_MakeAvailable(JSONCPP) file(GLOB_RECURSE additional_sources CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/../impl/*.cpp" + "${CMAKE_CURRENT_SOURCE_DIR}/src/Utils/*.cpp" ) add_executable(${PROJECT_NAME} diff --git a/include/Utils/ConfigManager.hpp b/Server/include/Utils/ConfigManager.hpp similarity index 100% rename from include/Utils/ConfigManager.hpp rename to Server/include/Utils/ConfigManager.hpp diff --git a/Server/src/Chattering.log b/Server/src/Chattering.log deleted file mode 100644 index b3dafae..0000000 --- a/Server/src/Chattering.log +++ /dev/null @@ -1,7 +0,0 @@ -[2025-04-22 20:30:06.838] [Chattering Logger] [critical] [bind()] Invalid argument -[2025-04-22 22:26:22.198] [Chattering Logger] [critical] [bind()] Socket operation on non-socket -[2025-04-22 23:38:21.098] [Chattering Logger] [critical] [listen()] Operation not supported -[2025-04-22 23:39:00.008] [Chattering Logger] [critical] [listen()] Operation not supported -[2025-04-22 23:40:54.696] [Chattering Logger] [critical] [listen()] Operation not supported -[2025-04-22 23:41:11.774] [Chattering Logger] [critical] [listen()] Operation not supported -[2025-04-22 23:41:21.669] [Chattering Logger] [critical] [listen()] Operation not supported diff --git a/impl/Utils/ConfigManager.cpp b/Server/src/Utils/ConfigManager.cpp similarity index 100% rename from impl/Utils/ConfigManager.cpp rename to Server/src/Utils/ConfigManager.cpp diff --git a/Server/src/config.json b/Server/src/config.json deleted file mode 100644 index 9b55171..0000000 --- a/Server/src/config.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "IP Version" : 4, - "Listen Port" : 9010, - "LogLevel" : 1, - "LogfileCount" : 5, - "LogfileName" : "Chattering.log", - "LogfileSize" : 4294967295 -} \ No newline at end of file diff --git a/Server/src/server.cpp b/Server/src/server.cpp index a73251e..dfb5d4d 100644 --- a/Server/src/server.cpp +++ b/Server/src/server.cpp @@ -7,27 +7,26 @@ int main() { auto config = Chattr::ConfigManager::load(); Chattr::log::setDefaultLogger(config.logLevel, config.logFileName, config.logfileSize, config.logfileCount); - struct Chattr::TCPSocket sock; if (config.ipVersion == 4) { sock.init(AF_INET); - struct sockaddr_in serveraddr; - serveraddr.sin_family = AF_INET; - serveraddr.sin_addr.s_addr = htonl(INADDR_ANY); - serveraddr.sin_port = htons(config.listenPort); + struct Chattr::Address serveraddr; + serveraddr.addr_in.sin_family = AF_INET; + serveraddr.addr_in.sin_addr.s_addr = htonl(INADDR_ANY); + serveraddr.addr_in.sin_port = htons(config.listenPort); - sock.bind(&serveraddr); + sock.bind(&serveraddr.addr_in); } else { sock.init(AF_INET6); - struct sockaddr_in6 serveraddr; - serveraddr.sin6_family = AF_INET6; - serveraddr.sin6_addr = in6addr_any; - serveraddr.sin6_port = htons(config.listenPort); + struct Chattr::Address serveraddr; + serveraddr.addr_in6.sin6_family = AF_INET6; + serveraddr.addr_in6.sin6_addr = in6addr_any; + serveraddr.addr_in6.sin6_port = htons(config.listenPort); - sock.bind(&serveraddr); + sock.bind(&serveraddr.addr_in6); } sock.listen(SOMAXCONN); struct Chattr::TCPSocket clientSock; diff --git a/impl/Socket/config.json b/config.json similarity index 100% rename from impl/Socket/config.json rename to config.json diff --git a/impl/Socket/Chattering.log b/impl/Socket/Chattering.log deleted file mode 100644 index 59f9b8a..0000000 --- a/impl/Socket/Chattering.log +++ /dev/null @@ -1 +0,0 @@ -[2025-04-22 23:42:27.045] [Chattering Logger] [critical] [listen()] Operation not supported diff --git a/impl/Socket/Socket.cpp b/impl/Socket/Socket.cpp index e199e42..25ced43 100644 --- a/impl/Socket/Socket.cpp +++ b/impl/Socket/Socket.cpp @@ -53,7 +53,7 @@ Socket::operator SOCKET() const { } void Socket::move(const SOCKET __sock) { - if (sock_ == INVALID_SOCKET) + if (__sock == INVALID_SOCKET) log::critical("socket()"); if (valid_) { diff --git a/impl/Socket/TCPSocket.cpp b/impl/Socket/TCPSocket.cpp index 8721a8c..39cd75d 100644 --- a/impl/Socket/TCPSocket.cpp +++ b/impl/Socket/TCPSocket.cpp @@ -16,8 +16,17 @@ void TCPSocket::listen(int __n) { } void TCPSocket::accept(TCPSocket& newSock, Address& __addr) { - newSock.move(::accept(sock_, &__addr.sockaddr, &__addr.length)); + newSock.move(::accept(sock_, &__addr.addr, &__addr.length)); if (newSock == INVALID_SOCKET) log::critical("accept()"); } -} \ No newline at end of file + +void TCPSocket::connect(Chattr::Address serveraddr) { + int retVal = ::connect(sock_, (struct sockaddr *)&serveraddr, serveraddr.length); + if (retVal == INVALID_SOCKET) + log::critical("connect()"); +} + +} + + diff --git a/include/Socket/Address.hpp b/include/Socket/Address.hpp index a9102d3..ee35196 100644 --- a/include/Socket/Address.hpp +++ b/include/Socket/Address.hpp @@ -4,10 +4,14 @@ namespace Chattr { struct Address { + Address() { + memset(&addr_in6, 0, sizeof(addr_in6)); + } + union { - struct sockaddr sockaddr; - struct sockaddr_in sockaddr_in; - struct sockaddr_in6 sockaddr_in6; + struct sockaddr addr; + struct sockaddr_in addr_in; + struct sockaddr_in6 addr_in6; }; socklen_t length; }; diff --git a/include/Socket/TCPSocket.hpp b/include/Socket/TCPSocket.hpp index 9349b9b..2a5ec71 100644 --- a/include/Socket/TCPSocket.hpp +++ b/include/Socket/TCPSocket.hpp @@ -6,10 +6,12 @@ namespace Chattr { class TCPSocket : public Socket { public: + using Socket::Socket; using Socket::init; void init(int domain); void listen(int __n); void accept(TCPSocket& newSock, Address& addr); + void connect(Chattr::Address serveraddr); }; } \ No newline at end of file diff --git a/include/precomp.hpp b/include/precomp.hpp index ea6bbd6..fedb21d 100644 --- a/include/precomp.hpp +++ b/include/precomp.hpp @@ -8,6 +8,7 @@ #elif __linux__ #include #include +#include #include #include #define SOCKET int