2019-07-17 19:37:18 +02:00
# 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"
2020-01-27 02:21:39 +01:00
# include "protocol/CryptHandler.h"
2019-07-17 19:37:18 +02:00
# include "VoiceClientConnection.h"
# include "PrecomputedPuzzles.h"
# include "../../lincense/TeamSpeakLicense.h"
//#define LOG_INCOMPING_PACKET_FRAGMENTS
//#define LOG_AUTO_ACK_AUTORESPONSE
//#define LOG_AUTO_ACK_REQUEST
//#define LOG_AUTO_ACK_RESPONSE
//#define LOG_PKT_RESEND
# 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 {
2020-01-26 18:04:38 +01:00
class VirtualServer ;
2019-07-17 19:37:18 +02:00
class VoiceClient : public SpeakingClient {
2020-01-26 18:04:38 +01:00
friend class VirtualServer ;
2019-07-17 19:37:18 +02:00
friend class VoiceServer ;
2020-01-24 02:57:58 +01:00
friend class POWHandler ;
2019-07-17 19:37:18 +02:00
friend class ts : : connection : : VoiceClientConnection ;
friend class ConnectedClient ;
friend class io : : IOServerHandler ;
public :
VoiceClient ( const std : : shared_ptr < VoiceServer > & server , const sockaddr_storage * ) ;
~ VoiceClient ( ) ;
2020-02-01 14:32:16 +01:00
bool close_connection ( const std : : chrono : : system_clock : : time_point & timeout ) override ;
2019-07-17 19:37:18 +02:00
bool disconnect ( const std : : string & ) override ;
2020-01-24 02:57:58 +01:00
bool disconnect ( ViewReasonId /* reason type */ , const std : : string & /* reason */ , const std : : shared_ptr < ts : : server : : ConnectedClient > & /* invoker */ , bool /* notify viewer */ ) ;
2019-07-17 19:37:18 +02:00
2020-01-26 14:21:34 +01:00
virtual void sendCommand ( const ts : : Command & command , bool low = false ) { return this - > sendCommand0 ( command . build ( ) , low ) ; }
virtual void sendCommand ( const ts : : command_builder & command , bool low ) { return this - > sendCommand0 ( command . build ( ) , low ) ; }
2020-01-24 02:57:58 +01:00
/* Note: Order is only guaranteed if progressDirectly is on! */
2020-01-26 14:21:34 +01:00
virtual void sendCommand0 ( const std : : string_view & /* data */ , bool low = false , bool progressDirectly = false , std : : unique_ptr < threads : : Future < bool > > listener = nullptr ) ;
2019-07-17 19:37:18 +02:00
virtual void sendAcknowledge ( uint16_t packetId , bool low = false ) ;
connection : : VoiceClientConnection * getConnection ( ) { return connection ; }
std : : shared_ptr < VoiceServer > getVoiceServer ( ) { return voice_server ; }
std : : chrono : : milliseconds calculatePing ( ) { return ping ; }
private :
connection : : VoiceClientConnection * connection ;
protected :
2020-01-24 02:57:58 +01:00
std : : shared_ptr < VoiceServer > voice_server ;
2019-07-17 19:37:18 +02:00
2020-01-24 02:57:58 +01:00
void initialize ( ) ;
2019-07-17 19:37:18 +02:00
virtual void tick ( const std : : chrono : : system_clock : : time_point & time ) override ;
2020-01-27 02:21:39 +01:00
void handlePacketCommand ( const pipes : : buffer_view & ) ;
void handlePacketAck ( const protocol : : IncomingClientPacketParser & ) ;
void handlePacketVoice ( const protocol : : IncomingClientPacketParser & ) ;
void handlePacketPing ( const protocol : : IncomingClientPacketParser & ) ;
void handlePacketInit ( const protocol : : IncomingClientPacketParser & ) ;
2019-07-17 19:37:18 +02:00
2020-01-24 02:57:58 +01:00
//Handshake helpers
2019-07-17 19:37:18 +02:00
2020-01-24 02:57:58 +01:00
public :
void send_voice_packet ( const pipes : : buffer_view & packet , const VoicePacketFlags & flags ) override ;
void send_voice_whisper_packet ( const pipes : : buffer_view & packet , const VoicePacketFlags & flags ) override ;
2019-07-17 19:37:18 +02:00
2020-01-24 02:57:58 +01:00
protected :
2020-01-25 23:42:37 +01:00
virtual command_result handleCommand ( Command & command ) override ;
2019-07-17 19:37:18 +02:00
//Some helper method
void sendPingRequest ( ) ;
//Ping/pong
uint16_t lastPingId = 0 ;
std : : chrono : : milliseconds ping = std : : chrono : : milliseconds ( 0 ) ;
2020-01-24 02:57:58 +01:00
std : : chrono : : system_clock : : time_point lastPingResponse ;
std : : chrono : : system_clock : : time_point lastPingRequest ;
2019-07-17 19:37:18 +02:00
2020-01-24 02:57:58 +01:00
std : : chrono : : system_clock : : time_point last_packet_handshake ;
2019-07-17 19:37:18 +02:00
private :
2020-01-24 02:57:58 +01:00
int socket = 0 ;
io : : pktinfo_storage address_info ;
2019-07-17 19:37:18 +02:00
void finalDisconnect ( ) ;
2020-01-24 02:57:58 +01:00
bool final_disconnected = false ;
2019-07-17 19:37:18 +02:00
//General TS3 manager commands
2020-01-25 23:42:37 +01:00
command_result handleCommandClientInitIv ( Command & ) ;
2020-01-27 02:21:39 +01:00
command_result handleCommandClientEk ( Command & ) ;
2020-01-25 23:42:37 +01:00
command_result handleCommandClientInit ( Command & ) override ;
command_result handleCommandClientDisconnect ( Command & ) ;
2019-07-17 19:37:18 +02:00
2020-01-24 02:57:58 +01:00
//Locked by finalDisconnect, disconnect and close connection
2019-07-17 19:37:18 +02:00
std : : shared_ptr < threads : : Thread > flushing_thread ;
2020-01-24 02:57:58 +01:00
struct {
bool client_init = false ;
bool new_protocol = false ;
bool protocol_encrypted = false ;
2019-07-17 19:37:18 +02:00
2020-01-24 02:57:58 +01:00
uint32_t client_time = 0 ;
std : : string alpha ;
std : : string beta ;
std : : shared_ptr < LicenseChainData > chain_data ;
std : : shared_ptr < ecc_key > remote_key ;
} crypto ;
2019-07-17 19:37:18 +02:00
2020-01-24 02:57:58 +01:00
std : : shared_ptr < event : : ProxiedEventEntry < VoiceClient > > event_handle_packet ;
void execute_handle_packet ( const std : : chrono : : system_clock : : time_point & /* scheduled */ ) ;
2019-07-17 19:37:18 +02:00
} ;
}
}