일단 서버구현 끝
This commit is contained in:
10
.vscode/settings.json
vendored
10
.vscode/settings.json
vendored
@@ -1,8 +1,5 @@
|
||||
{
|
||||
"files.associations": {
|
||||
"sstream": "cpp",
|
||||
"iosfwd": "cpp",
|
||||
"ostream": "cpp",
|
||||
"cctype": "cpp",
|
||||
"cmath": "cpp",
|
||||
"cstdarg": "cpp",
|
||||
@@ -45,12 +42,15 @@
|
||||
"format": "cpp",
|
||||
"initializer_list": "cpp",
|
||||
"iomanip": "cpp",
|
||||
"iosfwd": "cpp",
|
||||
"istream": "cpp",
|
||||
"limits": "cpp",
|
||||
"mutex": "cpp",
|
||||
"new": "cpp",
|
||||
"ostream": "cpp",
|
||||
"semaphore": "cpp",
|
||||
"span": "cpp",
|
||||
"sstream": "cpp",
|
||||
"stdexcept": "cpp",
|
||||
"stop_token": "cpp",
|
||||
"streambuf": "cpp",
|
||||
@@ -70,7 +70,7 @@
|
||||
"print": "cpp",
|
||||
"queue": "cpp",
|
||||
"stack": "cpp",
|
||||
"assert": "cpp",
|
||||
"zstring": "cpp"
|
||||
"fstream": "cpp",
|
||||
"regex": "cpp"
|
||||
}
|
||||
}
|
||||
@@ -11,7 +11,7 @@ include(FetchContent)
|
||||
FetchContent_Declare(
|
||||
spdlog
|
||||
GIT_REPOSITORY "https://github.com/gabime/spdlog.git"
|
||||
GIT_TAG "v1.15.2"
|
||||
GIT_TAG "v1.11.0"
|
||||
GIT_SHALLOW ON
|
||||
)
|
||||
FetchContent_MakeAvailable(spdlog)
|
||||
|
||||
7
Server/src/Chattering.log
Normal file
7
Server/src/Chattering.log
Normal 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
8
Server/src/config.json
Normal file
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"IP Version" : 6,
|
||||
"Listen Port" : 9010,
|
||||
"LogLevel" : 1,
|
||||
"LogfileCount" : 5,
|
||||
"LogfileName" : "Chattering.log",
|
||||
"LogfileSize" : 4294967295
|
||||
}
|
||||
@@ -1,68 +1,37 @@
|
||||
#include "Socket/Socket.hpp"
|
||||
#include "Socket/TCPSocket.hpp"
|
||||
#include "Socket/Log.hpp"
|
||||
#include "Utils/ConfigManager.hpp"
|
||||
#include "precomp.hpp"
|
||||
#include <fstream>
|
||||
#include <json/json.h>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
int main() {
|
||||
Json::Value configJsonRoot;
|
||||
std::uint32_t ipVersion = 0;
|
||||
std::uint32_t listenPort = 0;
|
||||
spdlog::level::level_enum logLevel;
|
||||
gsl::czstring logFileName;
|
||||
std::uint32_t logfileSize;
|
||||
std::uint32_t logfileCount;
|
||||
auto config = Chattr::ConfigManager::load();
|
||||
Chattr::log::setDefaultLogger(config.logLevel, config.logFileName, config.logfileSize, config.logfileCount);
|
||||
|
||||
std::ifstream config("config.json", std::ifstream::binary);
|
||||
if (!config.is_open()) {
|
||||
std::ofstream defaultConfig("config.json", std::ios::out);
|
||||
configJsonRoot["IP Version"] = 6;
|
||||
configJsonRoot["Listen Port"] = 9010;
|
||||
configJsonRoot["LogLevel"] = 1;
|
||||
configJsonRoot["LogfileName"] = "Chattering.log";
|
||||
configJsonRoot["LogfileSize"] = UINT32_MAX;
|
||||
configJsonRoot["LogfileCount"] = 5;
|
||||
defaultConfig << configJsonRoot;
|
||||
defaultConfig.close();
|
||||
spdlog::critical("\"config.json\" is missing. Default configuration has been written.");
|
||||
std::exit(EXIT_FAILURE);
|
||||
|
||||
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);
|
||||
|
||||
sock.bind(&serveraddr);
|
||||
}
|
||||
try {
|
||||
config >> configJsonRoot;
|
||||
else {
|
||||
sock.init(AF_INET6);
|
||||
|
||||
ipVersion = configJsonRoot["IP Version"].asInt();
|
||||
if (ipVersion != 4 && ipVersion != 6)
|
||||
throw std::runtime_error("Invalid IP Version.");
|
||||
struct sockaddr_in6 serveraddr;
|
||||
serveraddr.sin6_family = AF_INET6;
|
||||
serveraddr.sin6_addr = in6addr_any;
|
||||
serveraddr.sin6_port = htons(config.listenPort);
|
||||
|
||||
listenPort = configJsonRoot["Listen Port"].asInt();
|
||||
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();
|
||||
sock.bind(&serveraddr);
|
||||
}
|
||||
catch (Json::RuntimeError e) {
|
||||
spdlog::critical(std::string(std::string("[Json Error: ]") + e.what()).c_str());
|
||||
std::exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
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);
|
||||
sock.listen(SOMAXCONN);
|
||||
struct Chattr::TCPSocket clientSock;
|
||||
struct Chattr::Address clientAddr;
|
||||
spdlog::info("Waiting for connection...");
|
||||
sock.accept(clientSock, clientAddr);
|
||||
}
|
||||
1
impl/Socket/Chattering.log
Normal file
1
impl/Socket/Chattering.log
Normal file
@@ -0,0 +1 @@
|
||||
[2025-04-22 23:42:27.045] [Chattering Logger] [critical] [listen()] Operation not supported
|
||||
@@ -10,11 +10,7 @@ Socket::Socket(int domain, int type, int protocol) {
|
||||
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
|
||||
log::critical("WSAStartup()");
|
||||
#endif
|
||||
sock_ = ::socket(domain, type, protocol);
|
||||
if (sock_ == INVALID_SOCKET)
|
||||
log::critical("socket()");
|
||||
|
||||
valid_ = true;
|
||||
init(domain, type, protocol);
|
||||
}
|
||||
|
||||
Socket::~Socket() {
|
||||
@@ -28,37 +24,76 @@ Socket::~Socket() {
|
||||
#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 {
|
||||
if (valid_)
|
||||
return sock_;
|
||||
spdlog::critical("No valid socket created.");
|
||||
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) {
|
||||
int retVal = ::bind(__fd, __addr, __len);
|
||||
void Socket::move(const SOCKET __sock) {
|
||||
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)
|
||||
log::critical("bind()");
|
||||
}
|
||||
|
||||
void Socket::bind(int __fd, const sockaddr_in *__addr) {
|
||||
bind(__fd, (struct sockaddr *)__addr, sizeof(*__addr));
|
||||
void Socket::bind(const sockaddr_in *__addr) {
|
||||
bind((struct sockaddr *)__addr, sizeof(*__addr));
|
||||
}
|
||||
|
||||
void Socket::bind(int __fd, const sockaddr_in *__addr, socklen_t __len) {
|
||||
bind(__fd, (struct sockaddr *)__addr, __len);
|
||||
void Socket::bind(const sockaddr_in *__addr, socklen_t __len) {
|
||||
bind((struct sockaddr *)__addr, __len);
|
||||
}
|
||||
|
||||
void Socket::bind(int __fd, const sockaddr_in6 *__addr) {
|
||||
bind(__fd, (struct sockaddr *)__addr, sizeof(*__addr));
|
||||
void Socket::bind(const sockaddr_in6 *__addr) {
|
||||
bind((struct sockaddr *)__addr, sizeof(*__addr));
|
||||
}
|
||||
|
||||
void Socket::bind(int __fd, const sockaddr_in6 *__addr, socklen_t __len) {
|
||||
bind(__fd, (struct sockaddr *)__addr, __len);
|
||||
void Socket::bind(const sockaddr_in6 *__addr, socklen_t __len) {
|
||||
bind((struct sockaddr *)__addr, __len);
|
||||
}
|
||||
|
||||
}
|
||||
26
impl/Socket/TCPSocket.cpp
Normal file
26
impl/Socket/TCPSocket.cpp
Normal 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
8
impl/Socket/config.json
Normal file
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"IP Version" : 6,
|
||||
"Listen Port" : 9010,
|
||||
"LogLevel" : 1,
|
||||
"LogfileCount" : 5,
|
||||
"LogfileName" : "Chattering.log",
|
||||
"LogfileSize" : 4294967295
|
||||
}
|
||||
53
impl/Utils/ConfigManager.cpp
Normal file
53
impl/Utils/ConfigManager.cpp
Normal 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;
|
||||
}
|
||||
}
|
||||
15
include/Socket/Address.hpp
Normal file
15
include/Socket/Address.hpp
Normal 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;
|
||||
};
|
||||
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
#include "precomp.hpp"
|
||||
#include "Address.hpp"
|
||||
|
||||
namespace Chattr {
|
||||
|
||||
@@ -9,22 +10,27 @@ public:
|
||||
Socket(int domain, int type, int protocol);
|
||||
~Socket();
|
||||
|
||||
void init(int domain, int type, int protocol);
|
||||
|
||||
operator SOCKET() const;
|
||||
void move(const SOCKET);
|
||||
|
||||
void bind(int __fd, const sockaddr *__addr);
|
||||
void bind(int __fd, const sockaddr *__addr, socklen_t __len);
|
||||
void bind(const sockaddr *__addr);
|
||||
void bind(const sockaddr *__addr, socklen_t __len);
|
||||
|
||||
//IPV4
|
||||
void bind(int __fd, const sockaddr_in *__addr);
|
||||
void bind(int __fd, const sockaddr_in *__addr, socklen_t __len);
|
||||
void bind(const sockaddr_in *__addr);
|
||||
void bind(const sockaddr_in *__addr, socklen_t __len);
|
||||
|
||||
//IPV6
|
||||
void bind(int __fd, const sockaddr_in6 *__addr);
|
||||
void bind(int __fd, const sockaddr_in6 *__addr, socklen_t __len);
|
||||
void bind(const sockaddr_in6 *__addr);
|
||||
void bind(const sockaddr_in6 *__addr, socklen_t __len);
|
||||
|
||||
Socket(const Socket&) = delete;
|
||||
Socket& operator=(const Socket&) = delete;
|
||||
private:
|
||||
|
||||
struct Address bindAddr;
|
||||
protected:
|
||||
bool valid_ = false;
|
||||
SOCKET sock_ = INVALID_SOCKET;
|
||||
};
|
||||
|
||||
15
include/Socket/TCPSocket.hpp
Normal file
15
include/Socket/TCPSocket.hpp
Normal 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);
|
||||
};
|
||||
|
||||
}
|
||||
20
include/Utils/ConfigManager.hpp
Normal file
20
include/Utils/ConfigManager.hpp
Normal 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();
|
||||
};
|
||||
}
|
||||
@@ -3,6 +3,7 @@
|
||||
#ifdef _WIN32
|
||||
#include <winsock2.h>
|
||||
#include <ws2tcpip.h>
|
||||
#include <ws2bth.h>
|
||||
#include <windows.h>
|
||||
#elif __linux__
|
||||
#include <sys/types.h>
|
||||
|
||||
Reference in New Issue
Block a user