Twitch SDK (Internal)
Classes | Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | Private Member Functions | Private Attributes | List of all members
ttv::PubSubClient Class Reference

#include <pubsubclient.h>

Inheritance diagram for ttv::PubSubClient:
ttv::UserComponent ttv::Component ttv::IComponent

Classes

class  ConnectionListener
 
struct  ConnectionPreference
 
class  IListener
 
class  ITopicListener
 
struct  SubscribeState
 
struct  Topic
 
struct  TopicState
 

Public Types

typedef std::function< void(PubSubClient *source, TTV_ErrorCode ec)> SendMessageCallback
 
enum  State { State::Uninitialized, State::Initialized, State::ShuttingDown, State::Inert }
 

Public Member Functions

 PubSubClient (std::shared_ptr< User > user)
 
virtual ~PubSubClient ()
 
std::shared_ptr< UserGetUser ()
 
PubSubState GetConnectionState () const
 
void AddListener (std::shared_ptr< IListener > listener)
 
void RemoveListener (std::shared_ptr< IListener > listener)
 
TTV_ErrorCode Initialize () override
 
virtual TTV_ErrorCode Connect ()
 
TTV_ErrorCode Disconnect ()
 
TTV_ErrorCode SetConnectionPreference (ConnectionPreference::Enum preference)
 
TTV_ErrorCode SendMessage (const json::Value &jMessage, SendMessageCallback callback)
 
TTV_ErrorCode AddTopicListener (const std::string &topic, std::shared_ptr< ITopicListener > listener)
 
TTV_ErrorCode RemoveTopicListener (const std::string &topic, std::shared_ptr< ITopicListener > listener)
 
TTV_ErrorCode Shutdown () override
 
virtual void Update () override
 
virtual std::string GetLoggerName () const override
 
virtual void SetTaskRunner (std::shared_ptr< TaskRunner > taskRunner)
 
std::shared_ptr< TaskRunnerGetTaskRunner ()
 
virtual State GetState () const override
 

Static Public Member Functions

static std::string GetComponentName ()
 

Protected Member Functions

virtual void OnUserLogInComplete (TTV_ErrorCode ec)
 
virtual void OnUserLogOutComplete (TTV_ErrorCode ec)
 
virtual void OnUserInfoFetchComplete (TTV_ErrorCode ec)
 
virtual void OnUserAuthenticationIssue (std::shared_ptr< const OAuthToken > oauthToken, TTV_ErrorCode ec)
 
void Log (TTV_MessageLevel level, const char *format,...)
 
TTV_ErrorCode StartTask (std::shared_ptr< Task > task)
 
void CompleteTask (Task *task)
 
bool IsTaskRunning (Task *task) const
 
virtual void SetState (State state)
 
virtual void SetClientState (State state)
 
virtual void SetServerState (State state)
 

Protected Attributes

std::weak_ptr< UsermUser
 
bool mOAuthIssue
 Whether or not the internal updates of the component are on hold because of an issue with the OAuth token. More...
 
ClientServerValue< StatemState
 
std::shared_ptr< TaskRunnermTaskRunner
 
std::vector< std::shared_ptr< Task > > mRunningTasks
 
std::mutex mTaskMutex
 
uint64_t mShutdownTimeMilliseconds
 

Private Member Functions

void OnReconnectReceived (PubSubClientConnection *connection)
 
void OnConnectionStateChanged (PubSubClientConnection *connection, PubSubState state, TTV_ErrorCode ec)
 
void OnTopicSubscriptionChanged (PubSubClientConnection *connection, const std::string &topic, PubSubClientConnection::TopicSubscriptionState::Enum state, TTV_ErrorCode ec)
 
void OnTopicMessageReceived (PubSubClientConnection *connection, const std::string &topic, const json::Value &message)
 
void OnPongTimeout (PubSubClientConnection *connection)
 
void OnAuthenticationEror (PubSubClientConnection *connection, TTV_ErrorCode ec, const std::shared_ptr< const OAuthToken > &authToken)
 
virtual bool CheckShutdown () override
 
