이대로 제출해버릴까..
This commit is contained in:
@@ -6,20 +6,19 @@
|
||||
#include "socket/tcp_socket.h"
|
||||
#include "socket/udp_socket.h"
|
||||
#include "socket/wsa_manager.h"
|
||||
#include "utils/ConfigManager.h"
|
||||
#include "utils/log.h"
|
||||
#include "utils/snowflake.h"
|
||||
#include "utils/utils.h"
|
||||
#include "vulkan_engine/asset/object/model.h"
|
||||
#include "vulkan_engine/vulkan/physics.h"
|
||||
|
||||
#define LISTENIP "::"
|
||||
#define LISTENPORT 9010
|
||||
#define TICKRATE 60
|
||||
#define TICKRATE 30
|
||||
constexpr double TICKDELAY = (1 / TICKRATE);
|
||||
|
||||
std::uint8_t CLIENTID = 0;
|
||||
|
||||
std::unordered_map<utils::Snowflake, veng::Model> MODELS;
|
||||
std::unordered_map<utils::Snowflake, std::shared_ptr<veng::Model>> MODELS;
|
||||
std::mutex MODELS_MUTEX;
|
||||
std::list<std::shared_ptr<Network::Socket>> CLIENTS;
|
||||
std::mutex CLIENTS_MUTEX;
|
||||
@@ -49,11 +48,26 @@ void EchoClient(utils::ThreadPool* tp, Network::IOCP* iocp,
|
||||
auto ID = model.ID;
|
||||
if (MODELS.find(ID) == MODELS.end()) {
|
||||
spdlog::info("model received: {}:{}", model.name, ID.snowflake);
|
||||
MODELS[ID] = std::move(model);
|
||||
MODELS[ID] = std::make_shared<veng::Model>();
|
||||
MODELS[ID]->Deserialize(recv_data);
|
||||
MODELS[ID]->OnColision = [](utils::ThreadPool* thread_pool,
|
||||
std::shared_ptr<veng::Model> self,
|
||||
std::shared_ptr<veng::Model> other) {
|
||||
if (self->shouldBeDestroyed || other->shouldBeDestroyed) return;
|
||||
spdlog::info("[{}:{}] and [{}:{}] is nearby.", self->name,
|
||||
self->ID.snowflake, other->name, other->ID.snowflake);
|
||||
|
||||
other->colision = false;
|
||||
other->visible = false;
|
||||
other->shouldBeDestroyed = true;
|
||||
self->colision = false;
|
||||
self->visible = false;
|
||||
self->shouldBeDestroyed = true;
|
||||
};
|
||||
} else {
|
||||
MODELS[ID].Deserialize(recv_data);
|
||||
MODELS[ID]->Deserialize(recv_data);
|
||||
}
|
||||
MODELS[ID].needsUpdate = true;
|
||||
MODELS[ID]->needsUpdate = true;
|
||||
} break;
|
||||
default:
|
||||
spdlog::error("unknown data type");
|
||||
@@ -74,20 +88,19 @@ void ClientRefresher(utils::ThreadPool* tp, Network::IOCP* iocp) {
|
||||
auto delta_time = current_time - last_frame_time_;
|
||||
last_frame_time_ = current_time;
|
||||
|
||||
CLIENTS_MUTEX.lock();
|
||||
std::lock_guard client_lock(CLIENTS_MUTEX);
|
||||
for (auto it = CLIENTS.begin(); it != CLIENTS.end();) {
|
||||
if (*it == nullptr || it->get()->sock == 0) {
|
||||
if (*it == nullptr || it->get()->sock == 0)
|
||||
it = CLIENTS.erase(it);
|
||||
} else {
|
||||
else
|
||||
++it;
|
||||
}
|
||||
}
|
||||
CLIENTS_MUTEX.unlock();
|
||||
|
||||
std::lock_guard model_lock(MODELS_MUTEX);
|
||||
for (auto it = MODELS.begin(); it != MODELS.end();) {
|
||||
auto& model = it->second;
|
||||
|
||||
if (model.shouldBeDestroyed) {
|
||||
if (model->shouldBeDestroyed) {
|
||||
it = MODELS.erase(it);
|
||||
continue;
|
||||
}
|
||||
@@ -97,33 +110,33 @@ void ClientRefresher(utils::ThreadPool* tp, Network::IOCP* iocp) {
|
||||
for (auto it = MODELS.begin(); it != MODELS.end();) {
|
||||
auto& model = it->second;
|
||||
|
||||
if (std::abs(model.lifespan + 1.f) <
|
||||
if (std::abs(model->lifespan + 1.f) <
|
||||
std::numeric_limits<float>::epsilon()) {
|
||||
++it;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (model.lifespan <= 0.f) {
|
||||
it->second.shouldBeDestroyed = true;
|
||||
if (model->lifespan <= 0.f) {
|
||||
it->second->shouldBeDestroyed = true;
|
||||
++it;
|
||||
continue;
|
||||
}
|
||||
|
||||
model.lifespan -= delta_time;
|
||||
model->lifespan -= delta_time;
|
||||
++it;
|
||||
}
|
||||
|
||||
CLIENTS_MUTEX.lock();
|
||||
for (auto& model : MODELS) {
|
||||
if (model.second.name == "camera_lag") continue;
|
||||
model.second.Update(delta_time);
|
||||
if (model.second->name == "camera_lag") continue;
|
||||
model.second->Update(delta_time);
|
||||
veng::Physics::invokeOnColisionEvent(tp, MODELS);
|
||||
for (auto& client : CLIENTS) {
|
||||
if (client->sock == 0) continue;
|
||||
std::vector<char> send_data;
|
||||
auto model_serialized = model.second.Serialize();
|
||||
auto model_serialized = model.second->Serialize();
|
||||
Packet::Header header;
|
||||
|
||||
if (model.second.shouldBeDestroyed) {
|
||||
if (model.second->shouldBeDestroyed) {
|
||||
header.opcode = Packet::Opcode::DESPAWNMODEL;
|
||||
} else {
|
||||
header.opcode = Packet::Opcode::UPDATEMODEL;
|
||||
@@ -136,11 +149,11 @@ void ClientRefresher(utils::ThreadPool* tp, Network::IOCP* iocp) {
|
||||
header_serialized.end());
|
||||
send_data.insert(send_data.end(), model_serialized.begin(),
|
||||
model_serialized.end());
|
||||
if (!model.second.needsUpdate && !needs_all_update &&
|
||||
!model.second.shouldBeDestroyed) {
|
||||
if (!model.second->needsUpdate && !needs_all_update &&
|
||||
!model.second->shouldBeDestroyed) {
|
||||
continue;
|
||||
}
|
||||
model.second.needsUpdate = false;
|
||||
model.second->needsUpdate = false;
|
||||
if (iocp->send(client, send_data) == -1) {
|
||||
client->sock = 0;
|
||||
}
|
||||
@@ -148,7 +161,6 @@ void ClientRefresher(utils::ThreadPool* tp, Network::IOCP* iocp) {
|
||||
// model.second.ID.snowflake);
|
||||
}
|
||||
}
|
||||
CLIENTS_MUTEX.unlock();
|
||||
|
||||
tp->enqueueJob(ClientRefresher, iocp);
|
||||
}
|
||||
@@ -156,21 +168,20 @@ void ClientRefresher(utils::ThreadPool* tp, Network::IOCP* iocp) {
|
||||
int main(int argc, char* argv[]) {
|
||||
Network::WSAManager wsamanager;
|
||||
const veng::GlfwInitialization _glfw;
|
||||
#if !defined(NDEBUG)
|
||||
utils::setDefaultLogger(spdlog::level::level_enum::debug, "log.log",
|
||||
1024 * 1024, 2);
|
||||
#endif
|
||||
utils::Config config = utils::ConfigManager::load();
|
||||
utils::setDefaultLogger(config.logLevel, config.logFileName,
|
||||
config.logfileSize, config.logfileCount);
|
||||
|
||||
utils::ThreadPool tp(0);
|
||||
Network::IOCP iocp;
|
||||
iocp.init(&tp, SessionProtocol::TCP);
|
||||
|
||||
Network::Address addr;
|
||||
|
||||
addr.set(AF_INET6, "::", 9010);
|
||||
addr.set(config.ipVersion, config.IP, config.Port);
|
||||
|
||||
std::shared_ptr<Network::TCPSocket> TCPSock =
|
||||
std::make_shared<Network::TCPSocket>();
|
||||
TCPSock->init(AF_INET6);
|
||||
std::make_shared<Network::TCPSocket>(config.ipVersion);
|
||||
if (TCPSock->bind(addr) == INVALID_SOCKET) {
|
||||
spdlog::error("bind()");
|
||||
std::exit(EXIT_FAILURE);
|
||||
|
||||
57
Server/src/utils/ConfigManager.cpp
Normal file
57
Server/src/utils/ConfigManager.cpp
Normal file
@@ -0,0 +1,57 @@
|
||||
#include "utils/ConfigManager.h"
|
||||
|
||||
#include <json/json.h>
|
||||
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
namespace utils {
|
||||
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"] = AF_INET6;
|
||||
config.configJsonRoot["IP"] = "::1";
|
||||
config.configJsonRoot["Port"] = 9010;
|
||||
config.configJsonRoot["LogLevel"] = 1;
|
||||
config.configJsonRoot["LogfileName"] = "log.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 != AF_INET && config.ipVersion != AF_INET6)
|
||||
throw std::runtime_error("Invalid IP Version.");
|
||||
|
||||
config.IP = config.configJsonRoot["IP"].asCString();
|
||||
|
||||
config.Port = config.configJsonRoot["Port"].asInt();
|
||||
if (config.Port < 0 || config.Port > 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;
|
||||
}
|
||||
} // namespace Chattr
|
||||
Reference in New Issue
Block a user