일단 서버구현 끝

This commit is contained in:
2025-04-22 23:46:08 +09:00
parent 3a3cdc130a
commit 89cac9c54f
15 changed files with 254 additions and 90 deletions

10
.vscode/settings.json vendored
View File

@@ -1,8 +1,5 @@
{ {
"files.associations": { "files.associations": {
"sstream": "cpp",
"iosfwd": "cpp",
"ostream": "cpp",
"cctype": "cpp", "cctype": "cpp",
"cmath": "cpp", "cmath": "cpp",
"cstdarg": "cpp", "cstdarg": "cpp",
@@ -45,12 +42,15 @@
"format": "cpp", "format": "cpp",
"initializer_list": "cpp", "initializer_list": "cpp",
"iomanip": "cpp", "iomanip": "cpp",
"iosfwd": "cpp",
"istream": "cpp", "istream": "cpp",
"limits": "cpp", "limits": "cpp",
"mutex": "cpp", "mutex": "cpp",
"new": "cpp", "new": "cpp",
"ostream": "cpp",
"semaphore": "cpp", "semaphore": "cpp",
"span": "cpp", "span": "cpp",
"sstream": "cpp",
"stdexcept": "cpp", "stdexcept": "cpp",
"stop_token": "cpp", "stop_token": "cpp",
"streambuf": "cpp", "streambuf": "cpp",
@@ -70,7 +70,7 @@
"print": "cpp", "print": "cpp",
"queue": "cpp", "queue": "cpp",
"stack": "cpp", "stack": "cpp",
"assert": "cpp", "fstream": "cpp",
"zstring": "cpp" "regex": "cpp"
} }
} }

View File

@@ -11,7 +11,7 @@ include(FetchContent)
FetchContent_Declare( FetchContent_Declare(
spdlog spdlog
GIT_REPOSITORY "https://github.com/gabime/spdlog.git" GIT_REPOSITORY "https://github.com/gabime/spdlog.git"
GIT_TAG "v1.15.2" GIT_TAG "v1.11.0"
GIT_SHALLOW ON GIT_SHALLOW ON
) )
FetchContent_MakeAvailable(spdlog) FetchContent_MakeAvailable(spdlog)

View File

