Compare commits
11 Commits
c0e0279e5c
...
V0.02
| Author | SHA1 | Date | |
|---|---|---|---|
| 1b79d946c3 | |||
| 54fab0ad23 | |||
| 9949ec80fb | |||
| 711fc4c93d | |||
| 06a1e41dc8 | |||
| 93fda88d75 | |||
| 13ea246990 | |||
| 50c9bd68fc | |||
| 5ba62b5312 | |||
| 5fdae769e2 | |||
| 6428db9d31 |
@@ -3,7 +3,6 @@ set(PROJECT_NAME "Client")
|
|||||||
project(${PROJECT_NAME})
|
project(${PROJECT_NAME})
|
||||||
|
|
||||||
find_package(Vulkan REQUIRED)
|
find_package(Vulkan REQUIRED)
|
||||||
find_package(OpenSSL REQUIRED)
|
|
||||||
|
|
||||||
include(Shaders.cmake)
|
include(Shaders.cmake)
|
||||||
include(FetchContent)
|
include(FetchContent)
|
||||||
@@ -59,8 +58,6 @@ target_link_libraries(${PROJECT_NAME} PRIVATE glfw)
|
|||||||
target_link_libraries(${PROJECT_NAME} PRIVATE Microsoft.GSL::GSL)
|
target_link_libraries(${PROJECT_NAME} PRIVATE Microsoft.GSL::GSL)
|
||||||
target_link_libraries(${PROJECT_NAME} PRIVATE spdlog)
|
target_link_libraries(${PROJECT_NAME} PRIVATE spdlog)
|
||||||
target_link_libraries(${PROJECT_NAME} PRIVATE assimp::assimp)
|
target_link_libraries(${PROJECT_NAME} PRIVATE assimp::assimp)
|
||||||
target_link_libraries(${PROJECT_NAME} PRIVATE OpenSSL::Crypto)
|
|
||||||
target_link_libraries(${PROJECT_NAME} PRIVATE OpenSSL::SSL)
|
|
||||||
|
|
||||||
if(WIN32)
|
if(WIN32)
|
||||||
target_link_libraries(${PROJECT_NAME} PRIVATE ws2_32)
|
target_link_libraries(${PROJECT_NAME} PRIVATE ws2_32)
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
extern std::uint8_t CLIENTID;
|
||||||
|
|
||||||
namespace veng {
|
namespace veng {
|
||||||
class Engine;
|
class Engine;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,8 +2,14 @@
|
|||||||
|
|
||||||
#include "vulkan_engine/vulkan/engine.h"
|
#include "vulkan_engine/vulkan/engine.h"
|
||||||
|
|
||||||
|
|
||||||
void BeginPlay(veng::Engine& engine) {
|
void BeginPlay(veng::Engine& engine) {
|
||||||
veng::Model* const player = engine.SpawnModel("player", "player");
|
std::random_device rd;
|
||||||
|
std::mt19937 gen = std::mt19937(rd());
|
||||||
|
std::uniform_real_distribution<float> jitterDist = std::uniform_real_distribution<float>(-2, 2);
|
||||||
|
|
||||||
|
veng::Model* const player = engine.SpawnLifedModel("player", "player", -1);
|
||||||
|
player->position = glm::vec3(jitterDist(gen), jitterDist(gen), 0.f);
|
||||||
player->scale = glm::vec3(.02f);
|
player->scale = glm::vec3(.02f);
|
||||||
player->colision = true;
|
player->colision = true;
|
||||||
player->OnColision = [](veng::Model* self, veng::Model* other) {
|
player->OnColision = [](veng::Model* self, veng::Model* other) {
|
||||||
@@ -15,15 +21,17 @@ void BeginPlay(veng::Engine& engine) {
|
|||||||
|
|
||||||
other->colision = false;
|
other->colision = false;
|
||||||
other->visible = false;
|
other->visible = false;
|
||||||
|
other->shouldBeDestroyed = true;
|
||||||
};
|
};
|
||||||
veng::Model* const player_flame =
|
veng::Model* const player_flame =
|
||||||
engine.SpawnModel("player_flame", "player_flame");
|
engine.SpawnLifedModel("player_flame", "player_flame", -1);
|
||||||
player_flame->scale = player->scale;
|
player_flame->scale = player->scale;
|
||||||
player_flame->colision = false;
|
player_flame->colision = false;
|
||||||
|
player_flame->networkReplicated = false;
|
||||||
|
|
||||||
spdlog::info("player addr: {}", (void*)player);
|
spdlog::info("player addr: {}", (void*)player);
|
||||||
|
|
||||||
veng::Model* const other_player = engine.SpawnModel("player", "other_player");
|
veng::Model* const other_player = engine.SpawnLifedModel("player", "other_player", -1);
|
||||||
other_player->position = glm::vec3(1.f, 0.f, 0.f);
|
other_player->position = glm::vec3(1.f, 0.f, 0.f);
|
||||||
other_player->scale = glm::vec3(.02f);
|
other_player->scale = glm::vec3(.02f);
|
||||||
other_player->colision = true;
|
other_player->colision = true;
|
||||||
@@ -36,29 +44,35 @@ void BeginPlay(veng::Engine& engine) {
|
|||||||
|
|
||||||
other->colision = false;
|
other->colision = false;
|
||||||
other->visible = false;
|
other->visible = false;
|
||||||
|
other->shouldBeDestroyed = true;
|
||||||
};
|
};
|
||||||
|
|
||||||
spdlog::info("other player addr: {}", (void*)other_player);
|
spdlog::info("other player addr: {}", (void*)other_player);
|
||||||
|
|
||||||
veng::Model* const camera_lag = engine.SpawnModel("", "camera_lag");
|
veng::Model* const camera_lag = engine.SpawnLifedModel("", "camera_lag", -1);
|
||||||
camera_lag->colision = false;
|
camera_lag->colision = false;
|
||||||
camera_lag->position = player->position;
|
camera_lag->position = player->position;
|
||||||
|
|
||||||
veng::Model* const background = engine.SpawnModel("", "background");
|
veng::Model* const background = engine.SpawnLifedModel("", "background", -1);
|
||||||
background->colision = false;
|
background->colision = false;
|
||||||
background->position = {background->position.x, background->position.y, 30.f};
|
background->position = {background->position.x, background->position.y, 30.f};
|
||||||
background->scale *= 100;
|
background->scale *= 100;
|
||||||
|
background->networkReplicated = false;
|
||||||
|
|
||||||
veng::Model* const background0 =
|
veng::Model* const background0 =
|
||||||
engine.SpawnModel("background", "background0");
|
engine.SpawnLifedModel("background", "background0", -1);
|
||||||
background0->scale = background->scale;
|
background0->scale = background->scale;
|
||||||
|
background0->networkReplicated = false;
|
||||||
veng::Model* const background1 =
|
veng::Model* const background1 =
|
||||||
engine.SpawnModel("background", "background1");
|
engine.SpawnLifedModel("background", "background1", -1);
|
||||||
background1->scale = background->scale;
|
background1->scale = background->scale;
|
||||||
|
background1->networkReplicated = false;
|
||||||
veng::Model* const background2 =
|
veng::Model* const background2 =
|
||||||
engine.SpawnModel("background", "background2");
|
engine.SpawnLifedModel("background", "background2", -1);
|
||||||
background2->scale = background->scale;
|
background2->scale = background->scale;
|
||||||
|
background2->networkReplicated = false;
|
||||||
veng::Model* const background3 =
|
veng::Model* const background3 =
|
||||||
engine.SpawnModel("background", "background3");
|
engine.SpawnLifedModel("background", "background3", -1);
|
||||||
background3->scale = background->scale;
|
background3->scale = background->scale;
|
||||||
|
background3->networkReplicated = false;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,78 +3,78 @@
|
|||||||
#include "glfw/glfw_monitor.h"
|
#include "glfw/glfw_monitor.h"
|
||||||
#include "glfw/glfw_window.h"
|
#include "glfw/glfw_window.h"
|
||||||
#include "socket/iocp.h"
|
#include "socket/iocp.h"
|
||||||
|
#include "socket/packet.h"
|
||||||
#include "socket/tcp_socket.h"
|
#include "socket/tcp_socket.h"
|
||||||
|
#include "socket/udp_socket.h"
|
||||||
#include "socket/wsa_manager.h"
|
#include "socket/wsa_manager.h"
|
||||||
#include "utils/log.h"
|
#include "utils/log.h"
|
||||||
|
#include "utils/snowflake.h"
|
||||||
#include "vulkan_engine/vulkan/engine.h"
|
#include "vulkan_engine/vulkan/engine.h"
|
||||||
#include "vulkan_engine/vulkan/graphics.h"
|
#include "vulkan_engine/vulkan/graphics.h"
|
||||||
|
|
||||||
std::int32_t main(std::int32_t argc, gsl::zstring* argv) {
|
std::uint8_t CLIENTID = 0;
|
||||||
|
|
||||||
|
std::int32_t main(std::int32_t argc, gsl::zstring* argv) {
|
||||||
Network::WSAManager wsamanager;
|
Network::WSAManager wsamanager;
|
||||||
#if !defined(NDEBUG)
|
#if !defined(NDEBUG)
|
||||||
utils::setDefaultLogger(spdlog::level::level_enum::debug, "log.log", 1024,
|
utils::setDefaultLogger(spdlog::level::level_enum::debug, "log.log", 1024, 2);
|
||||||
2);
|
#endif
|
||||||
#endif
|
utils::ThreadPool tp(0);
|
||||||
utils::ThreadPool tp(0);
|
Network::IOCP iocp;
|
||||||
Network::IOCP iocp;
|
iocp.init(&tp, SessionProtocol::TCP);
|
||||||
iocp.init(&tp, SessionProtocol::TCP);
|
|
||||||
|
|
||||||
Network::Address addr;
|
Network::Address addr;
|
||||||
in6_addr in6addr;
|
in6_addr in6addr;
|
||||||
|
|
||||||
addr.set(AF_INET6, "::1", 9010);
|
addr.set(AF_INET6, "::1", 9010);
|
||||||
|
|
||||||
Network::TCPSocket sock;
|
Network::TCPSocket TCPSock;
|
||||||
sock.init(AF_INET6);
|
TCPSock.init(AF_INET6);
|
||||||
if (sock.connect(addr) == INVALID_SOCKET) {
|
if (TCPSock.connect(addr) == INVALID_SOCKET) {
|
||||||
spdlog::error("connect()");
|
spdlog::error("connect()");
|
||||||
std::exit(EXIT_FAILURE);
|
std::exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
iocp.registerTCPSocket(TCPSock, 16 * 1024);
|
||||||
|
|
||||||
Network::IOCPPASSINDATA* data = new Network::IOCPPASSINDATA(16 * 1024);
|
while (iocp.GetRecvedBytes(TCPSock.sock) < 6);
|
||||||
data->socket = std::make_shared<Network::TCPSocket>(sock);
|
std::vector<char> header_serialized(6);
|
||||||
data->IOCPInstance = &iocp;
|
iocp.recv(TCPSock, header_serialized);
|
||||||
iocp.registerSocket(data);
|
Packet::Header header;
|
||||||
|
header.Deserialize(header_serialized);
|
||||||
|
while (iocp.GetRecvedBytes(TCPSock.sock) < header.body_length);
|
||||||
|
std::vector<char> data(header.body_length);
|
||||||
|
iocp.recv(TCPSock, data);
|
||||||
|
::memcpy(&CLIENTID, data.data(), 1); //id 받기
|
||||||
|
|
||||||
std::vector<Network::IOCPPASSINDATA*> send_data;
|
header.opcode = Packet::Opcode::CLIENTID;
|
||||||
data->event = Network::IOCPEVENT::WRITE;
|
header.body_length = 0;
|
||||||
data->wsabuf.buf[0] = 'a';
|
header_serialized = header.Serialize();
|
||||||
data->wsabuf.buf[1] = 'b';
|
iocp.send(TCPSock, header_serialized);
|
||||||
data->wsabuf.buf[2] = '\0';
|
|
||||||
data->wsabuf.len = 3;
|
|
||||||
send_data.push_back(data);
|
|
||||||
iocp.send(sock.sock, &send_data);
|
|
||||||
|
|
||||||
Sleep(10000);
|
const veng::GlfwInitialization _glfw;
|
||||||
|
|
||||||
Network::IOCPPASSINDATA* recv_data = new Network::IOCPPASSINDATA(16 * 1024);
|
veng::Window window("Asteroid", {800, 600});
|
||||||
recv_data->socket = std::make_shared<Network::TCPSocket>(sock);
|
window.TryMoveToMonitor(0);
|
||||||
recv_data->IOCPInstance = &iocp;
|
|
||||||
iocp.recv(recv_data);
|
|
||||||
|
|
||||||
const veng::GlfwInitialization _glfw;
|
veng::Graphics graphics(&window);
|
||||||
|
veng::Engine engine(&graphics, &tp, &iocp);
|
||||||
|
|
||||||
veng::Window window("Vulkan Engine", {800, 600});
|
engine.LoadModelAsset("assets/player.fbx", "player");
|
||||||
window.TryMoveToMonitor(0);
|
engine.LoadModelAsset("assets/player_flame.fbx", "player_flame");
|
||||||
|
engine.LoadModelAsset("assets/bullet.fbx", "bullet");
|
||||||
|
engine.LoadModelAsset("assets/background.fbx", "background");
|
||||||
|
|
||||||
veng::Graphics graphics(&window);
|
engine.BeginPlay = BeginPlay;
|
||||||
veng::Engine engine(&graphics, &tp);
|
engine.Tick = Tick;
|
||||||
|
|
||||||
engine.LoadModelAsset("assets/player.fbx", "player");
|
engine.init();
|
||||||
engine.LoadModelAsset("assets/player_flame.fbx", "player_flame");
|
|
||||||
engine.LoadModelAsset("assets/bullet.fbx", "bullet");
|
|
||||||
engine.LoadModelAsset("assets/background.fbx", "background");
|
|
||||||
|
|
||||||
engine.BeginPlay = BeginPlay;
|
while (!window.ShouldClose()) {
|
||||||
engine.Tick = Tick;
|
glfwPollEvents();
|
||||||
|
|
||||||
engine.init();
|
engine.Update();
|
||||||
|
engine.NetUpdate(std::make_shared<Network::Socket>(TCPSock));
|
||||||
|
}
|
||||||
|
|
||||||
while (!window.ShouldClose()) {
|
return EXIT_SUCCESS;
|
||||||
glfwPollEvents();
|
}
|
||||||
|
|
||||||
engine.Update();
|
|
||||||
}
|
|
||||||
|
|
||||||
return EXIT_SUCCESS;
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -31,17 +31,27 @@ void Tick(veng::Engine& engine, std::float_t delta_time) {
|
|||||||
(-stiffness * displacement - damping * camera_lag->linear_velocity) *
|
(-stiffness * displacement - damping * camera_lag->linear_velocity) *
|
||||||
delta_time;
|
delta_time;
|
||||||
|
|
||||||
|
/*camera_lag->linear_velocity = glm::vec3(0);
|
||||||
|
camera_lag->position = player->position;*/
|
||||||
|
|
||||||
engine.view = glm::lookAt(
|
engine.view = glm::lookAt(
|
||||||
glm::vec3(camera_lag->position.x, camera_lag->position.y, -5.f),
|
glm::vec3(camera_lag->position.x, camera_lag->position.y, -5.f),
|
||||||
camera_lag->position, glm::vec3(0.f, -1.f, 0.f));
|
camera_lag->position, glm::vec3(0.f, -1.f, 0.f));
|
||||||
|
|
||||||
|
static bool w_laststate = false;
|
||||||
if (glfwGetKey(engine.vulkan_graphics->window->GetHandle(), GLFW_KEY_W) ==
|
if (glfwGetKey(engine.vulkan_graphics->window->GetHandle(), GLFW_KEY_W) ==
|
||||||
GLFW_PRESS) {
|
GLFW_PRESS) {
|
||||||
player->linear_acceleration = glm::normalize(forward) * 10.f;
|
player->linear_acceleration = glm::normalize(forward) * 10.f;
|
||||||
player_flame->visible = true;
|
player_flame->visible = true;
|
||||||
|
player->needsUpdate = true;
|
||||||
|
w_laststate = true;
|
||||||
} else {
|
} else {
|
||||||
player->linear_acceleration = forward * .0f;
|
player->linear_acceleration = forward * .0f;
|
||||||
player_flame->visible = false;
|
player_flame->visible = false;
|
||||||
|
if (w_laststate) {
|
||||||
|
player->needsUpdate = true;
|
||||||
|
w_laststate = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (bullet_cooldown > std::numeric_limits<std::float_t>::epsilon()) {
|
if (bullet_cooldown > std::numeric_limits<std::float_t>::epsilon()) {
|
||||||
@@ -60,21 +70,33 @@ void Tick(veng::Engine& engine, std::float_t delta_time) {
|
|||||||
bullet->owner = player;
|
bullet->owner = player;
|
||||||
bullet->scale = player->scale;
|
bullet->scale = player->scale;
|
||||||
bullet->colision = true;
|
bullet->colision = true;
|
||||||
|
bullet->networkReplicated = true;
|
||||||
|
bullet->needsUpdate = true;
|
||||||
|
|
||||||
spdlog::info("bullet address: {}", (void*)bullet);
|
spdlog::info("bullet address: {}", (void*)bullet);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static bool ad_laststate = false;
|
||||||
if (glfwGetKey(engine.vulkan_graphics->window->GetHandle(), GLFW_KEY_A) ==
|
if (glfwGetKey(engine.vulkan_graphics->window->GetHandle(), GLFW_KEY_A) ==
|
||||||
GLFW_PRESS) {
|
GLFW_PRESS) {
|
||||||
right = player->rotation * glm::vec3(0, 0, 1);
|
right = player->rotation * glm::vec3(0, 0, 1);
|
||||||
player->angular_velocity = right * 6.f;
|
player->angular_velocity = right * 6.f;
|
||||||
|
player->needsUpdate = true;
|
||||||
|
ad_laststate = true;
|
||||||
} else if (glfwGetKey(engine.vulkan_graphics->window->GetHandle(),
|
} else if (glfwGetKey(engine.vulkan_graphics->window->GetHandle(),
|
||||||
GLFW_KEY_D) == GLFW_PRESS) {
|
GLFW_KEY_D) == GLFW_PRESS) {
|
||||||
right = player->rotation * glm::vec3(0, 0, 1);
|
right = player->rotation * glm::vec3(0, 0, 1);
|
||||||
player->angular_velocity = right * -6.f;
|
player->angular_velocity = right * -6.f;
|
||||||
|
player->needsUpdate = true;
|
||||||
|
ad_laststate = true;
|
||||||
} else {
|
} else {
|
||||||
right = player->rotation * glm::vec3(0, 0, 1);
|
right = player->rotation * glm::vec3(0, 0, 1);
|
||||||
player->angular_velocity = right * 0.f;
|
player->angular_velocity = right * 0.f;
|
||||||
|
if (ad_laststate) {
|
||||||
|
player->needsUpdate = true;
|
||||||
|
ad_laststate = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
player_flame->rotation = player->rotation;
|
player_flame->rotation = player->rotation;
|
||||||
|
|||||||
@@ -1,30 +0,0 @@
|
|||||||
#include "vulkan_engine/asset/object/model.h"
|
|
||||||
|
|
||||||
#include "vulkan_engine/vulkan/graphics.h"
|
|
||||||
|
|
||||||
namespace veng {
|
|
||||||
|
|
||||||
Model::~Model() {
|
|
||||||
if (graphics_ == nullptr) return;
|
|
||||||
|
|
||||||
graphics_->DestroyTexture(material.texture_handle);
|
|
||||||
graphics_->DestroyBuffer(vertex_buffer);
|
|
||||||
graphics_->DestroyBuffer(index_buffer);
|
|
||||||
}
|
|
||||||
|
|
||||||
void veng::Model::Update(float dt) {
|
|
||||||
linear_velocity += linear_acceleration * dt;
|
|
||||||
position += linear_velocity * dt;
|
|
||||||
|
|
||||||
angular_velocity += angular_acceleration * dt;
|
|
||||||
if (glm::length(angular_velocity) > 1e-6f) {
|
|
||||||
rotation =
|
|
||||||
glm::normalize(glm::rotate(rotation, glm::length(angular_velocity * dt),
|
|
||||||
glm::normalize(angular_velocity)));
|
|
||||||
}
|
|
||||||
|
|
||||||
transform = glm::translate(glm::mat4(1.0f), position) *
|
|
||||||
glm::mat4_cast(rotation) * glm::scale(glm::mat4(1.0f), scale);
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace veng
|
|
||||||
@@ -1,177 +0,0 @@
|
|||||||
#include "vulkan_engine/vulkan/engine.h"
|
|
||||||
|
|
||||||
#include "precomp.h"
|
|
||||||
|
|
||||||
namespace veng {
|
|
||||||
|
|
||||||
void Engine::init() {
|
|
||||||
glm::ivec2 window_size_ = vulkan_graphics->window->GetFramebufferSize();
|
|
||||||
view = glm::lookAt(glm::vec3(0.f, 0.f, -5.f), glm::vec3(0.f, 0.f, 0.f),
|
|
||||||
glm::vec3(0.f, -1.f, 0.f));
|
|
||||||
projection = glm::perspective(
|
|
||||||
glm::radians(103.f),
|
|
||||||
(std::float_t)window_size_.x / (std::float_t)window_size_.y, 0.1f,
|
|
||||||
1000.f);
|
|
||||||
vulkan_graphics->SetViewProjection(view, projection);
|
|
||||||
|
|
||||||
if (BeginPlay != nullptr) BeginPlay(*this);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Engine::LoadModelAsset(std::string path, std::string name) {
|
|
||||||
veng::Model model(vulkan_graphics);
|
|
||||||
asset_loader_.setPath(path);
|
|
||||||
asset_loader_.loadModel(model);
|
|
||||||
|
|
||||||
model.vertex_buffer = vulkan_graphics->CreateVertexBuffer(model.vertices);
|
|
||||||
model.index_buffer = vulkan_graphics->CreateIndexBuffer(model.indices);
|
|
||||||
model.material.texture_handle =
|
|
||||||
vulkan_graphics->CreateTexture(asset_loader_.readTexture());
|
|
||||||
model_assets_[name] = std::move(model);
|
|
||||||
}
|
|
||||||
|
|
||||||
const Model* Engine::GetStaticModel(std::string name) {
|
|
||||||
if (model_assets_.find(name) != model_assets_.end())
|
|
||||||
return &model_assets_[name];
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
Model* Engine::SpawnModel(std::string asset_name, std::string name) {
|
|
||||||
if (asset_name == "") {
|
|
||||||
Model model_to_spawn(nullptr);
|
|
||||||
model_to_spawn.visible = false;
|
|
||||||
dynamic_immortal_models_[name] = std::move(model_to_spawn);
|
|
||||||
return &dynamic_immortal_models_[name];
|
|
||||||
}
|
|
||||||
|
|
||||||
if (dynamic_immortal_models_.find(name) == dynamic_immortal_models_.end()) {
|
|
||||||
Model model_to_spawn(*GetStaticModel(asset_name));
|
|
||||||
dynamic_immortal_models_[name] = std::move(model_to_spawn);
|
|
||||||
return &dynamic_immortal_models_[name];
|
|
||||||
}
|
|
||||||
|
|
||||||
std::uint32_t i = 0;
|
|
||||||
for (i = 0; i < std::numeric_limits<std::uint32_t>::max();) {
|
|
||||||
if (dynamic_immortal_models_.find(name + std::to_string(i)) ==
|
|
||||||
dynamic_immortal_models_.end()) {
|
|
||||||
Model model_to_spawn(*GetStaticModel(asset_name));
|
|
||||||
dynamic_immortal_models_[name + std::to_string(i)] =
|
|
||||||
std::move(model_to_spawn);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (i == std::numeric_limits<std::uint32_t>::max() - 1)
|
|
||||||
return nullptr;
|
|
||||||
else
|
|
||||||
return &dynamic_immortal_models_[name + std::to_string(i)];
|
|
||||||
}
|
|
||||||
|
|
||||||
Model* Engine::SpawnLifedModel(std::string asset_name, std::string name,
|
|
||||||
std::float_t lifespan) {
|
|
||||||
if (asset_name == "") {
|
|
||||||
Model model_to_spawn(nullptr);
|
|
||||||
model_to_spawn.visible = false;
|
|
||||||
dynamic_models_[name] = std::make_pair(std::move(model_to_spawn), lifespan);
|
|
||||||
return &dynamic_models_[name].first;
|
|
||||||
}
|
|
||||||
if (dynamic_models_.find(name) == dynamic_models_.end()) {
|
|
||||||
Model model_to_spawn(*GetStaticModel(asset_name));
|
|
||||||
dynamic_models_[name] = std::make_pair(std::move(model_to_spawn), lifespan);
|
|
||||||
return &dynamic_models_[name].first;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::uint32_t i = 0;
|
|
||||||
for (i = 0; i < std::numeric_limits<std::uint32_t>::max();) {
|
|
||||||
if (dynamic_models_.find(name + std::to_string(i)) ==
|
|
||||||
dynamic_models_.end()) {
|
|
||||||
Model model_to_spawn(*GetStaticModel(asset_name));
|
|
||||||
dynamic_models_[name + std::to_string(i)] =
|
|
||||||
std::make_pair(std::move(model_to_spawn), lifespan);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (i == std::numeric_limits<std::uint32_t>::max() - 1)
|
|
||||||
return nullptr;
|
|
||||||
else
|
|
||||||
return &dynamic_models_[name + std::to_string(i)].first;
|
|
||||||
}
|
|
||||||
|
|
||||||
Model* Engine::GetSpawnedObject(std::string name) {
|
|
||||||
for (auto it = dynamic_immortal_models_.begin();
|
|
||||||
it != dynamic_immortal_models_.end();) {
|
|
||||||
if (it->first == name) return &it->second;
|
|
||||||
++it;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (auto it = dynamic_models_.begin(); it != dynamic_models_.end();) {
|
|
||||||
if (it->first == name) return &it->second.first;
|
|
||||||
++it;
|
|
||||||
}
|
|
||||||
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Engine::Update() {
|
|
||||||
glm::ivec2 framebuffer_size = vulkan_graphics->window->GetFramebufferSize();
|
|
||||||
if (framebuffer_size != window_size_ && framebuffer_size.x != 0 &&
|
|
||||||
framebuffer_size.y != 0) {
|
|
||||||
window_size_ = framebuffer_size;
|
|
||||||
auto grater = (framebuffer_size.x > framebuffer_size.y)
|
|
||||||
? framebuffer_size.x
|
|
||||||
: framebuffer_size.y;
|
|
||||||
view = glm::lookAt(glm::vec3(0.f, 0.f, -5.f), glm::vec3(0.f, 0.f, 0.f),
|
|
||||||
glm::vec3(0.f, -1.f, 0.f));
|
|
||||||
projection = glm::perspective(
|
|
||||||
glm::radians(103.f),
|
|
||||||
(std::float_t)framebuffer_size.x / (std::float_t)framebuffer_size.y,
|
|
||||||
0.1f, 1000.f);
|
|
||||||
}
|
|
||||||
vulkan_graphics->SetViewProjection(view, projection);
|
|
||||||
|
|
||||||
if (vulkan_graphics->BeginFrame()) {
|
|
||||||
std::double_t current_time = glfwGetTime();
|
|
||||||
std::float_t delta_time =
|
|
||||||
static_cast<std::float_t>(current_time - last_frame_time_);
|
|
||||||
last_frame_time_ = current_time;
|
|
||||||
|
|
||||||
if (Tick != nullptr) Tick(*this, delta_time);
|
|
||||||
|
|
||||||
std::vector<Model*> models;
|
|
||||||
models.reserve(dynamic_immortal_models_.size() +
|
|
||||||
dynamic_models_.size());
|
|
||||||
|
|
||||||
for (auto it = dynamic_immortal_models_.begin();
|
|
||||||
it != dynamic_immortal_models_.end();) {
|
|
||||||
models.push_back(&it->second);
|
|
||||||
|
|
||||||
++it;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (auto it = dynamic_models_.begin(); it != dynamic_models_.end();) {
|
|
||||||
if (it->second.second < 0.f) {
|
|
||||||
it = dynamic_models_.erase(it);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
it->second.second -= delta_time;
|
|
||||||
}
|
|
||||||
models.push_back(&it->second.first);
|
|
||||||
|
|
||||||
++it;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (auto it : models) {
|
|
||||||
it->Update(delta_time);
|
|
||||||
vulkan_graphics->RenderModel(it);
|
|
||||||
}
|
|
||||||
|
|
||||||
physics_controller_.invokeOnColisionEvent(thread_pool_, {models.data(), models.size()});
|
|
||||||
|
|
||||||
vulkan_graphics->EndFrame();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace veng
|
|
||||||
@@ -4,10 +4,89 @@ set(PROJECT_NAME "Server")
|
|||||||
|
|
||||||
project(${PROJECT_NAME})
|
project(${PROJECT_NAME})
|
||||||
|
|
||||||
add_executable(${PROJECT_NAME}
|
find_package(Vulkan REQUIRED)
|
||||||
"${CMAKE_CURRENT_SOURCE_DIR}/src/echoserver.cpp"
|
|
||||||
|
include(FetchContent)
|
||||||
|
|
||||||
|
FetchContent_Declare(
|
||||||
|
glm
|
||||||
|
GIT_REPOSITORY "https://github.com/g-truc/glm.git"
|
||||||
|
GIT_TAG "1.0.1"
|
||||||
|
GIT_SHALLOW ON
|
||||||
)
|
)
|
||||||
|
FetchContent_MakeAvailable(glm)
|
||||||
|
|
||||||
|
FetchContent_Declare(
|
||||||
|
spdlog
|
||||||
|
GIT_REPOSITORY "https://github.com/gabime/spdlog.git"
|
||||||
|
GIT_TAG "v1.15.2"
|
||||||
|
GIT_SHALLOW ON
|
||||||
|
)
|
||||||
|
FetchContent_MakeAvailable(spdlog)
|
||||||
|
|
||||||
|
FetchContent_Declare(
|
||||||
|
glfw
|
||||||
|
GIT_REPOSITORY "https://github.com/glfw/glfw.git"
|
||||||
|
GIT_TAG "3.4"
|
||||||
|
GIT_SHALLOW ON
|
||||||
|
)
|
||||||
|
FetchContent_MakeAvailable(glfw)
|
||||||
|
|
||||||
|
FetchContent_Declare(
|
||||||
|
GSL
|
||||||
|
GIT_REPOSITORY "https://github.com/microsoft/GSL.git"
|
||||||
|
GIT_TAG "v4.2.0"
|
||||||
|
GIT_SHALLOW ON
|
||||||
|
)
|
||||||
|
FetchContent_MakeAvailable(GSL)
|
||||||
|
|
||||||
|
FetchContent_Declare(
|
||||||
|
ASSIMP
|
||||||
|
GIT_REPOSITORY "https://github.com/assimp/assimp.git"
|
||||||
|
GIT_TAG "v5.4.3"
|
||||||
|
GIT_SHALLOW ON
|
||||||
|
)
|
||||||
|
FetchContent_MakeAvailable(ASSIMP)
|
||||||
|
|
||||||
|
file(GLOB_RECURSE Sources CONFIGURE_DEPENDS
|
||||||
|
"${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp"
|
||||||
|
)
|
||||||
|
|
||||||
|
add_executable(${PROJECT_NAME} ${RootProjectSources} ${Sources})
|
||||||
|
target_link_libraries(${PROJECT_NAME} PRIVATE Vulkan::Vulkan)
|
||||||
|
target_link_libraries(${PROJECT_NAME} PRIVATE glm)
|
||||||
|
target_link_libraries(${PROJECT_NAME} PRIVATE glfw)
|
||||||
|
target_link_libraries(${PROJECT_NAME} PRIVATE Microsoft.GSL::GSL)
|
||||||
|
target_link_libraries(${PROJECT_NAME} PRIVATE spdlog)
|
||||||
|
target_link_libraries(${PROJECT_NAME} PRIVATE assimp::assimp)
|
||||||
|
|
||||||
if(WIN32)
|
if(WIN32)
|
||||||
target_link_libraries(${PROJECT_NAME} PRIVATE ws2_32)
|
target_link_libraries(${PROJECT_NAME} PRIVATE ws2_32)
|
||||||
endif()
|
endif()
|
||||||
|
if(UNIX AND NOT APPLE AND CMAKE_BUILD_TYPE STREQUAL "Release")
|
||||||
|
set_target_properties(${PROJECT_NAME} PROPERTIES
|
||||||
|
BUILD_WITH_INSTALL_RPATH TRUE
|
||||||
|
INSTALL_RPATH "$ORIGIN"
|
||||||
|
SKIP_BUILD_RPATH FALSE
|
||||||
|
BUILD_RPATH "$ORIGIN"
|
||||||
|
)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
target_include_directories(${PROJECT_NAME} PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/../include" "${CMAKE_CURRENT_SOURCE_DIR}/include")
|
||||||
|
target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_20)
|
||||||
|
|
||||||
|
target_precompile_headers(${PROJECT_NAME} PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/../include/precomp.h")
|
||||||
|
|
||||||
|
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
|
||||||
|
COMMAND ${CMAKE_COMMAND} -E copy_if_different
|
||||||
|
"$<TARGET_FILE:spdlog>"
|
||||||
|
"$<TARGET_FILE_DIR:${PROJECT_NAME}>"
|
||||||
|
COMMENT "Copying spdlog DLL/so to output directory"
|
||||||
|
)
|
||||||
|
|
||||||
|
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
|
||||||
|
COMMAND ${CMAKE_COMMAND} -E copy_directory
|
||||||
|
"$<TARGET_FILE_DIR:spdlog>"
|
||||||
|
"$<TARGET_FILE_DIR:${PROJECT_NAME}>"
|
||||||
|
COMMENT "Copying spdlog library files and symlinks to output directory"
|
||||||
|
)
|
||||||
@@ -1,93 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#ifdef _WIN32
|
|
||||||
#include <winsock2.h>
|
|
||||||
#include <ws2tcpip.h>
|
|
||||||
#include <ws2bth.h>
|
|
||||||
#include <ws2def.h>
|
|
||||||
#include <windows.h>
|
|
||||||
#define in_addr_t ULONG
|
|
||||||
#elif __linux__
|
|
||||||
#include <sys/types.h>
|
|
||||||
#include <sys/socket.h>
|
|
||||||
#include <sys/epoll.h>
|
|
||||||
#include <arpa/inet.h>
|
|
||||||
#include <fcntl.h>
|
|
||||||
#include <errno.h>
|
|
||||||
#include <netinet/in.h>
|
|
||||||
#include <sys/eventfd.h>
|
|
||||||
#define SOCKET int
|
|
||||||
#define INVALID_SOCKET -1
|
|
||||||
#define SOCKET_ERROR -1
|
|
||||||
#else
|
|
||||||
#error "이 플랫폼은 지원되지 않습니다."
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include <chrono>
|
|
||||||
|
|
||||||
void err_quit(const char *msg) {
|
|
||||||
char *msgbuf = strerror(errno);
|
|
||||||
fprintf(stderr, "[%s] %s\n", msg, msgbuf);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
void err_display(const char *msg) {
|
|
||||||
char *msgbuf = strerror(errno);
|
|
||||||
fprintf(stderr, "[%s] %s\n", msg, msgbuf);
|
|
||||||
}
|
|
||||||
|
|
||||||
#define LISTENIP "::"
|
|
||||||
#define LISTENPORT 9010
|
|
||||||
#define BUFSIZE 100
|
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
|
||||||
WSADATA wsa;
|
|
||||||
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0) {
|
|
||||||
err_quit("WSAStartup()");
|
|
||||||
return EXIT_FAILURE;
|
|
||||||
}
|
|
||||||
SOCKET listenSocket = socket(AF_INET6, SOCK_STREAM, 0);
|
|
||||||
if (listenSocket == INVALID_SOCKET) err_quit("socket()");
|
|
||||||
|
|
||||||
struct sockaddr_in6 serverAddr;
|
|
||||||
memset(&serverAddr, 0, sizeof(serverAddr));
|
|
||||||
serverAddr.sin6_family = AF_INET6;
|
|
||||||
inet_pton(AF_INET6, LISTENIP, &serverAddr.sin6_addr);
|
|
||||||
serverAddr.sin6_port = htons(LISTENPORT);
|
|
||||||
|
|
||||||
if (SOCKET_ERROR ==
|
|
||||||
bind(listenSocket, (struct sockaddr *)&serverAddr, sizeof(serverAddr)))
|
|
||||||
err_quit("bind()");
|
|
||||||
if (SOCKET_ERROR == listen(listenSocket, SOMAXCONN)) err_quit("listen()");
|
|
||||||
|
|
||||||
SOCKET client_sock;
|
|
||||||
struct sockaddr_in6 client_addr;
|
|
||||||
socklen_t addr_len;
|
|
||||||
|
|
||||||
addr_len = sizeof(client_addr);
|
|
||||||
printf("Waiting for connection...\n");
|
|
||||||
client_sock =
|
|
||||||
accept(listenSocket, (struct sockaddr *)&client_addr, &addr_len);
|
|
||||||
if (client_sock == INVALID_SOCKET) err_quit("accept()");
|
|
||||||
printf("Connection established!\n");
|
|
||||||
|
|
||||||
char buf[BUFSIZE + 1];
|
|
||||||
|
|
||||||
while (2) {
|
|
||||||
int retVal = recv(client_sock, buf, BUFSIZE, 0);
|
|
||||||
if (retVal == SOCKET_ERROR)
|
|
||||||
err_display("recv()");
|
|
||||||
else if (retVal == 0)
|
|
||||||
break;
|
|
||||||
|
|
||||||
char ipv6str[INET6_ADDRSTRLEN];
|
|
||||||
inet_ntop(AF_INET6, &client_addr, ipv6str, sizeof(ipv6str));
|
|
||||||
buf[retVal] = '\0';
|
|
||||||
printf("[TCP/%s:%d] %s\n", ipv6str, ntohs(client_addr.sin6_port), buf);
|
|
||||||
|
|
||||||
if (SOCKET_ERROR == send(client_sock, buf, BUFSIZE, 0))
|
|
||||||
err_display("send()");
|
|
||||||
}
|
|
||||||
|
|
||||||
WSACleanup();
|
|
||||||
}
|
|
||||||
201
Server/src/server.cpp
Normal file
201
Server/src/server.cpp
Normal file
@@ -0,0 +1,201 @@
|
|||||||
|
#include "glfw/glfw_initialization.h"
|
||||||
|
#include "glfw/glfw_monitor.h"
|
||||||
|
#include "glfw/glfw_window.h"
|
||||||
|
#include "socket/iocp.h"
|
||||||
|
#include "socket/packet.h"
|
||||||
|
#include "socket/tcp_socket.h"
|
||||||
|
#include "socket/udp_socket.h"
|
||||||
|
#include "socket/wsa_manager.h"
|
||||||
|
#include "utils/log.h"
|
||||||
|
#include "utils/snowflake.h"
|
||||||
|
#include "vulkan_engine/asset/object/model.h"
|
||||||
|
|
||||||
|
#define LISTENIP "::"
|
||||||
|
#define LISTENPORT 9010
|
||||||
|
std::uint8_t CLIENTID = 0;
|
||||||
|
|
||||||
|
std::unordered_map<utils::Snowflake, veng::Model> MODELS;
|
||||||
|
std::mutex MODELS_MUTEX;
|
||||||
|
std::vector<std::pair<Network::TCPSocket, double>> CLIENTS;
|
||||||
|
std::mutex CLIENTS_MUTEX;
|
||||||
|
|
||||||
|
void recv_fully(Network::IOCP* iocp, Network::TCPSocket& sock,
|
||||||
|
std::vector<char>& buffer) {
|
||||||
|
size_t total_received = 0;
|
||||||
|
size_t expected = buffer.size();
|
||||||
|
|
||||||
|
while (total_received < expected) {
|
||||||
|
if (total_received != 0)
|
||||||
|
Sleep(100); // 너무 빨리 재시도해서 큐가 채워질 틈이 없는 듯?
|
||||||
|
if (sock.sock == 0) return;
|
||||||
|
std::vector<char> temp(expected - total_received);
|
||||||
|
|
||||||
|
size_t received = iocp->recv(sock, temp);
|
||||||
|
|
||||||
|
std::copy(temp.begin(), temp.begin() + received,
|
||||||
|
buffer.begin() + total_received);
|
||||||
|
total_received += received;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void EchoClient(utils::ThreadPool* tp, Network::IOCP* iocp,
|
||||||
|
Network::TCPSocket* NewSock, Network::Address NewAddr) {
|
||||||
|
if (NewSock->sock == 0) return;
|
||||||
|
std::vector<char> recv_data(6);
|
||||||
|
recv_fully(iocp, *NewSock, recv_data);
|
||||||
|
Packet::Header header;
|
||||||
|
header.Deserialize(recv_data);
|
||||||
|
recv_data.resize(header.body_length);
|
||||||
|
recv_fully(iocp, *NewSock, recv_data);
|
||||||
|
|
||||||
|
switch (header.opcode) {
|
||||||
|
case Packet::Opcode::UPDATEMODEL: {
|
||||||
|
veng::Model model;
|
||||||
|
model.Deserialize(recv_data);
|
||||||
|
|
||||||
|
std::lock_guard lock(MODELS_MUTEX);
|
||||||
|
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].needsUpdate = true;
|
||||||
|
} break;
|
||||||
|
default:
|
||||||
|
spdlog::error("unknown data type");
|
||||||
|
}
|
||||||
|
|
||||||
|
tp->enqueueJob(EchoClient, iocp, NewSock, NewAddr);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ClientRefresher(utils::ThreadPool* tp, Network::IOCP* iocp) {
|
||||||
|
static auto last_all_updated = glfwGetTime();
|
||||||
|
static auto last_frame_time_ = glfwGetTime();
|
||||||
|
auto current_time = glfwGetTime();
|
||||||
|
bool needs_all_update = false;
|
||||||
|
if (current_time - last_all_updated >= 100) {
|
||||||
|
needs_all_update = true;
|
||||||
|
last_all_updated = glfwGetTime();
|
||||||
|
}
|
||||||
|
auto delta_time = current_time - last_frame_time_;
|
||||||
|
last_frame_time_ = current_time;
|
||||||
|
|
||||||
|
for (auto it = CLIENTS.begin(); it != CLIENTS.end();) {
|
||||||
|
if (it->first.sock == 0) {
|
||||||
|
it = CLIENTS.erase(it);
|
||||||
|
} else {
|
||||||
|
++it;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto it = MODELS.begin(); it != MODELS.end();) {
|
||||||
|
auto& model = it->second;
|
||||||
|
|
||||||
|
if (std::abs(model.lifespan + 1.f) <
|
||||||
|
std::numeric_limits<float>::epsilon()) {
|
||||||
|
++it;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (model.lifespan <= 0.f) {
|
||||||
|
it = MODELS.erase(it);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (model.shouldBeDestroyed) {
|
||||||
|
it = MODELS.erase(it);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
model.lifespan -= delta_time;
|
||||||
|
++it;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto& client : CLIENTS) {
|
||||||
|
if (client.first.sock == 0) continue;
|
||||||
|
for (auto& model : MODELS) {
|
||||||
|
if (model.second.name == "camera_lag") continue;
|
||||||
|
std::vector<char> send_data;
|
||||||
|
model.second.Update(delta_time + (client.second / 2));
|
||||||
|
auto model_serialized = model.second.Serialize();
|
||||||
|
Packet::Header header;
|
||||||
|
header.opcode = Packet::Opcode::UPDATEMODEL;
|
||||||
|
header.body_length = model_serialized.size();
|
||||||
|
auto header_serialized = header.Serialize();
|
||||||
|
send_data.insert(send_data.end(), header_serialized.begin(),
|
||||||
|
header_serialized.end());
|
||||||
|
send_data.insert(send_data.end(), model_serialized.begin(),
|
||||||
|
model_serialized.end());
|
||||||
|
if (!model.second.needsUpdate && !needs_all_update) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
model.second.needsUpdate = false;
|
||||||
|
if (client.first.send(send_data.data(), send_data.size(), 0) == -1) {
|
||||||
|
client.first.sock = 0;
|
||||||
|
}
|
||||||
|
spdlog::info("updated: {}", model.second.name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
tp->enqueueJob(ClientRefresher, 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::ThreadPool tp(0);
|
||||||
|
Network::IOCP iocp;
|
||||||
|
iocp.init(&tp, SessionProtocol::TCP);
|
||||||
|
|
||||||
|
Network::Address addr;
|
||||||
|
|
||||||
|
addr.set(AF_INET6, "::", 9010);
|
||||||
|
|
||||||
|
Network::Socket* sock;
|
||||||
|
Network::TCPSocket TCPSock;
|
||||||
|
TCPSock.init(AF_INET6);
|
||||||
|
sock = &TCPSock;
|
||||||
|
if (TCPSock.bind(addr) == INVALID_SOCKET) {
|
||||||
|
spdlog::error("bind()");
|
||||||
|
std::exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
if (TCPSock.listen(SOMAXCONN) == INVALID_SOCKET) {
|
||||||
|
spdlog::error("listen()");
|
||||||
|
std::exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
tp.enqueueJob(ClientRefresher, &iocp);
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
Network::TCPSocket NewSock;
|
||||||
|
Network::Address NewAddr;
|
||||||
|
NewAddr.length = addr.length;
|
||||||
|
spdlog::info("Waiting for connection");
|
||||||
|
TCPSock.accept(NewSock, NewAddr);
|
||||||
|
|
||||||
|
iocp.registerTCPSocket(NewSock, 16 * 1024);
|
||||||
|
|
||||||
|
Packet::Header header;
|
||||||
|
header.opcode = Packet::Opcode::CLIENTID;
|
||||||
|
header.body_length = 9;
|
||||||
|
auto packet = header.Serialize();
|
||||||
|
packet.resize(packet.size() + 9);
|
||||||
|
std::lock_guard lock(CLIENTS_MUTEX);
|
||||||
|
std::uint8_t client_id = CLIENTS.size() - 1;
|
||||||
|
::memcpy(packet.data() + 6, &client_id, 1);
|
||||||
|
|
||||||
|
std::double_t timestamp = glfwGetTime();
|
||||||
|
|
||||||
|
::memcpy(packet.data() + 7, ×tamp, 8);
|
||||||
|
iocp.send(NewSock, packet);
|
||||||
|
std::vector<char> header_buf(6);
|
||||||
|
recv_fully(&iocp, NewSock, header_buf);
|
||||||
|
auto ping = glfwGetTime() - timestamp;
|
||||||
|
CLIENTS.emplace_back(std::move(NewSock), ping);
|
||||||
|
|
||||||
|
tp.enqueueJob(EchoClient, &iocp, &CLIENTS.back().first, NewAddr);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -11,31 +11,7 @@ Session::Session(gsl::not_null<Network::IOCP*> iocp, utils::ThreadPool* tp,
|
|||||||
: iocp_(iocp), tp_(tp), proto_(proto) {
|
: iocp_(iocp), tp_(tp), proto_(proto) {
|
||||||
switch (proto) {
|
switch (proto) {
|
||||||
case SessionProtocol::UDP:
|
case SessionProtocol::UDP:
|
||||||
case SessionProtocol::QUIC: {
|
case SessionProtocol::QUIC:
|
||||||
Network::UDPSocket* sock = new Network::UDPSocket();
|
|
||||||
sock_ = sock;
|
|
||||||
sock->init(addr.family);
|
|
||||||
switch (type) {
|
|
||||||
case SessionType::CONNECT:
|
|
||||||
ssl_ctx_ = ::SSL_CTX_new(::OSSL_QUIC_client_method());
|
|
||||||
if (ssl_ctx_ == nullptr) {
|
|
||||||
spdlog::critical("SSL_CTX_new()");
|
|
||||||
std::exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
::SSL_CTX_set_verify(ssl_ctx_, SSL_VERIFY_PEER, NULL);
|
|
||||||
|
|
||||||
if (!::SSL_CTX_set_default_verify_paths(ssl_ctx_)) {
|
|
||||||
spdlog::critical("SSL_CTX_set_default_verify_paths()");
|
|
||||||
std::exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case SessionType::LISTEN:
|
|
||||||
sock->bind(addr);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
std::exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
} break;
|
|
||||||
case SessionProtocol::TCP:
|
case SessionProtocol::TCP:
|
||||||
case SessionProtocol::TLS: {
|
case SessionProtocol::TLS: {
|
||||||
Network::TCPSocket* sock = new Network::TCPSocket();
|
Network::TCPSocket* sock = new Network::TCPSocket();
|
||||||
@@ -56,12 +32,6 @@ Session::Session(gsl::not_null<Network::IOCP*> iocp, utils::ThreadPool* tp,
|
|||||||
default:
|
default:
|
||||||
std::exit(EXIT_FAILURE);
|
std::exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
ssl_ = ::SSL_new(ssl_ctx_);
|
|
||||||
if (ssl_ == nullptr) {
|
|
||||||
spdlog::critical("SSL_new()");
|
|
||||||
std::exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Session::~Session() {
|
Session::~Session() {
|
||||||
|
|||||||
@@ -11,32 +11,95 @@ IOCP::IOCP() {
|
|||||||
|
|
||||||
IOCP::~IOCP() { destruct(); }
|
IOCP::~IOCP() { destruct(); }
|
||||||
|
|
||||||
|
void IOCP::init(utils::ThreadPool* __IOCPThread, SessionProtocol proto) {
|
||||||
|
IOCPThread_ = __IOCPThread;
|
||||||
|
proto_ = proto;
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
completionPort_ = ::CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 0);
|
||||||
|
if (completionPort_ == NULL) {
|
||||||
|
spdlog::critical("CreateIoCompletionPort()");
|
||||||
|
std::exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
int tCount = __IOCPThread->threadCount;
|
||||||
|
|
||||||
|
spdlog::info("Resizing threadpool size to: {}", tCount * 2);
|
||||||
|
|
||||||
|
__IOCPThread->respawnWorker(tCount * 2);
|
||||||
|
|
||||||
|
for (int i = 0; i < tCount; i++)
|
||||||
|
IOCPThread_->enqueueJob(
|
||||||
|
[this](utils::ThreadPool* th, std::uint8_t __) { iocpWatcher_(th); },
|
||||||
|
0);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
void IOCP::destruct() {
|
void IOCP::destruct() {
|
||||||
#ifdef __linux__
|
#ifdef __linux__
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void IOCP::registerSocket(IOCPPASSINDATA* data) {
|
void IOCP::registerTCPSocket(Socket& sock, std::uint32_t bufsize) {
|
||||||
|
#ifdef _WIN32
|
||||||
|
HANDLE returnData = ::CreateIoCompletionPort((HANDLE)sock.sock,
|
||||||
|
completionPort_, sock.sock, 0);
|
||||||
|
if (returnData == 0) completionPort_ = returnData;
|
||||||
|
|
||||||
|
IOCPPASSINDATA* recv_data = new IOCPPASSINDATA(bufsize);
|
||||||
|
recv_data->event = IOCPEVENT::READ;
|
||||||
|
recv_data->socket = std::make_shared<Socket>(sock);
|
||||||
|
recv_data->IOCPInstance = this;
|
||||||
|
DWORD recvbytes = 0, flags = 0;
|
||||||
|
|
||||||
|
int result = SOCKET_ERROR;
|
||||||
|
|
||||||
|
result = ::WSARecv(recv_data->socket->sock, &recv_data->wsabuf, 1, &recvbytes,
|
||||||
|
&flags, &recv_data->overlapped, NULL);
|
||||||
|
if (result == SOCKET_ERROR) {
|
||||||
|
int err = ::WSAGetLastError();
|
||||||
|
if (err != WSA_IO_PENDING) {
|
||||||
|
auto err_msg = std::format("WSARecv failed: {}", err);
|
||||||
|
throw std::runtime_error(err_msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
void IOCP::registerUDPSocket(IOCPPASSINDATA* data, Address recv_addr) {
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
HANDLE returnData = ::CreateIoCompletionPort(
|
HANDLE returnData = ::CreateIoCompletionPort(
|
||||||
(HANDLE)data->socket->sock, completionPort_, data->socket->sock, 0);
|
(HANDLE)data->socket->sock, completionPort_, data->socket->sock, 0);
|
||||||
if (returnData == 0) completionPort_ = returnData;
|
if (returnData == 0) completionPort_ = returnData;
|
||||||
|
|
||||||
data->event = IOCPEVENT::READ;
|
IOCPPASSINDATA* recv_data = new IOCPPASSINDATA(data->bufsize);
|
||||||
|
recv_data->event = IOCPEVENT::READ;
|
||||||
|
recv_data->socket = data->socket;
|
||||||
DWORD recvbytes = 0, flags = 0;
|
DWORD recvbytes = 0, flags = 0;
|
||||||
::WSARecv(data->socket->sock, &data->wsabuf, 1, &recvbytes, &flags,
|
|
||||||
&data->overlapped, NULL);
|
int result = SOCKET_ERROR;
|
||||||
|
|
||||||
|
::WSARecvFrom(recv_data->socket->sock, &recv_data->wsabuf, 1, &recvbytes,
|
||||||
|
&flags, &recv_addr.addr, &recv_addr.length,
|
||||||
|
&recv_data->overlapped, NULL);
|
||||||
|
|
||||||
|
if (result == SOCKET_ERROR) {
|
||||||
|
int err = ::WSAGetLastError();
|
||||||
|
if (err != WSA_IO_PENDING) {
|
||||||
|
auto err_msg = std::format("WSARecv failed: {}", err);
|
||||||
|
throw std::runtime_error(err_msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
int IOCP::recv(IOCPPASSINDATA* data) {
|
int IOCP::recv(Socket& sock, std::vector<char>& data) {
|
||||||
SOCKET sock = data->socket->sock;
|
std::lock_guard lock(*GetRecvQueueMutex_(sock.sock));
|
||||||
std::lock_guard lock(*GetRecvQueueMutex_(sock));
|
auto queue = GetRecvQueue_(sock.sock);
|
||||||
auto queue = GetRecvQueue_(sock);
|
|
||||||
|
|
||||||
std::uint32_t left_data = data->wsabuf.len;
|
std::uint32_t left_data = data.size();
|
||||||
std::uint32_t copied = 0;
|
std::uint32_t copied = 0;
|
||||||
|
|
||||||
while (!queue->empty() && left_data != 0) {
|
while (!queue->empty() && left_data != 0) {
|
||||||
@@ -47,7 +110,7 @@ int IOCP::recv(IOCPPASSINDATA* data) {
|
|||||||
std::uint32_t available = front.first.size() - offset;
|
std::uint32_t available = front.first.size() - offset;
|
||||||
std::uint32_t to_copy = (left_data < available) ? left_data : available;
|
std::uint32_t to_copy = (left_data < available) ? left_data : available;
|
||||||
|
|
||||||
::memcpy(data->wsabuf.buf + copied, front.first.data() + offset, to_copy);
|
::memcpy(data.data() + copied, front.first.data() + offset, to_copy);
|
||||||
copied += to_copy;
|
copied += to_copy;
|
||||||
left_data -= to_copy;
|
left_data -= to_copy;
|
||||||
offset += to_copy;
|
offset += to_copy;
|
||||||
@@ -62,23 +125,95 @@ int IOCP::recv(IOCPPASSINDATA* data) {
|
|||||||
return copied;
|
return copied;
|
||||||
}
|
}
|
||||||
|
|
||||||
int IOCP::send(SOCKET sock, std::vector<IOCPPASSINDATA*>* data) {
|
int IOCP::send(Socket& sock, std::vector<char>& data) {
|
||||||
auto lk = GetSendQueueMutex_(sock);
|
auto lk = GetSendQueueMutex_(sock.sock);
|
||||||
auto queue = GetSendQueue_(sock);
|
auto queue = GetSendQueue_(sock.sock);
|
||||||
std::lock_guard lock(*lk);
|
std::lock_guard lock(*lk);
|
||||||
for (auto& it : *data) {
|
|
||||||
it->event = IOCPEVENT::WRITE;
|
Network::IOCPPASSINDATA* packet = new Network::IOCPPASSINDATA(data.size());
|
||||||
queue->push_back(it);
|
packet->event = IOCPEVENT::WRITE;
|
||||||
}
|
packet->socket = std::make_shared<Network::Socket>(sock);
|
||||||
|
packet->IOCPInstance = this;
|
||||||
|
::memcpy(packet->wsabuf.buf, data.data(), data.size());
|
||||||
|
packet->wsabuf.len = data.size();
|
||||||
|
queue->push_back(packet);
|
||||||
|
|
||||||
IOCPThread_->enqueueJob(
|
IOCPThread_->enqueueJob(
|
||||||
[this, sock](utils::ThreadPool* th, std::uint8_t __) {
|
[this, sock = sock.sock](utils::ThreadPool* th, std::uint8_t __) {
|
||||||
packet_sender_(sock);
|
packet_sender_(sock);
|
||||||
},
|
},
|
||||||
0);
|
0);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int IOCP::GetRecvedBytes(SOCKET sock) {
|
||||||
|
auto queue = GetRecvQueue_(sock);
|
||||||
|
std::lock_guard lock(socket_mod_mutex_);
|
||||||
|
|
||||||
|
int bytes = 0;
|
||||||
|
for (auto it : *queue) {
|
||||||
|
bytes += it.first.size() - it.second;
|
||||||
|
}
|
||||||
|
|
||||||
|
return bytes;
|
||||||
|
}
|
||||||
|
|
||||||
|
void IOCP::iocpWatcher_(utils::ThreadPool* IOCPThread) {
|
||||||
|
IOCPPASSINDATA* data;
|
||||||
|
SOCKET sock;
|
||||||
|
DWORD cbTransfrred;
|
||||||
|
int jitter = jitterDist_(gen_);
|
||||||
|
int retVal = GetQueuedCompletionStatus(completionPort_, &cbTransfrred,
|
||||||
|
(PULONG_PTR)&sock,
|
||||||
|
(LPOVERLAPPED*)&data, 1000 + jitter);
|
||||||
|
|
||||||
|
if (retVal == 0 || cbTransfrred == 0) {
|
||||||
|
DWORD lasterror = GetLastError();
|
||||||
|
if (lasterror == WAIT_TIMEOUT) {
|
||||||
|
IOCPThread->enqueueJob(
|
||||||
|
[this](utils::ThreadPool* th, std::uint8_t __) { iocpWatcher_(th); },
|
||||||
|
0);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
data->event = IOCPEVENT::QUIT;
|
||||||
|
spdlog::debug("Disconnected. [{}]",
|
||||||
|
(std::string)(data->socket->remoteAddr));
|
||||||
|
delete data;
|
||||||
|
IOCPThread->enqueueJob(
|
||||||
|
[this](utils::ThreadPool* th, std::uint8_t __) { iocpWatcher_(th); },
|
||||||
|
0);
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
data->transferredbytes = cbTransfrred;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<char> buf(16384); // SSL_read최대 반환 크기
|
||||||
|
int red_data = 0;
|
||||||
|
std::lock_guard lock(*GetRecvQueueMutex_(sock));
|
||||||
|
auto queue_list = GetRecvQueue_(sock);
|
||||||
|
if (data->event == IOCPEVENT::READ) {
|
||||||
|
::memcpy(buf.data(), data->wsabuf.buf, data->transferredbytes);
|
||||||
|
queue_list->emplace_back(std::make_pair(
|
||||||
|
std::vector<char>(buf.begin(), buf.begin() + data->transferredbytes),
|
||||||
|
0));
|
||||||
|
DWORD recvbytes = 0, flags = 0;
|
||||||
|
|
||||||
|
IOCPPASSINDATA* recv_data = new IOCPPASSINDATA(data->bufsize);
|
||||||
|
recv_data->event = IOCPEVENT::READ;
|
||||||
|
recv_data->socket = data->socket;
|
||||||
|
|
||||||
|
delete data;
|
||||||
|
::WSARecv(recv_data->socket->sock, &recv_data->wsabuf, 1, &recvbytes,
|
||||||
|
&flags, &recv_data->overlapped, NULL);
|
||||||
|
} else { // WRITE 시, 무시한다.
|
||||||
|
spdlog::debug("writed {} bytes to {}", cbTransfrred,
|
||||||
|
(std::string)(data->socket->remoteAddr));
|
||||||
|
delete data;
|
||||||
|
}
|
||||||
|
IOCPThread->enqueueJob(
|
||||||
|
[this](utils::ThreadPool* th, std::uint8_t __) { iocpWatcher_(th); }, 0);
|
||||||
|
}
|
||||||
|
|
||||||
std::shared_ptr<std::list<IOCPPASSINDATA*>> IOCP::GetSendQueue_(SOCKET sock) {
|
std::shared_ptr<std::list<IOCPPASSINDATA*>> IOCP::GetSendQueue_(SOCKET sock) {
|
||||||
std::lock_guard lock(socket_mod_mutex_);
|
std::lock_guard lock(socket_mod_mutex_);
|
||||||
if (send_queue_.find(sock) == send_queue_.end()) {
|
if (send_queue_.find(sock) == send_queue_.end()) {
|
||||||
@@ -130,32 +265,10 @@ void IOCP::packet_sender_(SOCKET sock) {
|
|||||||
|
|
||||||
int data_len = 0;
|
int data_len = 0;
|
||||||
|
|
||||||
if (proto_ == SessionProtocol::TLS || proto_ == SessionProtocol::QUIC) {
|
data_len = front->wsabuf.len;
|
||||||
int ret = ::SSL_write(front->ssl, front->wsabuf.buf, front->wsabuf.len);
|
wsabuf.buf = front->wsabuf.buf;
|
||||||
if (ret <= 0) {
|
wsabuf.len = data_len;
|
||||||
int err = ::SSL_get_error(front->ssl, ret);
|
::WSASend(sock, &wsabuf, 1, &sendbytes, 0, nullptr, nullptr);
|
||||||
if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) {
|
|
||||||
queue->push_front(front);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
std::unique_lock lk(socket_mod_mutex_);
|
|
||||||
send_queue_.erase(sock);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
while ((data_len = ::BIO_read(front->wbio, buf.data(), buf.size())) > 0) {
|
|
||||||
wsabuf.buf = buf.data();
|
|
||||||
wsabuf.len = data_len;
|
|
||||||
|
|
||||||
::WSASend(sock, &wsabuf, 1, &sendbytes, 0, nullptr, nullptr);
|
|
||||||
}
|
|
||||||
|
|
||||||
} else {
|
|
||||||
data_len = front->wsabuf.len;
|
|
||||||
wsabuf.buf = front->wsabuf.buf;
|
|
||||||
wsabuf.len = data_len;
|
|
||||||
::WSASend(sock, &wsabuf, 1, &sendbytes, 0, nullptr, nullptr);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ int Socket::init(int domain, int type, int protocol) {
|
|||||||
this->domain = domain;
|
this->domain = domain;
|
||||||
|
|
||||||
sock = ::socket(domain, type, protocol);
|
sock = ::socket(domain, type, protocol);
|
||||||
|
// sock = ::WSASocket(domain, type, protocol, NULL, 0, WSA_FLAG_OVERLAPPED);
|
||||||
if (sock == INVALID_SOCKET) spdlog::critical("socket()");
|
if (sock == INVALID_SOCKET) spdlog::critical("socket()");
|
||||||
|
|
||||||
valid_ = true;
|
valid_ = true;
|
||||||
@@ -27,6 +28,7 @@ void Socket::destruct() {
|
|||||||
::close(sock);
|
::close(sock);
|
||||||
#endif
|
#endif
|
||||||
valid_ = false;
|
valid_ = false;
|
||||||
|
sock = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
Socket::operator SOCKET() {
|
Socket::operator SOCKET() {
|
||||||
@@ -40,7 +42,7 @@ Socket::operator SOCKET() {
|
|||||||
|
|
||||||
void Socket::set(const SOCKET __sock, int __domain) {
|
void Socket::set(const SOCKET __sock, int __domain) {
|
||||||
if (__sock == INVALID_SOCKET) {
|
if (__sock == INVALID_SOCKET) {
|
||||||
spdlog::critical("socket()");
|
spdlog::error("set failed errno:{} {}", errno, strerror(errno));
|
||||||
std::exit(EXIT_FAILURE);
|
std::exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -65,6 +67,26 @@ int Socket::bind(Address __addr) {
|
|||||||
return retVal;
|
return retVal;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int Socket::connect(Address& serveraddr) {
|
||||||
|
std::string addr_string = serveraddr;
|
||||||
|
int retVal = -1;
|
||||||
|
if (serveraddr.family == AF_INET)
|
||||||
|
retVal = ::connect(sock, (const sockaddr*)&serveraddr.addr_in,
|
||||||
|
serveraddr.length);
|
||||||
|
else
|
||||||
|
retVal = ::connect(sock, (const sockaddr*)&serveraddr.addr_in6,
|
||||||
|
serveraddr.length);
|
||||||
|
memcpy(&remoteAddr, &serveraddr, sizeof(Address));
|
||||||
|
if (retVal == INVALID_SOCKET) {
|
||||||
|
#ifdef _WIN32
|
||||||
|
int err = WSAGetLastError();
|
||||||
|
spdlog::error("connect() failed: WSA error {} (0x{:X})", err, err);
|
||||||
|
#endif
|
||||||
|
spdlog::error("connect()");
|
||||||
|
}
|
||||||
|
return retVal;
|
||||||
|
}
|
||||||
|
|
||||||
int Socket::recvfrom(void* __restrict __buf, size_t __n, int __flags,
|
int Socket::recvfrom(void* __restrict __buf, size_t __n, int __flags,
|
||||||
struct Address& __addr) {
|
struct Address& __addr) {
|
||||||
std::lock_guard<std::mutex> lock(readMutex);
|
std::lock_guard<std::mutex> lock(readMutex);
|
||||||
|
|||||||
@@ -11,9 +11,14 @@ int TCPSocket::listen(int __n) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void TCPSocket::accept(TCPSocket &newSock, Address &__addr) {
|
void TCPSocket::accept(TCPSocket &newSock, Address &__addr) {
|
||||||
newSock.set(::accept(sock, &__addr.addr, &__addr.length), domain);
|
SOCKET newsock = INVALID_SOCKET;
|
||||||
|
newsock = ::accept(sock, &__addr.addr, &__addr.length);
|
||||||
|
if (newsock == INVALID_SOCKET) {
|
||||||
|
spdlog::error("accept() errno:{} {}", errno, strerror(errno));
|
||||||
|
std::exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
newSock.set(newsock, domain);
|
||||||
memcpy(&newSock.remoteAddr, &__addr, sizeof(Address));
|
memcpy(&newSock.remoteAddr, &__addr, sizeof(Address));
|
||||||
if (newSock.sock == INVALID_SOCKET) spdlog::error("accept()");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int TCPSocket::connect(Address &serveraddr) {
|
int TCPSocket::connect(Address &serveraddr) {
|
||||||
|
|||||||
@@ -4,4 +4,24 @@ namespace Network {
|
|||||||
|
|
||||||
int UDPSocket::init(int domain) { return init(domain, SOCK_DGRAM, 0); }
|
int UDPSocket::init(int domain) { return init(domain, SOCK_DGRAM, 0); }
|
||||||
|
|
||||||
|
int UDPSocket::connect(Address &serveraddr) {
|
||||||
|
std::string addr_string = serveraddr;
|
||||||
|
int retVal = -1;
|
||||||
|
if (serveraddr.family == AF_INET)
|
||||||
|
retVal = ::connect(sock, (const sockaddr *)&serveraddr.addr_in,
|
||||||
|
serveraddr.length);
|
||||||
|
else
|
||||||
|
retVal = ::connect(sock, (const sockaddr *)&serveraddr.addr_in6,
|
||||||
|
serveraddr.length);
|
||||||
|
memcpy(&remoteAddr, &serveraddr, sizeof(Address));
|
||||||
|
if (retVal == INVALID_SOCKET) {
|
||||||
|
#ifdef _WIN32
|
||||||
|
int err = WSAGetLastError();
|
||||||
|
spdlog::error("connect() failed: WSA error {} (0x{:X})", err, err);
|
||||||
|
#endif
|
||||||
|
spdlog::error("connect()");
|
||||||
|
}
|
||||||
|
return retVal;
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace Network
|
} // namespace Network
|
||||||
|
|||||||
@@ -9,13 +9,15 @@ static struct EpochInitializer {
|
|||||||
std::chrono::system_clock::time_point EPOCH;
|
std::chrono::system_clock::time_point EPOCH;
|
||||||
} epochInitializer;
|
} epochInitializer;
|
||||||
|
|
||||||
Snowflake GenerateID() {
|
Snowflake GenerateID(std::uint16_t instance) {
|
||||||
static std::mutex snowflakeGenerateMutex_;
|
static std::mutex snowflakeGenerateMutex_;
|
||||||
|
|
||||||
std::lock_guard<std::mutex> lock(snowflakeGenerateMutex_);
|
std::lock_guard<std::mutex> lock(snowflakeGenerateMutex_);
|
||||||
|
|
||||||
std::size_t tid =
|
std::size_t tid =
|
||||||
std::hash<std::thread::id>{}(std::this_thread::get_id());
|
std::hash<std::thread::id>{}(std::this_thread::get_id());
|
||||||
|
instance = instance << 3;
|
||||||
|
tid += instance;
|
||||||
|
|
||||||
thread_local static int sequence = 0;
|
thread_local static int sequence = 0;
|
||||||
Snowflake id = {};
|
Snowflake id = {};
|
||||||
|
|||||||
109
impl/vulkan_engine/asset/object/model.cpp
Normal file
109
impl/vulkan_engine/asset/object/model.cpp
Normal file
@@ -0,0 +1,109 @@
|
|||||||
|
#include "vulkan_engine/asset/object/model.h"
|
||||||
|
|
||||||
|
#include "vulkan_engine/vulkan/graphics.h"
|
||||||
|
|
||||||
|
namespace veng {
|
||||||
|
|
||||||
|
Model::~Model() {
|
||||||
|
if (graphics_ == nullptr) return;
|
||||||
|
|
||||||
|
graphics_->DestroyTexture(material.texture_handle);
|
||||||
|
graphics_->DestroyBuffer(vertex_buffer);
|
||||||
|
graphics_->DestroyBuffer(index_buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
void Append(std::vector<char>& buffer, const T& value) {
|
||||||
|
const char* data = reinterpret_cast<const char*>(&value);
|
||||||
|
buffer.insert(buffer.end(), data, data + sizeof(T));
|
||||||
|
}
|
||||||
|
|
||||||
|
void Append(std::vector<char>& buffer, const std::string& value) {
|
||||||
|
size_t length = value.size();
|
||||||
|
Append(buffer, length);
|
||||||
|
buffer.insert(buffer.end(), value.begin(), value.end());
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
void Read(const std::vector<char>& buffer, size_t& offset, T& out) {
|
||||||
|
std::memcpy(&out, buffer.data() + offset, sizeof(T));
|
||||||
|
offset += sizeof(T);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Read(const std::vector<char>& buffer, size_t& offset, std::string& out) {
|
||||||
|
size_t length = 0;
|
||||||
|
Read(buffer, offset, length);
|
||||||
|
out.assign(buffer.data() + offset, length);
|
||||||
|
offset += length;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<char> Model::Serialize() {
|
||||||
|
std::vector<char> buffer;
|
||||||
|
|
||||||
|
Append(buffer, ID);
|
||||||
|
Append(buffer, name);
|
||||||
|
Append(buffer, asset_name);
|
||||||
|
|
||||||
|
Append(buffer, position);
|
||||||
|
Append(buffer, linear_velocity);
|
||||||
|
Append(buffer, linear_acceleration);
|
||||||
|
|
||||||
|
Append(buffer, rotation);
|
||||||
|
Append(buffer, angular_velocity);
|
||||||
|
Append(buffer, angular_acceleration);
|
||||||
|
|
||||||
|
Append(buffer, scale);
|
||||||
|
Append(buffer, transform);
|
||||||
|
|
||||||
|
Append(buffer, original_offset);
|
||||||
|
Append(buffer, radius);
|
||||||
|
Append(buffer, lifespan);
|
||||||
|
|
||||||
|
Append(buffer, visible);
|
||||||
|
Append(buffer, colision);
|
||||||
|
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Model::Deserialize(std::vector<char> data) {
|
||||||
|
size_t offset = 0;
|
||||||
|
|
||||||
|
Read(data, offset, ID);
|
||||||
|
Read(data, offset, name);
|
||||||
|
Read(data, offset, asset_name);
|
||||||
|
|
||||||
|
Read(data, offset, position);
|
||||||
|
Read(data, offset, linear_velocity);
|
||||||
|
Read(data, offset, linear_acceleration);
|
||||||
|
|
||||||
|
Read(data, offset, rotation);
|
||||||
|
Read(data, offset, angular_velocity);
|
||||||
|
Read(data, offset, angular_acceleration);
|
||||||
|
|
||||||
|
Read(data, offset, scale);
|
||||||
|
Read(data, offset, transform);
|
||||||
|
|
||||||
|
Read(data, offset, original_offset);
|
||||||
|
Read(data, offset, radius);
|
||||||
|
Read(data, offset, lifespan);
|
||||||
|
|
||||||
|
Read(data, offset, visible);
|
||||||
|
Read(data, offset, colision);
|
||||||
|
}
|
||||||
|
|
||||||
|
void veng::Model::Update(float dt) {
|
||||||
|
linear_velocity += linear_acceleration * dt;
|
||||||
|
position += linear_velocity * dt;
|
||||||
|
|
||||||
|
angular_velocity += angular_acceleration * dt;
|
||||||
|
if (glm::length(angular_velocity) > 1e-6f) {
|
||||||
|
rotation =
|
||||||
|
glm::normalize(glm::rotate(rotation, glm::length(angular_velocity * dt),
|
||||||
|
glm::normalize(angular_velocity)));
|
||||||
|
}
|
||||||
|
|
||||||
|
transform = glm::translate(glm::mat4(1.0f), position) *
|
||||||
|
glm::mat4_cast(rotation) * glm::scale(glm::mat4(1.0f), scale);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace veng
|
||||||
291
impl/vulkan_engine/vulkan/engine.cpp
Normal file
291
impl/vulkan_engine/vulkan/engine.cpp
Normal file
@@ -0,0 +1,291 @@
|
|||||||
|
#include "vulkan_engine/vulkan/engine.h"
|
||||||
|
|
||||||
|
#include "precomp.h"
|
||||||
|
#include "socket/packet.h"
|
||||||
|
|
||||||
|
namespace veng {
|
||||||
|
|
||||||
|
void Engine::init() {
|
||||||
|
glm::ivec2 window_size_ = vulkan_graphics->window->GetFramebufferSize();
|
||||||
|
view = glm::lookAt(glm::vec3(0.f, 0.f, -5.f), glm::vec3(0.f, 0.f, 0.f),
|
||||||
|
glm::vec3(0.f, -1.f, 0.f));
|
||||||
|
projection = glm::perspective(
|
||||||
|
glm::radians(103.f),
|
||||||
|
(std::float_t)window_size_.x / (std::float_t)window_size_.y, 0.1f,
|
||||||
|
1000.f);
|
||||||
|
vulkan_graphics->SetViewProjection(view, projection);
|
||||||
|
|
||||||
|
if (BeginPlay != nullptr) BeginPlay(*this);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Engine::LoadModelAsset(std::string path, std::string name) {
|
||||||
|
veng::Model model(vulkan_graphics);
|
||||||
|
asset_loader_.setPath(path);
|
||||||
|
asset_loader_.loadModel(model);
|
||||||
|
|
||||||
|
model.vertex_buffer = vulkan_graphics->CreateVertexBuffer(model.vertices);
|
||||||
|
model.index_buffer = vulkan_graphics->CreateIndexBuffer(model.indices);
|
||||||
|
model.material.texture_handle =
|
||||||
|
vulkan_graphics->CreateTexture(asset_loader_.readTexture());
|
||||||
|
model_assets_[name] = std::move(model);
|
||||||
|
}
|
||||||
|
|
||||||
|
const Model* Engine::GetStaticModel(std::string name) {
|
||||||
|
if (model_assets_.find(name) != model_assets_.end())
|
||||||
|
return &model_assets_[name];
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
Model* Engine::SpawnLifedModel(std::string asset_name, std::string name,
|
||||||
|
std::float_t lifespan) {
|
||||||
|
if (asset_name == "") {
|
||||||
|
Model model_to_spawn(nullptr);
|
||||||
|
model_to_spawn.asset_name = "";
|
||||||
|
model_to_spawn.name = name;
|
||||||
|
model_to_spawn.visible = false;
|
||||||
|
model_to_spawn.lifespan = lifespan;
|
||||||
|
models_[name] = std::move(model_to_spawn);
|
||||||
|
return &models_[name];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (models_.find(name) == models_.end()) {
|
||||||
|
Model model_to_spawn(*GetStaticModel(asset_name));
|
||||||
|
model_to_spawn.asset_name = asset_name;
|
||||||
|
model_to_spawn.name = name;
|
||||||
|
model_to_spawn.lifespan = lifespan;
|
||||||
|
models_[name] = std::move(model_to_spawn);
|
||||||
|
return &models_[name];
|
||||||
|
}
|
||||||
|
|
||||||
|
std::uint32_t i = 0;
|
||||||
|
for (i = 0; i < std::numeric_limits<std::uint32_t>::max();) {
|
||||||
|
if (models_.find(name + std::to_string(i)) == models_.end()) {
|
||||||
|
Model model_to_spawn(*GetStaticModel(asset_name));
|
||||||
|
model_to_spawn.asset_name = asset_name;
|
||||||
|
model_to_spawn.name = name + std::to_string(i);
|
||||||
|
model_to_spawn.lifespan = lifespan;
|
||||||
|
models_[name + std::to_string(i)] = std::move(model_to_spawn);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (i == std::numeric_limits<std::uint32_t>::max() - 1)
|
||||||
|
return nullptr;
|
||||||
|
else
|
||||||
|
return &models_[name + std::to_string(i)];
|
||||||
|
}
|
||||||
|
|
||||||
|
Model* Engine::GetSpawnedObject(std::string name) {
|
||||||
|
for (auto it = models_.begin(); it != models_.end();) {
|
||||||
|
if (it->first == name) return &it->second;
|
||||||
|
++it;
|
||||||
|
}
|
||||||
|
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Engine::Update() {
|
||||||
|
glm::ivec2 framebuffer_size = vulkan_graphics->window->GetFramebufferSize();
|
||||||
|
if (framebuffer_size != window_size_ && framebuffer_size.x != 0 &&
|
||||||
|
framebuffer_size.y != 0) {
|
||||||
|
window_size_ = framebuffer_size;
|
||||||
|
auto grater = (framebuffer_size.x > framebuffer_size.y)
|
||||||
|
? framebuffer_size.x
|
||||||
|
: framebuffer_size.y;
|
||||||
|
view = glm::lookAt(glm::vec3(0.f, 0.f, -5.f), glm::vec3(0.f, 0.f, 0.f),
|
||||||
|
glm::vec3(0.f, -1.f, 0.f));
|
||||||
|
projection = glm::perspective(
|
||||||
|
glm::radians(103.f),
|
||||||
|
(std::float_t)framebuffer_size.x / (std::float_t)framebuffer_size.y,
|
||||||
|
0.1f, 1000.f);
|
||||||
|
}
|
||||||
|
vulkan_graphics->SetViewProjection(view, projection);
|
||||||
|
|
||||||
|
if (vulkan_graphics->BeginFrame()) {
|
||||||
|
std::double_t current_time = glfwGetTime();
|
||||||
|
std::float_t delta_time =
|
||||||
|
static_cast<std::float_t>(current_time - last_frame_time_);
|
||||||
|
last_frame_time_ = current_time;
|
||||||
|
|
||||||
|
if (Tick != nullptr) Tick(*this, delta_time);
|
||||||
|
|
||||||
|
std::vector<Model*> models;
|
||||||
|
models.reserve(models_.size());
|
||||||
|
|
||||||
|
for (auto it = models_.begin(); it != models_.end();) {
|
||||||
|
auto& model = it->second;
|
||||||
|
|
||||||
|
if (std::abs(model.lifespan + 1.f) <
|
||||||
|
std::numeric_limits<float>::epsilon()) {
|
||||||
|
models.push_back(&model);
|
||||||
|
++it;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (model.lifespan <= 0.f) {
|
||||||
|
it = models_.erase(it);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (model.shouldBeDestroyed) {
|
||||||
|
it = models_.erase(it);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
model.lifespan -= delta_time;
|
||||||
|
|
||||||
|
models.push_back(&model);
|
||||||
|
++it;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto& it : models_) {
|
||||||
|
it.second.Update(delta_time);
|
||||||
|
vulkan_graphics->RenderModel(&it.second);
|
||||||
|
}
|
||||||
|
|
||||||
|
physics_controller_.invokeOnColisionEvent(thread_pool_,
|
||||||
|
{models.data(), models.size()});
|
||||||
|
|
||||||
|
vulkan_graphics->EndFrame();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
void Engine::NetUpdate(std::shared_ptr<Network::Socket> sock) {
|
||||||
|
NetworkUpload(sock);
|
||||||
|
ResponseToServerAndRefresh(sock);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Engine::NetworkUpload(std::shared_ptr<Network::Socket> sock) {
|
||||||
|
std::vector<char> data;
|
||||||
|
for (auto& it : models_) {
|
||||||
|
if (!it.second.networkReplicated) continue;
|
||||||
|
if (!it.second.needsUpdate) continue;
|
||||||
|
|
||||||
|
Packet::Header header;
|
||||||
|
header.opcode = Packet::Opcode::UPDATEMODEL;
|
||||||
|
std::vector<char> model = it.second.Serialize();
|
||||||
|
header.body_length = model.size();
|
||||||
|
std::vector<char> header_serialized = header.Serialize();
|
||||||
|
|
||||||
|
data.insert(data.end(), header_serialized.begin(), header_serialized.end());
|
||||||
|
data.insert(data.end(), model.begin(), model.end());
|
||||||
|
it.second.needsUpdate = false;
|
||||||
|
}
|
||||||
|
iocp_->send(*sock, data);
|
||||||
|
}
|
||||||
|
|
||||||
|
int recv_partial(Network::IOCP* iocp, Network::Socket& sock,
|
||||||
|
std::vector<char>& buffer) {
|
||||||
|
size_t total_received = 0;
|
||||||
|
size_t expected = buffer.size();
|
||||||
|
|
||||||
|
while (total_received < expected) {
|
||||||
|
if (total_received != 0)
|
||||||
|
Sleep(100); // 너무 빨리 재시도해서 큐가 채워질 틈이 없는 듯?
|
||||||
|
if (sock.sock == 0) return 0;
|
||||||
|
std::vector<char> temp(expected - total_received);
|
||||||
|
|
||||||
|
size_t received = iocp->recv(sock, temp);
|
||||||
|
if (received == 0) return 0;
|
||||||
|
|
||||||
|
std::copy(temp.begin(), temp.begin() + received,
|
||||||
|
buffer.begin() + total_received);
|
||||||
|
total_received += received;
|
||||||
|
}
|
||||||
|
|
||||||
|
return total_received;
|
||||||
|
}
|
||||||
|
|
||||||
|
void recv_fully(Network::IOCP* iocp, Network::Socket& sock,
|
||||||
|
std::vector<char>& buffer) {
|
||||||
|
size_t total_received = 0;
|
||||||
|
size_t expected = buffer.size();
|
||||||
|
|
||||||
|
while (total_received < expected) {
|
||||||
|
if (total_received != 0)
|
||||||
|
Sleep(100); // 너무 빨리 재시도해서 큐가 채워질 틈이 없는 듯?
|
||||||
|
if (sock.sock == 0) return;
|
||||||
|
std::vector<char> temp(expected - total_received);
|
||||||
|
|
||||||
|
size_t received = iocp->recv(sock, temp);
|
||||||
|
|
||||||
|
std::copy(temp.begin(), temp.begin() + received,
|
||||||
|
buffer.begin() + total_received);
|
||||||
|
total_received += received;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Engine::ResponseToServerAndRefresh(std::shared_ptr<Network::Socket> sock) {
|
||||||
|
if (sock->sock == 0) return;
|
||||||
|
std::vector<char> recv_data(6);
|
||||||
|
if (recv_partial(iocp_, *sock, recv_data) == 0) return;
|
||||||
|
Packet::Header header;
|
||||||
|
header.Deserialize(recv_data);
|
||||||
|
recv_data.resize(header.body_length);
|
||||||
|
recv_fully(iocp_, *sock, recv_data);
|
||||||
|
|
||||||
|
switch (header.opcode) {
|
||||||
|
case Packet::Opcode::UPDATEMODEL: {
|
||||||
|
veng::Model model;
|
||||||
|
model.Deserialize(recv_data);
|
||||||
|
|
||||||
|
bool found = false;
|
||||||
|
for (auto& it : models_) {
|
||||||
|
if (it.second.ID == model.ID) {
|
||||||
|
it.second.position = model.position;
|
||||||
|
it.second.linear_velocity = model.linear_velocity;
|
||||||
|
it.second.linear_acceleration = model.linear_acceleration;
|
||||||
|
|
||||||
|
it.second.rotation = model.rotation;
|
||||||
|
it.second.angular_velocity = model.angular_velocity;
|
||||||
|
it.second.angular_acceleration = model.angular_acceleration;
|
||||||
|
|
||||||
|
it.second.scale = model.scale;
|
||||||
|
it.second.transform = model.transform;
|
||||||
|
|
||||||
|
it.second.original_offset = model.original_offset;
|
||||||
|
it.second.radius = model.radius;
|
||||||
|
it.second.lifespan = model.lifespan;
|
||||||
|
|
||||||
|
it.second.visible = model.visible;
|
||||||
|
it.second.colision = model.colision;
|
||||||
|
|
||||||
|
spdlog::info("model updated: {}", model.name);
|
||||||
|
spdlog::info("model pos: ({},{},{})", model.position.x,
|
||||||
|
model.position.y, model.position.z);
|
||||||
|
found = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!found) {
|
||||||
|
auto spawnedModel =
|
||||||
|
SpawnLifedModel(model.asset_name, model.name, model.lifespan);
|
||||||
|
spawnedModel->ID = model.ID;
|
||||||
|
spawnedModel->position = model.position;
|
||||||
|
spawnedModel->linear_velocity = model.linear_velocity;
|
||||||
|
spawnedModel->linear_acceleration = model.linear_acceleration;
|
||||||
|
|
||||||
|
spawnedModel->rotation = model.rotation;
|
||||||
|
spawnedModel->angular_velocity = model.angular_velocity;
|
||||||
|
spawnedModel->angular_acceleration = model.angular_acceleration;
|
||||||
|
|
||||||
|
spawnedModel->scale = model.scale;
|
||||||
|
spawnedModel->transform = model.transform;
|
||||||
|
|
||||||
|
spawnedModel->original_offset = model.original_offset;
|
||||||
|
spawnedModel->radius = model.radius;
|
||||||
|
spawnedModel->lifespan = model.lifespan;
|
||||||
|
|
||||||
|
spawnedModel->visible = model.visible;
|
||||||
|
spawnedModel->colision = model.colision;
|
||||||
|
spawnedModel->networkReplicated = false;
|
||||||
|
spdlog::info("model spawned: {}", model.ID.snowflake);
|
||||||
|
}
|
||||||
|
} break;
|
||||||
|
default:
|
||||||
|
spdlog::error("unknown data type");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace veng
|
||||||
@@ -1,9 +1,5 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <openssl/bio.h>
|
|
||||||
#include <openssl/err.h>
|
|
||||||
#include <openssl/ssl.h>
|
|
||||||
|
|
||||||
#include "socket/iocp.h"
|
#include "socket/iocp.h"
|
||||||
#include "utils/thread_pool.h"
|
#include "utils/thread_pool.h"
|
||||||
|
|
||||||
@@ -23,8 +19,5 @@ class Session {
|
|||||||
|
|
||||||
SessionProtocol proto_;
|
SessionProtocol proto_;
|
||||||
Network::Socket* sock_ = nullptr;
|
Network::Socket* sock_ = nullptr;
|
||||||
|
|
||||||
SSL_CTX* ssl_ctx_ = nullptr;
|
|
||||||
SSL* ssl_ = nullptr;
|
|
||||||
};
|
};
|
||||||
} // namespace Network
|
} // namespace Network
|
||||||
|
|||||||
@@ -1,9 +1,5 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <openssl/bio.h>
|
|
||||||
#include <openssl/err.h>
|
|
||||||
#include <openssl/ssl.h>
|
|
||||||
|
|
||||||
namespace Network {
|
namespace Network {
|
||||||
|
|
||||||
struct Address {
|
struct Address {
|
||||||
@@ -24,8 +20,6 @@ struct Address {
|
|||||||
struct sockaddr_in6 addr_in6;
|
struct sockaddr_in6 addr_in6;
|
||||||
};
|
};
|
||||||
socklen_t length;
|
socklen_t length;
|
||||||
|
|
||||||
BIO_ADDR* bio_addr_info;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace Chattr
|
} // namespace Chattr
|
||||||
|
|||||||
@@ -1,12 +1,9 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <openssl/bio.h>
|
|
||||||
#include <openssl/err.h>
|
|
||||||
#include <openssl/ssl.h>
|
|
||||||
|
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <queue>
|
#include <queue>
|
||||||
#include <vector>
|
|
||||||
#include <random>
|
#include <random>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
#include "socket.h"
|
#include "socket.h"
|
||||||
#include "utils/thread_pool.h"
|
#include "utils/thread_pool.h"
|
||||||
@@ -31,13 +28,10 @@ class IOCP;
|
|||||||
|
|
||||||
enum class IOCPEVENT { QUIT, READ, WRITE };
|
enum class IOCPEVENT { QUIT, READ, WRITE };
|
||||||
|
|
||||||
struct IOCPPASSINDATA { // 얘 double free 문제 있음..
|
struct IOCPPASSINDATA {
|
||||||
OVERLAPPED overlapped;
|
OVERLAPPED overlapped;
|
||||||
IOCPEVENT event;
|
IOCPEVENT event;
|
||||||
std::shared_ptr<Socket> socket;
|
std::shared_ptr<Socket> socket;
|
||||||
SSL* ssl;
|
|
||||||
BIO* rbio; // bio는 ssl별로 달라야 하므로 분리해야 함..
|
|
||||||
BIO* wbio;
|
|
||||||
std::uint32_t transferredbytes;
|
std::uint32_t transferredbytes;
|
||||||
WSABUF wsabuf;
|
WSABUF wsabuf;
|
||||||
std::uint32_t bufsize;
|
std::uint32_t bufsize;
|
||||||
@@ -50,23 +44,6 @@ struct IOCPPASSINDATA { // 얘 double free 문제 있음..
|
|||||||
std::memset(&overlapped, 0, sizeof(overlapped));
|
std::memset(&overlapped, 0, sizeof(overlapped));
|
||||||
event = IOCPEVENT::QUIT;
|
event = IOCPEVENT::QUIT;
|
||||||
socket = nullptr;
|
socket = nullptr;
|
||||||
rbio = nullptr;
|
|
||||||
wbio = nullptr;
|
|
||||||
transferredbytes = 0;
|
|
||||||
this->bufsize = bufsize;
|
|
||||||
IOCPInstance = nullptr;
|
|
||||||
|
|
||||||
wsabuf.buf = new char[bufsize];
|
|
||||||
wsabuf.len = bufsize;
|
|
||||||
}
|
|
||||||
IOCPPASSINDATA(std::uint32_t bufsize, SSL_CTX* ctx) {
|
|
||||||
std::memset(&overlapped, 0, sizeof(overlapped));
|
|
||||||
event = IOCPEVENT::QUIT;
|
|
||||||
socket = nullptr;
|
|
||||||
ssl = ::SSL_new(ctx);
|
|
||||||
rbio = ::BIO_new(::BIO_s_mem());
|
|
||||||
wbio = ::BIO_new(::BIO_s_mem());
|
|
||||||
::SSL_set_bio(ssl, rbio, wbio);
|
|
||||||
transferredbytes = 0;
|
transferredbytes = 0;
|
||||||
this->bufsize = bufsize;
|
this->bufsize = bufsize;
|
||||||
IOCPInstance = nullptr;
|
IOCPInstance = nullptr;
|
||||||
@@ -75,13 +52,12 @@ struct IOCPPASSINDATA { // 얘 double free 문제 있음..
|
|||||||
wsabuf.len = bufsize;
|
wsabuf.len = bufsize;
|
||||||
}
|
}
|
||||||
|
|
||||||
IOCPPASSINDATA(const IOCPPASSINDATA& other) {
|
/*IOCPPASSINDATA(const IOCPPASSINDATA& other) {
|
||||||
if (this != &other) {
|
if (this != &other) {
|
||||||
std::memset(&overlapped, 0, sizeof(overlapped));
|
std::memset(&overlapped, 0, sizeof(overlapped));
|
||||||
event = other.event;
|
event = other.event;
|
||||||
socket = other.socket;
|
socket = other.socket;
|
||||||
rbio = other.rbio;
|
ssl = other.ssl;
|
||||||
wbio = other.wbio;
|
|
||||||
transferredbytes = other.transferredbytes;
|
transferredbytes = other.transferredbytes;
|
||||||
bufsize = other.bufsize;
|
bufsize = other.bufsize;
|
||||||
IOCPInstance = other.IOCPInstance;
|
IOCPInstance = other.IOCPInstance;
|
||||||
@@ -92,20 +68,19 @@ struct IOCPPASSINDATA { // 얘 double free 문제 있음..
|
|||||||
wsabuf.len = other.bufsize;
|
wsabuf.len = other.bufsize;
|
||||||
std::memcpy(wsabuf.buf, other.wsabuf.buf, other.wsabuf.len);
|
std::memcpy(wsabuf.buf, other.wsabuf.buf, other.wsabuf.len);
|
||||||
}
|
}
|
||||||
}
|
}*/
|
||||||
|
|
||||||
~IOCPPASSINDATA() {
|
~IOCPPASSINDATA() {
|
||||||
if (wsabuf.buf != nullptr) delete[] wsabuf.buf;
|
if (wsabuf.buf != nullptr) delete[] wsabuf.buf;
|
||||||
wsabuf.buf = nullptr;
|
wsabuf.buf = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
IOCPPASSINDATA& operator=(const IOCPPASSINDATA& other) {
|
/*IOCPPASSINDATA& operator=(const IOCPPASSINDATA& other) {
|
||||||
if (this != &other) {
|
if (this != &other) {
|
||||||
std::memset(&overlapped, 0, sizeof(overlapped));
|
std::memset(&overlapped, 0, sizeof(overlapped));
|
||||||
event = other.event;
|
event = other.event;
|
||||||
socket = other.socket;
|
socket = other.socket;
|
||||||
rbio = other.rbio;
|
ssl = other.ssl;
|
||||||
wbio = other.wbio;
|
|
||||||
transferredbytes = other.transferredbytes;
|
transferredbytes = other.transferredbytes;
|
||||||
bufsize = other.bufsize;
|
bufsize = other.bufsize;
|
||||||
IOCPInstance = other.IOCPInstance;
|
IOCPInstance = other.IOCPInstance;
|
||||||
@@ -118,7 +93,10 @@ struct IOCPPASSINDATA { // 얘 double free 문제 있음..
|
|||||||
std::memcpy(wsabuf.buf, other.wsabuf.buf, other.wsabuf.len);
|
std::memcpy(wsabuf.buf, other.wsabuf.buf, other.wsabuf.len);
|
||||||
}
|
}
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}*/
|
||||||
|
|
||||||
|
IOCPPASSINDATA(const IOCPPASSINDATA& other) = delete;
|
||||||
|
IOCPPASSINDATA& operator=(const IOCPPASSINDATA&) = delete;
|
||||||
};
|
};
|
||||||
|
|
||||||
class IOCP {
|
class IOCP {
|
||||||
@@ -126,93 +104,22 @@ class IOCP {
|
|||||||
IOCP();
|
IOCP();
|
||||||
~IOCP();
|
~IOCP();
|
||||||
|
|
||||||
void init(utils::ThreadPool* __IOCPThread, SessionProtocol proto) {
|
void init(utils::ThreadPool* __IOCPThread, SessionProtocol proto);
|
||||||
IOCPThread_ = __IOCPThread;
|
|
||||||
proto_ = proto;
|
|
||||||
|
|
||||||
#ifdef _WIN32
|
|
||||||
completionPort_ =
|
|
||||||
::CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 0);
|
|
||||||
if (completionPort_ == NULL) {
|
|
||||||
spdlog::critical("CreateIoCompletionPort()");
|
|
||||||
std::exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
int tCount = __IOCPThread->threadCount;
|
|
||||||
|
|
||||||
spdlog::info("Resizing threadpool size to: {}", tCount * 2);
|
|
||||||
|
|
||||||
__IOCPThread->respawnWorker(tCount * 2);
|
|
||||||
|
|
||||||
for (int i = 0; i < tCount; i++)
|
|
||||||
IOCPThread_->enqueueJob(
|
|
||||||
[this](utils::ThreadPool* th, std::uint8_t __) { iocpWatcher_(th); },
|
|
||||||
0);
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
void destruct();
|
void destruct();
|
||||||
|
|
||||||
void registerSocket(IOCPPASSINDATA* data);
|
void registerTCPSocket(Socket& sock, std::uint32_t bufsize);
|
||||||
|
void registerUDPSocket(IOCPPASSINDATA* data, Address recv_addr);
|
||||||
|
|
||||||
|
int recv(Socket& sock, std::vector<char>& data);
|
||||||
// data는 한 가지 소켓에 보내는 패킷만 담아야 합니다
|
// data는 한 가지 소켓에 보내는 패킷만 담아야 합니다
|
||||||
int send(SOCKET sock, std::vector<IOCPPASSINDATA*>* data);
|
int send(Socket& sock, std::vector<char>& data);
|
||||||
int recv(IOCPPASSINDATA* data);
|
|
||||||
|
int GetRecvedBytes(SOCKET sock);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
void iocpWatcher_(utils::ThreadPool* IOCPThread) {
|
void iocpWatcher_(utils::ThreadPool* IOCPThread);
|
||||||
IOCPPASSINDATA* data;
|
|
||||||
SOCKET sock;
|
|
||||||
DWORD cbTransfrred;
|
|
||||||
int jitter = jitterDist_(gen_);
|
|
||||||
int retVal = GetQueuedCompletionStatus(completionPort_, &cbTransfrred,
|
|
||||||
(PULONG_PTR)&sock,
|
|
||||||
(LPOVERLAPPED*)&data, 1000 + jitter);
|
|
||||||
|
|
||||||
if (retVal == 0 || cbTransfrred == 0) {
|
|
||||||
DWORD lasterror = GetLastError();
|
|
||||||
if (lasterror == WAIT_TIMEOUT) {
|
|
||||||
IOCPThread->enqueueJob([this](utils::ThreadPool* th,
|
|
||||||
std::uint8_t __) { iocpWatcher_(th); },
|
|
||||||
0);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
data->event = IOCPEVENT::QUIT;
|
|
||||||
spdlog::debug("Disconnected. [{}]",
|
|
||||||
(std::string)(data->socket->remoteAddr));
|
|
||||||
delete data;
|
|
||||||
} else {
|
|
||||||
data->transferredbytes = cbTransfrred;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::vector<char> buf(16384); // SSL_read최대 반환 크기
|
|
||||||
int red_data = 0;
|
|
||||||
auto queue_list = GetRecvQueue_(data->socket->sock);
|
|
||||||
if (data->event == IOCPEVENT::READ) {
|
|
||||||
if (proto_ == SessionProtocol::TLS || proto_ == SessionProtocol::QUIC) {
|
|
||||||
::BIO_write(data->rbio, data->wsabuf.buf, cbTransfrred);
|
|
||||||
|
|
||||||
while ((red_data = ::SSL_read(data->ssl, buf.data(), buf.size())) > 0) {
|
|
||||||
queue_list->emplace_back(std::make_pair(
|
|
||||||
std::vector<char>(buf.begin(), buf.begin() + red_data), 0));
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
::memcpy(buf.data(), data->wsabuf.buf, data->transferredbytes);
|
|
||||||
queue_list->emplace_back(std::make_pair(
|
|
||||||
std::vector<char>(buf.begin(),
|
|
||||||
buf.begin() + data->transferredbytes),
|
|
||||||
0));
|
|
||||||
}
|
|
||||||
DWORD recvbytes = 0, flags = 0;
|
|
||||||
::WSARecv(data->socket->sock, &data->wsabuf, 1, &recvbytes, &flags,
|
|
||||||
&data->overlapped, NULL);
|
|
||||||
} else { // WRITE 시, 무시한다.
|
|
||||||
delete data;
|
|
||||||
}
|
|
||||||
IOCPThread->enqueueJob(
|
|
||||||
[this](utils::ThreadPool* th, std::uint8_t __) { iocpWatcher_(th); },
|
|
||||||
0);
|
|
||||||
}
|
|
||||||
#elif __linux__
|
#elif __linux__
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
34
include/socket/packet.h
Normal file
34
include/socket/packet.h
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
|
namespace Packet {
|
||||||
|
|
||||||
|
enum class Opcode : std::uint16_t {
|
||||||
|
NOP,
|
||||||
|
SPAWNMODEL,
|
||||||
|
DESPAWNMODEL,
|
||||||
|
UPDATEMODEL,
|
||||||
|
REQUESTMODELIDLIST,
|
||||||
|
CLIENTID,
|
||||||
|
COUNT
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Header {
|
||||||
|
Opcode opcode;
|
||||||
|
std::uint32_t body_length;
|
||||||
|
|
||||||
|
std::vector<char> Serialize() {
|
||||||
|
std::vector<char> serialize(6);
|
||||||
|
::memcpy(serialize.data(), &opcode, 2);
|
||||||
|
::memcpy(serialize.data() + 2, &body_length, 4);
|
||||||
|
return serialize;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Deserialize(std::vector<char>& data) {
|
||||||
|
::memcpy(&opcode, data.data(), 2);
|
||||||
|
::memcpy(&body_length, data.data() + 2, 4);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace Packet
|
||||||
@@ -19,6 +19,7 @@ class Socket {
|
|||||||
int setsockopt(int level, int optname, const char* optval, int optlen);
|
int setsockopt(int level, int optname, const char* optval, int optlen);
|
||||||
|
|
||||||
int bind(Address __addr);
|
int bind(Address __addr);
|
||||||
|
int connect(Network::Address& serveraddr);
|
||||||
|
|
||||||
int recvfrom(void* __restrict __buf, size_t __n, int __flags,
|
int recvfrom(void* __restrict __buf, size_t __n, int __flags,
|
||||||
struct Address& __addr);
|
struct Address& __addr);
|
||||||
@@ -38,7 +39,7 @@ class Socket {
|
|||||||
std::mutex readMutex;
|
std::mutex readMutex;
|
||||||
std::mutex writeMutex;
|
std::mutex writeMutex;
|
||||||
|
|
||||||
protected:
|
private:
|
||||||
bool valid_ = false;
|
bool valid_ = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ class UDPSocket : public Socket {
|
|||||||
using Socket::init;
|
using Socket::init;
|
||||||
using Socket::Socket;
|
using Socket::Socket;
|
||||||
int init(int domain);
|
int init(int domain);
|
||||||
|
int connect(Network::Address& serveraddr);
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace Socket
|
} // namespace Socket
|
||||||
|
|||||||
@@ -16,9 +16,16 @@ struct Snowflake {
|
|||||||
bool operator==(const Snowflake& other) const {
|
bool operator==(const Snowflake& other) const {
|
||||||
return snowflake == other.snowflake;
|
return snowflake == other.snowflake;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::vector<char> Serialize() {
|
||||||
|
std::vector<char> serialized;
|
||||||
|
serialized.insert(serialized.end(), &snowflake,
|
||||||
|
&snowflake + sizeof(snowflake));
|
||||||
|
return serialized;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
Snowflake GenerateID();
|
Snowflake GenerateID(std::uint16_t instance);
|
||||||
|
|
||||||
} // namespace Chattr
|
} // namespace Chattr
|
||||||
|
|
||||||
|
|||||||
@@ -7,6 +7,9 @@
|
|||||||
#include "material.h"
|
#include "material.h"
|
||||||
#include "vulkan_engine/vulkan/buffer_handle.h"
|
#include "vulkan_engine/vulkan/buffer_handle.h"
|
||||||
#include "vulkan_engine/vulkan/vertex.h"
|
#include "vulkan_engine/vulkan/vertex.h"
|
||||||
|
#include "utils/snowflake.h"
|
||||||
|
|
||||||
|
extern std::uint8_t CLIENTID;
|
||||||
|
|
||||||
namespace veng {
|
namespace veng {
|
||||||
struct Model {
|
struct Model {
|
||||||
@@ -15,7 +18,8 @@ struct Model {
|
|||||||
~Model();
|
~Model();
|
||||||
|
|
||||||
Model(const Model& other)
|
Model(const Model& other)
|
||||||
: vertices(other.vertices),
|
: asset_name(other.asset_name),
|
||||||
|
vertices(other.vertices),
|
||||||
vertex_buffer(other.vertex_buffer),
|
vertex_buffer(other.vertex_buffer),
|
||||||
indices(other.indices),
|
indices(other.indices),
|
||||||
index_buffer(other.index_buffer),
|
index_buffer(other.index_buffer),
|
||||||
@@ -31,17 +35,23 @@ struct Model {
|
|||||||
original_offset(other.original_offset),
|
original_offset(other.original_offset),
|
||||||
owner(other.owner),
|
owner(other.owner),
|
||||||
radius(other.radius),
|
radius(other.radius),
|
||||||
|
lifespan(other.lifespan),
|
||||||
OnColision(other.OnColision),
|
OnColision(other.OnColision),
|
||||||
visible(other.visible),
|
visible(other.visible),
|
||||||
colision(other.colision) {
|
colision(other.colision),
|
||||||
|
networkReplicated(other.networkReplicated) {
|
||||||
|
name = "_" + other.name;
|
||||||
|
ID = utils::GenerateID(CLIENTID);
|
||||||
graphics_ = nullptr;
|
graphics_ = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
Model(Model&& other)
|
Model(Model&& other)
|
||||||
: vertices(other.vertices),
|
: name(other.name),
|
||||||
vertex_buffer(other.vertex_buffer),
|
asset_name(other.asset_name),
|
||||||
indices(other.indices),
|
vertices(std::move(other.vertices)),
|
||||||
index_buffer(other.index_buffer),
|
vertex_buffer(std::move(other.vertex_buffer)),
|
||||||
|
indices(std::move(other.indices)),
|
||||||
|
index_buffer(std::move(other.index_buffer)),
|
||||||
transform(other.transform),
|
transform(other.transform),
|
||||||
position(other.position),
|
position(other.position),
|
||||||
linear_velocity(other.linear_velocity),
|
linear_velocity(other.linear_velocity),
|
||||||
@@ -54,46 +64,29 @@ struct Model {
|
|||||||
original_offset(other.original_offset),
|
original_offset(other.original_offset),
|
||||||
owner(other.owner),
|
owner(other.owner),
|
||||||
radius(other.radius),
|
radius(other.radius),
|
||||||
|
lifespan(other.lifespan),
|
||||||
OnColision(other.OnColision),
|
OnColision(other.OnColision),
|
||||||
visible(other.visible),
|
visible(other.visible),
|
||||||
colision(other.colision) {
|
colision(other.colision),
|
||||||
|
networkReplicated(other.networkReplicated)
|
||||||
|
{
|
||||||
|
ID = other.ID;
|
||||||
|
::memset(&other.ID, 0, 8);
|
||||||
|
|
||||||
graphics_ = other.graphics_;
|
graphics_ = other.graphics_;
|
||||||
other.graphics_ = nullptr;
|
other.graphics_ = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
Model& operator=(const Model& other) {
|
|
||||||
if (this != &other) {
|
|
||||||
vertices = other.vertices;
|
|
||||||
vertex_buffer = other.vertex_buffer;
|
|
||||||
indices = other.indices;
|
|
||||||
index_buffer = other.index_buffer;
|
|
||||||
transform = other.transform;
|
|
||||||
position = other.position;
|
|
||||||
linear_velocity = other.linear_velocity;
|
|
||||||
linear_acceleration = other.linear_acceleration;
|
|
||||||
rotation = other.rotation;
|
|
||||||
angular_velocity = other.angular_velocity;
|
|
||||||
angular_acceleration = other.angular_acceleration;
|
|
||||||
scale = other.scale;
|
|
||||||
material = other.material;
|
|
||||||
original_offset = other.original_offset;
|
|
||||||
owner = other.owner;
|
|
||||||
radius = other.radius;
|
|
||||||
OnColision = other.OnColision;
|
|
||||||
visible = other.visible;
|
|
||||||
colision = other.colision;
|
|
||||||
|
|
||||||
graphics_ = nullptr;
|
|
||||||
}
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
Model& operator=(Model&& other) noexcept {
|
Model& operator=(Model&& other) noexcept {
|
||||||
if (this != &other) {
|
if (this != &other) {
|
||||||
|
ID = other.ID;
|
||||||
|
::memset(&other.ID, 0, 8);
|
||||||
|
name = other.name;
|
||||||
|
asset_name = other.asset_name;
|
||||||
vertices = std::move(other.vertices);
|
vertices = std::move(other.vertices);
|
||||||
vertex_buffer = other.vertex_buffer;
|
vertex_buffer = std::move(other.vertex_buffer);
|
||||||
indices = std::move(other.indices);
|
indices = std::move(other.indices);
|
||||||
index_buffer = other.index_buffer;
|
index_buffer = std::move(other.index_buffer);
|
||||||
transform = other.transform;
|
transform = other.transform;
|
||||||
position = other.position;
|
position = other.position;
|
||||||
linear_velocity = other.linear_velocity;
|
linear_velocity = other.linear_velocity;
|
||||||
@@ -106,16 +99,23 @@ struct Model {
|
|||||||
original_offset = other.original_offset;
|
original_offset = other.original_offset;
|
||||||
owner = other.owner;
|
owner = other.owner;
|
||||||
radius = other.radius;
|
radius = other.radius;
|
||||||
|
lifespan = other.lifespan;
|
||||||
OnColision = other.OnColision;
|
OnColision = other.OnColision;
|
||||||
visible = other.visible;
|
visible = other.visible;
|
||||||
colision = other.colision;
|
colision = other.colision;
|
||||||
|
networkReplicated = other.networkReplicated;
|
||||||
|
|
||||||
// graphics_만 옮기고, 원본은 nullptr
|
|
||||||
graphics_ = other.graphics_;
|
graphics_ = other.graphics_;
|
||||||
other.graphics_ = nullptr;
|
other.graphics_ = nullptr;
|
||||||
}
|
}
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
utils::Snowflake ID;
|
||||||
|
std::string name;
|
||||||
|
std::string asset_name;
|
||||||
|
|
||||||
|
std::vector<char> Serialize();
|
||||||
|
void Deserialize(std::vector<char> data);
|
||||||
|
|
||||||
std::vector<veng::Vertex> vertices;
|
std::vector<veng::Vertex> vertices;
|
||||||
veng::BufferHandle vertex_buffer;
|
veng::BufferHandle vertex_buffer;
|
||||||
@@ -145,10 +145,16 @@ struct Model {
|
|||||||
|
|
||||||
std::float_t radius = 0.f;
|
std::float_t radius = 0.f;
|
||||||
|
|
||||||
|
std::float_t lifespan = -1.f;
|
||||||
|
|
||||||
std::function<void(Model* self, Model* other)> OnColision = nullptr;
|
std::function<void(Model* self, Model* other)> OnColision = nullptr;
|
||||||
|
|
||||||
bool visible = true;
|
bool visible = true;
|
||||||
bool colision = false;
|
bool colision = false;
|
||||||
|
bool networkReplicated = true;
|
||||||
|
bool shouldBeDestroyed = false;
|
||||||
|
|
||||||
|
bool needsUpdate = true;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
class Graphics* graphics_;
|
class Graphics* graphics_;
|
||||||
@@ -4,26 +4,31 @@
|
|||||||
#include "graphics.h"
|
#include "graphics.h"
|
||||||
#include "physics.h"
|
#include "physics.h"
|
||||||
#include "utils/thread_pool.h"
|
#include "utils/thread_pool.h"
|
||||||
|
#include "socket/iocp.h"
|
||||||
|
|
||||||
namespace veng {
|
namespace veng {
|
||||||
|
|
||||||
class Engine {
|
class Engine {
|
||||||
public:
|
public:
|
||||||
Engine(gsl::not_null<Graphics*> vulkan_graphics,
|
Engine(gsl::not_null<Graphics*> vulkan_graphics,
|
||||||
utils::ThreadPool* thread_pool)
|
utils::ThreadPool* thread_pool, Network::IOCP* iocp)
|
||||||
: vulkan_graphics(vulkan_graphics), thread_pool_(thread_pool) {}
|
: vulkan_graphics(vulkan_graphics), thread_pool_(thread_pool), iocp_(iocp) {}
|
||||||
|
|
||||||
void init();
|
void init();
|
||||||
|
|
||||||
void LoadModelAsset(std::string path, std::string name);
|
void LoadModelAsset(std::string path, std::string name);
|
||||||
const Model* GetStaticModel(std::string name);
|
const Model* GetStaticModel(std::string name);
|
||||||
|
|
||||||
Model* SpawnModel(std::string asset_name, std::string name);
|
// 수명을 무제한으로 놓고 싶으면 lifespan을 -1으로 설정하면 됨
|
||||||
Model* SpawnLifedModel(std::string asset_name, std::string name,
|
Model* SpawnLifedModel(std::string asset_name, std::string name,
|
||||||
std::float_t lifespan);
|
std::float_t lifespan);
|
||||||
Model* GetSpawnedObject(std::string name);
|
Model* GetSpawnedObject(std::string name);
|
||||||
|
|
||||||
void Update();
|
void Update();
|
||||||
|
void NetUpdate(std::shared_ptr<Network::Socket> sock);
|
||||||
|
|
||||||
|
void NetworkUpload(std::shared_ptr<Network::Socket> sock);
|
||||||
|
void ResponseToServerAndRefresh(std::shared_ptr<Network::Socket> sock);
|
||||||
|
|
||||||
std::function<void(Engine&)> BeginPlay = [](Engine& engine) {};
|
std::function<void(Engine&)> BeginPlay = [](Engine& engine) {};
|
||||||
std::function<void(Engine&, std::float_t delta_time)> Tick =
|
std::function<void(Engine&, std::float_t delta_time)> Tick =
|
||||||
@@ -41,15 +46,14 @@ class Engine {
|
|||||||
Loader asset_loader_;
|
Loader asset_loader_;
|
||||||
Physics physics_controller_;
|
Physics physics_controller_;
|
||||||
utils::ThreadPool* thread_pool_;
|
utils::ThreadPool* thread_pool_;
|
||||||
|
Network::IOCP* iocp_;
|
||||||
|
|
||||||
glm::ivec2 window_size_ = {0, 0};
|
glm::ivec2 window_size_ = {0, 0};
|
||||||
std::double_t last_frame_time_ = 0.0;
|
std::double_t last_frame_time_ = 0.0;
|
||||||
|
|
||||||
std::unordered_map<std::string, veng::Model> model_assets_;
|
std::unordered_map<std::string, veng::Model> model_assets_;
|
||||||
|
|
||||||
std::unordered_map<std::string, veng::Model> dynamic_immortal_models_;
|
std::unordered_map<std::string, veng::Model> models_;
|
||||||
std::unordered_map<std::string, std::pair<veng::Model, std::float_t>>
|
|
||||||
dynamic_models_;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace veng
|
} // namespace veng
|
||||||
Reference in New Issue
Block a user