4 Commits

17 changed files with 338 additions and 239 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

@@ -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,16 +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;
@@ -37,34 +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

@@ -28,29 +28,32 @@ std::int32_t main(std::int32_t argc, gsl::zstring* argv) {
addr.set(AF_INET6, "::1", 9010);
Network::Socket sock;
Network::TCPSocket TCPSock;
TCPSock.init(AF_INET6);
sock = TCPSock;
if (sock.connect(addr) == INVALID_SOCKET) {
if (TCPSock.connect(addr) == INVALID_SOCKET) {
spdlog::error("connect()");
std::exit(EXIT_FAILURE);
}
iocp.registerTCPSocket(sock, 16 * 1024);
iocp.registerTCPSocket(TCPSock, 16 * 1024);
while (iocp.GetRecvedBytes(sock.sock) < 6);
while (iocp.GetRecvedBytes(TCPSock.sock) < 6);
std::vector<char> header_serialized(6);
iocp.recv(sock, header_serialized);
iocp.recv(TCPSock, header_serialized);
Packet::Header header;
header.Deserialize(header_serialized);
while (iocp.GetRecvedBytes(sock.sock) < header.body_length);
while (iocp.GetRecvedBytes(TCPSock.sock) < header.body_length);
std::vector<char> data(header.body_length);
iocp.recv(sock, data);
iocp.recv(TCPSock, data);
::memcpy(&CLIENTID, data.data(), 1); //id 받기
header.opcode = Packet::Opcode::CLIENTID;
header.body_length = 0;
header_serialized = header.Serialize();
iocp.send(TCPSock, header_serialized);
const veng::GlfwInitialization _glfw;
veng::Window window("Vulkan Engine", {800, 600});
veng::Window window("Asteroid", {800, 600});
window.TryMoveToMonitor(0);
veng::Graphics graphics(&window);
@@ -70,7 +73,7 @@ std::int32_t main(std::int32_t argc, gsl::zstring* argv) {
glfwPollEvents();
engine.Update();
engine.NetUpdate(std::make_shared<Network::Socket>(sock));
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

@@ -5,7 +5,6 @@ set(PROJECT_NAME "Server")
project(${PROJECT_NAME})
find_package(Vulkan REQUIRED)
find_package(OpenSSL REQUIRED)
include(FetchContent)
@@ -60,8 +59,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,6 @@
#include <chrono>
#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"
@@ -15,19 +16,37 @@ std::uint8_t CLIENTID = 0;
std::unordered_map<utils::Snowflake, veng::Model> MODELS;
std::mutex MODELS_MUTEX;
std::vector<Network::TCPSocket> CLIENTS;
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) {
while (iocp->GetRecvedBytes(NewSock.sock) < 6);
Network::TCPSocket* NewSock, Network::Address NewAddr) {
if (NewSock->sock == 0) return;
std::vector<char> recv_data(6);
iocp->recv(NewSock, recv_data);
recv_fully(iocp, *NewSock, recv_data);
Packet::Header header;
header.Deserialize(recv_data);
recv_data.resize(header.body_length);
while (iocp->GetRecvedBytes(NewSock.sock) < header.body_length);
iocp->recv(NewSock, recv_data);
recv_fully(iocp, *NewSock, recv_data);
switch (header.opcode) {
case Packet::Opcode::UPDATEMODEL: {
@@ -35,24 +54,97 @@ void EchoClient(utils::ThreadPool* tp, Network::IOCP* iocp,
model.Deserialize(recv_data);
std::lock_guard lock(MODELS_MUTEX);
if (MODELS.find(model.ID) == MODELS.end())
spdlog::info("model received: {}", model.ID.snowflake);
MODELS[model.ID] = std::move(model);
}
break;
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");
}
// iocp->send(NewSock, recv_data);
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, 2);
utils::setDefaultLogger(spdlog::level::level_enum::debug, "log.log",
1024 * 1024, 2);
#endif
utils::ThreadPool tp(0);
Network::IOCP iocp;
@@ -75,6 +167,8 @@ int main(int argc, char* argv[]) {
std::exit(EXIT_FAILURE);
}
tp.enqueueJob(ClientRefresher, &iocp);
while (true) {
Network::TCPSocket NewSock;
Network::Address NewAddr;
@@ -86,15 +180,22 @@ int main(int argc, char* argv[]) {
Packet::Header header;
header.opcode = Packet::Opcode::CLIENTID;
header.body_length = 8;
header.body_length = 9;
auto packet = header.Serialize();
packet.resize(packet.size() + 8);
packet.resize(packet.size() + 9);
std::lock_guard lock(CLIENTS_MUTEX);
CLIENTS.push_back(NewSock);
std::uint8_t client_id = CLIENTS.size() - 1;
::memcpy(packet.data() + 6, &client_id, 1);
iocp.send(NewSock, packet);
tp.enqueueJob(EchoClient, &iocp, NewSock, NewAddr);
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

@@ -192,59 +192,10 @@ void IOCP::iocpWatcher_(utils::ThreadPool* IOCPThread) {
std::lock_guard lock(*GetRecvQueueMutex_(sock));
auto queue_list = GetRecvQueue_(sock);
if (data->event == IOCPEVENT::READ) {
if (proto_ == SessionProtocol::TLS ||
proto_ == SessionProtocol::QUIC) { // DEPRECATED. openssl을 사용할 수가
// 없기 때문에 추후 완성 뒤에 기능을
// 붙이든 해야 할 듯 함.
// DEBUG: BIO_write 전 OpenSSL 에러 스택 확인 (혹시 모를 이전 에러)
ERR_print_errors_fp(stderr); // 이미 오류 스택에 뭔가 있는지 확인용
fprintf(stderr, "--- Before BIO_write ---\n");
::BIO_write(::SSL_get_rbio(data->ssl.get()), data->wsabuf.buf,
cbTransfrred);
// DEBUG: BIO_write 후 OpenSSL 에러 스택 확인 (BIO_write에서 에러 발생 시)
ERR_print_errors_fp(stderr); // BIO_write에서도 에러가 발생할 수 있음
fprintf(stderr, "--- After BIO_write, cbTransfrred: %lu ---\n",
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));
}
if (red_data == -1) {
auto ssl_error_code = SSL_get_error(
data->ssl.get(), red_data); // 여기서 SSL_get_error 결과 저장
auto err_msg = std::format("SSL_read failed with SSL_get_error: {}",
ssl_error_code);
fprintf(stderr, "%s\n", err_msg.c_str());
// *** 가장 중요한 부분: SSL_ERROR_SSL일 때 상세 에러를 강제로 출력 시도
// ***
if (ssl_error_code == SSL_ERROR_SSL) {
fprintf(stderr, "Detailed SSL_ERROR_SSL trace:\n");
unsigned long err_peek;
// ERR_get_error()를 사용하여 스택의 모든 오류를 팝하고 출력
while ((err_peek = ERR_get_error()) != 0) {
char err_str[256];
ERR_error_string_n(err_peek, err_str, sizeof(err_str));
fprintf(stderr, "OpenSSL stack error: %s\n", err_str);
}
} else {
// SSL_ERROR_SSL이 아닌 다른 오류 (SYSCALL, WANT_READ 등)일 경우
// ERR_print_errors_fp는 여전히 유용할 수 있음
ERR_print_errors_fp(stderr);
}
throw std::runtime_error(err_msg); // 예외 발생
}
} 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));
}
::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);
@@ -255,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(
@@ -312,34 +265,10 @@ 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);
}
data_len = front->wsabuf.len;
wsabuf.buf = front->wsabuf.buf;
wsabuf.len = data_len;
::WSASend(sock, &wsabuf, 1, &sendbytes, 0, nullptr, nullptr);
}
}

