8 Commits

59 changed files with 1027 additions and 582 deletions

View File

@@ -3,7 +3,6 @@ set(PROJECT_NAME "Client")
project(${PROJECT_NAME})
find_package(Vulkan REQUIRED)
find_package(OpenSSL REQUIRED)
include(Shaders.cmake)
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 spdlog)
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)
target_link_libraries(${PROJECT_NAME} PRIVATE ws2_32)

View File

@@ -1,5 +1,7 @@
#pragma once
extern std::uint8_t CLIENTID;
namespace veng {
class Engine;
}

View File

@@ -2,8 +2,14 @@
#include "vulkan_engine/vulkan/engine.h"
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->colision = true;
player->OnColision = [](veng::Model* self, veng::Model* other) {
@@ -15,15 +21,17 @@ void BeginPlay(veng::Engine& engine) {
other->colision = false;
other->visible = false;
other->shouldBeDestroyed = true;
};
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->colision = false;
player_flame->networkReplicated = false;
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->scale = glm::vec3(.02f);
other_player->colision = true;
@@ -36,29 +44,35 @@ void BeginPlay(veng::Engine& engine) {
other->colision = false;
other->visible = false;
other->shouldBeDestroyed = true;
};
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->position = player->position;
veng::Model* const background = engine.SpawnModel("", "background");
veng::Model* const background = engine.SpawnLifedModel("", "background", -1);
background->colision = false;
background->position = {background->position.x, background->position.y, 30.f};
background->scale *= 100;
background->networkReplicated = false;
veng::Model* const background0 =
engine.SpawnModel("background", "background0");
engine.SpawnLifedModel("background", "background0", -1);
background0->scale = background->scale;
background0->networkReplicated = false;
veng::Model* const background1 =
engine.SpawnModel("background", "background1");
engine.SpawnLifedModel("background", "background1", -1);
background1->scale = background->scale;
background1->networkReplicated = false;
veng::Model* const background2 =
engine.SpawnModel("background", "background2");
engine.SpawnLifedModel("background", "background2", -1);
background2->scale = background->scale;
background2->networkReplicated = false;
veng::Model* const background3 =
engine.SpawnModel("background", "background3");
engine.SpawnLifedModel("background", "background3", -1);
background3->scale = background->scale;
background3->networkReplicated = false;
}

View File