virtual void CompleteShutdown () override
 
bool AnyConnected () const
 
bool MainConnected () const
 
void ThreadProc ()
 
void SetConnectionState (PubSubState state, TTV_ErrorCode ec)
 
void UpdateTopicSubscription (const std::string &topic)
 
void PerformReconnect ()
 
TTV_ErrorCode SyncTopicSubscriptions ()
 
SubscribeState::Enum GetEffectiveTopicState (const std::string &topic)
 
TTV_ErrorCode ProcessRequestQueue ()
 
TTV_ErrorCode ProcessConnectionPreference (std::shared_ptr< pubsub::ConnectionPreferenceServerMessage > msg)
 
TTV_ErrorCode ProcessConnect (std::shared_ptr< pubsub::ConnectServerMessage > msg)
 
TTV_ErrorCode ProcessDisconnect (std::shared_ptr< pubsub::DisconnectServerMessage > msg)
 
TTV_ErrorCode ProcessSendMessage (std::shared_ptr< pubsub::SendMessageServerMessage > msg)
 
TTV_ErrorCode ProcessSubscribeToTopic (std::shared_ptr< pubsub::SubscribeToTopicServerMessage > msg)
 
TTV_ErrorCode ProcessUnsubscribeFromTopic (std::shared_ptr< pubsub::UnsubscribeFromTopicServerMessage > msg)
 
TTV_ErrorCode ProcessShutdown (std::shared_ptr< pubsub::ShutdownServerMessage > msg)
 
TTV_ErrorCode PerformDisconnect ()
 
TTV_ErrorCode ScheduleConnect (TTV_ErrorCode ec)
 
TTV_ErrorCode AttemptConnection ()
 

Private Attributes

ConcurrentQueue< std::shared_ptr< pubsub::ClientMessage > > mIncomingQueue
 
ConcurrentQueue< std::shared_ptr< pubsub::ServerMessage > > mOutgoingQueue
 
std::map< std::string, std::shared_ptr< Topic > > mTopics
 
std::shared_ptr< IThreadmThread
 
std::shared_ptr< ConnectionListenermConnectionListener
 
std::shared_ptr< PubSubClientConnectionmConnection
 The active connection. More...
 
std::shared_ptr< PubSubClientConnectionmDyingConnection
 The connection that might be alive for a short time after a RECONNECT is received from the server. More...
 
EventSource< IListenermListeners
 
json::FastWriter mJsonWriter
 
RetryTimer mConnectionRetryTimer
 
ClientServerValue< PubSubStatemConnectionState
 
ConnectionPreference::Enum mConnectionPreference
 
bool mConnectionDesired
 Whether or not the client has called Connect(). More...
 

Detailed Description

Manages the pubsub connection for a user.

Features

Setup 1) Initialize() 2) Connect() and wait for stat callback indicating success.

Listening 1) Subscribe to a topic via AddTopicListener() and wait for a ITopicListener::OnTopicSubscribeStateChanged event with SubscribeState::Subscribed and TTV_EC_SUCCESS. 2) Once subscribed ITopicListener::OnTopicMessageReceived will fire events as they are received for that topic.

Unlistening 1) Unsubscribe from a topic via RemoveTopicListener() and wait for ITopicListener::OnTopicSubscribeStateChanged event with SubscribeState::Unsubscribed and TTV_EC_SUCCESS.

Sending messages 1) SendMessage() with the desired JSON data and an optional callback function. 2) Wait for the callback function for the result.

Disconnections When the connection to pubsub is dropped unexpecedly then all IListeners will be notified via IListener::OnStateChanged. If automatic connection retries are happening then the state will be TTV_CORE_PUBSUB_STATE_CONNECTING. Otherwise, it will be TTV_CORE_PUBSUB_STATE_DISCONNECTED.

Registered topic listeners will be notified via ITopicListener::OnTopicSubscribeStateChanged. If a connection retry attempt is underway then the subscription state reported will be Subscribing, otherwise it will be Unsubscribed. Topic listeners are not removed from the subscription of a topic until they receive ITopicListener::OnTopicSubscribeStateChanged event with SubscribeState::Unsubscribed and TTV_EC_SUCCESS. Any other error code means that it was an issue with the connection and retries will occur automatically.


