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