Teaspeak-Server/server/src/client/voice/VoiceClient.h

123 lines
4.9 KiB
C++

#pragma once
#include <cstdint>
#include <ThreadPool/Thread.h>
#include <ThreadPool/Mutex.h>
#include <protocol/buffers.h>
#include <netinet/in.h>
#include <deque>
#include <cstdint>
#include <src/server/VoiceServer.h>
#include <EventLoop.h>
#include "../SpeakingClient.h"
#include "../ConnectedClient.h"
#include "protocol/CryptHandler.h"
#include "VoiceClientConnection.h"
#include "src/server/PrecomputedPuzzles.h"
#include "../../lincense/TeamSpeakLicense.h"
#include "./ServerCommandExecutor.h"
//#define LOG_INCOMPING_PACKET_FRAGMENTS
//#define LOG_AUTO_ACK_AUTORESPONSE
//#define LOG_AUTO_ACK_REQUEST
//#define LOG_AUTO_ACK_RESPONSE
#define PKT_LOG_CMD
//#define PKT_LOG_VOICE
//#define PKT_LOG_WHISPER
//#define PKT_LOG_PING
//For CLion
#ifndef CLIENT_LOG_PREFIX
#define CLIENT_LOG_PREFIX "Undefined CLIENT_LOG_PREFIX"
#endif
namespace ts {
namespace connection {
class VoiceClientConnection;
}
namespace server {
namespace server::udp {
class ServerCommandExecutor;
class CryptSetupHandler;
}
class VirtualServer;
class VoiceClient : public SpeakingClient {
friend class VirtualServer;
friend class VoiceServer;
friend class POWHandler;
friend class ts::connection::VoiceClientConnection;
friend class ConnectedClient;
friend class io::IOServerHandler;
friend class server::udp::ServerCommandExecutor;
friend class server::udp::CryptSetupHandler;
using ServerCommandExecutor = ts::server::server::udp::ServerCommandExecutor;
public:
VoiceClient(const std::shared_ptr<VoiceServer>& server, const sockaddr_storage*);
~VoiceClient() override;
bool close_connection(const std::chrono::system_clock::time_point &timeout) override;
bool disconnect(const std::string&) override;
bool disconnect(ViewReasonId /* reason type */, const std::string& /* reason */, const std::shared_ptr<ts::server::ConnectedClient>& /* invoker */, bool /* notify viewer */);
void sendCommand(const ts::Command &command, bool low = false) override { return this->sendCommand0(command.build(), low); }
void sendCommand(const ts::command_builder &command, bool low) override { return this->sendCommand0(command.build(), low); }
/* Note: Order is only guaranteed if progressDirectly is on! */
virtual void sendCommand0(const std::string_view& /* data */, bool low = false, std::unique_ptr<threads::Future<bool>> listener = nullptr);
connection::VoiceClientConnection* getConnection(){ return connection; }
std::shared_ptr<VoiceServer> getVoiceServer(){ return voice_server; }
[[nodiscard]] std::chrono::milliseconds current_ping();
[[nodiscard]] float current_ping_deviation();
[[nodiscard]] float current_packet_loss() const;
[[nodiscard]] inline auto& server_command_executor() { return this->server_command_executor_; }
private:
connection::VoiceClientConnection* connection;
protected:
std::shared_ptr<VoiceServer> voice_server;
void initialize();
virtual void tick(const std::chrono::system_clock::time_point &time) override;
/* Attention these handle callbacks are not thread save! */
void handlePacketCommand(const pipes::buffer_view&);
public:
void send_voice_packet(const pipes::buffer_view &packet, const VoicePacketFlags &flags) override;
void send_voice_whisper_packet(
const pipes::buffer_view &/* teamspeak packet */,
const pipes::buffer_view &/* teaspeak packet */,
const VoicePacketFlags &flags
) override;
void processJoin() override;
protected:
virtual command_result handleCommand(Command &command) override;
private:
void finalDisconnect();
bool final_disconnected = false;
rtc::NativeAudioSourceSupplier rtc_audio_supplier{};
uint16_t stop_seq_counter{0};
//General TS3 manager commands
command_result handleCommandClientInit(Command&) override;
command_result handleCommandClientDisconnect(Command&);
//Locked by finalDisconnect, disconnect and close connection
std::shared_ptr<threads::Thread> flushing_thread;
ServerCommandExecutor server_command_executor_{this};
std::shared_ptr<event::ProxiedEventEntry<VoiceClient>> event_handle_packet;
void execute_handle_packet(const std::chrono::system_clock::time_point& /* scheduled */);
};
}
}