Skip to content

Commit

Permalink
wip-refact-deferred
Browse files Browse the repository at this point in the history
  • Loading branch information
Pospelove committed Dec 13, 2024
1 parent eb32b8d commit 31eed12
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 40 deletions.
19 changes: 7 additions & 12 deletions skymp5-server/cpp/addon/property_bindings/AppearanceBinding.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "AppearanceBinding.h"
#include "NapiHelper.h"
#include "UpdateAppearanceMessage.h"

Napi::Value AppearanceBinding::Get(Napi::Env env, ScampServer& scampServer,
uint32_t formId)
Expand Down Expand Up @@ -34,19 +35,13 @@ void AppearanceBinding::Set(Napi::Env env, ScampServer& scampServer,

auto appearance = actor.GetAppearance();

std::string msg;
msg += Networking::MinPacketId;
msg += nlohmann::json{
{ "data",
appearance ? nlohmann::json::parse(appearance->ToJson())
: nlohmann::json{} },
{ "idx", actor.GetIdx() },
{ "t", MsgType::UpdateAppearance }
}.dump();
UpdateAppearanceMessage message;
message.appearance = appearance ? *appearance : std::nullopt;
message.idx = actor.GetIdx();

for (auto listener : actor.GetActorListeners()) {
// TODO: change to SendToUser
listener->SendToUserDeferred(msg.data(), msg.size(), true,
kChannelAppearance, false);
// TODO: change to SendToUser, probably was deferred only for ability to
// send text packets
listener->SendToUserDeferred(message, true, kChannelAppearance, false);
}
}
24 changes: 0 additions & 24 deletions skymp5-server/cpp/mp_common/NetworkingInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,28 +60,4 @@ class IServer : public ISendTarget

virtual std::string GetIp(UserId userId) const = 0;
};

template <class FormatCallback, class... Ts>
inline void Format(const FormatCallback& cb, const char* format, Ts... args)
{
auto textSize = (size_t)snprintf(nullptr, 0, format, args...);

const auto n = textSize + sizeof('\0') + sizeof(MinPacketId);
std::vector<char> buf(n);

buf[0] = MinPacketId;
auto len = (size_t)snprintf(buf.data() + 1, n - 1, format, args...);

cb(reinterpret_cast<Networking::PacketData>(buf.data()), len + 1);
}

template <class... Ts>
inline void SendFormatted(Networking::ISendTarget* sendTarget,
Networking::UserId userId, const char* format,
Ts... args)
{
Format([&](Networking::PacketData data,
size_t length) { sendTarget->Send(userId, data, length, true); },
format, args...);
}
}
5 changes: 2 additions & 3 deletions skymp5-server/cpp/server_guest_lib/MpActor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -400,13 +400,12 @@ void MpActor::SendToUser(const IMessageBase& message, bool reliable)
}
}

void MpActor::SendToUserDeferred(const void* data, size_t size, bool reliable,
void MpActor::SendToUserDeferred(const IMessageBase& message, bool reliable,
int deferredChannelId,
bool overwritePreviousChannelMessages)
{
if (callbacks->sendToUserDeferred) {
callbacks->sendToUserDeferred(this, data, size, reliable,
deferredChannelId,
callbacks->sendToUserDeferred(this, data, reliable, deferredChannelId,
overwritePreviousChannelMessages);
} else {
throw std::runtime_error("sendToUserDeferred is nullptr");
Expand Down
2 changes: 1 addition & 1 deletion skymp5-server/cpp/server_guest_lib/MpActor.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class MpActor : public MpObjectReference
void Disable() override;

void SendToUser(const IMessageBase& message, bool reliable);
void SendToUserDeferred(const void* data, size_t size, bool reliable,
void SendToUserDeferred(const IMessageBase& message, bool reliable,
int deferredChannelId,
bool overwritePreviousChannelMessages);

Expand Down

0 comments on commit 31eed12

Please sign in to comment.