@@ -0,0 +1,7 @@
[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

8
Server/src/config.json Normal file
View File

@@ -0,0 +1,8 @@
{
"IP Version" : 6,
"Listen Port" : 9010,
"LogLevel" : 1,
"LogfileCount" : 5,
"LogfileName" : "Chattering.log",
"LogfileSize" : 4294967295
}

View File

@@ -1,68 +1,37 @@
#include "Socket/Socket.hpp" #include "Socket/TCPSocket.hpp"
#include "Socket/Log.hpp" #include "Socket/Log.hpp"
#include "Utils/ConfigManager.hpp"
#include "precomp.hpp" #include "precomp.hpp"
#include <fstream>
#include <json/json.h>
#include <iostream>
#include <string>
int main() { int main() {
Json::Value configJsonRoot; auto config = Chattr::ConfigManager::load();
std::uint32_t ipVersion = 0; Chattr::log::setDefaultLogger(config.logLevel, config.logFileName, config.logfileSize, config.logfileCount);
std::uint32_t listenPort = 0;
spdlog::level::level_enum logLevel;
gsl::czstring logFileName;
std::uint32_t logfileSize;
std::uint32_t logfileCount;
std::ifstream config("config.json", std::ifstream::binary);
if (!config.is_open()) { struct Chattr::TCPSocket sock;
std::ofstream defaultConfig("config.json", std::ios::out); if (config.ipVersion == 4) {
configJsonRoot["IP Version"] = 6; sock.init(AF_INET);
configJsonRoot["Listen Port"] = 9010;
configJsonRoot["LogLevel"] = 1; struct sockaddr_in serveraddr;
configJsonRoot["LogfileName"] = "Chattering.log"; serveraddr.sin_family = AF_INET;
configJsonRoot["LogfileSize"] = UINT32_MAX; serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
configJsonRoot["LogfileCount"] = 5; serveraddr.sin_port = htons(config.listenPort);
defaultConfig << configJsonRoot;
defaultConfig.close(); sock.bind(&serveraddr);
spdlog::critical("\"config.json\" is missing. Default configuration has been written.");
std::exit(EXIT_FAILURE);
} }
try { else {
config >> configJsonRoot; sock.init(AF_INET6);
ipVersion = configJsonRoot["IP Version"].asInt(); struct sockaddr_in6 serveraddr;
if (ipVersion != 4 && ipVersion != 6) serveraddr.sin6_family = AF_INET6;
throw std::runtime_error("Invalid IP Version."); serveraddr.sin6_addr = in6addr_any;
serveraddr.sin6_port = htons(config.listenPort);
listenPort = configJsonRoot["Listen Port"].asInt(); sock.bind(&serveraddr);
if (listenPort < 0 || listenPort > 65535)
throw std::runtime_error("Invalid listen port.");
int ll_ = configJsonRoot["LogLevel"].asInt();
if (ll_ >= 0 && ll_ < spdlog::level::n_levels)
logLevel = (spdlog::level::level_enum)ll_;
else
throw std::runtime_error("Invalid log level.");
logFileName = configJsonRoot["LogfileName"].asCString();
logfileSize = configJsonRoot["LogfileSize"].asUInt();
logfileCount = configJsonRoot["LogfileCount"].asUInt();
} }
catch (Json::RuntimeError e) { sock.listen(SOMAXCONN);
spdlog::critical(std::string(std::string("[Json Error: ]") + e.what()).c_str()); struct Chattr::TCPSocket clientSock;
std::exit(EXIT_FAILURE); struct Chattr::Address clientAddr;
} spdlog::info("Waiting for connection...");
sock.accept(clientSock, clientAddr);
Chattr::log::setDefaultLogger(logLevel, logFileName, logfileSize, logfileCount);
struct Chattr::Socket sock(AF_INET, SOCK_DGRAM, 0);
struct sockaddr_in serveraddr = {};
serveraddr.sin_family = AF_INET;
serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
serveraddr.sin_port = htons(9010);
sock.bind(sock, &serveraddr);
} }

View File

@@ -0,0 +1 @@
[2025-04-22 23:42:27.045] [Chattering Logger] [critical] [listen()] Operation not supported

View File

@@ -10,11 +10,7 @@ Socket::Socket(int domain, int type, int protocol) {
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0) if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
log::critical("WSAStartup()"); log::critical("WSAStartup()");
#endif #endif
sock_ = ::socket(domain, type, protocol); init(domain, type, protocol);
if (sock_ == INVALID_SOCKET)
log::critical("socket()");
valid_ = true;
} }
Socket::~Socket() { Socket::~Socket() {
@@ -28,37 +24,76 @@ Socket::~Socket() {
#endif #endif
} }
void Socket::init(int domain, int type, int protocol) {
if (domain == AF_INET)
bindAddr.length = sizeof(sockaddr_in);
else if (domain == AF_INET6)
bindAddr.length = sizeof(sockaddr_in6);
sock_ = ::socket(domain, type, protocol);
if (sock_ == INVALID_SOCKET)
log::critical("socket()");
valid_ = true;
}
Socket::operator SOCKET() const { Socket::operator SOCKET() const {
if (valid_) if (valid_)
return sock_; return sock_;
spdlog::critical("No valid socket created."); spdlog::critical("No valid socket created.");
return INVALID_SOCKET; return INVALID_SOCKET;
};
void Socket::bind(int __fd, const sockaddr *__addr) {
bind(__fd, (struct sockaddr *)__addr, sizeof(*__addr));
} }
void Socket::bind(int __fd, const sockaddr *__addr, socklen_t __len) { void Socket::move(const SOCKET __sock) {
int retVal = ::bind(__fd, __addr, __len); if (sock_ == INVALID_SOCKET)
log::critical("socket()");
if (valid_) {
#ifdef _WIN32
closesocket(sock_);
WSACleanup();
#elif __linux__
::close(sock_);
#endif
}
else {
#ifdef _WIN32
WSADATA wsa;
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
log::critical("WSAStartup()");
#endif
}
sock_ = __sock;
valid_ = true;
};
void Socket::bind(const sockaddr *__addr) {
bind((struct sockaddr *)__addr, sizeof(*__addr));
}
void Socket::bind(const sockaddr *__addr, socklen_t __len) {
bindAddr.length = __len;
std::memcpy(&bindAddr, __addr, __len);
int retVal = ::bind(sock_, __addr, __len);
if (retVal == INVALID_SOCKET) if (retVal == INVALID_SOCKET)
log::critical("bind()"); log::critical("bind()");
} }
void Socket::bind(int __fd, const sockaddr_in *__addr) { void Socket::bind(const sockaddr_in *__addr) {
bind(__fd, (struct sockaddr *)__addr, sizeof(*__addr)); bind((struct sockaddr *)__addr, sizeof(*__addr));
} }
void Socket::bind(int __fd, const sockaddr_in *__addr, socklen_t __len) { void Socket::bind(const sockaddr_in *__addr, socklen_t __len) {
bind(__fd, (struct sockaddr *)__addr, __len); bind((struct sockaddr *)__addr, __len);
} }
void Socket::bind(int __fd, const sockaddr_in6 *__addr) { void Socket::bind(const sockaddr_in6 *__addr) {
bind(__fd, (struct sockaddr *)__addr, sizeof(*__addr)); bind((struct sockaddr *)__addr, sizeof(*__addr));
} }
void Socket::bind(int __fd, const sockaddr_in6 *__addr, socklen_t __len) { void Socket::bind(const sockaddr_in6 *__addr, socklen_t __len) {
bind(__fd, (struct sockaddr *)__addr, __len); bind((struct sockaddr *)__addr, __len);
} }
} }

