Twitch SDK (Internal)
chatconnection.h
Go to the documentation of this file.
1 /********************************************************************************************
2 * Twitch Broadcasting SDK
3 *
4 * This software is supplied under the terms of a license agreement with Twitch Interactive, Inc. and
5 * may not be copied or used except in accordance with the terms of that agreement
6 * Copyright (c) 2012-2016 Twitch Interactive, Inc.
7 *********************************************************************************************/
8 
9 #pragma once
10 
13 
16 
17 namespace ttv
18 {
19  class User;
20 
21  namespace chat
22  {
23  class ChatConnection;
24  class ChatConnectionListenerProxy;
25 
26  class ChatAPI;
27  class ChatChannel;
28  class ChatSession;
29  class ChatReader;
30  class ChatWriter;
31  class IChatTransport;
32  class IChatObjectFactory;
33  }
34 }
35 
36 
41 {
42 public:
43  class Listener
44  {
45  public:
46  virtual void OnConnected(ChatConnection* source) = 0;
47  virtual void OnConnectionFailed(ChatConnection* source, TTV_ErrorCode ec) = 0;
48  virtual void OnConnectionLost(ChatConnection* source, TTV_ErrorCode ec) = 0;
49  virtual void OnCleared(ChatConnection* source, const std::string& username, const std::map<std::string, std::string>& messageTags) = 0;
50  virtual void OnPrivateMessageReceived(ChatConnection* source, const std::string& username, const std::string& message, const std::map<std::string, std::string>& messageTags, bool action) = 0;
51  virtual void OnUserNoticeReceived(ChatConnection* source, const std::string& message, const std::map<std::string, std::string>& messageTags) = 0;
52  virtual void OnUserStateChanged(ChatConnection* source, const std::map<std::string, std::string>& messageTags) = 0;
53  virtual void OnChatRestrictionsChanged(ChatConnection* source, const ChatChannelRestrictions& restrictions) = 0;
54  virtual void OnPermanentBanChanged(ChatConnection* source, bool banned) = 0;
55  virtual void OnTemporaryBanChanged(ChatConnection* source, bool temporarilyBanned, uint32_t timeout) = 0;
56  virtual void OnBadgesChanged(ChatConnection* source, const std::string& username, const std::string& badgesMessageTag) = 0;
57  virtual void OnHostTargetChanged(ChatConnection* source, const std::string& targetChannel, uint32_t numViewers) = 0;
58  virtual void OnNoticeReceived(ChatConnection* source, const std::string& id, const std::map<std::string, std::string>& params) = 0;
59  virtual void OnIgnoreChanged(ChatConnection* source, const std::string& blockUserName, bool ignore) = 0;
60  };
61 
62 private:
64  {
73  };
74 
75 public:
76  ChatConnection(ChannelId channelId, const std::shared_ptr<User>& user);
77  virtual ~ChatConnection();
78 
79  void SetListener(Listener* listener) { mListener = listener; }
80  void SetChatObjectFactory(std::shared_ptr<IChatObjectFactory> factory);
81  void SetChannelName(const std::string& channelName);
82 
86  TTV_ErrorCode Connect(const std::string& uri);
94  TTV_ErrorCode SendChatMessage(const std::string& message, const ChatUserInfo& localUserInfo);
95 
97 
98  std::string GetLocalUserName() const { return mUserName; }
99 
100  // IChatReceiveNetworkEvent implementation
101  virtual void ReceiveEvent(const ChatNetworkEvent& evt);
102 
103  static TTV_ErrorCode CheckFactoryAvailability(const std::string& uri);
104 
105 private:
106  void SetState(ConnectionState state);
107 
108  void Join();
109 
110  void HandleCapMessage(const ChatNetworkEvent& evt);
114  void HandlePrivateMessage(const ChatNetworkEvent& evt);
115  void HandleUserState(const ChatNetworkEvent& evt);
116  void HandleNotice(const ChatNetworkEvent& evt);
117  void HandleClearChatMessage(const ChatNetworkEvent& evt);
118  void HandleHostTargetMessage(const ChatNetworkEvent& evt);
119  void HandleUserNotice(const ChatNetworkEvent& evt);
120 
121  void HandleMessageTags(const ChatNetworkEvent& evt);
122  void HandleRoomState(const ChatNetworkEvent& evt);
123 
124  void CreateObjects();
125  void ReleaseObjects();
126 
127  std::string CreateAnonymousUserName();
128 
129  std::shared_ptr<ChatReader> mReader;
130  std::shared_ptr<ChatWriter> mWriter;
131  std::shared_ptr<ChatSession> mSession;
132  std::shared_ptr<IChatObjectFactory> mChatObjectFactory;
133 
134  std::shared_ptr<IChatTransport> mTransport;
135 
138 
139  std::string mIrcChannelName;
140  std::string mUserName;
141 
145 
146  std::shared_ptr<User> mUser;
147 
150 
151  bool mAnonymous;
154 };
155 
156 
161 {
162 public:
163  typedef std::function<void(ChatConnection* source)> OnConnectedFunc;
164  typedef std::function<void(ChatConnection* source, TTV_ErrorCode ec)> OnConnectionFailedFunc;
165  typedef std::function<void(ChatConnection* source, TTV_ErrorCode ec)> OnConnectionLostFunc;
166  typedef std::function<void(ChatConnection* source, const std::string& username, const std::map<std::string, std::string>& messageTags)> OnClearedFunc;
167  typedef std::function<void(ChatConnection* source, const std::string& username, const std::string& message, const std::map<std::string, std::string>& messageTags, bool action)> OnPrivateMessageReceivedFunc;
168  typedef std::function<void(ChatConnection* source, const std::string& message, const std::map<std::string, std::string>& messageTags)> OnUserNoticeReceivedFunc;
169  typedef std::function<void(ChatConnection* source, const std::map<std::string, std::string>& messageTags)> OnUserStateChangedFunc;
170  typedef std::function<void(ChatConnection* source, const std::string& username, const std::string& message, const std::map<std::string, std::string>& messageTags, bool action)> OnWhisperReceivedFunc;
171  typedef std::function<void(ChatConnection* source, const std::string& receivingUserName, const std::string& message, const std::map<std::string, std::string>& messageTags, bool action)> OnWhisperSentFunc;
172  typedef std::function<void(ChatConnection* source, ChatChannelRestrictions)> OnChatRestrictionsChangedFunc;
173  typedef std::function<void(ChatConnection* source, bool banned)> OnPermanentBanChangedFunc;
174  typedef std::function<void(ChatConnection* source, bool temporarilyBanned, uint32_t timeout)> OnTemporaryBanChangedFunc;
175  typedef std::function<void(ChatConnection* source, const std::string& username, const std::vector<EmoticonSetId>& emoticonSets)> OnEmoticonSetsChangedFunc;
176  typedef std::function<void(ChatConnection* source, const std::string& username, const std::string& badgesMessageTag)> OnBadgesChangedFunc;
177  typedef std::function<void(ChatConnection* source, const std::string& targetChannel, uint32_t numViewers)> OnHostTargetChangedFunc;
178  typedef std::function<void(ChatConnection* source, const std::string& id, const std::map<std::string, std::string>& params)> OnNoticeReceivedFunc;
179  typedef std::function<void(ChatConnection* source, const std::string& blockUserName, bool ignore)> OnIgnoreChangedFunc;
180 
181 public:
182  virtual void OnConnected(ChatConnection* source) override
183  {
184  if (mOnConnectedFunc != nullptr)
185  {
186  mOnConnectedFunc(source);
187  }
188  }
189 
190  virtual void OnConnectionFailed(ChatConnection* source, TTV_ErrorCode ec) override
191  {
192  if (mOnConnectionFailedFunc != nullptr)
193  {
194  mOnConnectionFailedFunc(source, ec);
195  }
196  }
197 
198  virtual void OnConnectionLost(ChatConnection* source, TTV_ErrorCode ec) override
199  {
200  if (mOnConnectionLostFunc != nullptr)
201  {
202  mOnConnectionLostFunc(source, ec);
203  }
204  }
205 
206  virtual void OnCleared(ChatConnection* source, const std::string& username, const std::map<std::string, std::string>& messageTags) override
207  {
208  if (mOnClearedFunc != nullptr)
209  {
210  mOnClearedFunc(source, username, messageTags);
211  }
212  }
213 
214  virtual void OnPrivateMessageReceived(ChatConnection* source, const std::string& username, const std::string& message, const std::map<std::string, std::string>& messageTags, bool action) override
215  {
216  if (mOnPrivateMessageReceivedFunc != nullptr)
217  {
218  mOnPrivateMessageReceivedFunc(source, username, message, messageTags, action);
219  }
220  }
221 
222  virtual void OnUserNoticeReceived(ChatConnection* source, const std::string& message, const std::map<std::string, std::string>& messageTags) override
223  {
224  if (mOnUserNoticeReceivedFunc != nullptr)
225  {
226  mOnUserNoticeReceivedFunc(source, message, messageTags);
227  }
228  }
229 
230  virtual void OnUserStateChanged(ChatConnection* source, const std::map<std::string, std::string>& messageTags) override
231  {
232  if (mOnUserStateChangedFunc != nullptr)
233  {
234  mOnUserStateChangedFunc(source, messageTags);
235  }
236  }
237 
238  virtual void OnChatRestrictionsChanged(ChatConnection* source, const ChatChannelRestrictions& restrictions) override
239  {
240  if (mOnChatRestrictionsChangedFunc != nullptr)
241  {
242  mOnChatRestrictionsChangedFunc(source, restrictions);
243  }
244  }
245 
246  virtual void OnPermanentBanChanged(ChatConnection* source, bool banned) override
247  {
248  if (mOnPermanentBanChangedFunc != nullptr)
249  {
250  mOnPermanentBanChangedFunc(source, banned);
251  }
252  }
253 
254  virtual void OnTemporaryBanChanged(ChatConnection* source, bool temporarilyBanned, uint32_t timeout) override
255  {
256  if (mOnTemporaryBanChangedFunc != nullptr)
257  {
258  mOnTemporaryBanChangedFunc(source, temporarilyBanned, timeout);
259  }
260  }
261 
262  virtual void OnBadgesChanged(ChatConnection* source, const std::string& username, const std::string& badgesMessageTag) override
263  {
264  if (mOnBadgesChangedFunc != nullptr)
265  {
266  mOnBadgesChangedFunc(source, username, badgesMessageTag);
267  }
268  }
269 
270  virtual void OnHostTargetChanged(ChatConnection* source, const std::string& targetChannel, uint32_t numViewers) override
271  {
272  if (mOnHostTargetChangedFunc != nullptr)
273  {
274  mOnHostTargetChangedFunc(source, targetChannel, numViewers);
275  }
276  }
277 
278  virtual void OnNoticeReceived(ChatConnection* source, const std::string& id, const std::map<std::string, std::string>& params) override
279  {
280  if (mOnNoticeReceivedFunc != nullptr)
281  {
282  mOnNoticeReceivedFunc(source, id, params);
283  }
284  }
285 
286  virtual void OnIgnoreChanged(ChatConnection* source, const std::string& blockUserName, bool ignore) override
287  {
288  if (mOnIgnoreChangedFunc != nullptr)
289  {
290  mOnIgnoreChangedFunc(source, blockUserName, ignore);
291  }
292  }
293 
294 public:
295  OnConnectedFunc mOnConnectedFunc;
296  OnConnectionFailedFunc mOnConnectionFailedFunc;
297  OnConnectionLostFunc mOnConnectionLostFunc;
304  OnChatRestrictionsChangedFunc mOnChatRestrictionsChangedFunc;
305  OnPermanentBanChangedFunc mOnPermanentBanChangedFunc;
306  OnTemporaryBanChangedFunc mOnTemporaryBanChangedFunc;
307  OnBadgesChangedFunc mOnBadgesChangedFunc;
308  OnHostTargetChangedFunc mOnHostTargetChangedFunc;
310  OnIgnoreChangedFunc mOnIgnoreChangedFunc;
311 };
virtual void OnConnectionLost(ChatConnection *source, TTV_ErrorCode ec) override
Definition: chatconnection.h:198
void SetChannelName(const std::string &channelName)
std::function< void(ChatConnection *source, TTV_ErrorCode ec)> OnConnectionFailedFunc
Definition: chatconnection.h:164
std::string CreateAnonymousUserName()
bool mAnonymous
Whether or not connecting anonymously to the channel.
Definition: chatconnection.h:151
ConnectionState mConnectionState
Definition: chatconnection.h:142
virtual void OnNoticeReceived(ChatConnection *source, const std::string &id, const std::map< std::string, std::string > &params) override
Definition: chatconnection.h:278
ChatConnection(ChannelId channelId, const std::shared_ptr< User > &user)
virtual void OnTemporaryBanChanged(ChatConnection *source, bool temporarilyBanned, uint32_t timeout) override
Definition: chatconnection.h:254
virtual void OnConnectionFailed(ChatConnection *source, TTV_ErrorCode ec)=0
virtual void OnConnectionLost(ChatConnection *source, TTV_ErrorCode ec)=0
std::function< void(ChatConnection *source, bool banned)> OnPermanentBanChangedFunc
Definition: chatconnection.h:173
std::function< void(ChatConnection *source)> OnConnectedFunc
Definition: chatconnection.h:163
std::string mUserName
The cached username.
Definition: chatconnection.h:140
virtual void OnConnectionFailed(ChatConnection *source, TTV_ErrorCode ec) override
Definition: chatconnection.h:190
std::function< void(ChatConnection *source, const std::string &username, const std::string &message, const std::map< std::string, std::string > &messageTags, bool action)> OnWhisperReceivedFunc
Definition: chatconnection.h:170
std::string mIrcChannelName
Definition: chatconnection.h:139
std::function< void(ChatConnection *source, const std::string &blockUserName, bool ignore)> OnIgnoreChangedFunc
Definition: chatconnection.h:179
virtual void OnPermanentBanChanged(ChatConnection *source, bool banned)=0
ConnectionState
Definition: chatconnection.h:63
virtual void OnIgnoreChanged(ChatConnection *source, const std::string &blockUserName, bool ignore) override
Definition: chatconnection.h:286
Definition: chatconnection.h:40
virtual void OnPrivateMessageReceived(ChatConnection *source, const std::string &username, const std::string &message, const std::map< std::string, std::string > &messageTags, bool action) override
Definition: chatconnection.h:214
Definition: chattypes.h:169
OnTemporaryBanChangedFunc mOnTemporaryBanChangedFunc
Definition: chatconnection.h:306
OnNoticeReceivedFunc mOnNoticeReceivedFunc
Definition: chatconnection.h:309
Listener * mListener
Definition: chatconnection.h:143
virtual void OnNoticeReceived(ChatConnection *source, const std::string &id, const std::map< std::string, std::string > &params)=0
TTV_ErrorCode SendChatMessage(const std::string &message, const ChatUserInfo &localUserInfo)
Definition: chatconnection.h:43
std::function< void(ChatConnection *source, const std::string &receivingUserName, const std::string &message, const std::map< std::string, std::string > &messageTags, bool action)> OnWhisperSentFunc
Definition: chatconnection.h:171
OnHostTargetChangedFunc mOnHostTargetChangedFunc
Definition: chatconnection.h:308
void SetListener(Listener *listener)
Definition: chatconnection.h:79
OnWhisperReceivedFunc mOnWhisperReceivedFunc
Definition: chatconnection.h:302
std::function< void(ChatConnection *source, const std::string &targetChannel, uint32_t numViewers)> OnHostTargetChangedFunc
Definition: chatconnection.h:177
void HandleHostTargetMessage(const ChatNetworkEvent &evt)
std::function< void(ChatConnection *source, const std::string &username, const std::string &badgesMessageTag)> OnBadgesChangedFunc
Definition: chatconnection.h:176
void SetState(ConnectionState state)
TTV_ErrorCode Connect(const std::string &uri)
JSON (JavaScript Object Notation).
Definition: adsapi.h:16
OnWhisperSentFunc mOnWhisperSentFunc
Definition: chatconnection.h:303
TTV_ErrorCode Update()
std::function< void(ChatConnection *source, bool temporarilyBanned, uint32_t timeout)> OnTemporaryBanChangedFunc
Definition: chatconnection.h:174
virtual void OnUserStateChanged(ChatConnection *source, const std::map< std::string, std::string > &messageTags)=0
void HandleUserNotice(const ChatNetworkEvent &evt)
std::shared_ptr< ChatWriter > mWriter
The socket writer.
Definition: chatconnection.h:130
OnUserNoticeReceivedFunc mOnUserNoticeReceivedFunc
Definition: chatconnection.h:300
Definition: timer.h:26
void HandleMessageTags(const ChatNetworkEvent &evt)
void HandleClearChatMessage(const ChatNetworkEvent &evt)
std::shared_ptr< IChatTransport > mTransport
The transport for sending and receiving chat messages.
Definition: chatconnection.h:134
std::function< void(ChatConnection *source, const std::string &username, const std::string &message, const std::map< std::string, std::string > &messageTags, bool action)> OnPrivateMessageReceivedFunc
Definition: chatconnection.h:167
std::function< void(ChatConnection *source, const std::string &username, const std::map< std::string, std::string > &messageTags)> OnClearedFunc
Definition: chatconnection.h:166
virtual void OnUserStateChanged(ChatConnection *source, const std::map< std::string, std::string > &messageTags) override
Definition: chatconnection.h:230
virtual void OnChatRestrictionsChanged(ChatConnection *source, const ChatChannelRestrictions &restrictions)=0
std::function< void(ChatConnection *source, const std::string &id, const std::map< std::string, std::string > &params)> OnNoticeReceivedFunc
Definition: chatconnection.h:178
void HandlePrivateMessage(const ChatNetworkEvent &evt)
std::shared_ptr< IChatObjectFactory > mChatObjectFactory
The internal chat object factory.
Definition: chatconnection.h:132
OnPrivateMessageReceivedFunc mOnPrivateMessageReceivedFunc
Definition: chatconnection.h:299
virtual void OnCleared(ChatConnection *source, const std::string &username, const std::map< std::string, std::string > &messageTags)=0
uint32_t TTV_ErrorCode
Definition: errortypes.h:30
OnBadgesChangedFunc mOnBadgesChangedFunc
Definition: chatconnection.h:307
OnChatRestrictionsChangedFunc mOnChatRestrictionsChangedFunc
Definition: chatconnection.h:304
virtual void OnPermanentBanChanged(ChatConnection *source, bool banned) override
Definition: chatconnection.h:246
virtual void OnTemporaryBanChanged(ChatConnection *source, bool temporarilyBanned, uint32_t timeout)=0
bool mFireConnectionErrorEvents
Whether or not to fire a connection error event.
Definition: chatconnection.h:153
OnUserStateChangedFunc mOnUserStateChangedFunc
Definition: chatconnection.h:301
bool mDisconnectionRequested
Whether or not expecting a disconnect.
Definition: chatconnection.h:152
TTV_ErrorCode Disconnect()
std::string GetLocalUserName() const
Definition: chatconnection.h:98
std::shared_ptr< ChatReader > mReader
The socket reader.
Definition: chatconnection.h:129
Definition: chatconnection.h:160
OnClearedFunc mOnClearedFunc
Definition: chatconnection.h:298
OnConnectionLostFunc mOnConnectionLostFunc
Definition: chatconnection.h:297
virtual void OnConnected(ChatConnection *source) override
Definition: chatconnection.h:182
OnIgnoreChangedFunc mOnIgnoreChangedFunc
Definition: chatconnection.h:310
WaitForEventWithTimeout mWelcomeMessageTimeout
Definition: chatconnection.h:136
std::shared_ptr< User > mUser
Definition: chatconnection.h:146
std::function< void(ChatConnection *source, const std::map< std::string, std::string > &messageTags)> OnUserStateChangedFunc
Definition: chatconnection.h:169
OnPermanentBanChangedFunc mOnPermanentBanChangedFunc
Definition: chatconnection.h:305
virtual void ReceiveEvent(const ChatNetworkEvent &evt)
WaitForEventWithTimeout mJoinAttemptTimeout
Definition: chatconnection.h:137
virtual void OnIgnoreChanged(ChatConnection *source, const std::string &blockUserName, bool ignore)=0
virtual void OnBadgesChanged(ChatConnection *source, const std::string &username, const std::string &badgesMessageTag) override
Definition: chatconnection.h:262
void HandleCapMessage(const ChatNetworkEvent &evt)
void HandleRoomState(const ChatNetworkEvent &evt)
void HandleNotice(const ChatNetworkEvent &evt)
OnConnectionFailedFunc mOnConnectionFailedFunc
Definition: chatconnection.h:296
std::function< void(ChatConnection *source, const std::string &message, const std::map< std::string, std::string > &messageTags)> OnUserNoticeReceivedFunc
Definition: chatconnection.h:168
uint32_t ChannelId
Definition: coretypes.h:23
virtual void OnUserNoticeReceived(ChatConnection *source, const std::string &message, const std::map< std::string, std::string > &messageTags)=0
virtual void OnChatRestrictionsChanged(ChatConnection *source, const ChatChannelRestrictions &restrictions) override
Definition: chatconnection.h:238
virtual void OnHostTargetChanged(ChatConnection *source, const std::string &targetChannel, uint32_t numViewers) override
Definition: chatconnection.h:270
virtual void OnUserNoticeReceived(ChatConnection *source, const std::string &message, const std::map< std::string, std::string > &messageTags) override
Definition: chatconnection.h:222
Definition: chattypes.h:126
std::shared_ptr< ChatSession > mSession
The message interpreter.
Definition: chatconnection.h:131
virtual void OnBadgesChanged(ChatConnection *source, const std::string &username, const std::string &badgesMessageTag)=0
void HandleUserState(const ChatNetworkEvent &evt)
TTV_ErrorCode mConnectionError
Definition: chatconnection.h:144
std::function< void(ChatConnection *source, TTV_ErrorCode ec)> OnConnectionLostFunc
Definition: chatconnection.h:165
ChatChannelRestrictions mChatRestrictions
Stores the state of chat restrictions received by ROOMSTATE.
Definition: chatconnection.h:149
virtual void OnPrivateMessageReceived(ChatConnection *source, const std::string &username, const std::string &message, const std::map< std::string, std::string > &messageTags, bool action)=0
Definition: chatnetworkevent.h:86
void SetChatObjectFactory(std::shared_ptr< IChatObjectFactory > factory)
virtual void OnHostTargetChanged(ChatConnection *source, const std::string &targetChannel, uint32_t numViewers)=0
ChannelId mChannelId
Definition: chatconnection.h:148
Definition: chatnetworkevent.h:29
OnConnectedFunc mOnConnectedFunc
Definition: chatconnection.h:295
std::function< void(ChatConnection *source, ChatChannelRestrictions)> OnChatRestrictionsChangedFunc
Definition: chatconnection.h:172
static TTV_ErrorCode CheckFactoryAvailability(const std::string &uri)
std::function< void(ChatConnection *source, const std::string &username, const std::vector< EmoticonSetId > &emoticonSets)> OnEmoticonSetsChangedFunc
Definition: chatconnection.h:175
virtual void OnCleared(ChatConnection *source, const std::string &username, const std::map< std::string, std::string > &messageTags) override
Definition: chatconnection.h:206
virtual void OnConnected(ChatConnection *source)=0