Class Documentation

◆ ttv::PubSubClient::TopicState

struct ttv::PubSubClient::TopicState
Class Members
Enum state The current state of the connection.

Member Typedef Documentation

◆ SendMessageCallback

typedef std::function<void(PubSubClient* source, TTV_ErrorCode ec)> ttv::PubSubClient::SendMessageCallback

Member Enumeration Documentation

◆ State

enum ttv::IComponent::State
stronginherited
Enumerator
Uninitialized 
Initialized 
ShuttingDown 
Inert 

The component doesn't require shutting down.

Constructor & Destructor Documentation

◆ PubSubClient()

ttv::PubSubClient::PubSubClient ( std::shared_ptr< User user)

◆ ~PubSubClient()

virtual ttv::PubSubClient::~PubSubClient ( )
virtual

Member Function Documentation

◆ AddListener()

void ttv::PubSubClient::AddListener ( std::shared_ptr< IListener listener)

◆ AddTopicListener()

TTV_ErrorCode ttv::PubSubClient::AddTopicListener ( const std::string &  topic,
std::shared_ptr< ITopicListener listener 
)

◆ AnyConnected()

bool ttv::PubSubClient::AnyConnected ( ) const
private

◆ AttemptConnection()

TTV_ErrorCode ttv::PubSubClient::AttemptConnection ( )
private

◆ CheckShutdown()

virtual bool ttv::PubSubClient::CheckShutdown ( )
overrideprivatevirtual

Reimplemented from ttv::Component.

◆ CompleteShutdown()

virtual void ttv::PubSubClient::CompleteShutdown ( )
overrideprivatevirtual

Reimplemented from ttv::UserComponent.

◆ CompleteTask()

void ttv::Component::CompleteTask ( Task task)
protectedinherited

◆ Connect()

virtual TTV_ErrorCode ttv::PubSubClient::Connect ( )
virtual

◆ Disconnect()

TTV_ErrorCode ttv::PubSubClient::Disconnect ( )

◆ GetComponentName()

static std::string ttv::PubSubClient::GetComponentName ( )
inlinestatic

◆ GetConnectionState()

PubSubState ttv::PubSubClient::GetConnectionState ( ) const
inline

◆ GetEffectiveTopicState()

SubscribeState::Enum ttv::PubSubClient::GetEffectiveTopicState ( const std::string &  topic)
private

◆ GetLoggerName()

virtual std::string ttv::PubSubClient::GetLoggerName ( ) const
overridevirtual

Implements ttv::Component.

◆ GetState()

virtual State ttv::Component::GetState ( ) const
overridevirtualinherited

Implements ttv::IComponent.

◆ GetTaskRunner()

std::shared_ptr<TaskRunner> ttv::Component::GetTaskRunner ( )
inlineinherited

◆ GetUser()

std::shared_ptr<User> ttv::PubSubClient::GetUser ( )

◆ Initialize()

TTV_ErrorCode ttv::PubSubClient::Initialize ( )
overridevirtual

Reimplemented from ttv::UserComponent.

◆ IsTaskRunning()

bool ttv::Component::IsTaskRunning ( Task task) const
protectedinherited

◆ Log()

void ttv::UserComponent::Log ( TTV_MessageLevel  level,
const char *  format,
  ... 
)
protectedinherited

◆ MainConnected()

bool ttv::PubSubClient::MainConnected ( ) const
private

◆ OnAuthenticationEror()

void ttv::PubSubClient::OnAuthenticationEror ( PubSubClientConnection connection,
TTV_ErrorCode  ec,
const std::shared_ptr< const OAuthToken > &  authToken 
)
private

◆ OnConnectionStateChanged()

void ttv::PubSubClient::OnConnectionStateChanged ( PubSubClientConnection connection,
PubSubState  state,
TTV_ErrorCode  ec 
)
private

