Rename universe to world
This commit is contained in:
parent
35ac5f68d9
commit
bc9145e632
@ -1,11 +0,0 @@
|
||||
#pragma once
|
||||
#include <deerith/core/universe.h>
|
||||
|
||||
namespace deerith
|
||||
{
|
||||
bool connect_to_universe(const char *address, int port);
|
||||
void shutdown_universe();
|
||||
|
||||
bool is_universe_running();
|
||||
void render_universe();
|
||||
}
|
||||
15
deerith_engine_client/include/deerith/world.h
Normal file
15
deerith_engine_client/include/deerith/world.h
Normal file
@ -0,0 +1,15 @@
|
||||
#pragma once
|
||||
#include <deerith/world/world_core.h>
|
||||
|
||||
namespace deerith
|
||||
{
|
||||
struct client_t;
|
||||
|
||||
bool connect_to_world(const char *address, int port);
|
||||
void shutdown_world();
|
||||
|
||||
bool is_world_running();
|
||||
void render_world();
|
||||
|
||||
client_t *get_server_client();
|
||||
}
|
||||
@ -1,7 +1,7 @@
|
||||
#include "deerith/graphics/window.h"
|
||||
#include "primitive_internals.h"
|
||||
#include "../network/network_internals.h"
|
||||
#include <deerith/universe.h>
|
||||
#include <deerith/world.h>
|
||||
|
||||
#include <glad/gl.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
@ -130,7 +130,7 @@ void deerith::shutdown_window()
|
||||
ImGui::DestroyContext();
|
||||
|
||||
shutdown_primitives();
|
||||
shutdown_universe();
|
||||
shutdown_world();
|
||||
shutdown_network();
|
||||
|
||||
glfwDestroyWindow(window);
|
||||
|
||||
@ -1,9 +1,9 @@
|
||||
#include <deerith/network/router.h>
|
||||
#include "network/network_internals.h"
|
||||
|
||||
#include <deerith/packets/sync_universe_tick.h>
|
||||
#include <deerith/packets/sync_world_tick.h>
|
||||
|
||||
void deerith::setup_base_protocols()
|
||||
{
|
||||
router_add_packet_processor<sync_universe_tick_t>();
|
||||
router_add_packet_processor<sync_world_tick_t>();
|
||||
}
|
||||
@ -1,15 +1,24 @@
|
||||
#include <deerith/engine.h>
|
||||
#include <deerith/network/router.h>
|
||||
#include <deerith/world.h>
|
||||
|
||||
#include <enet/enet.h>
|
||||
#include <thread>
|
||||
#include <memory>
|
||||
|
||||
#include "network_internals.h"
|
||||
|
||||
namespace deerith
|
||||
{
|
||||
ENetHost *client;
|
||||
ENetPeer *client_peer;
|
||||
ENetPeer *server_peer;
|
||||
|
||||
std::unique_ptr<client_t> server_client;
|
||||
}
|
||||
|
||||
deerith::client_t *deerith::get_server_client()
|
||||
{
|
||||
return server_client.get();
|
||||
}
|
||||
|
||||
void deerith::connect_to_server(const char *address, int port)
|
||||
@ -23,8 +32,10 @@ void deerith::connect_to_server(const char *address, int port)
|
||||
client = enet_host_create(NULL, 1, 2, 0, 0);
|
||||
deerith_assert(client, "connect_to_server failed to create host");
|
||||
|
||||
client_peer = enet_host_connect(client, &eaddress, 2, 0);
|
||||
deerith_assert(client_peer, "enet_host_connect failed on connect_to_server");
|
||||
server_peer = enet_host_connect(client, &eaddress, 2, 0);
|
||||
deerith_assert(server_peer, "enet_host_connect failed on connect_to_server");
|
||||
|
||||
server_client = std::make_unique<client_t>(server_peer);
|
||||
}
|
||||
|
||||
int deerith::init_network()
|
||||
@ -58,7 +69,7 @@ void deerith::flush_network_events()
|
||||
|
||||
case ENET_EVENT_TYPE_RECEIVE:
|
||||
ENetPacket *packet = event.packet;
|
||||
router_process_packet(packet->data, packet->dataLength);
|
||||
router_process_packet(packet->data, packet->dataLength, server_client.get());
|
||||
enet_packet_destroy(event.packet);
|
||||
break;
|
||||
}
|
||||
@ -75,7 +86,7 @@ bool deerith::wait_till_connected_to_server()
|
||||
}
|
||||
else
|
||||
{
|
||||
enet_peer_reset(client_peer);
|
||||
enet_peer_reset(server_peer);
|
||||
deerith_error("failed to connect to server");
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -1,59 +0,0 @@
|
||||
#include <deerith/engine.h>
|
||||
#include <deerith/universe.h>
|
||||
|
||||
#include "network/network_internals.h"
|
||||
|
||||
#include <thread>
|
||||
#include <memory>
|
||||
|
||||
namespace deerith
|
||||
{
|
||||
std::unique_ptr<std::thread> universe_thread;
|
||||
std::atomic<bool> universe_running = false;
|
||||
|
||||
void universe_simulation();
|
||||
}
|
||||
|
||||
bool deerith::connect_to_universe(const char *address, int port)
|
||||
{
|
||||
deerith_assert(!universe_running, "connect_to_universe failed because there is a current running universe");
|
||||
|
||||
shutdown_universe();
|
||||
connect_to_server(address, port);
|
||||
|
||||
if (!wait_till_connected_to_server())
|
||||
{
|
||||
deerith_error("engine was not able to connect to server");
|
||||
return false;
|
||||
}
|
||||
|
||||
universe_running = true;
|
||||
universe_thread = std::make_unique<std::thread>(universe_simulation);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool deerith::is_universe_running()
|
||||
{
|
||||
return universe_running;
|
||||
}
|
||||
|
||||
void deerith::universe_simulation()
|
||||
{
|
||||
while (universe_running)
|
||||
{
|
||||
flush_network_events();
|
||||
|
||||
wait_till_next_click();
|
||||
}
|
||||
|
||||
universe_running = false;
|
||||
deerith_trace("universe shutdown");
|
||||
}
|
||||
|
||||
void deerith::shutdown_universe()
|
||||
{
|
||||
universe_running = false;
|
||||
if (universe_thread && universe_thread->joinable())
|
||||
universe_thread->join();
|
||||
universe_thread.reset();
|
||||
}
|
||||
@ -1,14 +0,0 @@
|
||||
#include <deerith/packets/sync_universe_tick.h>
|
||||
#include <deerith/network/router.h>
|
||||
#include <deerith/universe.h>
|
||||
|
||||
namespace deerith
|
||||
{
|
||||
}
|
||||
|
||||
template <>
|
||||
void deerith::process_packet(sync_universe_tick_t &universe_tick)
|
||||
{
|
||||
deerith_trace("sync_universe_tick_t tick set to {}", universe_tick.tick_id);
|
||||
set_tick(universe_tick.tick_id);
|
||||
}
|
||||
@ -1,5 +0,0 @@
|
||||
#include <deerith/universe.h>
|
||||
|
||||
void deerith::render_universe()
|
||||
{
|
||||
}
|
||||
27
deerith_engine_client/src/world/player_movment.cpp
Normal file
27
deerith_engine_client/src/world/player_movment.cpp
Normal file
@ -0,0 +1,27 @@
|
||||
#include <deerith/engine.h>
|
||||
#include <deerith/graphics/input.h>
|
||||
#include <deerith/network/router.h>
|
||||
|
||||
#include <deerith/world/character_sync_interface.h>
|
||||
#include <deerith/packets/sync_character_interface.h>
|
||||
|
||||
#include "world/world_internals.h"
|
||||
|
||||
namespace deerith
|
||||
{
|
||||
extern character_sync_interface_t character_sync_interface;
|
||||
}
|
||||
|
||||
void deerith::process_player_input()
|
||||
{
|
||||
glm::vec2 movment = get_joystick_axes(0, 0);
|
||||
character_sync_interface.move_input.push_value(movment);
|
||||
}
|
||||
|
||||
void deerith::send_player_input()
|
||||
{
|
||||
sync_character_interface_t sync_character_interface;
|
||||
sync_character_interface.load(character_sync_interface);
|
||||
|
||||
router_send_packet<sync_character_interface_t>(sync_character_interface, get_server_client());
|
||||
}
|
||||
71
deerith_engine_client/src/world/universe.cpp
Normal file
71
deerith_engine_client/src/world/universe.cpp
Normal file
@ -0,0 +1,71 @@
|
||||
#include <deerith/engine.h>
|
||||
#include <deerith/world.h>
|
||||
|
||||
#include <deerith/world/character_sync_interface.h>
|
||||
|
||||
#include "network/network_internals.h"
|
||||
#include "world/world_internals.h"
|
||||
|
||||
#include <thread>
|
||||
#include <memory>
|
||||
|
||||
namespace deerith
|
||||
{
|
||||
std::unique_ptr<std::thread> world_thread;
|
||||
std::atomic<bool> world_running = false;
|
||||
|
||||
character_sync_interface_t character_sync_interface;
|
||||
|
||||
void world_simulation();
|
||||
}
|
||||
|
||||
bool deerith::connect_to_world(const char *address, int port)
|
||||
{
|
||||
deerith_assert(!world_running, "connect_to_world failed because there is a current running world");
|
||||
|
||||
shutdown_world();
|
||||
connect_to_server(address, port);
|
||||
|
||||
if (!wait_till_connected_to_server())
|
||||
{
|
||||
deerith_error("engine was not able to connect to server");
|
||||
return false;
|
||||
}
|
||||
|
||||
world_running = true;
|
||||
world_thread = std::make_unique<std::thread>(world_simulation);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool deerith::is_world_running()
|
||||
{
|
||||
return world_running;
|
||||
}
|
||||
|
||||
void deerith::world_simulation()
|
||||
{
|
||||
while (world_running)
|
||||
{
|
||||
flush_network_events();
|
||||
|
||||
process_player_input();
|
||||
|
||||
if (get_world_tick() % 2 == 0)
|
||||
{
|
||||
send_player_input();
|
||||
}
|
||||
|
||||
wait_till_next_click();
|
||||
}
|
||||
|
||||
world_running = false;
|
||||
deerith_trace("world shutdown");
|
||||
}
|
||||
|
||||
void deerith::shutdown_world()
|
||||
{
|
||||
world_running = false;
|
||||
if (world_thread && world_thread->joinable())
|
||||
world_thread->join();
|
||||
world_thread.reset();
|
||||
}
|
||||
7
deerith_engine_client/src/world/universe_internals.h
Normal file
7
deerith_engine_client/src/world/universe_internals.h
Normal file
@ -0,0 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
namespace deerith
|
||||
{
|
||||
void process_player_input();
|
||||
void send_player_input();
|
||||
}
|
||||
14
deerith_engine_client/src/world/universe_protocols.cpp
Normal file
14
deerith_engine_client/src/world/universe_protocols.cpp
Normal file
@ -0,0 +1,14 @@
|
||||
#include <deerith/packets/sync_world_tick.h>
|
||||
#include <deerith/network/router.h>
|
||||
#include <deerith/world.h>
|
||||
|
||||
namespace deerith
|
||||
{
|
||||
}
|
||||
|
||||
template <>
|
||||
void deerith::process_packet(sync_world_tick_t &world_tick, client_t *client)
|
||||
{
|
||||
deerith_trace("sync_world_tick_t tick set to {}", world_tick.tick_id);
|
||||
set_tick(world_tick.tick_id);
|
||||
}
|
||||
5
deerith_engine_client/src/world/universe_render.cpp
Normal file
5
deerith_engine_client/src/world/universe_render.cpp
Normal file
@ -0,0 +1,5 @@
|
||||
#include <deerith/world.h>
|
||||
|
||||
void deerith::render_world()
|
||||
{
|
||||
}
|
||||
18
deerith_engine_core/include/deerith/core/serialization_ext.h
Normal file
18
deerith_engine_core/include/deerith/core/serialization_ext.h
Normal file
@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
namespace cereal
|
||||
{
|
||||
template <class Archive>
|
||||
void serialize(Archive &archive, glm::vec2 &vec)
|
||||
{
|
||||
archive(vec.x, vec.y);
|
||||
}
|
||||
|
||||
template <class Archive>
|
||||
void serialize(Archive &archive, glm::vec3 &vec)
|
||||
{
|
||||
archive(vec.x, vec.y, vec.z);
|
||||
}
|
||||
}
|
||||
@ -3,8 +3,9 @@
|
||||
#include <deerith/core/log.h>
|
||||
#include <deerith/core/core.h>
|
||||
|
||||
#define UNIVERSE_SIMULATION_TICK_FREQ 50
|
||||
#define world_SIMULATION_TICK_FREQ 50
|
||||
#define NETWORK_RELATIVE_TICK 2
|
||||
|
||||
#define NETWORK_DELAY 10
|
||||
|
||||
namespace deerith
|
||||
|
||||
11
deerith_engine_core/include/deerith/network/client.h
Normal file
11
deerith_engine_core/include/deerith/network/client.h
Normal file
@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
#include <enet/enet.h>
|
||||
|
||||
namespace deerith
|
||||
{
|
||||
struct client_t
|
||||
{
|
||||
client_t(ENetPeer *_peer) : peer(_peer) {}
|
||||
ENetPeer *peer;
|
||||
};
|
||||
}
|
||||
@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
#include <deerith/engine.h>
|
||||
#include <deerith/network/client.h>
|
||||
|
||||
#include <cereal/cereal.hpp>
|
||||
#include <cereal/archives/portable_binary.hpp>
|
||||
@ -10,25 +11,31 @@
|
||||
|
||||
namespace deerith
|
||||
{
|
||||
using packet_processor_t = std::function<void(const uint8_t *, size_t)>;
|
||||
using packet_processor_t = std::function<void(const uint8_t *, size_t, client_t *)>;
|
||||
extern std::unordered_map<uint16_t, packet_processor_t> packet_processors;
|
||||
|
||||
template <typename T>
|
||||
void router_add_packet_processor();
|
||||
void router_process_packet(const uint8_t *data, size_t size);
|
||||
void router_process_packet(const uint8_t *data, size_t size, client_t *client);
|
||||
|
||||
template <typename T>
|
||||
void router_send_packet(T &, ENetPeer *);
|
||||
void router_send_packet(T &, client_t *);
|
||||
|
||||
template <typename T>
|
||||
void process_packet(T &);
|
||||
void process_packet(T &, client_t *client);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void deerith::router_add_packet_processor()
|
||||
{
|
||||
packet_processors[T::packet_id] = [](const uint8_t *data, size_t size)
|
||||
packet_processors[T::packet_id] = [](const uint8_t *data, size_t size, client_t *client)
|
||||
{
|
||||
if (!client)
|
||||
{
|
||||
deerith_error("router_add_packet_processor client is nullptr");
|
||||
return;
|
||||
}
|
||||
|
||||
std::string buffer(reinterpret_cast<const char *>(data), size);
|
||||
std::istringstream stream(buffer, std::ios::in | std::ios::binary);
|
||||
|
||||
@ -37,14 +44,20 @@ void deerith::router_add_packet_processor()
|
||||
T packet{};
|
||||
archive(packet);
|
||||
|
||||
process_packet<T>(packet);
|
||||
process_packet<T>(packet, client);
|
||||
};
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void deerith::router_send_packet(T &packet, ENetPeer *peer)
|
||||
void deerith::router_send_packet(T &packet, client_t *client)
|
||||
{
|
||||
|
||||
if (!client)
|
||||
{
|
||||
deerith_error("router_send_packet client is nullptr");
|
||||
return;
|
||||
}
|
||||
|
||||
std::ostringstream stream(std::ios::out | std::ios::binary);
|
||||
cereal::PortableBinaryOutputArchive archive(stream);
|
||||
|
||||
@ -73,7 +86,7 @@ void deerith::router_send_packet(T &packet, ENetPeer *peer)
|
||||
return;
|
||||
}
|
||||
|
||||
if (enet_peer_send(peer, 0, enet_packet) < 0)
|
||||
if (enet_peer_send(client->peer, 0, enet_packet) < 0)
|
||||
{
|
||||
enet_packet_destroy(enet_packet);
|
||||
deerith_error("failed to send ENet packet");
|
||||
|
||||
@ -1,27 +1,110 @@
|
||||
#pragma once
|
||||
#include <deerith/engine.h>
|
||||
#include <deerith/world.h>
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
#include <array>
|
||||
|
||||
#define BUFFER_SIZE (NETWORK_DELAY + 20)
|
||||
#include <cereal/types/array.hpp>
|
||||
#include <deerith/core/serialization_ext.h>
|
||||
|
||||
#define SYNC_VARIABLE_STORAGE 30
|
||||
#define BUFFER_SIZE (NETWORK_DELAY + SYNC_VARIABLE_STORAGE)
|
||||
|
||||
namespace deerith
|
||||
{
|
||||
inline size_t get_sync_variable_index(size_t tick = get_world_tick())
|
||||
{
|
||||
return ((size_t)tick) % BUFFER_SIZE;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct sync_variable_transport_t;
|
||||
|
||||
template <typename T>
|
||||
struct sync_variable_entry_t
|
||||
{
|
||||
T value;
|
||||
uint32_t tick_id;
|
||||
int tick_id = 0;
|
||||
|
||||
template <typename Archive>
|
||||
void serialize(Archive &archive)
|
||||
{
|
||||
archive(value);
|
||||
archive(tick_id);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
class sync_variable_t
|
||||
{
|
||||
public:
|
||||
~sync_variable_t() override = default;
|
||||
T pull_value()
|
||||
{
|
||||
sync_variable_entry_t<T> &entry = buffer[get_sync_variable_index()];
|
||||
if (entry.tick_id != get_world_tick())
|
||||
{
|
||||
return T();
|
||||
}
|
||||
return entry.value;
|
||||
}
|
||||
|
||||
void push_value(const T &v)
|
||||
{
|
||||
set_value(v, get_world_tick() + NETWORK_DELAY);
|
||||
}
|
||||
|
||||
void set_value(const T &v, int tick_id = 0)
|
||||
{
|
||||
if (tick_id > get_world_tick() + SYNC_VARIABLE_STORAGE)
|
||||
{
|
||||
deerith_error("set_value error, tick_id > get_world_tick() + SYNC_VARIABLE_STORAGE, tick_id : {}", tick_id);
|
||||
return;
|
||||
}
|
||||
sync_variable_entry_t<T> &entry = buffer[get_sync_variable_index(tick_id)];
|
||||
entry.tick_id = tick_id;
|
||||
entry.value = v;
|
||||
}
|
||||
|
||||
private:
|
||||
std::array<sync_variable_entry_t<T>, BUFFER_SIZE> buffer;
|
||||
friend sync_variable_transport_t<T>;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct sync_variable_transport_t
|
||||
{
|
||||
std::array<sync_variable_entry_t<T>, NETWORK_DELAY> variable_transport_data;
|
||||
|
||||
void load(const sync_variable_t<T> &sync_var)
|
||||
{
|
||||
int begin_tick_id = get_world_tick();
|
||||
|
||||
for (int i = 0; i < NETWORK_DELAY; i++)
|
||||
{
|
||||
variable_transport_data[i] =
|
||||
sync_var.buffer[get_sync_variable_index(i + begin_tick_id)];
|
||||
}
|
||||
}
|
||||
|
||||
void dipose(sync_variable_t<T> &sync_var)
|
||||
{
|
||||
int begin_tick_id = get_world_tick();
|
||||
for (int i = 0; i < NETWORK_DELAY; i++)
|
||||
{
|
||||
const sync_variable_entry_t<T> &entry = variable_transport_data[i];
|
||||
if (entry.tick_id < begin_tick_id || entry.tick_id >= begin_tick_id + NETWORK_DELAY)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
sync_var.buffer[get_sync_variable_index(entry.tick_id)] = entry;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename Archive>
|
||||
void serialize(Archive &archive)
|
||||
{
|
||||
archive(variable_transport_data);
|
||||
}
|
||||
};
|
||||
}
|
||||
@ -0,0 +1,30 @@
|
||||
#pragma once
|
||||
#include <deerith/engine.h>
|
||||
#include <deerith/world/character_sync_interface.h>
|
||||
|
||||
namespace deerith
|
||||
{
|
||||
struct sync_character_interface_t
|
||||
{
|
||||
static constexpr uint16_t packet_id = 2;
|
||||
static constexpr bool reliable = false;
|
||||
|
||||
sync_variable_transport_t<glm::vec2> move_input_transport;
|
||||
|
||||
void load(const character_sync_interface_t &ci)
|
||||
{
|
||||
move_input_transport.load(ci.move_input);
|
||||
}
|
||||
|
||||
void dipose(character_sync_interface_t &ci)
|
||||
{
|
||||
move_input_transport.load(ci.move_input);
|
||||
}
|
||||
|
||||
template <typename Archive>
|
||||
void serialize(Archive &archive)
|
||||
{
|
||||
archive(move_input_transport);
|
||||
}
|
||||
};
|
||||
}
|
||||
@ -3,7 +3,7 @@
|
||||
|
||||
namespace deerith
|
||||
{
|
||||
struct sync_universe_tick_t
|
||||
struct sync_world_tick_t
|
||||
{
|
||||
static constexpr uint16_t packet_id = 1;
|
||||
static constexpr bool reliable = true;
|
||||
10
deerith_engine_core/include/deerith/world/character.h
Normal file
10
deerith_engine_core/include/deerith/world/character.h
Normal file
@ -0,0 +1,10 @@
|
||||
#pragma once
|
||||
#include <deerith/world/character_sync_interface.h>
|
||||
|
||||
namespace deerith
|
||||
{
|
||||
struct character_t
|
||||
{
|
||||
};
|
||||
|
||||
}
|
||||
@ -3,7 +3,7 @@
|
||||
|
||||
namespace deerith
|
||||
{
|
||||
struct sync_user_t
|
||||
struct character_sync_interface_t
|
||||
{
|
||||
sync_variable_t<glm::vec2> move_input;
|
||||
sync_variable_t<glm::vec2> position;
|
||||
@ -3,8 +3,10 @@
|
||||
|
||||
namespace deerith
|
||||
{
|
||||
uint32_t get_universe_tick();
|
||||
uint32_t get_world_tick();
|
||||
void wait_till_next_click();
|
||||
|
||||
void set_tick(uint32_t);
|
||||
|
||||
}
|
||||
^^
|
||||
@ -8,7 +8,7 @@ namespace deerith
|
||||
std::unordered_map<uint16_t, packet_processor_t> packet_processors;
|
||||
}
|
||||
|
||||
void deerith::router_process_packet(const uint8_t *data, size_t size)
|
||||
void deerith::router_process_packet(const uint8_t *data, size_t size, client_t *client)
|
||||
{
|
||||
if (size <= 2)
|
||||
{
|
||||
@ -23,5 +23,5 @@ void deerith::router_process_packet(const uint8_t *data, size_t size)
|
||||
return;
|
||||
}
|
||||
|
||||
packet_processors[packet_id](&data[2], size - 2);
|
||||
packet_processors[packet_id](&data[2], size - 2, client);
|
||||
}
|
||||
@ -1,4 +1,4 @@
|
||||
#include <deerith/core/universe.h>
|
||||
#include <deerith/world/world_core.h>
|
||||
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
@ -13,7 +13,7 @@ void deerith::set_tick(uint32_t v)
|
||||
tick_id = v;
|
||||
}
|
||||
|
||||
uint32_t deerith::get_universe_tick()
|
||||
uint32_t deerith::get_world_tick()
|
||||
{
|
||||
return tick_id;
|
||||
}
|
||||
|
||||
@ -11,6 +11,8 @@ target_sources(DeerithServer
|
||||
)
|
||||
|
||||
target_include_directories(DeerithServer
|
||||
PRIVATE
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src
|
||||
PUBLIC
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/include
|
||||
)
|
||||
|
||||
@ -1,10 +0,0 @@
|
||||
#pragma once
|
||||
#include <deerith/core/universe.h>
|
||||
|
||||
namespace deerith
|
||||
{
|
||||
void run_universe(int port);
|
||||
void shutdown_universe();
|
||||
|
||||
bool is_universe_running();
|
||||
}
|
||||
10
deerith_engine_server/include/deerith/world.h
Normal file
10
deerith_engine_server/include/deerith/world.h
Normal file
@ -0,0 +1,10 @@
|
||||
#pragma once
|
||||
#include <deerith/world/world_core.h>
|
||||
|
||||
namespace deerith
|
||||
{
|
||||
void run_world(int port);
|
||||
void shutdown_world();
|
||||
|
||||
bool is_world_running();
|
||||
}
|
||||
9
deerith_engine_server/src/network/base_protocols.cpp
Normal file
9
deerith_engine_server/src/network/base_protocols.cpp
Normal file
@ -0,0 +1,9 @@
|
||||
#include "network/network_internals.h"
|
||||
#include <deerith/network/router.h>
|
||||
|
||||
#include <deerith/packets/sync_character_interface.h>
|
||||
|
||||
void deerith::load_base_protocols()
|
||||
{
|
||||
router_add_packet_processor<sync_character_interface_t>();
|
||||
}
|
||||
@ -1,10 +1,15 @@
|
||||
#include <deerith/core/log.h>
|
||||
#include <enet/enet.h>
|
||||
#include <deerith/network/client.h>
|
||||
#include <deerith/network/router.h>
|
||||
#include <deerith/packets/sync_world_tick.h>
|
||||
|
||||
#include <deerith/core/log.h>
|
||||
#include <deerith/world.h>
|
||||
|
||||
#include <enet/enet.h>
|
||||
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
|
||||
#include "network_internals.h"
|
||||
|
||||
@ -12,7 +17,9 @@ namespace deerith
|
||||
{
|
||||
ENetHost *server;
|
||||
|
||||
std::vector<ENetPeer *> new_clients;
|
||||
std::vector<std::unique_ptr<client_t>> clients;
|
||||
|
||||
void create_client(ENetPeer *peer);
|
||||
}
|
||||
|
||||
bool deerith::init_server(int port)
|
||||
@ -29,6 +36,9 @@ bool deerith::init_server(int port)
|
||||
deerith_error("init_server failed");
|
||||
return false;
|
||||
}
|
||||
|
||||
load_base_protocols();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -48,23 +58,15 @@ void deerith::shutdown_network()
|
||||
enet_deinitialize();
|
||||
}
|
||||
|
||||
std::vector<ENetPeer *> &deerith::get_new_clients()
|
||||
{
|
||||
return new_clients;
|
||||
}
|
||||
|
||||
void deerith::flush_network_events()
|
||||
{
|
||||
ENetEvent event;
|
||||
new_clients.clear();
|
||||
|
||||
int result = 1;
|
||||
while ((result = enet_host_service(server, &event, 0)) > 0)
|
||||
while (enet_host_service(server, &event, 0) > 0)
|
||||
{
|
||||
switch (event.type)
|
||||
{
|
||||
case ENET_EVENT_TYPE_CONNECT:
|
||||
new_clients.push_back(event.peer);
|
||||
create_client(event.peer);
|
||||
break;
|
||||
|
||||
case ENET_EVENT_TYPE_DISCONNECT:
|
||||
@ -72,17 +74,27 @@ void deerith::flush_network_events()
|
||||
break;
|
||||
|
||||
case ENET_EVENT_TYPE_RECEIVE:
|
||||
printf("A packet of length %lu containing %s was received from %s on channel %u.\n",
|
||||
event.packet->dataLength,
|
||||
event.packet->data,
|
||||
event.peer->data,
|
||||
event.channelID);
|
||||
|
||||
ENetPacket *packet = event.packet;
|
||||
router_process_packet(packet->data, packet->dataLength, (client_t *)event.peer->data);
|
||||
enet_packet_destroy(event.packet);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
case ENET_EVENT_TYPE_NONE:
|
||||
break;
|
||||
}
|
||||
}
|
||||
void deerith::create_client(ENetPeer *peer)
|
||||
{
|
||||
char enet_address[32];
|
||||
enet_address_get_host_ip(&peer->address, enet_address, 31);
|
||||
deerith_trace("a new client has connected : {}", enet_address);
|
||||
|
||||
sync_world_tick_t sync_world_tick;
|
||||
sync_world_tick.tick_id = get_world_tick();
|
||||
|
||||
std::unique_ptr<client_t> client = std::make_unique<client_t>(peer);
|
||||
peer->data = client.get();
|
||||
|
||||
clients.push_back(std::move(client));
|
||||
|
||||
router_send_packet<sync_world_tick_t>(sync_world_tick, (client_t *)peer->data);
|
||||
}
|
||||
@ -10,5 +10,6 @@ namespace deerith
|
||||
bool init_server(int port);
|
||||
void flush_network_events();
|
||||
|
||||
std::vector<ENetPeer *> &get_new_clients();
|
||||
void load_base_protocols();
|
||||
|
||||
}
|
||||
@ -1,70 +0,0 @@
|
||||
#include <deerith/universe.h>
|
||||
|
||||
#include <deerith/core/log.h>
|
||||
#include <deerith/network/router.h>
|
||||
|
||||
#include <deerith/packets/sync_universe_tick.h>
|
||||
|
||||
#include "network/network_internals.h"
|
||||
|
||||
#include <csignal>
|
||||
#include <atomic>
|
||||
|
||||
namespace deerith
|
||||
{
|
||||
int universe_tick = 0;
|
||||
void universe_simulation();
|
||||
|
||||
std::atomic<bool> running = true;
|
||||
|
||||
void create_client(ENetPeer *peer);
|
||||
void handle_signal(int signal);
|
||||
}
|
||||
|
||||
void deerith::handle_signal(int signal)
|
||||
{
|
||||
if (signal == SIGTERM)
|
||||
running = false;
|
||||
}
|
||||
|
||||
void deerith::run_universe(int port)
|
||||
{
|
||||
std::signal(SIGTERM, handle_signal);
|
||||
|
||||
if (init_server(port))
|
||||
{
|
||||
deerith_trace("server init on port {}", port);
|
||||
while (running)
|
||||
{
|
||||
universe_simulation();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
deerith_error("could not init server");
|
||||
}
|
||||
}
|
||||
|
||||
void deerith::universe_simulation()
|
||||
{
|
||||
flush_network_events();
|
||||
|
||||
for (ENetPeer *new_client : get_new_clients())
|
||||
{
|
||||
create_client(new_client);
|
||||
}
|
||||
|
||||
wait_till_next_click();
|
||||
}
|
||||
|
||||
void deerith::create_client(ENetPeer *client)
|
||||
{
|
||||
char enet_address[32];
|
||||
enet_address_get_host_ip(&client->address, enet_address, 31);
|
||||
deerith_trace("A new client has connected : {}", enet_address);
|
||||
|
||||
sync_universe_tick_t sync_universe_tick;
|
||||
sync_universe_tick.tick_id = get_universe_tick();
|
||||
|
||||
router_send_packet<sync_universe_tick_t>(sync_universe_tick, client);
|
||||
}
|
||||
51
deerith_engine_server/src/world.cpp
Normal file
51
deerith_engine_server/src/world.cpp
Normal file
@ -0,0 +1,51 @@
|
||||
#include <deerith/world.h>
|
||||
|
||||
#include <deerith/core/log.h>
|
||||
#include "network/network_internals.h"
|
||||
|
||||
#include <csignal>
|
||||
#include <atomic>
|
||||
|
||||
namespace deerith
|
||||
{
|
||||
int world_tick = 0;
|
||||
void world_simulation();
|
||||
|
||||
std::atomic<bool> running = true;
|
||||
|
||||
void create_client(ENetPeer *peer);
|
||||
void handle_signal(int signal);
|
||||
}
|
||||
|
||||
void deerith::handle_signal(int signal)
|
||||
{
|
||||
if (signal == SIGTERM)
|
||||
running = false;
|
||||
}
|
||||
|
||||
void deerith::run_world(int port)
|
||||
{
|
||||
std::signal(SIGTERM, handle_signal);
|
||||
|
||||
if (init_server(port))
|
||||
{
|
||||
deerith_trace("server init on port {}", port);
|
||||
while (running)
|
||||
{
|
||||
world_simulation();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
deerith_error("could not init server");
|
||||
}
|
||||
}
|
||||
|
||||
void deerith::world_simulation()
|
||||
{
|
||||
flush_network_events();
|
||||
|
||||
// process movments
|
||||
|
||||
wait_till_next_click();
|
||||
}
|
||||
15
deerith_engine_server/src/world/character_sync.cpp
Normal file
15
deerith_engine_server/src/world/character_sync.cpp
Normal file
@ -0,0 +1,15 @@
|
||||
#include <deerith/engine.h>
|
||||
|
||||
#include <deerith/packets/sync_character_interface.h>
|
||||
#include <deerith/network/router.h>
|
||||
|
||||
namespace deerith
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
template <>
|
||||
void deerith::process_packet(sync_character_interface_t &interface, client_t *client)
|
||||
{
|
||||
deerith_trace("sync player input {} {}");
|
||||
}
|
||||
6
deerith_engine_server/src/world/universe_internals.h
Normal file
6
deerith_engine_server/src/world/universe_internals.h
Normal file
@ -0,0 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
namespace deerith
|
||||
{
|
||||
void update_players_characters();
|
||||
}
|
||||
5
deerith_engine_server/src/world/universe_simulation.cpp
Normal file
5
deerith_engine_server/src/world/universe_simulation.cpp
Normal file
@ -0,0 +1,5 @@
|
||||
#include "world/world_internals.h"
|
||||
|
||||
void update_players_characters()
|
||||
{
|
||||
}
|
||||
@ -1,5 +1,5 @@
|
||||
#include <deerith/engine.h>
|
||||
#include <deerith/universe.h>
|
||||
#include <deerith/world.h>
|
||||
#include <thread>
|
||||
|
||||
#ifdef DEERITH_CLIENT
|
||||
@ -11,7 +11,7 @@ int main(int args_count, const char **args)
|
||||
deerith::init_engine();
|
||||
|
||||
#ifdef DEERITH_SERVER
|
||||
deerith::run_universe(8080);
|
||||
deerith::run_world(8080);
|
||||
#endif
|
||||
|
||||
#ifdef DEERITH_CLIENT
|
||||
@ -24,13 +24,13 @@ int main(int args_count, const char **args)
|
||||
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
|
||||
if (deerith::connect_to_universe("127.0.0.1", 8080))
|
||||
if (deerith::connect_to_world("127.0.0.1", 8080))
|
||||
{
|
||||
deerith_assert(deerith::init_window() == 0, "Failed to init_window");
|
||||
|
||||
while (deerith::begin_window_display())
|
||||
{
|
||||
deerith::render_universe();
|
||||
deerith::render_world();
|
||||
|
||||
deerith::finish_window_display();
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user