commit
38e86d4a07
23 files changed
+3219
No files matched your search
@@ -0,0 +1,195 @@
|
||||
#pragma once
|
||||
|
||||
// Full-client connection to a TeamSpeak 3/6 server over the native UDP voice
|
||||
// protocol. Owns the socket, handshake, per-packet crypto, resend/keepalive and
|
||||
// voice send path. Ported from ts6-manager tslib/client.ts.
|
||||
//
|
||||
// Threading: a single background thread runs the receive + timer loop. Public
|
||||
// send methods are thread-safe (guarded by an internal mutex) so the audio
|
||||
// thread can call sendVoice() directly. Callbacks fire on the loop thread.
|
||||
|
||||
#include "echoion/protocol/Command.hpp"
|
||||
#include "echoion/protocol/Identity.hpp"
|
||||
|
||||
#include <atomic>
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <span>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
namespace Echoion::Protocol
|
||||
{
|
||||
|
||||
enum class PacketType : uint8_t {
|
||||
Voice = 0,
|
||||
VoiceWhisper = 1,
|
||||
Command = 2,
|
||||
CommandLow = 3,
|
||||
Ping = 4,
|
||||
Pong = 5,
|
||||
Ack = 6,
|
||||
AckLow = 7,
|
||||
Init1 = 8,
|
||||
};
|
||||
|
||||
enum class ClientState {
|
||||
Disconnected,
|
||||
Init,
|
||||
Handshake,
|
||||
Connected,
|
||||
Disconnecting,
|
||||
};
|
||||
|
||||
struct TsClientOptions {
|
||||
std::string host;
|
||||
uint16_t port = 9987;
|
||||
const Identity* identity = nullptr; // not owned
|
||||
std::string nickname = "EchoionBot";
|
||||
std::string serverPassword;
|
||||
std::string defaultChannel; // channel id or name
|
||||
std::string channelPassword;
|
||||
};
|
||||
|
||||
// All callbacks fire on the internal loop thread.
|
||||
struct TsClientCallbacks {
|
||||
std::function<void()> onConnected;
|
||||
std::function<void()> onDisconnected;
|
||||
std::function<void(const std::string&)> onError;
|
||||
std::function<void(const ParsedCommand&)> onCommand;
|
||||
std::function<void(const CommandParams&)> onTextMessage;
|
||||
std::function<void(std::span<const uint8_t>)> onVoice;
|
||||
std::function<void(const std::string&)> onDebug;
|
||||
};
|
||||
|
||||
class TsClient
|
||||
{
|
||||
public:
|
||||
|
||||
TsClient();
|
||||
~TsClient();
|
||||
|
||||
TsClient(const TsClient&) = delete;
|
||||
TsClient& operator=(const TsClient&) = delete;
|
||||
|
||||
void setCallbacks(TsClientCallbacks cb) { cb_ = std::move(cb); }
|
||||
|
||||
// Begin connecting. Returns immediately; onConnected/onError fire
|
||||
// asynchronously. Throws only on immediate socket setup failure.
|
||||
void connect(const TsClientOptions& opts);
|
||||
// Graceful: send clientdisconnect, then close.
|
||||
void disconnect();
|
||||
// Immediate close, no disconnect command.
|
||||
void forceClose();
|
||||
|
||||
ClientState state() const { return state_.load(); }
|
||||
uint16_t clientId() const { return clientId_.load(); }
|
||||
|
||||
// Send one already-Opus-encoded 20 ms frame to the current channel.
|
||||
void sendVoice(std::span<const uint8_t> opus);
|
||||
// Whisper the frame to the given channel/client targets.
|
||||
void sendWhisper(std::span<const uint8_t> opus,
|
||||
const std::vector<uint64_t>& channelIds,
|
||||
const std::vector<uint16_t>& clientIds);
|
||||
// End-of-voice marker (empty frame).
|
||||
void sendVoiceStop();
|
||||
|
||||
// Send a raw pre-built TS3 command line (fragmented if large).
|
||||
void sendCommand(const std::string& cmd);
|
||||
|
||||
private:
|
||||
|
||||
// ---- packet path ----
|
||||
void loopThread();
|
||||
void handleIncomingPacket(std::span<const uint8_t> raw);
|
||||
void sendOutgoing(std::span<const uint8_t> data, PacketType type,
|
||||
uint8_t extraFlags = 0);
|
||||
std::vector<uint8_t> buildRawPacket(std::span<const uint8_t> data,
|
||||
PacketType type,
|
||||
uint16_t packetId,
|
||||
uint8_t flags);
|
||||
void encryptPacket(std::vector<uint8_t>& raw, PacketType type,
|
||||
uint16_t packetId, uint32_t gen, uint8_t flags,
|
||||
std::span<const uint8_t> data);
|
||||
std::vector<uint8_t> decryptPacket(std::span<const uint8_t> raw,
|
||||
PacketType type,
|
||||
uint16_t packetId, uint32_t gen,
|
||||
uint8_t flags, bool& ok);
|
||||
void sendRawLocked(const std::vector<uint8_t>& raw);
|
||||
void sendInitPacket(const std::vector<uint8_t>& data);
|
||||
|
||||
// ---- handshake / commands ----
|
||||
std::vector<uint8_t> buildInit0();
|
||||
void handleInit(std::span<const uint8_t> data);
|
||||
void handleCommandData(std::span<const uint8_t> data, uint8_t flags);
|
||||
void processCommand(std::span<const uint8_t> data, uint8_t flags);
|
||||
void handleInitIvExpand(const CommandParams& params);
|
||||
void handleInitIvExpand2(const CommandParams& params);
|
||||
void sendClientInit();
|
||||
void handleInitServer(const CommandParams& params);
|
||||
void handleChannelList(const ParsedCommand& parsed);
|
||||
void handleChannelListFinished();
|
||||
|
||||
void sendAck(uint16_t ackId, PacketType ackType);
|
||||
void handlePing(uint16_t packetId);
|
||||
void resendTick();
|
||||
void debug(const std::string& msg);
|
||||
void cleanup();
|
||||
|
||||
// counters / crypto
|
||||
struct Counter {
|
||||
uint16_t id = 0;
|
||||
uint32_t gen = 0;
|
||||
};
|
||||
Counter getCounter(PacketType type);
|
||||
void incCounter(PacketType type);
|
||||
|
||||
TsClientOptions opts_;
|
||||
TsClientCallbacks cb_;
|
||||
|
||||
int sock_ = -1;
|
||||
std::atomic<ClientState> state_{ClientState::Disconnected};
|
||||
std::atomic<uint16_t> clientId_{0};
|
||||
|
||||
std::thread thread_;
|
||||
std::atomic<bool> running_{false};
|
||||
// Recursive: command handlers running under the loop lock call
|
||||
// sendCommand()/sendOutgoing() which re-lock.
|
||||
mutable std::recursive_mutex mu_;
|
||||
int64_t disconnectDeadline_ = 0;
|
||||
|
||||
std::array<uint16_t, 9> packetCounter_{};
|
||||
std::array<uint32_t, 9> generationCounter_{};
|
||||
|
||||
bool cryptoInitComplete_ = false;
|
||||
std::vector<uint8_t> ivStruct_;
|
||||
std::array<uint8_t, 8> fakeSignature_{};
|
||||
std::vector<uint8_t> alphaTmp_;
|
||||
|
||||
// resend tracking
|
||||
struct Resend {
|
||||
std::vector<uint8_t> raw;
|
||||
uint16_t packetId = 0;
|
||||
int64_t firstSend = 0;
|
||||
int64_t lastSend = 0;
|
||||
};
|
||||
std::map<uint16_t, Resend> resendMap_;
|
||||
std::unique_ptr<Resend> initResend_;
|
||||
int64_t lastMessageTime_ = 0;
|
||||
int64_t lastPing_ = 0;
|
||||
int64_t connectStart_ = 0;
|
||||
|
||||
// fragment reassembly
|
||||
std::vector<uint8_t> fragmentBuffer_;
|
||||
bool fragmenting_ = false;
|
||||
uint8_t fragmentFlags_ = 0;
|
||||
|
||||
// channel map for auto-move
|
||||
std::map<std::string, uint64_t> channelMap_;
|
||||
};
|
||||
|
||||
} // namespace Echoion::Protocol
|
||||
Reference in new issue
Block a user