◆ OnPongTimeout()

void ttv::PubSubClient::OnPongTimeout ( PubSubClientConnection connection)
private

◆ OnReconnectReceived()

void ttv::PubSubClient::OnReconnectReceived ( PubSubClientConnection connection)
private

◆ OnTopicMessageReceived()

void ttv::PubSubClient::OnTopicMessageReceived ( PubSubClientConnection connection,
const std::string &  topic,
const json::Value message 
)
private

◆ OnTopicSubscriptionChanged()

void ttv::PubSubClient::OnTopicSubscriptionChanged ( PubSubClientConnection connection,
const std::string &  topic,
PubSubClientConnection::TopicSubscriptionState::Enum  state,
TTV_ErrorCode  ec 
)
private

◆ OnUserAuthenticationIssue()

virtual void ttv::UserComponent::OnUserAuthenticationIssue ( std::shared_ptr< const OAuthToken oauthToken,
TTV_ErrorCode  ec 
)
protectedvirtualinherited

◆ OnUserInfoFetchComplete()

virtual void ttv::UserComponent::OnUserInfoFetchComplete ( TTV_ErrorCode  ec)
protectedvirtualinherited

◆ OnUserLogInComplete()

virtual void ttv::UserComponent::OnUserLogInComplete ( TTV_ErrorCode  ec)
protectedvirtualinherited

◆ OnUserLogOutComplete()

virtual void ttv::UserComponent::OnUserLogOutComplete ( TTV_ErrorCode  ec)
protectedvirtualinherited

◆ PerformDisconnect()

TTV_ErrorCode ttv::PubSubClient::PerformDisconnect ( )
private

◆ PerformReconnect()

void ttv::PubSubClient::PerformReconnect ( )
private

◆ ProcessConnect()

TTV_ErrorCode ttv::PubSubClient::ProcessConnect ( std::shared_ptr< pubsub::ConnectServerMessage msg)
private

◆ ProcessConnectionPreference()

TTV_ErrorCode ttv::PubSubClient::ProcessConnectionPreference ( std::shared_ptr< pubsub::ConnectionPreferenceServerMessage msg)
private

◆ ProcessDisconnect()

TTV_ErrorCode ttv::PubSubClient::ProcessDisconnect ( std::shared_ptr< pubsub::DisconnectServerMessage msg)
private

◆ ProcessRequestQueue()

TTV_ErrorCode ttv::PubSubClient::ProcessRequestQueue ( )
private

◆ ProcessSendMessage()

TTV_ErrorCode ttv::PubSubClient::ProcessSendMessage ( std::shared_ptr< pubsub::SendMessageServerMessage msg)
private

◆ ProcessShutdown()

TTV_ErrorCode ttv::PubSubClient::ProcessShutdown ( std::shared_ptr< pubsub::ShutdownServerMessage msg)
private

◆ ProcessSubscribeToTopic()

TTV_ErrorCode ttv::PubSubClient::ProcessSubscribeToTopic ( std::shared_ptr< pubsub::SubscribeToTopicServerMessage msg)
private

◆ ProcessUnsubscribeFromTopic()

TTV_ErrorCode ttv::PubSubClient::ProcessUnsubscribeFromTopic ( std::shared_ptr< pubsub::UnsubscribeFromTopicServerMessage msg)
private

◆ RemoveListener()

void ttv::PubSubClient::RemoveListener ( std::shared_ptr< IListener listener)

◆ RemoveTopicListener()

TTV_ErrorCode ttv::PubSubClient::RemoveTopicListener ( const std::string &  topic,
std::shared_ptr< ITopicListener listener 
)

◆ ScheduleConnect()

TTV_ErrorCode ttv::PubSubClient::ScheduleConnect ( TTV_ErrorCode  ec)
private

◆ SendMessage()

TTV_ErrorCode ttv::PubSubClient::SendMessage ( const json::Value jMessage,
SendMessageCallback  callback 
)

◆ SetClientState()

virtual void ttv::Component::SetClientState ( State  state)
protectedvirtualinherited

