Skip to content

Commit

Permalink
jijij
Browse files Browse the repository at this point in the history
  • Loading branch information
WtzLAS committed Dec 27, 2023
1 parent acb3afb commit 8af9f1e
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 245 deletions.
2 changes: 1 addition & 1 deletion NorthstarDLL/client/igig/kcpstats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ static void draw_kcp_stats()
}

auto ng = NetGraphSink::instance();
std::shared_lock lk(ng->windowsMutex);
std::unique_lock lk(ng->windowsMutex);

if (ng->windows.empty())
{
Expand Down
30 changes: 10 additions & 20 deletions NorthstarDLL/engine/netgraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ void sendThreadPayload(std::stop_token stoken, NetGraphSink* ng)
while (!stoken.stop_requested())
{
std::this_thread::sleep_for(std::chrono::milliseconds(100));
std::shared_lock lk(manager->routingTableMutex);
std::unique_lock lk1(manager->routingTableMutex);
std::unique_lock lk2(ng->windowsMutex);
for (const auto& entry : manager->routingTable)
{
if (itimediff(iclock(), entry.second.second) > lastSeenInterval)
if (itimediff(iclock(), std::get<2>(entry.second)) > lastSeenInterval)
{
continue;
}
NetBuffer buf(128, 0, 128);
std::shared_lock lk(ng->windowsMutex);
std::get<0>(ng->windows[entry.first]).encode(buf);
entry.second.first.second->sendto(std::move(buf), entry.first, NetGraphSink::instance().get());
std::get<1>(entry.second)->sendto(std::move(buf), entry.first, NetGraphSink::instance().get());
}
}
}
Expand All @@ -49,7 +49,7 @@ int NetGraphSink::input(NetBuffer&& buf, const NetContext& ctx, const NetSource*
{
NetStats s {};
s.decode(buf);
std::shared_lock lk(windowsMutex);
std::unique_lock lk(windowsMutex);
std::get<3>(windows[ctx]).rotate(s);
return 0;
}
Expand All @@ -67,33 +67,23 @@ std::shared_ptr<NetGraphSink> NetGraphSink::instance()

void NetStats::encode(NetBuffer& buf) const
{
if (buf.size() < sizeof(float) + 5 * sizeof(IUINT64))
if (buf.size() < sizeof(NetStats))
{
buf.resize(buf.size() + sizeof(float) + 5 * sizeof(IUINT64), 0);
buf.resize(buf.size() + sizeof(NetStats), 0);
}

memcpy(buf.data(), &frameTime, sizeof(float));
memcpy(buf.data() + sizeof(float), &outsegs, sizeof(IUINT64));
memcpy(buf.data() + sizeof(float) + 1 * sizeof(IUINT64), &lostsegs, sizeof(IUINT64));
memcpy(buf.data() + sizeof(float) + 2 * sizeof(IUINT64), &retranssegs, sizeof(IUINT64));
memcpy(buf.data() + sizeof(float) + 3 * sizeof(IUINT64), &insegs, sizeof(IUINT64));
memcpy(buf.data() + sizeof(float) + 4 * sizeof(IUINT64), &reconsegs, sizeof(IUINT64));
memcpy(buf.data(), this, sizeof(NetStats));
}

void NetStats::decode(const NetBuffer& buf)
{
if (buf.size() < sizeof(float) + 5 * sizeof(IUINT64))
if (buf.size() < sizeof(NetStats))
{
NS::log::NEW_NET->warn("[NG] Spurious input of NetStats::decode");
return;
}

memcpy(&frameTime, buf.data(), sizeof(float));
memcpy(&outsegs, buf.data() + sizeof(float), sizeof(IUINT64));
memcpy(&lostsegs, buf.data() + sizeof(float) + 1 * sizeof(IUINT64), sizeof(IUINT64));
memcpy(&retranssegs, buf.data() + sizeof(float) + 2 * sizeof(IUINT64), sizeof(IUINT64));
memcpy(&insegs, buf.data() + sizeof(float) + 3 * sizeof(IUINT64), sizeof(IUINT64));
memcpy(&reconsegs, buf.data() + sizeof(float) + 4 * sizeof(IUINT64), sizeof(IUINT64));
memcpy(this, buf.data(), sizeof(NetStats));
}

void NetStats::sync(ikcpcb* cb)
Expand Down
6 changes: 2 additions & 4 deletions NorthstarDLL/engine/netgraph.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#pragma once

#include "shared/kcpintegration.h"
#include <shared_mutex>
#include <concurrent_unordered_map.h>

extern float* g_frameTime;

Expand Down Expand Up @@ -68,8 +66,8 @@ class NetGraphSink : public NetSink

static std::shared_ptr<NetGraphSink> instance();

std::shared_mutex windowsMutex;
concurrency::concurrent_unordered_map<NetContext, std::tuple<NetStats, sliding_window, NetSlidingWindows, NetSlidingWindows>> windows;
std::mutex windowsMutex;
std::unordered_map<NetContext, std::tuple<NetStats, sliding_window, NetSlidingWindows, NetSlidingWindows>> windows;

private:
NetGraphSink();
Expand Down
Loading

0 comments on commit 8af9f1e

Please sign in to comment.