@@ -3,18 +3,21 @@
#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/vulkan/engine.h"
#include "vulkan_engine/vulkan/graphics.h"
std::uint8_t CLIENTID = 0;
std::int32_t main(std::int32_t argc, gsl::zstring* argv) {
Network::WSAManager wsamanager;
#if !defined(NDEBUG)
utils::setDefaultLogger(spdlog::level::level_enum::debug, "log.log", 1024,
2);
utils::setDefaultLogger(spdlog::level::level_enum::debug, "log.log", 1024, 2);
#endif
utils::ThreadPool tp(0);
Network::IOCP iocp;
@@ -25,62 +28,28 @@
addr.set(AF_INET6, "::1", 9010);
Network::TCPSocket sock;
sock.init(AF_INET6);
if (sock.connect(addr) == INVALID_SOCKET) {
Network::TCPSocket TCPSock;
TCPSock.init(AF_INET6);
if (TCPSock.connect(addr) == INVALID_SOCKET) {
spdlog::error("connect()");
std::exit(EXIT_FAILURE);
}
iocp.registerTCPSocket(TCPSock, 16 * 1024);
Network::IOCPPASSINDATA* data = new Network::IOCPPASSINDATA(16 * 1024);
data->socket = std::make_shared<Network::TCPSocket>(sock);
data->IOCPInstance = &iocp;
iocp.registerSocket(data);
while (iocp.GetRecvedBytes(TCPSock.sock) < 6);
std::vector<char> header_serialized(6);
iocp.recv(TCPSock, header_serialized);
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;
data->event = Network::IOCPEVENT::WRITE;
data->wsabuf.buf[0] = 'a';
data->wsabuf.buf[1] = 'b';
data->wsabuf.buf[2] = ' ';
data->wsabuf.len = 3;
send_data.push_back(data);
Network::IOCPPASSINDATA* data2 = new Network::IOCPPASSINDATA(16 * 1024);
data2->wsabuf.buf[0] = 'b';
data2->wsabuf.buf[1] = 'a';
data2->wsabuf.buf[2] = '\0';
data2->wsabuf.len = 3;
send_data.push_back(data2);
iocp.send(sock.sock, &send_data);
Network::IOCPPASSINDATA* recv_data = new Network::IOCPPASSINDATA(16 * 1024);
recv_data->socket = std::make_shared<Network::TCPSocket>(sock);
recv_data->IOCPInstance = &iocp;
while (!iocp.recv(recv_data)); // 어떤 데이터를 읽는걸 보장받고 싶다면 그냥 스린락 걸어버리기.
spdlog::info("recv_data: {}", recv_data->wsabuf.buf);
std::vector<Network::IOCPPASSINDATA*> send_data2;
data->event = Network::IOCPEVENT::WRITE;
data->wsabuf.buf[0] = 'c';
data->wsabuf.buf[1] = 'd';
data->wsabuf.buf[2] = ' ';
data->wsabuf.len = 3;
send_data2.push_back(data);
data2 = new Network::IOCPPASSINDATA(16 * 1024);
data2->wsabuf.buf[0] = 'd';
data2->wsabuf.buf[1] = 'c';
data2->wsabuf.buf[2] = '\0';
data2->wsabuf.len = 3;
send_data2.push_back(data2);
iocp.send(sock.sock, &send_data2);
recv_data = new Network::IOCPPASSINDATA(16 * 1024);
recv_data->socket = std::make_shared<Network::TCPSocket>(sock);
recv_data->IOCPInstance = &iocp;
while (!iocp.recv(recv_data)); // 어떤 데이터를 읽는걸 보장받고 싶다면 그냥
// 스린락 걸어버리기.
spdlog::info("recv_data: {}", recv_data->wsabuf.buf);
header.opcode = Packet::Opcode::CLIENTID;
header.body_length = 0;
header_serialized = header.Serialize();
iocp.send(TCPSock, header_serialized);
const veng::GlfwInitialization _glfw;
@@ -88,7 +57,7 @@
window.TryMoveToMonitor(0);
veng::Graphics graphics(&window);
veng::Engine engine(&graphics, &tp);
veng::Engine engine(&graphics, &tp, &iocp);
engine.LoadModelAsset("assets/player.fbx", "player");
engine.LoadModelAsset("assets/player_flame.fbx", "player_flame");
@@ -104,6 +73,7 @@
glfwPollEvents();
engine.Update();
engine.NetUpdate(std::make_shared<Network::Socket>(TCPSock));
}
return EXIT_SUCCESS;

View File

@@ -31,17 +31,27 @@ void Tick(veng::Engine& engine, std::float_t delta_time) {
(-stiffness * displacement - damping * camera_lag->linear_velocity) *
delta_time;
/*camera_lag->linear_velocity = glm::vec3(0);
camera_lag->position = player->position;*/
engine.view = glm::lookAt(
glm::vec3(camera_lag->position.x, camera_lag->position.y, -5.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) ==
GLFW_PRESS) {
player->linear_acceleration = glm::normalize(forward) * 10.f;
player_flame->visible = true;
player->needsUpdate = true;
w_laststate = true;
} else {
player->linear_acceleration = forward * .0f;
player_flame->visible = false;
if (w_laststate) {
player->needsUpdate = true;
w_laststate = false;
}
}
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->scale = player->scale;
bullet->colision = true;
bullet->networkReplicated = true;
bullet->needsUpdate = true;
spdlog::info("bullet address: {}", (void*)bullet);
}
}
static bool ad_laststate = false;
if (glfwGetKey(engine.vulkan_graphics->window->GetHandle(), GLFW_KEY_A) ==
GLFW_PRESS) {
right = player->rotation * glm::vec3(0, 0, 1);
player->angular_velocity = right * 6.f;
player->needsUpdate = true;
ad_laststate = true;
} else if (glfwGetKey(engine.vulkan_graphics->window->GetHandle(),
GLFW_KEY_D) == GLFW_PRESS) {
right = player->rotation * glm::vec3(0, 0, 1);
player->angular_velocity = right * -6.f;
player->needsUpdate = true;
ad_laststate = true;
} else {
right = player->rotation * glm::vec3(0, 0, 1);
player->angular_velocity = right * 0.f;
if (ad_laststate) {
player->needsUpdate = true;
ad_laststate = false;
}
}
player_flame->rotation = player->rotation;

View File

@@ -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

View File

@@ -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

View File

