diff --git a/.vscode/settings.json b/.vscode/settings.json index 16ddb66..0f61f28 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,6 +1,7 @@ { "files.associations": { "assert": "cpp", - "zstring": "cpp" + "zstring": "cpp", + "ostream": "cpp" } } \ No newline at end of file diff --git a/Server/CMakeLists.txt b/Server/CMakeLists.txt index b3dc68f..a734f78 100644 --- a/Server/CMakeLists.txt +++ b/Server/CMakeLists.txt @@ -33,7 +33,10 @@ 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) + +if(WIN32) + target_link_libraries(${PROJECT_NAME} PRIVATE ws2_32) +endif() target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../include diff --git a/Server/src/server.cpp b/Server/src/server.cpp index 5ae0dff..e5cf14c 100644 --- a/Server/src/server.cpp +++ b/Server/src/server.cpp @@ -3,6 +3,6 @@ #include "precomp.hpp" int main() { - struct Chattr::Socket_Init socket(AF_INET, SOCK_STREAM, 0); - std::cout << "Hello, world!" << socket.get() << std::endl; + struct Chattr::Socket_Init socket; + std::cout << "Hello, world!" << socket << std::endl; } \ No newline at end of file diff --git a/impl/Socket/Log.cpp b/impl/Socket/Log.cpp index 3a2b636..a27f09c 100644 --- a/impl/Socket/Log.cpp +++ b/impl/Socket/Log.cpp @@ -4,47 +4,55 @@ namespace Chattr::log { -void critical(gsl::czstring msg) { +void critical(gsl::czstring msg, bool bShowDBGMessage) { + if (!bShowDBGMessage) + spdlog::critical("{}", msg); + else { #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); + 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); + gsl::czstring msgbuf = strerror(errno); + spdlog::critical("[{}] {}", msg, msgbuf); #else #error "이 플랫폼은 지원되지 않습니다." #endif + } std::exit(EXIT_FAILURE); } -void error(gsl::czstring msg) { +void error(gsl::czstring msg, bool bShowDBGMessage) { + if (!bShowDBGMessage) + spdlog::critical("{}", msg); + else { #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); + 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::error("[{}] {}", msg, msgbuf); + gsl::czstring msgbuf = strerror(errno); + spdlog::critical("[{}] {}", 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 701749e..d2d32d2 100644 --- a/impl/Socket/Socket_Init.cpp +++ b/impl/Socket/Socket_Init.cpp @@ -4,18 +4,28 @@ namespace Chattr { -Socket_Init::Socket_Init(std::int32_t domain, std::int32_t type, std::int32_t protocol) { +Socket_Init::Socket_Init(const SOCKET& socket) { + sock_ = socket; + valid_ = true; +} + +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()"); + log::critical("WSAStartup()", true); #endif sock_ = socket(domain, type, protocol); if (sock_ == INVALID_SOCKET) - log::critical("socket()"); + log::critical("socket()", true); + + valid_ = true; } Socket_Init::~Socket_Init() { + if (!valid_) + return; #ifdef _WIN32 closesocket(sock_); WSACleanup(); @@ -24,6 +34,11 @@ Socket_Init::~Socket_Init() { #endif } -SOCKET Socket_Init::get() const { return sock_; } +Socket_Init::operator SOCKET() const { + if (valid_) + return sock_; + log::critical("No valid socket created.", false); + return INVALID_SOCKET; +}; } \ No newline at end of file diff --git a/include/Socket/Log.hpp b/include/Socket/Log.hpp index 561ae20..f5479d2 100644 --- a/include/Socket/Log.hpp +++ b/include/Socket/Log.hpp @@ -2,6 +2,6 @@ #include "precomp.hpp" namespace Chattr::log { -void critical(gsl::czstring msg); -void error(gsl::czstring msg); +void critical(gsl::czstring msg, bool bShowDBGMessage); +void error(gsl::czstring msg, bool bShowDBGMessage); } \ No newline at end of file diff --git a/include/Socket/Socket_Init.hpp b/include/Socket/Socket_Init.hpp index 970a002..3e85919 100644 --- a/include/Socket/Socket_Init.hpp +++ b/include/Socket/Socket_Init.hpp @@ -5,14 +5,17 @@ namespace Chattr { struct Socket_Init { public: + Socket_Init() {} Socket_Init(std::int32_t domain, std::int32_t type, std::int32_t protocol); ~Socket_Init(); + Socket_Init(const SOCKET& socket); Socket_Init(const Socket_Init&) = delete; Socket_Init& operator=(const Socket_Init&) = delete; - SOCKET get() const; + operator SOCKET() const; private: + bool valid_ = false; SOCKET sock_; }; diff --git a/include/precomp.hpp b/include/precomp.hpp index cda1ecd..68e6b4b 100644 --- a/include/precomp.hpp +++ b/include/precomp.hpp @@ -4,11 +4,13 @@ #include #include #elif __linux__ +#include +#include #include #define SOCKET std::int32_t #define INVALID_SOCKET -1 #else -#error " ÷ ʽϴ." +#error "이 플랫폼은 지원되지 않습니다." #endif #include \ No newline at end of file