From a464fdfe82f14beb10346933f8ada42a90512ff0 Mon Sep 17 00:00:00 2001 From: HappyTanuki Date: Mon, 21 Apr 2025 21:52:33 +0900 Subject: [PATCH] =?UTF-8?q?=EC=9C=88=EB=8F=84=EC=9A=B0=EC=9A=A9=20?= =?UTF-8?q?=EC=BD=94=EB=93=9C=20=EC=99=84=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 4 ++- Server/CMakeLists.txt | 5 +++- Server/src/server.cpp | 6 ++-- impl/Socket/Error.cpp | 29 -------------------- impl/Socket/Log.cpp | 50 ++++++++++++++++++++++++++++++++++ impl/Socket/Socket_Init.cpp | 20 +++++++++++++- include/Common.hpp | 0 include/Socket/Error.hpp | 16 ----------- include/Socket/Log.hpp | 7 +++++ include/Socket/Socket_Init.hpp | 19 +++++++------ include/precomp.hpp | 14 ++++++++++ 11 files changed, 111 insertions(+), 59 deletions(-) delete mode 100644 impl/Socket/Error.cpp create mode 100644 impl/Socket/Log.cpp delete mode 100644 include/Common.hpp delete mode 100644 include/Socket/Error.hpp create mode 100644 include/Socket/Log.hpp create mode 100644 include/precomp.hpp diff --git a/.gitignore b/.gitignore index 07ed706..e80ae3b 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ -build/* \ No newline at end of file +build/* +out +.vs \ No newline at end of file diff --git a/Server/CMakeLists.txt b/Server/CMakeLists.txt index 69d62cb..b3dc68f 100644 --- a/Server/CMakeLists.txt +++ b/Server/CMakeLists.txt @@ -33,9 +33,12 @@ add_executable(${PROJECT_NAME} ) target_link_libraries(${PROJECT_NAME} PRIVATE Microsoft.GSL::GSL) target_link_libraries(${PROJECT_NAME} PRIVATE spdlog) +target_link_libraries(${PROJECT_NAME} PRIVATE ws2_32) target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../include ${CMAKE_CURRENT_SOURCE_DIR}/include ) -target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_20) \ 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/Server/src/server.cpp b/Server/src/server.cpp index c7fd739..5ae0dff 100644 --- a/Server/src/server.cpp +++ b/Server/src/server.cpp @@ -1,6 +1,8 @@ #include -#include "Socket/Error.hpp" +#include "Socket/Socket_Init.hpp" +#include "precomp.hpp" int main() { - std::cout << "Hello, world!" << std::endl; + struct Chattr::Socket_Init socket(AF_INET, SOCK_STREAM, 0); + std::cout << "Hello, world!" << socket.get() << std::endl; } \ No newline at end of file diff --git a/impl/Socket/Error.cpp b/impl/Socket/Error.cpp deleted file mode 100644 index d3a2089..0000000 --- a/impl/Socket/Error.cpp +++ /dev/null @@ -1,29 +0,0 @@ -#include "Socket/Error.hpp" -#include "spdlog/spdlog.h" - -namespace Chattr::error { - -void critical(gsl::czstring msg) { -#ifdef _WIN32 - //TODO 윈도우에서 구현할 것 -#elif __linux__ - gsl::czstring msgbuf = strerror(errno); -#else -#error "이 플랫폼은 지원되지 않습니다." -#endif - spdlog::critical(msgbuf); - std::exit(EXIT_FAILURE); -} - -void error(gsl::czstring msg) { -#ifdef _WIN32 - //TODO 윈도우에서 구현할 것 -#elif __linux__ - gsl::czstring msgbuf = strerror(errno); -#else -#error "이 플랫폼은 지원되지 않습니다." -#endif - spdlog::error(msgbuf); -} - -} \ No newline at end of file diff --git a/impl/Socket/Log.cpp b/impl/Socket/Log.cpp new file mode 100644 index 0000000..3a2b636 --- /dev/null +++ b/impl/Socket/Log.cpp @@ -0,0 +1,50 @@ +#include "Socket/Log.hpp" +#include "precomp.hpp" +#include "spdlog/spdlog.h" + +namespace Chattr::log { + +void critical(gsl::czstring msg) { +#ifdef _WIN32 + gsl::zstring msgbuf = nullptr; + FormatMessageA( + FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, + NULL, + WSAGetLastError(), + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + msgbuf, + 0, + NULL); + spdlog::critical("[{}] {}", msg, msgbuf); + LocalFree(msgbuf); +#elif __linux__ + gsl::czstring msgbuf = strerror(errno); + spdlog::critical("[{}] {}", msg, msgbuf); +#else +#error "이 플랫폼은 지원되지 않습니다." +#endif + std::exit(EXIT_FAILURE); +} + +void error(gsl::czstring msg) { +#ifdef _WIN32 + gsl::zstring msgbuf = nullptr; + FormatMessageA( + FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, + NULL, + WSAGetLastError(), + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + msgbuf, + 0, + NULL); + spdlog::error("[{}] {}", msg, msgbuf); + LocalFree(msgbuf); +#elif __linux__ + gsl::czstring msgbuf = strerror(errno); + spdlog::error("[{}] {}", msg, msgbuf); +#else +#error "이 플랫폼은 지원되지 않습니다." +#endif +} + +} \ No newline at end of file diff --git a/impl/Socket/Socket_Init.cpp b/impl/Socket/Socket_Init.cpp index f12a42e..701749e 100644 --- a/impl/Socket/Socket_Init.cpp +++ b/impl/Socket/Socket_Init.cpp @@ -1,11 +1,29 @@ #include "Socket/Socket_Init.hpp" +#include "Socket/Log.hpp" +#include "precomp.hpp" namespace Chattr { -Socket_Init::Socket_Init() { +Socket_Init::Socket_Init(std::int32_t domain, std::int32_t type, std::int32_t protocol) { +#ifdef _WIN32 + WSADATA wsa; + if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0) + log::critical("WSAStartup()"); +#endif + sock_ = socket(domain, type, protocol); + if (sock_ == INVALID_SOCKET) + log::critical("socket()"); } Socket_Init::~Socket_Init() { +#ifdef _WIN32 + closesocket(sock_); + WSACleanup(); +#elif __linux__ + close(sock_); +#endif } +SOCKET Socket_Init::get() const { return sock_; } + } \ No newline at end of file diff --git a/include/Common.hpp b/include/Common.hpp deleted file mode 100644 index e69de29..0000000 diff --git a/include/Socket/Error.hpp b/include/Socket/Error.hpp deleted file mode 100644 index 3d9c372..0000000 --- a/include/Socket/Error.hpp +++ /dev/null @@ -1,16 +0,0 @@ -#pragma once - -#ifdef _WIN32 -#include -#elif __linux__ -#include -#else -#error "이 플랫폼은 지원되지 않습니다." -#endif - -#include - -namespace Chattr::error { -void critical(gsl::czstring msg); -void error(gsl::czstring msg); -} \ No newline at end of file diff --git a/include/Socket/Log.hpp b/include/Socket/Log.hpp new file mode 100644 index 0000000..561ae20 --- /dev/null +++ b/include/Socket/Log.hpp @@ -0,0 +1,7 @@ +#pragma once +#include "precomp.hpp" + +namespace Chattr::log { +void critical(gsl::czstring msg); +void error(gsl::czstring msg); +} \ No newline at end of file diff --git a/include/Socket/Socket_Init.hpp b/include/Socket/Socket_Init.hpp index 91c9380..970a002 100644 --- a/include/Socket/Socket_Init.hpp +++ b/include/Socket/Socket_Init.hpp @@ -1,18 +1,19 @@ #pragma once - -#ifdef _WIN32 - -#elif __linux__ - -#else -#error "이 플랫폼은 지원되지 않습니다." -#endif +#include "precomp.hpp" namespace Chattr { struct Socket_Init { - Socket_Init(); +public: + Socket_Init(std::int32_t domain, std::int32_t type, std::int32_t protocol); ~Socket_Init(); + + Socket_Init(const Socket_Init&) = delete; + Socket_Init& operator=(const Socket_Init&) = delete; + + SOCKET get() const; +private: + SOCKET sock_; }; } \ No newline at end of file diff --git a/include/precomp.hpp b/include/precomp.hpp new file mode 100644 index 0000000..cda1ecd --- /dev/null +++ b/include/precomp.hpp @@ -0,0 +1,14 @@ +#pragma once + +#ifdef _WIN32 +#include +#include +#elif __linux__ +#include +#define SOCKET std::int32_t +#define INVALID_SOCKET -1 +#else +#error " ÷ ʽϴ." +#endif + +#include \ No newline at end of file