26
impl/Socket/TCPSocket.cpp Normal file
View File

@@ -0,0 +1,26 @@
#include "Socket/TCPSocket.hpp"
#include "Socket/Log.hpp"
#include "precomp.hpp"
namespace Chattr {
void TCPSocket::init(int domain) {
sock_ = ::socket(domain, SOCK_STREAM, 0);
if (sock_ == INVALID_SOCKET)
log::critical("socket()");
valid_ = true;
}
void TCPSocket::listen(int __n) {
int retVal = ::listen(sock_, __n);
if (retVal == INVALID_SOCKET)
log::critical("listen()");
}
void TCPSocket::accept(TCPSocket& newSock, Address& __addr) {
newSock.move(::accept(sock_, &__addr.sockaddr, &__addr.length));
if (newSock == INVALID_SOCKET)
log::critical("accept()");
}
}

8
impl/Socket/config.json Normal file
View File

@@ -0,0 +1,8 @@
{
"IP Version" : 6,
"Listen Port" : 9010,
"LogLevel" : 1,
"LogfileCount" : 5,
"LogfileName" : "Chattering.log",
"LogfileSize" : 4294967295
}

View File

@@ -0,0 +1,53 @@
#include "Utils/ConfigManager.hpp"
#include "precomp.hpp"
#include <fstream>
#include <json/json.h>
#include <iostream>
#include <string>
namespace Chattr {
ConfigManager::Config ConfigManager::load() {
Config config;
std::ifstream configfile("config.json", std::ifstream::binary);
if (!configfile.is_open()) {
std::ofstream defaultConfig("config.json", std::ios::out);
config.configJsonRoot["IP Version"] = 6;
config.configJsonRoot["Listen Port"] = 9010;
config.configJsonRoot["LogLevel"] = 1;
config.configJsonRoot["LogfileName"] = "Chattering.log";
config.configJsonRoot["LogfileSize"] = UINT32_MAX;
config.configJsonRoot["LogfileCount"] = 5;
defaultConfig << config.configJsonRoot;
defaultConfig.close();
spdlog::critical("\"config.json\" is missing. Default configuration has been written.");
}
try {
if (configfile.is_open())
configfile >> config.configJsonRoot;
config.ipVersion = config.configJsonRoot["IP Version"].asInt();
if (config.ipVersion != 4 && config.ipVersion != 6)
throw std::runtime_error("Invalid IP Version.");
config.listenPort = config.configJsonRoot["Listen Port"].asInt();
if (config.listenPort < 0 || config.listenPort > 65535)
throw std::runtime_error("Invalid listen port.");
int ll_ = config.configJsonRoot["LogLevel"].asInt();
if (ll_ >= 0 && ll_ < spdlog::level::n_levels)
config.logLevel = (spdlog::level::level_enum)ll_;
else
throw std::runtime_error("Invalid log level.");
config.logFileName = config.configJsonRoot["LogfileName"].asCString();
config.logfileSize = config.configJsonRoot["LogfileSize"].asUInt();
config.logfileCount = config.configJsonRoot["LogfileCount"].asUInt();
}
catch (Json::RuntimeError e) {
spdlog::critical(std::string(std::string("[Json Error: ]") + e.what()).c_str());
std::exit(EXIT_FAILURE);
}
return config;
}
}

