일단 서버구현 끝
This commit is contained in:
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user