자세 조정
This commit is contained in:
3
.vscode/settings.json
vendored
3
.vscode/settings.json
vendored
@@ -1,6 +1,7 @@
|
||||
{
|
||||
"files.associations": {
|
||||
"assert": "cpp",
|
||||
"zstring": "cpp"
|
||||
"zstring": "cpp",
|
||||
"ostream": "cpp"
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
@@ -4,7 +4,10 @@
|
||||
|
||||
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(
|
||||
@@ -23,10 +26,14 @@ void critical(gsl::czstring msg) {
|
||||
#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(
|
||||
@@ -37,14 +44,15 @@ void error(gsl::czstring msg) {
|
||||
msgbuf,
|
||||
0,
|
||||
NULL);
|
||||
spdlog::error("[{}] {}", msg, msgbuf);
|
||||
spdlog::critical("[{}] {}", msg, msgbuf);
|
||||
LocalFree(msgbuf);
|
||||
#elif __linux__
|
||||
gsl::czstring msgbuf = strerror(errno);
|
||||
spdlog::error("[{}] {}", msg, msgbuf);
|
||||
spdlog::critical("[{}] {}", msg, msgbuf);
|
||||
#else
|
||||
#error "이 플랫폼은 지원되지 않습니다."
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
};
|
||||
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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_;
|
||||
};
|
||||
|
||||
|
||||
@@ -4,11 +4,13 @@
|
||||
#include <winsock2.h>
|
||||
#include <windows.h>
|
||||
#elif __linux__
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <errno.h>
|
||||
#define SOCKET std::int32_t
|
||||
#define INVALID_SOCKET -1
|
||||
#else
|
||||
#error "<EFBFBD><EFBFBD> <20>÷<EFBFBD><C3B7><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20>ʽ<EFBFBD><CABD>ϴ<EFBFBD>."
|
||||
#error "이 플랫폼은 지원되지 않습니다."
|
||||
#endif
|
||||
|
||||
#include <gsl/gsl>
|
||||
Reference in New Issue
Block a user