View File

@@ -0,0 +1,15 @@
#pragma once
#include "precomp.hpp"
namespace Chattr {
struct Address {
union {
struct sockaddr sockaddr;
struct sockaddr_in sockaddr_in;
struct sockaddr_in6 sockaddr_in6;
};
socklen_t length;
};
}

View File

@@ -1,5 +1,6 @@
#pragma once #pragma once
#include "precomp.hpp" #include "precomp.hpp"
#include "Address.hpp"
namespace Chattr { namespace Chattr {
@@ -9,22 +10,27 @@ public:
Socket(int domain, int type, int protocol); Socket(int domain, int type, int protocol);
~Socket(); ~Socket();
void init(int domain, int type, int protocol);
operator SOCKET() const; operator SOCKET() const;
void move(const SOCKET);
void bind(int __fd, const sockaddr *__addr); void bind(const sockaddr *__addr);
void bind(int __fd, const sockaddr *__addr, socklen_t __len); void bind(const sockaddr *__addr, socklen_t __len);
//IPV4 //IPV4
void bind(int __fd, const sockaddr_in *__addr); void bind(const sockaddr_in *__addr);
void bind(int __fd, const sockaddr_in *__addr, socklen_t __len); void bind(const sockaddr_in *__addr, socklen_t __len);
//IPV6 //IPV6
void bind(int __fd, const sockaddr_in6 *__addr); void bind(const sockaddr_in6 *__addr);
void bind(int __fd, const sockaddr_in6 *__addr, socklen_t __len); void bind(const sockaddr_in6 *__addr, socklen_t __len);
Socket(const Socket&) = delete; Socket(const Socket&) = delete;
Socket& operator=(const Socket&) = delete; Socket& operator=(const Socket&) = delete;
private:
struct Address bindAddr;
protected:
bool valid_ = false; bool valid_ = false;
SOCKET sock_ = INVALID_SOCKET; SOCKET sock_ = INVALID_SOCKET;
}; };

View File

@@ -0,0 +1,15 @@
#pragma once
#include "Socket.hpp"
#include "precomp.hpp"
namespace Chattr {
class TCPSocket : public Socket {
public:
using Socket::init;
void init(int domain);
void listen(int __n);
void accept(TCPSocket& newSock, Address& addr);
};
}

View File

@@ -0,0 +1,20 @@
#pragma once
#include "precomp.hpp"
#include <json/json.h>
namespace Chattr {
class ConfigManager {
public:
struct Config {
Json::Value configJsonRoot;
std::uint32_t ipVersion = 0;
std::uint32_t listenPort = 0;
spdlog::level::level_enum logLevel = spdlog::level::off;
gsl::czstring logFileName = "";
std::uint32_t logfileSize = 0;
std::uint32_t logfileCount = 0;
};
static Config load();
};
}

View File

@@ -3,6 +3,7 @@
#ifdef _WIN32 #ifdef _WIN32
#include <winsock2.h> #include <winsock2.h>
#include <ws2tcpip.h> #include <ws2tcpip.h>
#include <ws2bth.h>
#include <windows.h> #include <windows.h>
#elif __linux__ #elif __linux__
#include <sys/types.h> #include <sys/types.h>