View File

@@ -28,6 +28,7 @@ void Socket::destruct() {
::close(sock);
#endif
valid_ = false;
sock = 0;
}
Socket::operator SOCKET() {

View File

@@ -18,16 +18,31 @@ void Append(std::vector<char>& buffer, const T& 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);
@@ -54,6 +69,8 @@ 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);

View File

@@ -36,40 +36,12 @@ const Model* Engine::GetStaticModel(std::string 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;
models_[name] = std::move(model_to_spawn);
return &models_[name];
}
if (models_.find(name) == models_.end()) {
Model model_to_spawn(*GetStaticModel(asset_name));
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));
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::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);
@@ -78,6 +50,8 @@ Model* Engine::SpawnLifedModel(std::string asset_name, std::string 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];
@@ -87,6 +61,8 @@ Model* Engine::SpawnLifedModel(std::string asset_name, std::string name,
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;
@@ -152,6 +128,11 @@ void Engine::Update() {
continue;
}
if (model.shouldBeDestroyed) {
it = models_.erase(it);
continue;
}
model.lifespan -= delta_time;
models.push_back(&model);
@@ -178,6 +159,7 @@ 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;
@@ -187,19 +169,123 @@ void Engine::NetworkUpload(std::shared_ptr<Network::Socket> sock) {
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) {
while (iocp_->GetRecvedBytes(sock->sock) < 6);
if (sock->sock == 0) return;
std::vector<char> recv_data(6);
iocp_->recv(*sock, recv_data);
if (recv_partial(iocp_, *sock, recv_data) == 0) return;
Packet::Header header;
header.Deserialize(recv_data);
recv_data.resize(header.body_length);
while (iocp_->GetRecvedBytes(sock->sock) < header.body_length);
iocp_->recv(*sock, recv_data);
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,32 +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);
if (ssl == nullptr) {
throw std::runtime_error("SSL_new failed. Check SSL_CTX or memory.");
}
BIO* rbio = BIO_new(BIO_s_mem());
BIO* wbio = BIO_new(BIO_s_mem());
if (!rbio || !wbio) {
BIO_free(rbio);
BIO_free(wbio);
throw std::runtime_error("BIO_new failed");
}
SSL_set_bio(ssl.get(), rbio, wbio);
transferredbytes = 0;
this->bufsize = bufsize;
IOCPInstance = nullptr;

View File

@@ -39,7 +39,7 @@ class Socket {
std::mutex readMutex;
std::mutex writeMutex;
protected:
private:
bool valid_ = false;
};

View File

@@ -18,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),
@@ -39,12 +40,15 @@ struct Model {
visible(other.visible),
colision(other.colision),
networkReplicated(other.networkReplicated) {
name = "_" + other.name;
ID = utils::GenerateID(CLIENTID);
graphics_ = nullptr;
}
Model(Model&& other)
: vertices(std::move(other.vertices)),
: 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)),
@@ -77,6 +81,8 @@ struct Model {
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 = std::move(other.vertex_buffer);
indices = std::move(other.indices);
@@ -105,6 +111,8 @@ struct Model {
return *this;
}
utils::Snowflake ID;
std::string name;
std::string asset_name;
std::vector<char> Serialize();
void Deserialize(std::vector<char> data);
@@ -144,6 +152,9 @@ struct Model {
bool visible = true;
bool colision = false;
bool networkReplicated = true;
bool shouldBeDestroyed = false;
bool needsUpdate = true;
private:
class Graphics* graphics_;

View File

@@ -19,7 +19,7 @@ class Engine {
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);