From 09a3a4fee3c460e1d2cf23df4bc8dc9367879d54 Mon Sep 17 00:00:00 2001 From: faumaray Date: Sun, 12 Jul 2026 03:00:26 +0300 Subject: [PATCH] Update Signed-off-by: faumaray --- SDK_PARITY.md | 2 +- include/Void/TeamSpeak/Client.hpp | 8 ++ include/Void/TeamSpeak/Types.hpp | 45 ++++++++++ src/Client.cpp | 132 ++++++++++++++++++++++++------ src/TsClient.cpp | 11 +-- tests/test_modern_api.cpp | 8 ++ 6 files changed, 177 insertions(+), 29 deletions(-) diff --git a/SDK_PARITY.md b/SDK_PARITY.md index 3ccf39b..5ef4f51 100644 --- a/SDK_PARITY.md +++ b/SDK_PARITY.md @@ -17,7 +17,7 @@ does not reproduce the proprietary C ABI. | Independent voice timelines | Implemented | one `Client` per timeline | | Voice receive callback | Implemented | `Events::voice` | | File download | Implemented | `download` | -| Typed server/channel/client notifications | Partial | common client/channel/text events typed; batched command fallback | +| Typed server/channel/client notifications | Implemented | client lifecycle/update/talk, channel, server, group, text, file-transfer events; batched fallback | | Permissions and channel groups | Implemented | typed lists and channel-group assignment | | File upload/delete | Implemented | `upload`, `deleteFile` | | Audio preprocessing/device GUI | Not applicable | client-local SDK behavior | diff --git a/include/Void/TeamSpeak/Client.hpp b/include/Void/TeamSpeak/Client.hpp index f6412b3..27093f7 100644 --- a/include/Void/TeamSpeak/Client.hpp +++ b/include/Void/TeamSpeak/Client.hpp @@ -48,8 +48,16 @@ namespace Void::TeamSpeak std::function clientEntered; std::function clientLeft; std::function clientMoved; + std::function clientUpdated; + std::function talkStatusChanged; std::function textMessage; std::function channelChanged; + std::function serverUpdated; + std::function + serverGroupMembershipChanged; + std::function + channelGroupMembershipChanged; + std::function fileTransferStatus; std::function)> voice; }; diff --git a/include/Void/TeamSpeak/Types.hpp b/include/Void/TeamSpeak/Types.hpp index fc2cf25..bea4320 100644 --- a/include/Void/TeamSpeak/Types.hpp +++ b/include/Void/TeamSpeak/Types.hpp @@ -1,6 +1,7 @@ #pragma once #include +#include #include #include #include @@ -57,6 +58,23 @@ namespace Void::TeamSpeak ChannelId channel; }; + struct ClientUpdated { + ClientId id; + std::optional nickname; + std::optional uniqueId; + std::optional inputMuted; + std::optional outputMuted; + std::optional away; + std::optional recording; + std::map properties; + }; + + struct TalkStatusChanged { + ClientId id; + bool talking{false}; + bool whisper{false}; + }; + struct Channel { ChannelId id; std::optional parent; @@ -101,6 +119,33 @@ namespace Void::TeamSpeak Channel channel; }; + struct ServerUpdated { + std::optional name; + std::optional welcomeMessage; + ClientId invoker; + std::map properties; + }; + + struct ServerGroupMembershipChanged { + bool added{false}; + ServerGroupId group; + ClientDatabaseId client; + ClientId invoker; + }; + + struct ChannelGroupMembershipChanged { + ChannelGroupId group; + ChannelId channel; + ClientDatabaseId client; + ClientId invoker; + }; + + struct FileTransferStatus { + std::uint64_t id{}; + int status{}; + std::string message; + }; + struct Error { enum class Code { InvalidArgument, diff --git a/src/Client.cpp b/src/Client.cpp index 4990dbd..0fe9ed2 100644 --- a/src/Client.cpp +++ b/src/Client.cpp @@ -5,6 +5,7 @@ #include #include +#include #include #include #include @@ -18,8 +19,10 @@ namespace Void::TeamSpeak class Client::Impl { public: - Void::TeamSpeak::Protocol::TsClient client; - Events events; + std::atomic> events{ + std::make_shared()}; + std::atomic callbacksInstalled{false}; + Void::TeamSpeak::Protocol::TsClient client; }; namespace @@ -60,30 +63,60 @@ namespace Void::TeamSpeak const auto iterator = record.find(std::string{key}); return iterator == record.end() ? std::string{} : iterator->second; } + + std::optional optionalText( + const Void::TeamSpeak::Protocol::CommandParams& record, + std::string_view key) + { + const auto iterator = record.find(std::string{key}); + if (iterator == record.end()) return std::nullopt; + return iterator->second; + } + + std::optional optionalBool( + const Void::TeamSpeak::Protocol::CommandParams& record, + std::string_view key) + { + if (!record.contains(std::string{key})) return std::nullopt; + return number(record, key) != 0; + } } - Client::Client() : impl_(std::make_unique()) {} + Client::Client() : impl_(std::make_unique()) { setEvents({}); } Client::~Client() = default; Client::Client(Client&&) noexcept = default; Client& Client::operator=(Client&&) noexcept = default; - void Client::setEvents(Events events) + void Client::setEvents(Events handlers) { - impl_->events = std::move(events); + impl_->events.store(std::make_shared(std::move(handlers)), + std::memory_order_release); + if (impl_->callbacksInstalled.exchange(true, std::memory_order_acq_rel)) { + return; + } auto* impl = impl_.get(); Void::TeamSpeak::Protocol::TsClientCallbacks callbacks; - callbacks.onConnected = [impl] { if (impl->events.connected) impl->events.connected(); }; - callbacks.onDisconnected = [impl] { if (impl->events.disconnected) impl->events.disconnected(); }; + callbacks.onConnected = [impl] { + auto events = impl->events.load(std::memory_order_acquire); + if (events->connected) events->connected(); + }; + callbacks.onDisconnected = [impl] { + auto events = impl->events.load(std::memory_order_acquire); + if (events->disconnected) events->disconnected(); + }; callbacks.onError = [impl](const std::string& message) { - if (impl->events.error) impl->events.error(protocolError(message)); + auto events = impl->events.load(std::memory_order_acquire); + if (events->error) events->error(protocolError(message)); }; callbacks.onDebug = [impl](const std::string& message) { - if (impl->events.debug) impl->events.debug(message); + auto events = impl->events.load(std::memory_order_acquire); + if (events->debug) events->debug(message); }; callbacks.onCommand = [impl](const Void::TeamSpeak::Protocol::ParsedCommand& command) { - if (impl->events.command) impl->events.command(command.name, command.params); - if (impl->events.commandBatch) { - impl->events.commandBatch(CommandEvent{ + auto events = impl->events.load(std::memory_order_acquire); + if (events->command) events->command(command.name, command.params); + if (events->commandBatch) { + events->commandBatch(CommandEvent{ .name = command.name, .records = command.groups.empty() ? std::vector{command.params} @@ -94,33 +127,85 @@ namespace Void::TeamSpeak : command.groups; for (const auto& record : records) { if (command.name == "notifycliententerview" && - impl->events.clientEntered) { - impl->events.clientEntered(RemoteClient{ + events->clientEntered) { + events->clientEntered(RemoteClient{ .id = ClientId{static_cast(number(record, "clid"))}, .channel = ChannelId{number(record, "ctid")}, .nickname = text(record, "client_nickname"), .uniqueId = text(record, "client_unique_identifier")}); } else if (command.name == "notifyclientleftview" && - impl->events.clientLeft) { - impl->events.clientLeft(ClientLeft{ + events->clientLeft) { + events->clientLeft(ClientLeft{ .id = ClientId{static_cast(number(record, "clid"))}, .reasonId = static_cast(number(record, "reasonid")), .reasonMessage = text(record, "reasonmsg")}); } else if (command.name == "notifyclientmoved" && - impl->events.clientMoved) { - impl->events.clientMoved(ClientMoved{ + events->clientMoved) { + events->clientMoved(ClientMoved{ .id = ClientId{static_cast(number(record, "clid"))}, .channel = ChannelId{number(record, "ctid")}}); + } else if (command.name == "notifyclientupdated" && + events->clientUpdated) { + events->clientUpdated(ClientUpdated{ + .id = ClientId{static_cast(number(record, "clid"))}, + .nickname = optionalText(record, "client_nickname"), + .uniqueId = optionalText(record, "client_unique_identifier"), + .inputMuted = optionalBool(record, "client_input_muted"), + .outputMuted = optionalBool(record, "client_output_muted"), + .away = optionalBool(record, "client_away"), + .recording = optionalBool(record, "client_is_recording"), + .properties = record}); + } else if (command.name == "notifytalkstatuschange" && + events->talkStatusChanged) { + events->talkStatusChanged(TalkStatusChanged{ + .id = ClientId{static_cast(number(record, "clid"))}, + .talking = number(record, "status") != 0, + .whisper = number(record, "isreceivedwhisper") != 0}); } else if (command.name == "notifytextmessage" && - impl->events.textMessage) { - impl->events.textMessage(TextMessage{ + events->textMessage) { + events->textMessage(TextMessage{ .target = TextTarget{ .kind = static_cast(number(record, "targetmode")), .id = number(record, "target")}, .invoker = ClientId{static_cast(number(record, "invokerid"))}, .invokerName = text(record, "invokername"), .message = text(record, "msg")}); - } else if (impl->events.channelChanged) { + } else if ((command.name == "notifyserveredited" || + command.name == "notifyserverupdated") && + events->serverUpdated) { + events->serverUpdated(ServerUpdated{ + .name = optionalText(record, "virtualserver_name"), + .welcomeMessage = optionalText(record, + "virtualserver_welcomemessage"), + .invoker = ClientId{static_cast(number(record, + "invokerid"))}, + .properties = record}); + } else if ((command.name == "notifyservergroupclientadded" || + command.name == "notifyservergroupclientdeleted") && + events->serverGroupMembershipChanged) { + events->serverGroupMembershipChanged( + ServerGroupMembershipChanged{ + .added = command.name == "notifyservergroupclientadded", + .group = ServerGroupId{number(record, "sgid")}, + .client = ClientDatabaseId{number(record, "cldbid")}, + .invoker = ClientId{static_cast(number(record, + "invokerid"))}}); + } else if (command.name == "notifyclientchannelgroupchanged" && + events->channelGroupMembershipChanged) { + events->channelGroupMembershipChanged( + ChannelGroupMembershipChanged{ + .group = ChannelGroupId{number(record, "cgid")}, + .channel = ChannelId{number(record, "cid")}, + .client = ClientDatabaseId{number(record, "cldbid")}, + .invoker = ClientId{static_cast(number(record, + "invokerid"))}}); + } else if (command.name == "notifyfiletransferstatus" && + events->fileTransferStatus) { + events->fileTransferStatus(FileTransferStatus{ + .id = number(record, "ftid"), + .status = static_cast(number(record, "status")), + .message = text(record, "msg")}); + } else if (events->channelChanged) { std::optional kind; if (command.name == "notifychannelcreated") kind = ChannelChangeKind::Created; else if (command.name == "notifychanneledited") kind = ChannelChangeKind::Updated; @@ -128,7 +213,7 @@ namespace Void::TeamSpeak else if (command.name == "notifychanneldeleted") kind = ChannelChangeKind::Deleted; if (kind) { const auto parent = number(record, "pid"); - impl->events.channelChanged(ChannelChanged{ + events->channelChanged(ChannelChanged{ .kind = *kind, .channel = Channel{ .id = ChannelId{number(record, "cid")}, @@ -142,7 +227,8 @@ namespace Void::TeamSpeak } }; callbacks.onVoice = [impl](std::span voice) { - if (impl->events.voice) impl->events.voice(voice); + auto events = impl->events.load(std::memory_order_acquire); + if (events->voice) events->voice(voice); }; impl_->client.setCallbacks(std::move(callbacks)); } diff --git a/src/TsClient.cpp b/src/TsClient.cpp index b299575..4334ccd 100644 --- a/src/TsClient.cpp +++ b/src/TsClient.cpp @@ -203,7 +203,7 @@ namespace Void::TeamSpeak::Protocol std::chrono::duration_cast( std::chrono::system_clock::now().time_since_epoch()) .count()); - buf[5] = (ts >> 24) & 0xff; + buf[5] = static_cast((ts >> 24) & 0xffU); buf[6] = (ts >> 16) & 0xff; buf[7] = (ts >> 8) & 0xff; buf[8] = ts & 0xff; @@ -827,10 +827,11 @@ namespace Void::TeamSpeak::Protocol init4[4] = 0x04; std::memcpy(init4.data() + 5, x.data(), 64); std::memcpy(init4.data() + 69, n.data(), 64); - init4[133] = (level >> 24) & 0xff; - init4[134] = (level >> 16) & 0xff; - init4[135] = (level >> 8) & 0xff; - init4[136] = level & 0xff; + const auto unsignedLevel = static_cast(level); + init4[133] = static_cast((unsignedLevel >> 24) & 0xffU); + init4[134] = static_cast((unsignedLevel >> 16) & 0xffU); + init4[135] = static_cast((unsignedLevel >> 8) & 0xffU); + init4[136] = static_cast(unsignedLevel & 0xffU); std::memcpy(init4.data() + 137, serverData.data(), 100); std::memcpy(init4.data() + 237, y.data(), 64); std::memcpy(init4.data() + 301, textBytes.data(), textBytes.size()); diff --git a/tests/test_modern_api.cpp b/tests/test_modern_api.cpp index 41f5b2e..bdb4fd2 100644 --- a/tests/test_modern_api.cpp +++ b/tests/test_modern_api.cpp @@ -12,6 +12,14 @@ static_assert(std::is_move_constructible_v); static_assert(std::is_same_v); static_assert(std::is_same_v); static_assert(std::is_same_v); +static_assert(std::is_same_v>); +static_assert(std::is_same_v>); +static_assert(std::is_same_v>); +static_assert(std::is_same_v>); static_assert(std::is_same_v().deleteFile("x")), Void::Result::Result>);