@@ -4,10 +4,89 @@ set(PROJECT_NAME "Server")
project(${PROJECT_NAME})
add_executable(${PROJECT_NAME}
"${CMAKE_CURRENT_SOURCE_DIR}/src/echoserver.cpp"
find_package(Vulkan REQUIRED)
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)
target_link_libraries(${PROJECT_NAME} PRIVATE ws2_32)
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"
)

View File

@@ -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
View 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, &timestamp, 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);
}
}

View File

@@ -11,31 +11,7 @@ Session::Session(gsl::not_null<Network::IOCP*> iocp, utils::ThreadPool* tp,
: iocp_(iocp), tp_(tp), proto_(proto) {
switch (proto) {
case SessionProtocol::UDP:
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::QUIC:
case SessionProtocol::TCP:
case SessionProtocol::TLS: {
Network::TCPSocket* sock = new Network::TCPSocket();
@@ -56,12 +32,6 @@ Session::Session(gsl::not_null<Network::IOCP*> iocp, utils::ThreadPool* tp,
default:
std::exit(EXIT_FAILURE);
}
ssl_ = ::SSL_new(ssl_ctx_);
if (ssl_ == nullptr) {
spdlog::critical("SSL_new()");
std::exit(EXIT_FAILURE);
}
}
Session::~Session() {

View File

@@ -40,7 +40,34 @@ void IOCP::destruct() {
#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
HANDLE returnData = ::CreateIoCompletionPort(
(HANDLE)data->socket->sock, completionPort_, data->socket->sock, 0);
@@ -50,25 +77,29 @@ void IOCP::registerSocket(IOCPPASSINDATA* data) {
recv_data->event = IOCPEVENT::READ;
recv_data->socket = data->socket;
DWORD recvbytes = 0, flags = 0;
int result = ::WSARecv(recv_data->socket->sock, &recv_data->wsabuf, 1,
&recvbytes, &flags, &recv_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) {
spdlog::error("WSARecv failed: {}", err);
// 반드시 여기서 리턴하거나 처리해야 합니다.
auto err_msg = std::format("WSARecv failed: {}", err);
throw std::runtime_error(err_msg);
}
}
#endif
}
int IOCP::recv(IOCPPASSINDATA* data) { //읽은 바이트수가 무조건 100임? 왜..?
SOCKET sock = data->socket->sock;
std::lock_guard lock(*GetRecvQueueMutex_(sock));
auto queue = GetRecvQueue_(sock);
int IOCP::recv(Socket& sock, std::vector<char>& data) {
std::lock_guard lock(*GetRecvQueueMutex_(sock.sock));
auto queue = GetRecvQueue_(sock.sock);
std::uint32_t left_data = data->wsabuf.len;
std::uint32_t left_data = data.size();
std::uint32_t copied = 0;
while (!queue->empty() && left_data != 0) {
@@ -79,7 +110,7 @@ int IOCP::recv(IOCPPASSINDATA* data) { //읽은 바이트수가 무조건 100임
std::uint32_t available = front.first.size() - offset;
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;
left_data -= to_copy;
offset += to_copy;
@@ -94,27 +125,37 @@ int IOCP::recv(IOCPPASSINDATA* data) { //읽은 바이트수가 무조건 100임
return copied;
}
int IOCP::send(SOCKET sock, std::vector<IOCPPASSINDATA*>* data) {
auto lk = GetSendQueueMutex_(sock);
auto queue = GetSendQueue_(sock);
int IOCP::send(Socket& sock, std::vector<char>& data) {
auto lk = GetSendQueueMutex_(sock.sock);
auto queue = GetSendQueue_(sock.sock);
std::lock_guard lock(*lk);
for (auto& it : *data) {
it->event = IOCPEVENT::WRITE;
queue->push_back(it);
}
Network::IOCPPASSINDATA* packet = new Network::IOCPPASSINDATA(data.size());
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(
[this, sock](utils::ThreadPool* th, std::uint8_t __) {
[this, sock = sock.sock](utils::ThreadPool* th, std::uint8_t __) {
packet_sender_(sock);
},
0);
return 0;
}
int IOCP::GetRecvedPacketCount(SOCKET sock) {
std::lock_guard lock(socket_mod_mutex_);
int IOCP::GetRecvedBytes(SOCKET sock) {
auto queue = GetRecvQueue_(sock);
return queue->size();
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) {
@@ -138,6 +179,10 @@ void IOCP::iocpWatcher_(utils::ThreadPool* IOCPThread) {
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;
}
@@ -145,23 +190,12 @@ void IOCP::iocpWatcher_(utils::ThreadPool* IOCPThread) {
std::vector<char> buf(16384); // SSL_read최대 반환 크기
int red_data = 0;
std::lock_guard lock(*GetRecvQueueMutex_(sock));
auto queue_list = GetRecvQueue_(data->socket->sock);
auto queue_list = GetRecvQueue_(sock);
if (data->event == IOCPEVENT::READ) {
if (proto_ == SessionProtocol::TLS || proto_ == SessionProtocol::QUIC) {
::BIO_write(::SSL_get_rbio(data->ssl.get()), data->wsabuf.buf,
cbTransfrred);
while ((red_data = ::SSL_read(data->ssl.get(), 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;
IOCPPASSINDATA* recv_data = new IOCPPASSINDATA(data->bufsize);
@@ -172,6 +206,8 @@ void IOCP::iocpWatcher_(utils::ThreadPool* IOCPThread) {
::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(
@@ -229,33 +265,11 @@ void IOCP::packet_sender_(SOCKET sock) {
int data_len = 0;
if (proto_ == SessionProtocol::TLS || proto_ == SessionProtocol::QUIC) {
int ret = ::SSL_write(front->ssl.get(), front->wsabuf.buf, front->wsabuf.len);
if (ret <= 0) {
int err = ::SSL_get_error(front->ssl.get(), ret);
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(::SSL_get_wbio(front->ssl.get()), 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);
}
}
}
} // namespace Network

View File

@@ -12,6 +12,7 @@ int Socket::init(int domain, int type, int protocol) {
this->domain = domain;
sock = ::socket(domain, type, protocol);
// sock = ::WSASocket(domain, type, protocol, NULL, 0, WSA_FLAG_OVERLAPPED);
if (sock == INVALID_SOCKET) spdlog::critical("socket()");
valid_ = true;
@@ -27,6 +28,7 @@ void Socket::destruct() {
::close(sock);
#endif
valid_ = false;
sock = 0;
}
Socket::operator SOCKET() {
@@ -40,7 +42,7 @@ Socket::operator SOCKET() {
void Socket::set(const SOCKET __sock, int __domain) {
if (__sock == INVALID_SOCKET) {
spdlog::critical("socket()");
spdlog::error("set failed errno:{} {}", errno, strerror(errno));
std::exit(EXIT_FAILURE);
}
@@ -65,6 +67,26 @@ int Socket::bind(Address __addr) {
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,
struct Address& __addr) {
std::lock_guard<std::mutex> lock(readMutex);

View File

@@ -11,9 +11,14 @@ int TCPSocket::listen(int __n) {
}
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));
if (newSock.sock == INVALID_SOCKET) spdlog::error("accept()");
}
int TCPSocket::connect(Address &serveraddr) {

View File

@@ -4,4 +4,24 @@ namespace Network {
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

View File

@@ -9,13 +9,15 @@ static struct EpochInitializer {
std::chrono::system_clock::time_point EPOCH;
} epochInitializer;
Snowflake GenerateID() {
Snowflake GenerateID(std::uint16_t instance) {
static std::mutex snowflakeGenerateMutex_;
std::lock_guard<std::mutex> lock(snowflakeGenerateMutex_);
std::size_t tid =
std::hash<std::thread::id>{}(std::this_thread::get_id());
instance = instance << 3;
tid += instance;
thread_local static int sequence = 0;
Snowflake id = {};

View 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

View 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

View File

@@ -1,9 +1,5 @@
#pragma once
#include <openssl/bio.h>
#include <openssl/err.h>
#include <openssl/ssl.h>
#include "socket/iocp.h"
#include "utils/thread_pool.h"
@@ -23,8 +19,5 @@ class Session {
SessionProtocol proto_;
Network::Socket* sock_ = nullptr;
SSL_CTX* ssl_ctx_ = nullptr;
SSL* ssl_ = nullptr;
};
} // namespace Network

View File

@@ -1,9 +1,5 @@
#pragma once
#include <openssl/bio.h>
#include <openssl/err.h>
#include <openssl/ssl.h>
namespace Network {
struct Address {
@@ -24,8 +20,6 @@ struct Address {
struct sockaddr_in6 addr_in6;
};
socklen_t length;
BIO_ADDR* bio_addr_info;
};
} // namespace Chattr

View File

@@ -1,7 +1,4 @@
#pragma once
#include <openssl/bio.h>
#include <openssl/err.h>
#include <openssl/ssl.h>
#include <functional>
#include <queue>
@@ -35,7 +32,6 @@ struct IOCPPASSINDATA {
OVERLAPPED overlapped;
IOCPEVENT event;
std::shared_ptr<Socket> socket;
std::shared_ptr<SSL> ssl;
std::uint32_t transferredbytes;
WSABUF wsabuf;
std::uint32_t bufsize;
@@ -48,21 +44,6 @@ struct IOCPPASSINDATA {
std::memset(&overlapped, 0, sizeof(overlapped));
event = IOCPEVENT::QUIT;
socket = nullptr;
ssl = 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 = std::shared_ptr<SSL>(::SSL_new(ctx), ::SSL_free);
::SSL_set_bio(ssl.get(), ::BIO_new(::BIO_s_mem()),
::BIO_new(::BIO_s_mem()));
transferredbytes = 0;
this->bufsize = bufsize;
IOCPInstance = nullptr;
@@ -71,11 +52,12 @@ struct IOCPPASSINDATA {
wsabuf.len = bufsize;
}
IOCPPASSINDATA(const IOCPPASSINDATA& other) {
/*IOCPPASSINDATA(const IOCPPASSINDATA& other) {
if (this != &other) {
std::memset(&overlapped, 0, sizeof(overlapped));
event = other.event;
socket = other.socket;
ssl = other.ssl;
transferredbytes = other.transferredbytes;
bufsize = other.bufsize;
IOCPInstance = other.IOCPInstance;
@@ -86,18 +68,19 @@ struct IOCPPASSINDATA {
wsabuf.len = other.bufsize;
std::memcpy(wsabuf.buf, other.wsabuf.buf, other.wsabuf.len);
}
}
}*/
~IOCPPASSINDATA() {
if (wsabuf.buf != nullptr) delete[] wsabuf.buf;
wsabuf.buf = nullptr;
}
IOCPPASSINDATA& operator=(const IOCPPASSINDATA& other) {
/*IOCPPASSINDATA& operator=(const IOCPPASSINDATA& other) {
if (this != &other) {
std::memset(&overlapped, 0, sizeof(overlapped));
event = other.event;
socket = other.socket;
ssl = other.ssl;
transferredbytes = other.transferredbytes;
bufsize = other.bufsize;
IOCPInstance = other.IOCPInstance;
@@ -110,7 +93,10 @@ struct IOCPPASSINDATA {
std::memcpy(wsabuf.buf, other.wsabuf.buf, other.wsabuf.len);
}
return *this;
}
}*/
IOCPPASSINDATA(const IOCPPASSINDATA& other) = delete;
IOCPPASSINDATA& operator=(const IOCPPASSINDATA&) = delete;
};
class IOCP {
@@ -122,13 +108,14 @@ class IOCP {
void destruct();
void registerSocket(IOCPPASSINDATA* data);
void registerTCPSocket(Socket& sock, std::uint32_t bufsize);
void registerUDPSocket(IOCPPASSINDATA* data, Address recv_addr);
int recv(IOCPPASSINDATA* data);
int recv(Socket& sock, std::vector<char>& data);
// data는 한 가지 소켓에 보내는 패킷만 담아야 합니다
int send(SOCKET sock, std::vector<IOCPPASSINDATA*>* data);
int send(Socket& sock, std::vector<char>& data);
int GetRecvedPacketCount(SOCKET sock);
int GetRecvedBytes(SOCKET sock);
private:
#ifdef _WIN32

34
include/socket/packet.h Normal file
View 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

View File

@@ -19,6 +19,7 @@ class Socket {
int setsockopt(int level, int optname, const char* optval, int optlen);
int bind(Address __addr);
int connect(Network::Address& serveraddr);
int recvfrom(void* __restrict __buf, size_t __n, int __flags,
struct Address& __addr);
@@ -38,7 +39,7 @@ class Socket {
std::mutex readMutex;
std::mutex writeMutex;
protected:
private:
bool valid_ = false;
};

View File

@@ -10,6 +10,7 @@ class UDPSocket : public Socket {
using Socket::init;
using Socket::Socket;
int init(int domain);
int connect(Network::Address& serveraddr);
};
} // namespace Socket

View File

@@ -16,9 +16,16 @@ struct Snowflake {
bool operator==(const Snowflake& other) const {
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

View File

@@ -7,6 +7,9 @@
#include "material.h"
#include "vulkan_engine/vulkan/buffer_handle.h"
#include "vulkan_engine/vulkan/vertex.h"
#include "utils/snowflake.h"
extern std::uint8_t CLIENTID;
namespace veng {
struct Model {
@@ -15,7 +18,8 @@ struct Model {
~Model();
Model(const Model& other)
: vertices(other.vertices),
: asset_name(other.asset_name),
vertices(other.vertices),
vertex_buffer(other.vertex_buffer),
indices(other.indices),
index_buffer(other.index_buffer),
@@ -31,17 +35,23 @@ struct Model {
original_offset(other.original_offset),
owner(other.owner),
radius(other.radius),
lifespan(other.lifespan),
OnColision(other.OnColision),
visible(other.visible),
colision(other.colision) {
colision(other.colision),
networkReplicated(other.networkReplicated) {
name = "_" + other.name;
ID = utils::GenerateID(CLIENTID);
graphics_ = nullptr;
}
Model(Model&& other)
: vertices(other.vertices),
vertex_buffer(other.vertex_buffer),
indices(other.indices),
index_buffer(other.index_buffer),
: name(other.name),
asset_name(other.asset_name),
vertices(std::move(other.vertices)),
vertex_buffer(std::move(other.vertex_buffer)),
indices(std::move(other.indices)),
index_buffer(std::move(other.index_buffer)),
transform(other.transform),
position(other.position),
linear_velocity(other.linear_velocity),
@@ -54,46 +64,29 @@ struct Model {
original_offset(other.original_offset),
owner(other.owner),
radius(other.radius),
lifespan(other.lifespan),
OnColision(other.OnColision),
visible(other.visible),
colision(other.colision) {
colision(other.colision),
networkReplicated(other.networkReplicated)
{
ID = other.ID;
::memset(&other.ID, 0, 8);
graphics_ = other.graphics_;
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 {
if (this != &other) {
ID = other.ID;
::memset(&other.ID, 0, 8);
name = other.name;
asset_name = other.asset_name;
vertices = std::move(other.vertices);
vertex_buffer = other.vertex_buffer;
vertex_buffer = std::move(other.vertex_buffer);
indices = std::move(other.indices);
index_buffer = other.index_buffer;
index_buffer = std::move(other.index_buffer);
transform = other.transform;
position = other.position;
linear_velocity = other.linear_velocity;
@@ -106,16 +99,23 @@ struct Model {
original_offset = other.original_offset;
owner = other.owner;
radius = other.radius;
lifespan = other.lifespan;
OnColision = other.OnColision;
visible = other.visible;
colision = other.colision;
networkReplicated = other.networkReplicated;
// graphics_만 옮기고, 원본은 nullptr
graphics_ = other.graphics_;
other.graphics_ = nullptr;
}
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;
veng::BufferHandle vertex_buffer;
@@ -145,10 +145,16 @@ struct Model {
std::float_t radius = 0.f;
std::float_t lifespan = -1.f;
std::function<void(Model* self, Model* other)> OnColision = nullptr;
bool visible = true;
bool colision = false;
bool networkReplicated = true;
bool shouldBeDestroyed = false;
bool needsUpdate = true;
private:
class Graphics* graphics_;

View File

@@ -4,26 +4,31 @@
#include "graphics.h"
#include "physics.h"
#include "utils/thread_pool.h"
#include "socket/iocp.h"
namespace veng {
class Engine {
public:
Engine(gsl::not_null<Graphics*> vulkan_graphics,
utils::ThreadPool* thread_pool)
: vulkan_graphics(vulkan_graphics), thread_pool_(thread_pool) {}
utils::ThreadPool* thread_pool, Network::IOCP* iocp)
: vulkan_graphics(vulkan_graphics), thread_pool_(thread_pool), iocp_(iocp) {}
void init();
void LoadModelAsset(std::string path, 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,
std::float_t lifespan);
Model* GetSpawnedObject(std::string name);
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&, std::float_t delta_time)> Tick =
@@ -41,15 +46,14 @@ class Engine {
Loader asset_loader_;
Physics physics_controller_;
utils::ThreadPool* thread_pool_;
Network::IOCP* iocp_;
glm::ivec2 window_size_ = {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> dynamic_immortal_models_;
std::unordered_map<std::string, std::pair<veng::Model, std::float_t>>
dynamic_models_;
std::unordered_map<std::string, veng::Model> models_;
};
} // namespace veng