Compare commits
5 Commits
2e95c941ba
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| ee1cdeaeca | |||
| 2508f12e86 | |||
| 0260b4a0a0 | |||
| b71f740b9a | |||
| 0e639abcd2 |
+2
-6
@@ -1,5 +1,5 @@
|
||||
cmake_minimum_required(VERSION 3.26)
|
||||
project(towards)
|
||||
project(tw_common)
|
||||
|
||||
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
||||
set(CMAKE_CXX_STANDARD 23)
|
||||
@@ -42,11 +42,7 @@ target_include_directories(${PROJECT_NAME}
|
||||
|
||||
target_link_libraries(${PROJECT_NAME}
|
||||
PUBLIC
|
||||
tw::network
|
||||
tw::protocol
|
||||
loft::common
|
||||
loft::base
|
||||
loft::render_graph
|
||||
spdlog::spdlog
|
||||
glm::glm
|
||||
EnTT::EnTT
|
||||
Jolt
|
||||
|
||||
@@ -4,6 +4,7 @@ Game engine built as a modular monolith.
|
||||
|
||||
## Building
|
||||
|
||||
### Locally
|
||||
Dependencies:
|
||||
- Conan 2 (`pipx install conan`, or `pip install conan`)
|
||||
- Vulkan SDK
|
||||
@@ -27,8 +28,8 @@ $ conan install . --output-folder=build --build=missing -s build_type=Debug
|
||||
Configure and build using the generated preset:
|
||||
|
||||
```bash
|
||||
$ cmake --preset conan-debug
|
||||
$ cmake --build --preset conan-debug
|
||||
$ cmake -S . -B ./build -DCMAKE_TOOLCHAIN_FILE="build/conan_toolchain.cmake" -DCMAKE_BUILD_TYPE=Debug
|
||||
$ cmake --build ./build
|
||||
```
|
||||
|
||||
For a release build, repeat both steps with `-s build_type=Release` and the
|
||||
@@ -42,7 +43,7 @@ Client is at `build/modules/client/mmo_client`
|
||||
|
||||
First run the server, so that client can connect. The server will try to use port 8080, but if it is already occupied, it will try use the next free higher one.
|
||||
|
||||
## Docker build
|
||||
### Docker
|
||||
|
||||
It also possible to build the project using Docker. Advantage is that you do not have to set up the environment. The current Dockerfile also uses older linux distribution to be backwards compatible with older systems. To build everything, build the Dockerfile at `docker/Dockerfile` with until target `builder`:
|
||||
|
||||
@@ -55,3 +56,29 @@ To easily export the built executables, run the target `export` of the same Dock
|
||||
```
|
||||
$ docker build -f docker/Dockerfile --target export --output type=local,dest=dist .
|
||||
```
|
||||
|
||||
|
||||
## Running
|
||||
|
||||
### Server
|
||||
|
||||
To run the system as inteded, run the `tw_server` executable first. It should output something like this:
|
||||
|
||||
```
|
||||
[2026-08-05 16:43:53.806] [info] quicr-port=8101 cluster-port=8102 timescaledb=disabled
|
||||
[2026-08-05 16:43:53.807] [info] Running on port: 8101
|
||||
[2026-08-05 16:43:53.807] [info] ZoneClusterLink listening on port 8102
|
||||
[2026-08-05 16:43:53.810] [info] Registered as zone 1 (-5000,-5000) (0,5000)
|
||||
```
|
||||
|
||||
Note the `Running on port: 8101` as it reports on which port it is currently running. Use it when connecting the clients.
|
||||
|
||||
### Client
|
||||
|
||||
Run the `tw_client` for connecting to the game. It should open into a lobby, where you can input an address of the server. It should remember the last address used and you can make some favourites. This is useful for testing on multiple cloud instances for example.
|
||||
|
||||
### Mock Client
|
||||
|
||||
The `tw_mock_client` module is for stress testing the server. It simulates 300 connected clients and moves them randomly. All those simulated players are actual established connections, which means the bandwidth of the server will be accurate.
|
||||
|
||||
|
||||
|
||||
+2
-11
@@ -1,6 +1,6 @@
|
||||
services:
|
||||
timescaledb:
|
||||
image: timescale/timescaledb:latest-pg16
|
||||
image: timescale/timescaledb:latest-pg15
|
||||
restart: unless-stopped
|
||||
environment:
|
||||
POSTGRES_USER: mmo
|
||||
@@ -9,7 +9,7 @@ services:
|
||||
ports:
|
||||
- "5432:5432"
|
||||
volumes:
|
||||
- timescaledb_data:/var/lib/postgresql/data
|
||||
- timescaledb_data:/var/lib/towards_db/data
|
||||
|
||||
grafana:
|
||||
image: grafana/grafana:latest
|
||||
@@ -24,15 +24,6 @@ services:
|
||||
depends_on:
|
||||
- timescaledb
|
||||
|
||||
# The zone server is not containerised yet: it needs libpqxx >= 7.7 for
|
||||
# pqxx::params and jammy carries 6.4.
|
||||
|
||||
# ── e2e harness ────────────────────────────────────────────────────────────
|
||||
# Opt in with `--profile e2e`, so a plain `docker compose up` still brings up
|
||||
# the database and dashboards on their own.
|
||||
#
|
||||
# docker compose --profile e2e up --build \
|
||||
# --abort-on-container-exit --exit-code-from chat-mock-client
|
||||
chat-server:
|
||||
profiles: [e2e]
|
||||
build:
|
||||
|
||||
@@ -1,12 +1,15 @@
|
||||
[requires]
|
||||
protobuf/7.35.0
|
||||
sdl/3.4.8
|
||||
libpqxx/8.0.2
|
||||
|
||||
[tool_requires]
|
||||
protobuf/7.35.0
|
||||
|
||||
[options]
|
||||
protobuf/*:shared=False
|
||||
libpqxx/*:shared=False
|
||||
libpq/*:shared=True
|
||||
sdl/*:shared=False
|
||||
sdl/*:camera=False
|
||||
sdl/*:dialog=False
|
||||
|
||||
+17
-54
@@ -14,20 +14,14 @@
|
||||
|
||||
ARG UBUNTU_VERSION=22.04
|
||||
|
||||
# ── toolchain stage ──────────────────────────────────────────────────────────
|
||||
FROM ubuntu:${UBUNTU_VERSION} AS toolchain
|
||||
|
||||
ENV DEBIAN_FRONTEND=noninteractive
|
||||
|
||||
# jammy's Vulkan headers are 1.3.204, which predates the VkBufferUsageFlags2
|
||||
# constants the renderer uses, and its glslang predates the -gVS the shader
|
||||
# target passes. LunarG publishes current headers, loader and glslang for jammy,
|
||||
# and outranks the distro packages on version.
|
||||
ARG VULKAN_SDK_VERSION=1.4.313
|
||||
|
||||
# The apt lists are kept: conan installs the xorg and egl system packages itself
|
||||
# while resolving SDL.
|
||||
RUN apt-get update && apt-get install -y --no-install-recommends \
|
||||
RUN apt-get update \
|
||||
&& apt-get install -y --no-install-recommends \
|
||||
software-properties-common ca-certificates gnupg wget \
|
||||
&& add-apt-repository -y ppa:ubuntu-toolchain-r/test \
|
||||
&& wget -qO /etc/apt/trusted.gpg.d/lunarg.asc \
|
||||
@@ -39,15 +33,12 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
|
||||
git make ninja-build ccache pkg-config \
|
||||
python3-pip \
|
||||
vulkan-headers libvulkan-dev glslang-tools \
|
||||
libdecor-0-dev
|
||||
libdecor-0-dev libpqxx-dev
|
||||
|
||||
# cmake comes from pip because jammy ships 3.22 and the project asks for 3.26.
|
||||
ARG CMAKE_VERSION=4.4.0
|
||||
ARG CONAN_VERSION=2.31.1
|
||||
RUN pip3 install --no-cache-dir "cmake==${CMAKE_VERSION}" "conan==${CONAN_VERSION}"
|
||||
|
||||
# Some autotools dependencies (flex, by way of SDL) run sub-configures that look
|
||||
# for an unsuffixed gcc/cc and ignore $CC, so 14 has to answer to the plain names.
|
||||
RUN update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-14 100 \
|
||||
--slave /usr/bin/g++ g++ /usr/bin/g++-14 \
|
||||
--slave /usr/bin/gcov gcov /usr/bin/gcov-14 \
|
||||
@@ -59,10 +50,9 @@ ENV CXX=g++
|
||||
|
||||
COPY docker/conan/jammy /etc/conan/jammy
|
||||
|
||||
# ── dependency stage ─────────────────────────────────────────────────────────
|
||||
# Kept apart from the build so that editing sources does not rebuild SDL and
|
||||
# protobuf. The conan cache lives in the image layer rather than a cache mount,
|
||||
# so the generators in /deps can never outlive the packages they point at.
|
||||
|
||||
|
||||
# toolchain installs Conan packages
|
||||
FROM toolchain AS deps
|
||||
|
||||
COPY conanfile.txt /src/conanfile.txt
|
||||
@@ -72,19 +62,14 @@ RUN conan install /src/conanfile.txt \
|
||||
--build=missing \
|
||||
--output-folder=/deps
|
||||
|
||||
# ── build stage ──────────────────────────────────────────────────────────────
|
||||
# The build tree is a cache mount, so an edit rebuilds only what it touched.
|
||||
# That is also why glm, entt and Jolt have to travel in the build context: their
|
||||
# FetchContent SOURCE_DIRs point into external/, while the stamps that record
|
||||
# them as populated live in the cached build tree. Dropping them from the
|
||||
# context would leave the stamps pointing at empty directories.
|
||||
|
||||
|
||||
# Builds the project
|
||||
FROM deps AS builder
|
||||
|
||||
WORKDIR /src
|
||||
COPY . .
|
||||
|
||||
# tw_server is absent on purpose: it needs libpqxx >= 7.7 for pqxx::params and
|
||||
# jammy carries 6.4.
|
||||
RUN --mount=type=cache,target=/build \
|
||||
--mount=type=cache,target=/root/.ccache \
|
||||
cmake -S /src -B /build -G Ninja \
|
||||
@@ -94,54 +79,32 @@ RUN --mount=type=cache,target=/build \
|
||||
-DCMAKE_C_COMPILER_LAUNCHER=ccache \
|
||||
-DCMAKE_CXX_COMPILER_LAUNCHER=ccache \
|
||||
-DCMAKE_EXE_LINKER_FLAGS="-static-libstdc++ -static-libgcc" \
|
||||
&& cmake --build /build --target \
|
||||
tw_client \
|
||||
tw_mock_client \
|
||||
tw_chat_server_exe \
|
||||
tw_chat_mock_client \
|
||||
tw_message_protocol_tests \
|
||||
tw_metrics_tests \
|
||||
tw_network_tests \
|
||||
tw_peer_to_peer_tests \
|
||||
&& cmake --build /build \
|
||||
&& mkdir -p /out/bin /out/shaders \
|
||||
&& cp /build/modules/client/tw_client \
|
||||
/build/modules/mock_client/tw_mock_client \
|
||||
/build/modules/server/tw_server \
|
||||
/build/modules/chat_service/chat_server_exe/tw_chat_server_exe \
|
||||
/build/modules/chat_service/chat_service/tests/chat_mock_client/tw_chat_mock_client \
|
||||
/out/bin/ \
|
||||
&& cp /build/modules/client/shaders/*.spirv /out/shaders/
|
||||
|
||||
# ── unit test stage ──────────────────────────────────────────────────────────
|
||||
# Catch2 suites only. The process harnesses below need peers and a network, so
|
||||
# they run as compose services instead.
|
||||
FROM builder AS test
|
||||
|
||||
RUN --mount=type=cache,target=/build \
|
||||
ctest --test-dir /build --output-on-failure
|
||||
|
||||
# ── client export stage ──────────────────────────────────────────────────────
|
||||
# Not runnable as a container; the client needs a GPU and a display. Extract it:
|
||||
# docker build -f docker/Dockerfile --target export --output type=local,dest=dist .
|
||||
# Extracts client
|
||||
FROM scratch AS export
|
||||
|
||||
COPY --from=builder /out/bin/tw_client /tw_client
|
||||
COPY --from=builder /out/bin/tw_server /tw_server
|
||||
COPY --from=builder /out/bin/tw_mock_client /tw_mock_client
|
||||
COPY --from=builder /out/shaders/ /shaders/
|
||||
|
||||
# ── runtime stages ───────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
# Mock client
|
||||
FROM gcr.io/distroless/base-debian12:nonroot AS mock-client
|
||||
|
||||
COPY --from=builder /out/bin/tw_mock_client /usr/local/bin/
|
||||
ENTRYPOINT ["/usr/local/bin/tw_mock_client"]
|
||||
|
||||
|
||||
FROM gcr.io/distroless/base-debian12:nonroot AS chat-server
|
||||
|
||||
COPY --from=builder /out/bin/tw_chat_server_exe /usr/local/bin/
|
||||
EXPOSE 8101/udp
|
||||
ENTRYPOINT ["/usr/local/bin/tw_chat_server_exe"]
|
||||
|
||||
|
||||
FROM gcr.io/distroless/base-debian12:nonroot AS chat-mock-client
|
||||
|
||||
COPY --from=builder /out/bin/tw_chat_mock_client /usr/local/bin/
|
||||
ENTRYPOINT ["/usr/local/bin/tw_chat_mock_client"]
|
||||
|
||||
@@ -21,7 +21,7 @@ add_dependencies(tw_shaders imgui::plot imgui::plot)
|
||||
|
||||
target_link_libraries(${PROJECT_NAME}
|
||||
PUBLIC
|
||||
towards
|
||||
tw_common
|
||||
tw::network
|
||||
tw::metrics
|
||||
tw::quicr
|
||||
@@ -41,8 +41,6 @@ target_link_libraries(${PROJECT_NAME}
|
||||
Jolt
|
||||
Tracy::TracyClient
|
||||
TracyClient
|
||||
# pqxx
|
||||
# pq
|
||||
)
|
||||
|
||||
target_include_directories(${PROJECT_NAME}
|
||||
|
||||
@@ -2,6 +2,9 @@
|
||||
|
||||
layout(location = 0) out vec4 outColor;
|
||||
|
||||
layout(location = 0) in vec3 inNormal;
|
||||
|
||||
void main() {
|
||||
float diffuse = max(0.1, dot(vec3(0.8, 1.5, -1.0), inNormal));
|
||||
outColor = vec4(1.0, 0.0, 0.0, 1.0);
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
layout(location = 0) in vec3 pos;
|
||||
layout(location = 1) in vec3 norm;
|
||||
layout(location = 0) out vec3 outNormal;
|
||||
|
||||
layout(set = 0, binding = 0) uniform Camera {
|
||||
mat4 proj;
|
||||
@@ -21,5 +22,6 @@ void main() {
|
||||
// outPos = pos.xyz * 0.05;
|
||||
// outNormal = norm.xyz;
|
||||
// outUV = uv;
|
||||
outNormal = norm;
|
||||
gl_Position = cam.proj * cam.view * PushConstants.transform * vec4(pos, 1.0);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
#include "ClientArgs.hpp"
|
||||
|
||||
#include <arpa/inet.h>
|
||||
#include <charconv>
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
@@ -25,12 +24,6 @@ std::string_view trim(std::string_view str) {
|
||||
return str.substr(start, end - start);
|
||||
}
|
||||
|
||||
bool is_valid_ipv4(std::string_view ip_str) {
|
||||
// Use inet_pton to validate IPv4 format
|
||||
struct in_addr addr;
|
||||
return inet_pton(AF_INET, std::string(ip_str).c_str(), &addr) == 1;
|
||||
}
|
||||
|
||||
tl::expected<int, std::string> parse_port(std::string_view port_str) {
|
||||
if(port_str.empty()) {
|
||||
return 8080; // Default port
|
||||
@@ -63,15 +56,27 @@ tl::expected<net::Address, std::string> parse_address(std::string_view text) {
|
||||
return tl::make_unexpected("address cannot be empty");
|
||||
}
|
||||
|
||||
// Find the colon to split host and port
|
||||
size_t colon_pos = text.rfind(':');
|
||||
|
||||
std::string_view host;
|
||||
std::string_view port_str;
|
||||
|
||||
if(colon_pos == std::string_view::npos) {
|
||||
// No colon found: treat entire string as port or host
|
||||
// If it's all digits, treat as port; otherwise as host (will fail validation)
|
||||
// An IPv6 literal carries colons of its own, so the brackets it is written
|
||||
// in are what says where the host ends. This is the form to_string() emits.
|
||||
if(text.front() == '[') {
|
||||
size_t closing = text.find(']');
|
||||
if(closing == std::string_view::npos) {
|
||||
return tl::make_unexpected("address is missing a closing bracket");
|
||||
}
|
||||
|
||||
host = text.substr(1, closing - 1);
|
||||
port_str = text.substr(closing + 1);
|
||||
|
||||
if(!port_str.empty()) {
|
||||
if(port_str.front() != ':') {
|
||||
return tl::make_unexpected("expected a port after the closing bracket");
|
||||
}
|
||||
port_str.remove_prefix(1);
|
||||
}
|
||||
} else if(size_t colon_pos = text.rfind(':'); colon_pos == std::string_view::npos) {
|
||||
bool all_digits = !text.empty() && std::all_of(text.begin(), text.end(),
|
||||
[](unsigned char c) { return std::isdigit(c); });
|
||||
|
||||
@@ -79,7 +84,6 @@ tl::expected<net::Address, std::string> parse_address(std::string_view text) {
|
||||
host = "127.0.0.1";
|
||||
port_str = text;
|
||||
} else {
|
||||
// Treat as host with no port
|
||||
host = text;
|
||||
port_str = "";
|
||||
}
|
||||
@@ -93,10 +97,6 @@ tl::expected<net::Address, std::string> parse_address(std::string_view text) {
|
||||
return tl::make_unexpected("host cannot be empty");
|
||||
}
|
||||
|
||||
if(!is_valid_ipv4(host)) {
|
||||
return tl::make_unexpected("not a valid IPv4 address");
|
||||
}
|
||||
|
||||
// Parse port
|
||||
auto port_result = parse_port(port_str);
|
||||
if(!port_result) {
|
||||
@@ -104,7 +104,13 @@ tl::expected<net::Address, std::string> parse_address(std::string_view text) {
|
||||
}
|
||||
|
||||
int port = port_result.value();
|
||||
return net::Address(std::optional<std::string>(std::string(host)), port);
|
||||
|
||||
auto address_r = net::Address::resolve(std::string(host), port);
|
||||
if(!address_r) {
|
||||
return tl::make_unexpected(address_r.error().message());
|
||||
}
|
||||
|
||||
return address_r.value();
|
||||
}
|
||||
|
||||
std::optional<std::string> server_arg(int argc, char** argv) {
|
||||
|
||||
@@ -12,10 +12,13 @@ namespace tw::app {
|
||||
/**
|
||||
* Parse an address string into a network address.
|
||||
*
|
||||
* Accepts "host:port" or a bare port number. Bare port uses 127.0.0.1.
|
||||
* Missing port defaults to 8080. Trims surrounding whitespace.
|
||||
* Validates the host with inet_pton and returns an error string
|
||||
* for non-IPv4 addresses or invalid ports.
|
||||
* Accepts "host:port" or a bare port number, where the host may be a name as
|
||||
* well as an address literal; an IPv6 literal has to be bracketed, as
|
||||
* "[::1]:8080". Bare port uses 127.0.0.1. Missing port defaults to 8080.
|
||||
* Trims surrounding whitespace.
|
||||
*
|
||||
* Looks the host up, so it blocks for as long as that takes, and returns an
|
||||
* error string for a host that does not resolve or an invalid port.
|
||||
*/
|
||||
tl::expected<net::Address, std::string> parse_address(std::string_view text);
|
||||
|
||||
|
||||
@@ -10,34 +10,5 @@ class tw::dbg::ComponentGui<tw::CharacterController> {
|
||||
public:
|
||||
void draw(tw::CharacterController* instance) {
|
||||
ImGui::SeparatorText("Character Controller Component");
|
||||
|
||||
if(ImGui::BeginTable("history", 3)) {
|
||||
for(auto key : instance->input_history().buffer()) {
|
||||
// ImGui::TableNextRow();
|
||||
|
||||
// ImGui::TableNextColumn();
|
||||
// ImGui::Text("%u", key.first);
|
||||
|
||||
// ImGui::TableNextColumn();
|
||||
// if(instance->input_history().get(key.first).has_value()) {
|
||||
// glm::vec3 input_vec = *instance->input_history().get(key.first).value();
|
||||
// std::string input = std::format("{} {} {}", input_vec.x, input_vec.y, input_vec.z);
|
||||
// ImGui::Text("%s", input.c_str());
|
||||
// } else {
|
||||
// ImGui::Text("None");
|
||||
// }
|
||||
|
||||
// ImGui::TableNextColumn();
|
||||
// if(instance->position_history().get(key).has_value()) {
|
||||
// glm::vec3 position_vec = *instance->position_history().get(key).value();
|
||||
// std::string position = std::format("{} {} {}", position_vec.x, position_vec.y, position_vec.z);
|
||||
// ImGui::Text("%s", position.c_str());
|
||||
// } else {
|
||||
// ImGui::Text("None");
|
||||
// }
|
||||
}
|
||||
|
||||
ImGui::EndTable();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -101,10 +101,54 @@ class tw::dbg::ComponentGui<tw::net::EntityPositionInterpolation> {
|
||||
private:
|
||||
using Clock = std::chrono::high_resolution_clock;
|
||||
|
||||
/** How far back the plot reaches. */
|
||||
static constexpr float WINDOW_IN_SECONDS = 10.0f;
|
||||
|
||||
/** Ten seconds of frames at sixty a second. */
|
||||
static constexpr size_t CAPACITY = 600;
|
||||
|
||||
inline static const net::EntityPositionInterpolation* m_subject = nullptr;
|
||||
|
||||
inline static int m_last_frame = -1;
|
||||
inline static size_t m_head = 0;
|
||||
inline static size_t m_count = 0;
|
||||
|
||||
inline static std::vector<Clock::time_point> m_read_times;
|
||||
inline static std::vector<glm::vec3> m_read_values;
|
||||
|
||||
static int64_t millis_since(Clock::time_point point) {
|
||||
return std::chrono::duration_cast<std::chrono::milliseconds>(Clock::now() - point).count();
|
||||
}
|
||||
|
||||
void restart(const net::EntityPositionInterpolation* instance) {
|
||||
m_subject = instance;
|
||||
m_last_frame = -1;
|
||||
m_head = 0;
|
||||
m_count = 0;
|
||||
|
||||
m_read_times.resize(CAPACITY);
|
||||
m_read_values.resize(CAPACITY);
|
||||
}
|
||||
|
||||
/**
|
||||
* Keeps one reading per frame, so drawing twice does not double up. The
|
||||
* whole value is kept rather than the axis on show, so switching axis
|
||||
* still shows the trace that was already gathered.
|
||||
*/
|
||||
void sample(Clock::time_point time, glm::vec3 value) {
|
||||
const int frame = ImGui::GetFrameCount();
|
||||
if(m_last_frame == frame) {
|
||||
return;
|
||||
}
|
||||
m_last_frame = frame;
|
||||
|
||||
m_read_times[m_head] = time;
|
||||
m_read_values[m_head] = value;
|
||||
|
||||
m_head = (m_head + 1) % CAPACITY;
|
||||
m_count = std::min(m_count + 1, CAPACITY);
|
||||
}
|
||||
|
||||
public:
|
||||
void draw(net::EntityPositionInterpolation* instance) {
|
||||
ImGui::SeparatorText("Received Positions");
|
||||
@@ -122,5 +166,73 @@ public:
|
||||
instance->values()[0].y,
|
||||
instance->values()[0].z,
|
||||
millis_since(instance->times()[0]));
|
||||
|
||||
static int axis = 0;
|
||||
ImGui::Combo("Axis##received", &axis, "X\0Y\0Z\0");
|
||||
|
||||
/** How far behind the present the reader looks; matches the caller. */
|
||||
static int delay_in_millis = 1000;
|
||||
ImGui::SliderInt("Delay (ms)", &delay_in_millis, 0, 2000);
|
||||
|
||||
if(m_subject != instance) {
|
||||
restart(instance);
|
||||
}
|
||||
|
||||
// The buffer wraps, so the index says nothing about when a sample
|
||||
// arrived. Placing each one at its own age puts it where it belongs
|
||||
// without having to know where the ring currently starts. Slots
|
||||
// nothing was pushed into are left out.
|
||||
const auto now = Clock::now();
|
||||
const size_t count = instance->size();
|
||||
|
||||
std::vector<float> ages(count);
|
||||
std::vector<float> positions(count);
|
||||
|
||||
for(size_t i = 0; i < count; i++) {
|
||||
ages[i] = -std::chrono::duration<float>(now - instance->times()[i]).count();
|
||||
positions[i] = instance->values()[i][axis];
|
||||
}
|
||||
|
||||
if(ImPlot::BeginPlot("Buffer", ImVec2(-1.0f, 180.0f))) {
|
||||
ImPlot::SetupAxes("seconds ago", "position", ImPlotAxisFlags_None, ImPlotAxisFlags_AutoFit);
|
||||
ImPlot::SetupAxisLimits(ImAxis_X1,
|
||||
-WINDOW_IN_SECONDS, 0.0f, ImGuiCond_Always);
|
||||
|
||||
ImPlot::PlotScatter("Received", ages.data(), positions.data(), (int)count);
|
||||
|
||||
// Reading the same way the position is read for drawing shows
|
||||
// which two samples the delay lands between, and how far the
|
||||
// result sits from either of them.
|
||||
const auto point = now - std::chrono::milliseconds(delay_in_millis);
|
||||
const auto [from, to, alpha] = instance->get_values_around(point);
|
||||
|
||||
const float read_at = -std::chrono::duration<float>(now - point).count();
|
||||
const float read_out = glm::mix(from, to, alpha)[axis];
|
||||
|
||||
ImPlot::SetNextMarkerStyle(ImPlotMarker_Circle, 5.0f);
|
||||
ImPlot::PlotScatter("Interpolated", &read_at, &read_out, 1);
|
||||
|
||||
// Each reading is kept at the moment it was read for, so the trace
|
||||
// ends on the marker above and runs back along the same timeline
|
||||
// the received samples sit on.
|
||||
sample(point, glm::mix(from, to, alpha));
|
||||
|
||||
std::vector<float> trace_ages(m_count);
|
||||
std::vector<float> trace_positions(m_count);
|
||||
|
||||
const size_t oldest = m_count == CAPACITY ? m_head : 0;
|
||||
|
||||
for(size_t i = 0; i < m_count; i++) {
|
||||
const size_t index = (oldest + i) % CAPACITY;
|
||||
|
||||
trace_ages[i] = -std::chrono::duration<float>(now - m_read_times[index]).count();
|
||||
trace_positions[i] = m_read_values[index][axis];
|
||||
}
|
||||
|
||||
ImPlot::PlotLine("Interpolated Trace",
|
||||
trace_ages.data(), trace_positions.data(), (int)m_count);
|
||||
|
||||
ImPlot::EndPlot();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -25,7 +25,7 @@ void EntityPositionInterpolator::set_position(Clock::time_point time_point, entt
|
||||
return;
|
||||
}
|
||||
|
||||
interpolation->push(Clock::now(), position);
|
||||
interpolation->push(time_point, position);
|
||||
}
|
||||
|
||||
glm::vec3 EntityPositionInterpolator::get_position(Clock::time_point time_point, entt::entity entity) {
|
||||
@@ -35,9 +35,7 @@ glm::vec3 EntityPositionInterpolator::get_position(Clock::time_point time_point,
|
||||
return glm::vec3();
|
||||
}
|
||||
|
||||
auto now = Clock::now() - std::chrono::milliseconds(m_bufferingIntervalInMillis);
|
||||
|
||||
auto [from, to, value] = interpolation->get_values_around(now);
|
||||
auto [from, to, value] = interpolation->get_values_around(time_point);
|
||||
return glm::mix(from, to, value);
|
||||
}
|
||||
|
||||
|
||||
@@ -5,15 +5,21 @@
|
||||
|
||||
#include "common.hpp"
|
||||
|
||||
template<typename T, int length = 3>
|
||||
template<typename T, int length = 10>
|
||||
class InterpolatedProperty {
|
||||
using Clock = std::chrono::high_resolution_clock;
|
||||
|
||||
std::array<T, length> m_buffer;
|
||||
std::array<Clock::time_point, length> m_time_buffer;
|
||||
std::vector<T> m_buffer;
|
||||
std::vector<Clock::time_point> m_time_buffer;
|
||||
|
||||
uint32_t m_head;
|
||||
uint32_t m_size;
|
||||
|
||||
public:
|
||||
InterpolatedProperty(T initial_value) {
|
||||
InterpolatedProperty(T initial_value) :
|
||||
m_buffer(length),
|
||||
m_time_buffer(length),
|
||||
m_size(0), m_head(0) {
|
||||
for(int i = 0; i < length; i++) {
|
||||
m_buffer[i] = initial_value;
|
||||
m_time_buffer[i] = Clock::now();
|
||||
@@ -23,7 +29,21 @@ public:
|
||||
GET_REF(m_buffer, values);
|
||||
GET_REF(m_time_buffer, times);
|
||||
|
||||
/** How many slots hold something that was pushed. */
|
||||
inline uint32_t size() const {
|
||||
return m_size;
|
||||
}
|
||||
|
||||
void push(Clock::time_point point, T value) {
|
||||
// m_buffer[m_head] = std::move(value);
|
||||
// m_time_buffer[m_head] = point;
|
||||
//
|
||||
// m_head = (m_head + 1) % m_buffer.size();
|
||||
//
|
||||
// if (m_size < m_buffer.size())
|
||||
// {
|
||||
// ++m_size;
|
||||
// }
|
||||
if(point < m_time_buffer.at(0)) {
|
||||
return;
|
||||
}
|
||||
@@ -64,6 +84,46 @@ public:
|
||||
}
|
||||
|
||||
inline std::tuple<T, T, float> get_values_around(Clock::time_point point) const {
|
||||
/* const std::size_t capacity = m_buffer.size();
|
||||
const std::size_t oldest = (m_head + capacity - m_size) % capacity;
|
||||
|
||||
if (m_size == 0)
|
||||
{
|
||||
return { T(), T(), 0.0f };
|
||||
}
|
||||
|
||||
// Asking for a time the buffer no longer reaches back to. Standing on
|
||||
// the oldest is wrong by however much was missed, but it is wrong in
|
||||
// the direction the values were heading, unlike the newest.
|
||||
if (point < m_time_buffer[oldest])
|
||||
{
|
||||
return { m_buffer[oldest], m_buffer[oldest], 0.0f };
|
||||
}
|
||||
|
||||
for (std::size_t i = 0; i + 1 < m_size; ++i)
|
||||
{
|
||||
const std::size_t idx0 = (oldest + i) % capacity;
|
||||
const std::size_t idx1 = (oldest + i + 1) % capacity;
|
||||
|
||||
const auto t0 = m_time_buffer[idx0];
|
||||
const auto t1 = m_time_buffer[idx1];
|
||||
|
||||
if (point >= t0 && point <= t1)
|
||||
{
|
||||
const auto total =
|
||||
std::chrono::duration<float>(t1 - t0).count();
|
||||
const auto elapsed =
|
||||
std::chrono::duration<float>(point - t0).count();
|
||||
|
||||
const float alpha = total > 0.0f ? elapsed / total : 0.0f;
|
||||
|
||||
return { m_buffer[idx0], m_buffer[idx1], alpha };
|
||||
}
|
||||
}
|
||||
|
||||
// Handle exact last sample (or clamp)
|
||||
const std::size_t last = (oldest + m_size - 1) % capacity;
|
||||
return { m_buffer[last], m_buffer[last], 0.0f }; */
|
||||
T prev = m_buffer.at(0);
|
||||
Clock::time_point prev_point = m_time_buffer.at(0);
|
||||
|
||||
|
||||
@@ -151,6 +151,10 @@ void ReplicatorClient::handle_snapshot_entity(
|
||||
// return;
|
||||
}
|
||||
|
||||
// Applies correction from record_frame_idx to current_frame_idx (if any) on entity. Compares previous position on
|
||||
// frame record_frame_idx with p
|
||||
m_rollback.apply_correction(record_frame_idx, p, entity.value(), &m_world->registry(), current_frame_idx);
|
||||
|
||||
//snap_player_to(current_frame_idx, entity.value(), p);
|
||||
|
||||
// bool reconcile_happened = m_reconciler.reconcile(record_frame_idx, p, entity.value(), &m_world->registry(), current_frame_idx);
|
||||
@@ -168,15 +172,29 @@ void ReplicatorClient::handle_snapshot_entity(
|
||||
// }
|
||||
// m_network_metrics->record_ack_lag(ack_lag);
|
||||
// }
|
||||
}
|
||||
|
||||
m_entity_interpolator->set_position(std::chrono::high_resolution_clock::now(), entity.value(), p);
|
||||
} else {
|
||||
m_entity_interpolator->set_position(std::chrono::high_resolution_clock::now(), entity.value(), p);
|
||||
}
|
||||
}
|
||||
|
||||
void ReplicatorClient::handle_snapshot(uint32_t current_frame_idx, serial::WorldStateReader& reader) {
|
||||
auto header = reader.read_header();
|
||||
measure_response_time(current_frame_idx, header.frame_idx);
|
||||
|
||||
// Snapshots are sent unreliably, so one can overtake another. Taking a
|
||||
// late one would put entities back where they have already been seen.
|
||||
if(header.tick_idx <= m_latest_tick_idx) {
|
||||
return;
|
||||
}
|
||||
|
||||
m_latest_tick_idx = header.tick_idx;
|
||||
|
||||
// Arrival is the only time the client can be sure of, and it grows with
|
||||
// every snapshot, which is what reading the buffer back relies on. What it
|
||||
// costs is spacing: the gaps between samples carry the jitter of the way
|
||||
// here rather than the even gaps the server sent them on.
|
||||
const auto taken_at = std::chrono::high_resolution_clock::now();
|
||||
|
||||
while(reader.has_entity()) {
|
||||
auto entity_r = reader.read_entity();
|
||||
|
||||
|
||||
@@ -86,6 +86,7 @@ private:
|
||||
net::PlayerRollback m_rollback;
|
||||
World *m_world;
|
||||
net::ServerConnection* m_server_connection;
|
||||
uint32_t m_latest_tick_idx = 0;
|
||||
|
||||
std::optional<entt::entity> m_player_entity;
|
||||
|
||||
|
||||
@@ -50,7 +50,12 @@ void ThirdPersonPlayerController::update(const tw::io::InputManager* input, doub
|
||||
m_camera_rotation = yaw * m_camera_rotation * pitch;
|
||||
|
||||
glm::vec3 offset = m_camera_rotation * glm::vec3(0, 0, m_camera_zoom);
|
||||
m_camera->view().look_at(get_target_position() + offset, get_target_position());
|
||||
|
||||
glm::vec3 from = m_camera->view().position();
|
||||
glm::vec3 to = get_target_position() + offset;
|
||||
glm::vec3 interpolated = glm::mix(from, to, 0.8f);
|
||||
|
||||
m_camera->view().look_at(interpolated, get_target_position());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -25,8 +25,6 @@
|
||||
|
||||
namespace tw {
|
||||
|
||||
typedef HistoryBuffer<long, glm::vec3> EntityPositionHistory;
|
||||
|
||||
entt::entity
|
||||
ClientWorldController::create_entity(const std::string& name, glm::vec3 position) {
|
||||
const auto entity = m_world->registry().create();
|
||||
@@ -82,8 +80,8 @@ void ClientWorldController::spawn_entity(const std::string& name, uint32_t serve
|
||||
if(m_controlled_server_id.has_value() && m_controlled_server_id.value() == server_id) {
|
||||
try_bind_player_entity();
|
||||
} else {
|
||||
m_interpolator.register_entity(entity);
|
||||
}
|
||||
m_interpolator.register_entity(entity);
|
||||
}
|
||||
|
||||
ClientWorldController::ClientWorldController(
|
||||
@@ -165,9 +163,9 @@ void ClientWorldController::try_bind_player_entity() {
|
||||
position
|
||||
));
|
||||
|
||||
// if(m_world->registry().all_of<net::EntityPositionInterpolation>(entity)) {
|
||||
// m_world->registry().remove<net::EntityPositionInterpolation>(entity);
|
||||
// }
|
||||
if(m_world->registry().all_of<net::EntityPositionInterpolation>(entity)) {
|
||||
m_world->registry().remove<net::EntityPositionInterpolation>(entity);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -187,7 +185,6 @@ void ClientWorldController::update_network() {
|
||||
});
|
||||
|
||||
|
||||
m_replicator_client.record_prediction(m_frame_idx);
|
||||
|
||||
|
||||
// m_world->registry().view<Transform>()
|
||||
@@ -216,10 +213,11 @@ void ClientWorldController::update(double delta_time) {
|
||||
if(controller) {
|
||||
// controller->set_input(m_network_frame_idx, input);
|
||||
m_replicator_client.set_input(m_network_frame_idx, input);
|
||||
m_replicator_client.record_prediction(m_network_frame_idx);
|
||||
}
|
||||
}
|
||||
|
||||
// m_physics_world->step(m_network_frame_idx, JoltPhysicsWorld::FIXED_DELTA_TIME, true);
|
||||
m_physics_world->step(m_network_frame_idx, JoltPhysicsWorld::FIXED_DELTA_TIME, true);
|
||||
m_network_frame_idx++;
|
||||
}
|
||||
|
||||
@@ -230,7 +228,7 @@ void ClientWorldController::update(double delta_time) {
|
||||
// TODO: The third person controller could pull
|
||||
Transform* player_transform = m_world->registry().try_get<Transform>(m_player_entity.value());
|
||||
if(player_transform) {
|
||||
m_player_controller.set_target(player_transform->position());
|
||||
// m_player_controller.set_target(player_transform->position());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
#pragma once
|
||||
|
||||
#include <cerrno>
|
||||
#include <cstring>
|
||||
#include <netdb.h>
|
||||
#include <string>
|
||||
|
||||
namespace tw::net {
|
||||
|
||||
/**
|
||||
* A failed host lookup.
|
||||
*
|
||||
* Kept apart from NetworkError because getaddrinfo reports EAI_ codes, which
|
||||
* are their own mostly-negative space: sharing one enum would map a lookup
|
||||
* failure onto whichever errno happened to carry the same number.
|
||||
*/
|
||||
struct ResolutionError {
|
||||
int m_code;
|
||||
int m_errno;
|
||||
|
||||
public:
|
||||
/**
|
||||
* Only EAI_SYSTEM defers to errno, and errno will not have survived by the
|
||||
* time message() runs, so it is captured here.
|
||||
*/
|
||||
static ResolutionError from_gai(int code) {
|
||||
return { code, errno };
|
||||
}
|
||||
|
||||
std::string message() const {
|
||||
switch (m_code) {
|
||||
case EAI_NONAME:
|
||||
return "The host name is not known.";
|
||||
case EAI_AGAIN:
|
||||
return "The name server is unreachable or busy; the lookup may succeed later.";
|
||||
case EAI_FAIL:
|
||||
return "The name server returned a permanent failure.";
|
||||
case EAI_FAMILY:
|
||||
return "The requested address family is not supported.";
|
||||
case EAI_SERVICE:
|
||||
return "The requested port is not available for this socket type.";
|
||||
case EAI_MEMORY:
|
||||
return "Insufficient memory was available to complete the lookup.";
|
||||
case EAI_SYSTEM:
|
||||
return std::string(strerror(m_errno));
|
||||
default:
|
||||
return std::string(gai_strerror(m_code));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
#pragma once
|
||||
|
||||
#include "ResolutionError.hpp"
|
||||
#include "tl/expected.hpp"
|
||||
|
||||
#include <cstring>
|
||||
#include <netdb.h>
|
||||
#include <string>
|
||||
#include <sys/socket.h>
|
||||
|
||||
namespace tw::net {
|
||||
|
||||
/**
|
||||
* Turns a host name or an address literal into a socket address.
|
||||
*
|
||||
* `family` is the family of the socket the result will be given to. AF_INET6
|
||||
* asks for IPv4-only names as ::ffff: mapped addresses, so that one dual-stack
|
||||
* socket reaches both; AF_UNSPEC takes the name as it comes and suits addresses
|
||||
* that are only being validated, displayed or stored.
|
||||
*
|
||||
* Blocks for the length of a DNS round trip when the name is not already known,
|
||||
* so it belongs at connect time rather than anywhere periodic.
|
||||
*/
|
||||
inline tl::expected<sockaddr_storage, ResolutionError>
|
||||
resolve_host(const std::string& host, int port, sa_family_t family = AF_UNSPEC) {
|
||||
addrinfo hints {};
|
||||
hints.ai_family = family;
|
||||
hints.ai_socktype = SOCK_DGRAM;
|
||||
|
||||
// AI_ADDRCONFIG is deliberately absent. Together with AF_INET6 it discards
|
||||
// every result on a host that carries no global IPv6 address, which is the
|
||||
// default state of a container on a bridge network.
|
||||
if(family == AF_INET6) {
|
||||
hints.ai_flags = AI_V4MAPPED | AI_ALL;
|
||||
}
|
||||
|
||||
// Passing the port as the service spares us setting sin_port or sin6_port
|
||||
// by hand once the family of the answer is known.
|
||||
const std::string service = std::to_string(port);
|
||||
|
||||
addrinfo* results = nullptr;
|
||||
const int rc = ::getaddrinfo(host.c_str(), service.c_str(), &hints, &results);
|
||||
if(rc != 0) {
|
||||
return tl::make_unexpected(ResolutionError::from_gai(rc));
|
||||
}
|
||||
|
||||
// The list arrives ordered by RFC 6724, so the head is the address the
|
||||
// system would have picked for itself.
|
||||
sockaddr_storage storage {};
|
||||
std::memcpy(&storage, results->ai_addr, results->ai_addrlen);
|
||||
|
||||
::freeaddrinfo(results);
|
||||
|
||||
return storage;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -70,7 +70,13 @@ MessageConnection* MessageEndpoint::add_peer(net::quicr::QuicrConnection* connec
|
||||
}
|
||||
|
||||
tl::expected<MessageConnection*, MessageError> MessageEndpoint::connect(const std::string& host, int port) {
|
||||
auto connection_r = m_endpoint->connect(net::quicr::QuicrAddress(host, port));
|
||||
auto address_r = net::quicr::QuicrAddress::resolve(host, port, m_endpoint->family());
|
||||
if(!address_r) {
|
||||
return tl::make_unexpected(
|
||||
MessageError(MessageErrorType::ConnectFailed, address_r.error().message()));
|
||||
}
|
||||
|
||||
auto connection_r = m_endpoint->connect(address_r.value());
|
||||
if(!connection_r) {
|
||||
return tl::make_unexpected(
|
||||
MessageError(MessageErrorType::ConnectFailed, connection_r.error().message()));
|
||||
|
||||
@@ -8,7 +8,7 @@ add_executable(${PROJECT_NAME} ${FILES})
|
||||
|
||||
target_link_libraries(${PROJECT_NAME}
|
||||
PUBLIC
|
||||
towards
|
||||
tw_common
|
||||
tw::network
|
||||
tw::quicr
|
||||
tw::protocol
|
||||
|
||||
@@ -112,9 +112,25 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
const int NUM_CLIENTS = 10;
|
||||
tw::net::Address address = {"127.0.0.1", 8101};
|
||||
int main(int argc, char** argv) {
|
||||
if(argc > 4) {
|
||||
spdlog::error("Usage: {} [client_count] [host] [port]", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
const uint32_t NUM_CLIENTS = argc > 1 ? std::strtoul(argv[1], nullptr, 10) : 300;
|
||||
const std::string host = argc > 2 ? argv[2] : "127.0.0.1";
|
||||
const int port = argc > 3 ? std::atoi(argv[3]) : 8101;
|
||||
|
||||
auto address_r = tw::net::Address::resolve(host, port);
|
||||
if(!address_r) {
|
||||
spdlog::error("Failed to resolve {}:{}: {}", host, port, address_r.error().message());
|
||||
return 1;
|
||||
}
|
||||
|
||||
const tw::net::Address address = address_r.value();
|
||||
|
||||
spdlog::info("Starting {} clients against {}", NUM_CLIENTS, address.to_string());
|
||||
|
||||
std::vector<std::thread> threads;
|
||||
for(uint32_t i = 0; i < NUM_CLIENTS; i++) {
|
||||
@@ -129,7 +145,7 @@ int main() {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
||||
}
|
||||
|
||||
for(uint32_t i = 0; i < NUM_CLIENTS; i++) {
|
||||
for(uint32_t i = 0; i < threads.size(); i++) {
|
||||
threads[i].join();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "io/HostResolver.hpp"
|
||||
|
||||
#include <arpa/inet.h>
|
||||
#include <spdlog/spdlog.h>
|
||||
#include <string>
|
||||
@@ -7,6 +9,7 @@
|
||||
#include <optional>
|
||||
#include <sys/socket.h>
|
||||
#include <format>
|
||||
#include <tl/expected.hpp>
|
||||
|
||||
namespace tw::net {
|
||||
|
||||
@@ -38,6 +41,24 @@ public:
|
||||
: m_storage(storage)
|
||||
{ }
|
||||
|
||||
/**
|
||||
* Look a host up, accepting a name where the constructor above takes only
|
||||
* an address literal.
|
||||
*
|
||||
* `family` should be the family of the socket the address will be used
|
||||
* with. The default suits an address that is only being validated or
|
||||
* displayed, and takes whatever the name resolves to.
|
||||
*/
|
||||
static tl::expected<Address, ResolutionError>
|
||||
resolve(const std::string& host, int port, sa_family_t family = AF_UNSPEC) {
|
||||
auto storage_r = resolve_host(host, port, family);
|
||||
if(!storage_r) {
|
||||
return tl::make_unexpected(storage_r.error());
|
||||
}
|
||||
|
||||
return Address(std::move(storage_r.value()));
|
||||
}
|
||||
|
||||
/** Return a const pointer suitable for connect / sendto / bind. */
|
||||
const struct sockaddr* sockaddr() const {
|
||||
return reinterpret_cast<const struct sockaddr*>(&m_storage);
|
||||
|
||||
@@ -22,7 +22,7 @@ target_include_directories(tw_peer_to_peer_lib
|
||||
|
||||
target_link_libraries(tw_peer_to_peer_lib
|
||||
PUBLIC
|
||||
towards
|
||||
tw_common
|
||||
tw::network
|
||||
tw::quicr
|
||||
tw::protocol
|
||||
@@ -32,8 +32,6 @@ target_link_libraries(tw_peer_to_peer_lib
|
||||
spdlog::spdlog
|
||||
)
|
||||
|
||||
# ── standalone executable ─────────────────────────────────────────────────────
|
||||
|
||||
add_executable(${PROJECT_NAME} src/PeerToPeer.cpp)
|
||||
|
||||
target_link_libraries(${PROJECT_NAME}
|
||||
@@ -41,6 +39,4 @@ target_link_libraries(${PROJECT_NAME}
|
||||
tw_peer_to_peer_lib
|
||||
)
|
||||
|
||||
# ── tests ─────────────────────────────────────────────────────────────────────
|
||||
|
||||
add_subdirectory(tests)
|
||||
|
||||
@@ -15,7 +15,18 @@ QuicrPeerLink::QuicrPeerLink(uint32_t self_id, uint16_t port)
|
||||
{}
|
||||
|
||||
void QuicrPeerLink::connect_to(uint32_t peer_id, const tw::net::Address& addr) {
|
||||
auto r = m_endpoint->connect(net::quicr::QuicrAddress(addr.ip_string(), addr.port()));
|
||||
// The address is rebuilt from its text, which for a mapped or IPv6 peer is
|
||||
// more than the literal constructor can parse, so it goes back through the
|
||||
// resolver — into the endpoint's family, since that is what will send it.
|
||||
auto address_r = net::quicr::QuicrAddress::resolve(addr.ip_string(), addr.port(),
|
||||
m_endpoint->family());
|
||||
if (!address_r) {
|
||||
spdlog::warn("QuicrPeerLink[{}]: address of peer {} failed to resolve: {}",
|
||||
m_self_id, peer_id, address_r.error().message());
|
||||
return;
|
||||
}
|
||||
|
||||
auto r = m_endpoint->connect(address_r.value());
|
||||
if (!r) {
|
||||
spdlog::warn("QuicrPeerLink[{}]: connect to peer {} failed", m_self_id, peer_id);
|
||||
return;
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "io/HostResolver.hpp"
|
||||
|
||||
#include <arpa/inet.h>
|
||||
#include <spdlog/spdlog.h>
|
||||
#include <string>
|
||||
@@ -7,6 +9,7 @@
|
||||
#include <optional>
|
||||
#include <sys/socket.h>
|
||||
#include <format>
|
||||
#include <tl/expected.hpp>
|
||||
|
||||
namespace tw::net::quicr {
|
||||
|
||||
@@ -38,6 +41,24 @@ public:
|
||||
: m_storage(storage)
|
||||
{ }
|
||||
|
||||
/**
|
||||
* Look a host up, accepting a name where the constructor above takes only
|
||||
* an address literal.
|
||||
*
|
||||
* `family` should be the family of the endpoint socket the address will be
|
||||
* sent from, so that a dual-stack socket is handed a mapped address rather
|
||||
* than a bare IPv4 one.
|
||||
*/
|
||||
static tl::expected<QuicrAddress, ResolutionError>
|
||||
resolve(const std::string& host, int port, sa_family_t family = AF_UNSPEC) {
|
||||
auto storage_r = resolve_host(host, port, family);
|
||||
if(!storage_r) {
|
||||
return tl::make_unexpected(storage_r.error());
|
||||
}
|
||||
|
||||
return QuicrAddress(std::move(storage_r.value()));
|
||||
}
|
||||
|
||||
/** Return a const pointer suitable for connect / sendto / bind. */
|
||||
const struct sockaddr* sockaddr() const {
|
||||
return reinterpret_cast<const struct sockaddr*>(&m_storage);
|
||||
|
||||
@@ -18,6 +18,7 @@ class QuicrConnectionListener;
|
||||
|
||||
class QuicrEndpoint {
|
||||
int32_t m_socket_fd;
|
||||
sa_family_t m_family;
|
||||
std::unordered_map<uint64_t, std::shared_ptr<QuicrConnection>> m_connections;
|
||||
|
||||
std::vector<std::byte> m_inbound_buffer;
|
||||
@@ -26,7 +27,7 @@ class QuicrEndpoint {
|
||||
|
||||
void process_datagram(std::span<std::byte> datagram, QuicrAddress from);
|
||||
|
||||
QuicrEndpoint(int socket_fd);
|
||||
QuicrEndpoint(int socket_fd, sa_family_t family);
|
||||
|
||||
public:
|
||||
QuicrEndpoint(const QuicrEndpoint&) = delete;
|
||||
@@ -47,6 +48,12 @@ public:
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* The family the socket was opened with. Addresses have to be resolved
|
||||
* into it before they can be sent to.
|
||||
*/
|
||||
sa_family_t family() const { return m_family; }
|
||||
|
||||
static tl::expected<std::unique_ptr<QuicrEndpoint>, QuicrError> create();
|
||||
|
||||
/**
|
||||
|
||||
@@ -10,8 +10,8 @@
|
||||
|
||||
namespace tw::net::quicr {
|
||||
|
||||
QuicrEndpoint::QuicrEndpoint(int socket_fd)
|
||||
: m_inbound_buffer(64 * 1024), m_socket_fd(socket_fd),
|
||||
QuicrEndpoint::QuicrEndpoint(int socket_fd, sa_family_t family)
|
||||
: m_inbound_buffer(64 * 1024), m_socket_fd(socket_fd), m_family(family),
|
||||
m_new_connection_handler(nullptr) {
|
||||
|
||||
}
|
||||
@@ -31,29 +31,57 @@ tl::expected<std::unique_ptr<QuicrEndpoint>, QuicrError> QuicrEndpoint::create_a
|
||||
}
|
||||
|
||||
tl::expected<std::unique_ptr<QuicrEndpoint>, QuicrError> QuicrEndpoint::create() {
|
||||
const int domain = AF_INET;
|
||||
// An IPv6 socket with IPV6_V6ONLY cleared also carries IPv4 peers, which
|
||||
// arrive as ::ffff: mapped addresses. A host with IPv6 switched off answers
|
||||
// EAFNOSUPPORT instead, and there the endpoint stays IPv4 as it was.
|
||||
sa_family_t domain = AF_INET6;
|
||||
int socket_fd = socket(domain, SOCK_DGRAM, IPPROTO_UDP);
|
||||
if(socket_fd < 0 && errno == EAFNOSUPPORT) {
|
||||
domain = AF_INET;
|
||||
socket_fd = socket(domain, SOCK_DGRAM, IPPROTO_UDP);
|
||||
}
|
||||
|
||||
if(socket_fd < 0) {
|
||||
spdlog::error("Failed to create socket: {}", strerror(errno));
|
||||
return tl::make_unexpected(QuicrError::from_errno(errno));
|
||||
}
|
||||
|
||||
if(domain == AF_INET6) {
|
||||
const int v6_only = 0;
|
||||
if(setsockopt(socket_fd, IPPROTO_IPV6, IPV6_V6ONLY, &v6_only, sizeof(v6_only)) < 0) {
|
||||
spdlog::error("Failed to accept IPv4 peers on the socket: {}", strerror(errno));
|
||||
::close(socket_fd);
|
||||
return tl::make_unexpected(QuicrError::from_errno(errno));
|
||||
}
|
||||
}
|
||||
|
||||
if(fcntl(socket_fd, F_SETFL, fcntl(socket_fd, F_GETFL, 0) | O_NONBLOCK, 1) == -1) {
|
||||
spdlog::error("Failed to set non-blocking mode: {}", strerror(errno));
|
||||
return tl::make_unexpected(QuicrError::from_errno(errno));
|
||||
}
|
||||
|
||||
return std::unique_ptr<QuicrEndpoint>(new QuicrEndpoint(socket_fd));
|
||||
return std::unique_ptr<QuicrEndpoint>(new QuicrEndpoint(socket_fd, domain));
|
||||
}
|
||||
|
||||
tl::expected<void, QuicrError> QuicrEndpoint::bind(int port) {
|
||||
const int domain = AF_INET;
|
||||
struct sockaddr_in addr = {};
|
||||
addr.sin_family = domain;
|
||||
addr.sin_port = htons(port);
|
||||
addr.sin_addr.s_addr = INADDR_ANY;
|
||||
sockaddr_storage storage = {};
|
||||
socklen_t length;
|
||||
|
||||
if(::bind(m_socket_fd, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
|
||||
if(m_family == AF_INET6) {
|
||||
auto& addr = reinterpret_cast<sockaddr_in6&>(storage);
|
||||
addr.sin6_family = AF_INET6;
|
||||
addr.sin6_port = htons(port);
|
||||
addr.sin6_addr = in6addr_any;
|
||||
length = sizeof(sockaddr_in6);
|
||||
} else {
|
||||
auto& addr = reinterpret_cast<sockaddr_in&>(storage);
|
||||
addr.sin_family = AF_INET;
|
||||
addr.sin_port = htons(port);
|
||||
addr.sin_addr.s_addr = INADDR_ANY;
|
||||
length = sizeof(sockaddr_in);
|
||||
}
|
||||
|
||||
if(::bind(m_socket_fd, reinterpret_cast<struct sockaddr*>(&storage), length) < 0) {
|
||||
spdlog::error("Failed to bind socket: {}", strerror(errno));
|
||||
return tl::make_unexpected(QuicrError::from_errno(errno));
|
||||
}
|
||||
|
||||
@@ -20,8 +20,6 @@ target_link_libraries(tw_serialization
|
||||
EnTT::EnTT
|
||||
)
|
||||
|
||||
# Require C++20 for concepts / span
|
||||
target_compile_features(tw_serialization INTERFACE cxx_std_20)
|
||||
|
||||
# ── Tests / benchmarks ───────────────────────────────────────────────────────
|
||||
add_subdirectory(tests)
|
||||
# add_subdirectory(tests)
|
||||
|
||||
@@ -23,7 +23,7 @@ class WorldStateWriter {
|
||||
public:
|
||||
explicit WorldStateWriter(BinaryBuffer& buf) noexcept : m_w(buf) {}
|
||||
|
||||
void begin(uint32_t frame_idx, uint32_t message_type) noexcept {
|
||||
void begin(uint32_t frame_idx, uint32_t tick_idx, uint32_t message_type) noexcept {
|
||||
m_entity_count = 0;
|
||||
|
||||
// message_type — lets the receiver dispatch without peeking further
|
||||
@@ -35,6 +35,10 @@ public:
|
||||
// frame_idx
|
||||
m_w.encode<uint32_t>(frame_idx);
|
||||
|
||||
// tick_idx — counts the states that went out, so the receiver can tell
|
||||
// a late one from a new one. Says nothing about the frame answered.
|
||||
m_w.encode<uint32_t>(tick_idx);
|
||||
|
||||
// entity_count placeholder — patched when end() is called
|
||||
m_entity_count_offset = m_w.reserve_u32();
|
||||
}
|
||||
@@ -85,6 +89,7 @@ public:
|
||||
struct WorldStateHeader {
|
||||
uint32_t packet_type;
|
||||
uint32_t frame_idx;
|
||||
uint32_t tick_idx;
|
||||
uint32_t entity_count;
|
||||
};
|
||||
|
||||
@@ -109,10 +114,11 @@ public:
|
||||
explicit WorldStateReader(std::span<const std::byte> data) noexcept
|
||||
: m_r(data) {}
|
||||
|
||||
/** Read the 12-byte header. Must be called first. */
|
||||
/** Read the header. Must be called first. */
|
||||
WorldStateHeader read_header() noexcept {
|
||||
// m_header.packet_type = m_r.decode<uint32_t>();
|
||||
m_header.frame_idx = m_r.decode<uint32_t>();
|
||||
m_header.tick_idx = m_r.decode<uint32_t>();
|
||||
m_header.entity_count = m_r.decode<uint32_t>();
|
||||
|
||||
// spawn count follows immediately
|
||||
|
||||
@@ -17,7 +17,7 @@ file(GLOB FILES
|
||||
# )
|
||||
|
||||
# add_executable(${PROJECT_NAME})
|
||||
add_executable(SerializationBenchmarks ./SerializationBenchmarks.cpp)
|
||||
# add_executable(SerializationBenchmarks ./SerializationBenchmarks.cpp)
|
||||
|
||||
target_compile_definitions(SerializationBenchmarks PRIVATE TRACY_ON_DEMAND=1)
|
||||
|
||||
|
||||
@@ -14,6 +14,7 @@ file(GLOB FILES
|
||||
src/network/*.cpp
|
||||
)
|
||||
|
||||
find_package(libpqxx CONFIG REQUIRED)
|
||||
add_library(tw_server_lib STATIC ${FILES})
|
||||
|
||||
target_include_directories(tw_server_lib
|
||||
@@ -23,7 +24,7 @@ target_include_directories(tw_server_lib
|
||||
|
||||
target_link_libraries(tw_server_lib
|
||||
PUBLIC
|
||||
towards
|
||||
tw_common
|
||||
tw::io
|
||||
tw::network
|
||||
tw::protocol
|
||||
@@ -35,10 +36,8 @@ target_link_libraries(tw_server_lib
|
||||
Jolt
|
||||
protobuf::libprotobuf
|
||||
${Boost_LIBRARIES}
|
||||
${LIBDECOR_LIB}
|
||||
Tracy::TracyClient
|
||||
pqxx
|
||||
pq
|
||||
libpqxx::pqxx
|
||||
)
|
||||
|
||||
add_executable(${PROJECT_NAME} src/server.cpp)
|
||||
|
||||
@@ -62,6 +62,9 @@ void ZoneServer::player_update_handler(SessionId session_id, mmo::PlayerMoveMess
|
||||
if (it == m_session_zone.end()) return;
|
||||
|
||||
if (auto* session = m_player_session_registry->session(session_id)) {
|
||||
if(session->last_received_frame >= message.frame_idx()) {
|
||||
return;
|
||||
}
|
||||
session->last_received_frame = message.frame_idx();
|
||||
}
|
||||
|
||||
@@ -141,7 +144,7 @@ void ZoneServer::run() {
|
||||
}
|
||||
}
|
||||
|
||||
m_replicator->replicate(zone->registry(), zone->interest());
|
||||
m_replicator->replicate(frame_idx, zone->registry(), zone->interest());
|
||||
}
|
||||
|
||||
m_network_receiver->update();
|
||||
|
||||
@@ -7,7 +7,7 @@ TimescaleDbMetricsReporter::TimescaleDbMetricsReporter(const TimescaleDbConfigur
|
||||
m_last_flush_time(std::chrono::steady_clock::now())
|
||||
{
|
||||
try {
|
||||
m_connection = pqxx::connection(config.connection.to_string());
|
||||
m_connection.emplace(config.connection.to_string());
|
||||
spdlog::info("Connected to TimescaleDB at {}:{}", config.connection.host, config.connection.port);
|
||||
} catch (const std::exception& e) {
|
||||
spdlog::error("Failed to connect to TimescaleDB: {}", e.what());
|
||||
@@ -23,9 +23,9 @@ void TimescaleDbMetricsReporter::flush() {
|
||||
pqxx::work tx{*m_connection};
|
||||
|
||||
const auto table = m_connection->quote_name(m_metrics_table);
|
||||
tx.exec("INSERT INTO " + table + " (time, outbound, inbound, player_count) VALUES (NOW(), $1, $2, $3)",
|
||||
pqxx::params(m_outbound_bucket, m_inbound_bucket, m_player_count))
|
||||
.no_rows();
|
||||
tx.exec_prepared("INSERT INTO " + table + " (time, outbound, inbound, player_count) VALUES (NOW(), $1, $2, $3)",
|
||||
m_outbound_bucket, m_inbound_bucket, m_player_count)
|
||||
.empty();
|
||||
tx.commit();
|
||||
|
||||
TracyPlot("outbound", (int64_t)m_outbound_bucket);
|
||||
|
||||
@@ -29,8 +29,8 @@ class StateReplicator {
|
||||
// Per-client backing buffers reused every frame.
|
||||
std::vector<tw::serial::BinaryBuffer> m_frames;
|
||||
|
||||
// Header(16) + spawn_hdr(4) + despawn_hdr(4) + 512 entities × 16 bytes
|
||||
static constexpr std::size_t kHeaderCapacity = 24;
|
||||
// Header(20) + spawn_hdr(4) + despawn_hdr(4) + 512 entities × 16 bytes
|
||||
static constexpr std::size_t kHeaderCapacity = 28;
|
||||
static constexpr std::size_t kInitialCapacity = kHeaderCapacity + 512 * 16;
|
||||
|
||||
public:
|
||||
@@ -46,6 +46,7 @@ public:
|
||||
* Replicates the current world state for one zone to its connected clients.
|
||||
*/
|
||||
void replicate(
|
||||
uint32_t frame_idx,
|
||||
const entt::registry& registry,
|
||||
const im::InterestSystem<Backend>* interest_manager
|
||||
) {
|
||||
@@ -88,7 +89,8 @@ public:
|
||||
|
||||
m_frames[i].reserve(needed);
|
||||
writers[i].reset();
|
||||
writers[i].begin(session->last_received_frame, Message<mmo::WorldStateMessage>::value);
|
||||
writers[i].begin(session->last_received_frame, frame_idx,
|
||||
Message<mmo::WorldStateMessage>::value);
|
||||
writers[i].write_spawns(state->spawn());
|
||||
writers[i].write_despawns(state->despawn());
|
||||
|
||||
|
||||
@@ -24,7 +24,7 @@ target_link_libraries(
|
||||
tw::protocol
|
||||
tw::message_protocol
|
||||
tw::quicr
|
||||
towards
|
||||
tw_common
|
||||
tw::gui
|
||||
imgui::imgui
|
||||
imgui::plot
|
||||
|
||||
@@ -33,8 +33,6 @@ target_link_libraries(tw_server_lib
|
||||
protobuf::libprotobuf
|
||||
${Boost_LIBRARIES}
|
||||
Tracy::TracyClient
|
||||
pqxx
|
||||
pq
|
||||
)
|
||||
|
||||
add_executable(${PROJECT_NAME} src/server.cpp)
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#include "metrics/HistoryBuffer.hpp"
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
#include <Jolt/Jolt.h>
|
||||
|
||||
@@ -4,8 +4,6 @@
|
||||
#include <vector>
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
#include "metrics/HistoryBuffer.hpp"
|
||||
|
||||
namespace tw {
|
||||
|
||||
/**
|
||||
@@ -17,9 +15,6 @@ private:
|
||||
|
||||
using Clock = std::chrono::steady_clock;
|
||||
|
||||
HistoryBuffer<Clock::time_point, glm::vec3> m_history;
|
||||
HistoryBuffer<Clock::time_point, glm::vec3> m_position_history;
|
||||
|
||||
struct InputSlot {
|
||||
uint32_t frame;
|
||||
glm::vec3 input;
|
||||
@@ -34,13 +29,9 @@ private:
|
||||
|
||||
public:
|
||||
GET(m_speed, speed);
|
||||
GET_MUT_REF(m_history, input_history);
|
||||
GET_MUT_REF(m_position_history, position_history);
|
||||
|
||||
CharacterController(float speed) :
|
||||
m_speed(speed),
|
||||
m_history(Clock::now(), glm::vec3(), 10 * 20),
|
||||
m_position_history(Clock::now(), glm::vec3(), 10 * 20),
|
||||
m_input_ring(64),
|
||||
m_last_input_frame(0),
|
||||
m_frame_idx(0)
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
#include "world/RigidBody.hpp"
|
||||
#include <iostream>
|
||||
#include <print>
|
||||
#include "metrics/HistoryBuffer.hpp"
|
||||
|
||||
#include <array>
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
Reference in New Issue
Block a user