◆ SetConnectionPreference()

TTV_ErrorCode ttv::PubSubClient::SetConnectionPreference ( ConnectionPreference::Enum  preference)

◆ SetConnectionState()

void ttv::PubSubClient::SetConnectionState ( PubSubState  state,
TTV_ErrorCode  ec 
)
private

◆ SetServerState()

virtual void ttv::Component::SetServerState ( State  state)
protectedvirtualinherited

◆ SetState()

virtual void ttv::Component::SetState ( State  state)
protectedvirtualinherited

◆ SetTaskRunner()

virtual void ttv::Component::SetTaskRunner ( std::shared_ptr< TaskRunner taskRunner)
virtualinherited

◆ Shutdown()

TTV_ErrorCode ttv::PubSubClient::Shutdown ( )
overridevirtual

Reimplemented from ttv::Component.

◆ StartTask()

TTV_ErrorCode ttv::Component::StartTask ( std::shared_ptr< Task task)
protectedinherited

◆ SyncTopicSubscriptions()

TTV_ErrorCode ttv::PubSubClient::SyncTopicSubscriptions ( )
private

◆ ThreadProc()

void ttv::PubSubClient::ThreadProc ( )
private

◆ Update()

virtual void ttv::PubSubClient::Update ( )
overridevirtual

Reimplemented from ttv::Component.

◆ UpdateTopicSubscription()

void ttv::PubSubClient::UpdateTopicSubscription ( const std::string &  topic)
private

Member Data Documentation

◆ mConnection

std::shared_ptr<PubSubClientConnection> ttv::PubSubClient::mConnection
private

The active connection.

◆ mConnectionDesired

bool ttv::PubSubClient::mConnectionDesired
private

Whether or not the client has called Connect().

◆ mConnectionListener

std::shared_ptr<ConnectionListener> ttv::PubSubClient::mConnectionListener
private

◆ mConnectionPreference

ConnectionPreference::Enum ttv::PubSubClient::mConnectionPreference
private

◆ mConnectionRetryTimer

RetryTimer ttv::PubSubClient::mConnectionRetryTimer
private

◆ mConnectionState

ClientServerValue<PubSubState> ttv::PubSubClient::mConnectionState
private

◆ mDyingConnection

std::shared_ptr<PubSubClientConnection> ttv::PubSubClient::mDyingConnection
private

The connection that might be alive for a short time after a RECONNECT is received from the server.

◆ mIncomingQueue

ConcurrentQueue< std::shared_ptr<pubsub::ClientMessage> > ttv::PubSubClient::mIncomingQueue
private

◆ mJsonWriter

json::FastWriter ttv::PubSubClient::mJsonWriter
private

◆ mListeners

EventSource<IListener> ttv::PubSubClient::mListeners
private

◆ mOAuthIssue

bool ttv::UserComponent::mOAuthIssue
protectedinherited

Whether or not the internal updates of the component are on hold because of an issue with the OAuth token.

◆ mOutgoingQueue

ConcurrentQueue< std::shared_ptr<pubsub::ServerMessage> > ttv::PubSubClient::mOutgoingQueue
private

◆ mRunningTasks

std::vector<std::shared_ptr<Task> > ttv::Component::mRunningTasks
protectedinherited

◆ mShutdownTimeMilliseconds

uint64_t ttv::Component::mShutdownTimeMilliseconds
protectedinherited

◆ mState

ClientServerValue<State> ttv::Component::mState
protectedinherited

◆ mTaskMutex

std::mutex ttv::Component::mTaskMutex
mutableprotectedinherited

◆ mTaskRunner

std::shared_ptr<TaskRunner> ttv::Component::mTaskRunner
protectedinherited

◆ mThread

std::shared_ptr<IThread> ttv::PubSubClient::mThread
private

◆ mTopics

std::map<std::string, std::shared_ptr<Topic> > ttv::PubSubClient::mTopics
private

◆ mUser

std::weak_ptr<User> ttv::UserComponent::mUser
protectedinherited

The documentation for this class was generated from the following file: