Sendbird Chat SDK for Unreal
Sendbird.h
1// Copyright (c) 2021 Sendbird, Inc. All rights reserved.
2
3#ifndef SENDBIRD_SENDBIRD_H_
4#define SENDBIRD_SENDBIRD_H_
5
6#include "SBDAdminMessage.h"
7#include "SBDBaseChannel.h"
8#include "SBDBaseInterface.h"
9#include "SBDBaseMessage.h"
10#include "SBDError.h"
11#include "SBDFileMessage.h"
12#include "SBDFileMessageParams.h"
13#include "SBDGroupChannel.h"
14#include "SBDGroupChannelListQuery.h"
15#include "SBDGroupChannelParams.h"
16#include "SBDGroupChannelTotalUnreadMessageCountParams.h"
17#include "SBDMember.h"
18#include "SBDMessageMetaArray.h"
19#include "SBDOpenChannel.h"
20#include "SBDOpenChannelListQuery.h"
21#include "SBDOpenChannelParams.h"
22#include "SBDOperatorListQuery.h"
23#include "SBDPreviousMessageListQuery.h"
24#include "SBDReadStatus.h"
25#include "SBDTypes.h"
26#include "SBDUser.h"
27#include "SBDUserListQuery.h"
28#include "SBDUserMessage.h"
29#include "SBDUserMessageParams.h"
30
31#include <string>
32#include <sstream>
33#include <ctime>
34#include <map>
35#include <cstdio>
36#include <memory>
37#include <mutex>
38#include <condition_variable>
39#include <codecvt>
40#include <exception>
41
42#define SBD_NULL_STRING "SBD_NULL_STRING_f2648f84e7e941208c8d9df02230a7c8"
43#define SBD_NULL_WSTRING L"SBD_NULL_STRING_f2648f84e7e941208c8d9df02230a7c8"
44#define IsNullString(str) (((str).compare(SBD_NULL_STRING)) == 0)
45#define IsNullWString(str) (((str).compare(SBD_NULL_WSTRING)) == 0)
46
47class SBDWebSocketClient;
48class SBDLoginTimerHandler;
49class SBDUpdateUserInfoWithBinaryFileRequestHandler;
50class SBDWebSocketSendCommandHandler;
51class SBDReconnectTimerHandler;
52class SBDTypingStatusTimerHandler;
53class SBDAckTimerHandler;
54class SBDUnReadMessageCount;
55class FSBDMain;
56class SBDThreadManager;
57
61class SBDOption final {
62public:
68};
69
74public:
75 virtual void Started() {};
76 virtual void Succeeded() {};
77 virtual void Failed() {};
78};
79
86public:
87
95 virtual void TotalUnreadMessageCountChanged(int total_count, std::map<std::wstring, int> total_count_by_custom_type) {}
96};
97
103class SBDMain final {
104public:
110 static std::wstring GetApplicationId();
111
117 static std::wstring GetSdkVersion();
118
124 static bool IsInitialized();
125
131 static void Init(std::wstring application_id);
132
143 static SBDConnectionState GetConnectionState();
144
151 static void AddConnectionHandler(std::wstring identifier, SBDConnectionHandler* handler);
152
158 static void RemoveConnectionHandler(std::wstring identifier);
159
164
172 static void AddUserEventHandler(std::wstring identifier, SBDUserEventHandler* handler);
173
180 static void RemoveUserEventHandler(std::wstring identifier);
181
188
195 static void AddChannelHandler(std::wstring identifier, SBDChannelHandler* handler);
196
202 static void RemoveChannelHandler(std::wstring identifier);
203
208
216 static void Connect(std::wstring user_id, std::wstring access_token, std::function<void(SBDUser*, SBDError*)> completion_handler);
217
223 static void Disconnect(std::function<void()> completion_handler);
224
232
240 static void UpdateCurrentUserInfo(std::wstring nickname, std::wstring profile_url, std::function<void(SBDError*)> completion_handler);
241
250 static void UpdateCurrentUserInfoWithBinaryProfileImage(std::wstring nickname, std::wstring profile_image_file_path, std::wstring mime_type,
251 std::function<void(SBDError*)> completion_handler);
252
259
266 static SBDUserListQuery* CreateUserListQuery(std::vector<std::wstring> user_ids);
267
274
281 static void BlockUser(const SBDUser& user, std::function<void(SBDUser* blocked_user, SBDError*)> completion_handler);
282
289 static void UnblockUser(const SBDUser& user, std::function<void(SBDError*)> completion_handler);
290
296 static bool Reconnect();
297
304 static void SetChannelInvitationPreference(bool auto_accept, std::function<void(SBDError*)> completion_handler);
305
311 static void GetChannelInvitationPreference(std::function<void(bool is_auto_accept, SBDError*)> completion_handler);
312
319 static void GetTotalUnreadChannelCount(std::function<void(int total_unread_channel_count, SBDError*)> completion_handler);
320
328 static void GetTotalUnreadMessageCount(SBDGroupChannelTotalUnreadMessageCountParams& params, std::function<void(int total_unread_message_count, SBDError*)> completion_handler);
329
337
345
353 static int GetSubscribedCustomTypeUnreadMessageCount(std::wstring custom_type);
354
367 static void RegisterPushTokenForCurrentUser(SBDPushTokenType type, std::wstring token, bool unique, std::function<void(SBDPushTokenRegistrationStatus, SBDError*)> completion_handler);
368
375 static std::wstring GetPendingPushToken();
376
385 static void UnregisterPushTokenForCurrentUser(SBDPushTokenType type, std::wstring token, std::function<void(SBDError*)> completion_handler);
386
393 static void UnregisterPushTokenAllForCurrentUser(std::function<void(SBDError*)> completion_handler);
394
395private:
396 std::wstring application_id;
397 SBDUser current_user;
398 std::mutex current_user_mutex;
399
400 std::map<std::wstring, SBDConnectionHandler*> reconnection_handlers;
401 std::mutex reconnection_handlers_mutex;
402 std::map<std::wstring, SBDUserEventHandler*> user_event_handlers;
403 std::mutex user_event_handlers_mutex;
404 std::map<std::wstring, SBDChannelHandler*> channel_handlers;
405 std::mutex channel_handlers_mutex;
406
407 std::unique_ptr<SBDWebSocketClient> websocket_client;
408 std::mutex websocket_client_mutex;
409
410 std::mutex reconnect_in_progress_mutex;
411 bool reconnect_in_progress;
412 SBDReconnectTimerHandler* reconnect_timer = nullptr;
413 std::mutex reconnect_timer_mutex;
414
415 SBDTypingStatusTimerHandler* typing_status_timer = nullptr;
416 std::mutex typing_status_timer_mutex;
417
418 bool session_opened;
419 bool has_current_user;
420
421 std::vector<std::unique_ptr<SBDUserListQuery>> user_list_queries;
422 std::vector<std::unique_ptr<SBDUpdateUserInfoWithBinaryFileRequestHandler>> update_user_info_with_binary_file_request_handlers;
423
424 SBDLoginTimerHandler* login_timer = nullptr;
425 std::mutex login_timer_mutex;
426
427 unsigned long reconnect_delay;
428 uint8_t reconnect_count;
429 std::map<std::string, SBDAckTimerHandler*> ack_timer_map;
430 std::mutex ack_timer_map_mutex;
431
432 std::vector<std::function<void(SBDUser*, SBDError*)>> connect_callbacks;
433 std::mutex connect_callbacks_mutex;
434 std::wstring connecting_user_id;
435
436 std::vector<std::function<void()>> disconnect_callbacks;
437 std::mutex disconnect_callbacks_mutex;
438 bool is_disconnecting;
439
440 std::unique_ptr<SBDUnReadMessageCount> un_read_message_count;
441 std::unique_ptr<SBDThreadManager> thread_manager; // FEATURE_THREAD_MANAGER
442
443 std::wstring pending_push_token;
444
445private:
446 SBDMain();
447 SBDMain(SBDMain const&) = delete;
448 void operator=(SBDMain const&) = delete;
449 void ReceiveMessage(std::string message, bool reconnect);
450 void Send(SBDCommand* command, std::function<void(SBDCommand*, SBDError*)> completion_handler);
451 static SBDMain& GetInstance();
452 static void Disconnect(bool logout, bool from_disconnect, std::function<void()> completion_handler);
453 static void ClearUserData();
454 static void ClearLocalData();
455
456 static SBDConnectionHandler* GetReconnectionHandler(std::wstring identifier);
457 static SBDChannelHandler* GetChannelHandler(std::wstring identifier);
458 static std::map<std::wstring, SBDConnectionHandler*> GetReconnectionHandlers();
459 static std::map<std::wstring, SBDUserEventHandler*> GetUserEventHandlers();
460 static std::map<std::wstring, SBDChannelHandler*> GetChannelHandlers();
461
462 static void ClearCurrentUser();
463 static void ProcessSubscribedUnreadMessageCount(SBDCommand* command);
464 static void ClearUnreadCount();
465 static void Reconnect(unsigned long timeout);
466 static void ConnectToWebSocketServer(bool reconnect,
467 std::wstring user_id, std::wstring access_token, std::string session_key,
468 std::function<void(SBDUser*, SBDError*)> connect_callback);
469 static void SetHosts(std::wstring api_host, std::wstring ws_host);
470 static std::wstring GetApiServer();
471 static std::wstring GetCustomApiServer();
472 static std::wstring GetApiHostScheme();
473 static std::wstring GetApiHost();
474 static int GetApiPort();
475 static std::wstring GetWsServer();
476 static std::wstring GetCustomWsServer();
477 static std::wstring GetWsHostScheme();
478 static std::wstring GetWsHost();
479 static int GetWsPort();
480 static SBDWebSocketClient* GetWebSocket();
481 static bool HasCurrentUser();
482 static std::wstring GetCurrentUserId();
483 static void SetCurrentUser(const SBDUser& user);
484
485 static void StartAckTimer(SBDCommand* command, std::function<void(SBDCommand*, SBDError*)> command_completion_handler);
486 static void RunAckTimerCallback(SBDCommand* command, SBDError* error);
487 static void StopAckTimer(SBDCommand* command);
488 static std::map<std::string, SBDAckTimerHandler*>& GetAckTimerMap();
489 static std::mutex& GetAckTimerMapMutex();
490 static void InvalidateAckTimers();
491 static void ProcessAckTimersToErrors();
492 static void StartLoginTimer(std::wstring user_id, std::function<void(SBDUser*, SBDError*)> completion_handler);
493 static void StopLoginTimer();
494
495 static void IncreaseReconnectCount();
496 static void ResetReconnectCount();
497 static uint8_t GetReconnectCount();
498 static unsigned long GetReconnectDelay();
499 static void IncreaseReconnectDelay();
500 static void ResetReconnectDelay();
501 static void SetReconnectionProgress(bool is_in_progress);
502 static bool GetReconnectionProgress();
503 static void SetReconnectTimer(unsigned long time_interval);
504 static void StopReconnectTimer();
505
506 static void Connect(std::wstring user_id, std::wstring access_token, std::wstring api_host, std::wstring ws_host,
507 std::function<void(SBDUser*, SBDError*)> completion_handler);
508 static void ProcessConnectCallbacks(SBDUser* user, SBDError* error);
509 static void ProcessDisconnectCallbacks(bool logout);
510 static std::wstring GetPlatformName();
511 static std::wstring GetPlatformVersion();
512 static std::wstring GetDeviceOsName();
513 static std::wstring GetDeviceOsVersion();
514 static SBDThreadManager* GetThreadManager();
515
516 friend FSBDMain;
517};
518
519#endif /* SENDBIRD_SENDBIRD_H_ */
Definition: SBDBaseChannel.h:42
Definition: Sendbird.h:73
Definition: SBDError.h:13
Definition: SBDGroupChannelTotalUnreadMessageCountParams.h:16
Definition: Sendbird.h:103
static void RemoveUserEventHandler(std::wstring identifier)
static SBDUserListQuery * CreateBlockedUserListQuery()
static void UnblockUser(const SBDUser &user, std::function< void(SBDError *)> completion_handler)
static void RemoveConnectionHandler(std::wstring identifier)
static void RemoveAllConnectionHandlers()
static void RemoveAllChannelHandlers()
static void Init(std::wstring application_id)
static void UnregisterPushTokenForCurrentUser(SBDPushTokenType type, std::wstring token, std::function< void(SBDError *)> completion_handler)
static void Connect(std::wstring user_id, std::wstring access_token, std::function< void(SBDUser *, SBDError *)> completion_handler)
static int GetSubscribedCustomTypeTotalUnreadMessageCount()
static SBDUser * GetCurrentUser()
static void BlockUser(const SBDUser &user, std::function< void(SBDUser *blocked_user, SBDError *)> completion_handler)
static void AddConnectionHandler(std::wstring identifier, SBDConnectionHandler *handler)
static std::wstring GetApplicationId()
static void Disconnect(std::function< void()> completion_handler)
static void RegisterPushTokenForCurrentUser(SBDPushTokenType type, std::wstring token, bool unique, std::function< void(SBDPushTokenRegistrationStatus, SBDError *)> completion_handler)
static void GetTotalUnreadChannelCount(std::function< void(int total_unread_channel_count, SBDError *)> completion_handler)
static void UpdateCurrentUserInfo(std::wstring nickname, std::wstring profile_url, std::function< void(SBDError *)> completion_handler)
static void GetChannelInvitationPreference(std::function< void(bool is_auto_accept, SBDError *)> completion_handler)
static void AddChannelHandler(std::wstring identifier, SBDChannelHandler *handler)
static void GetTotalUnreadMessageCount(SBDGroupChannelTotalUnreadMessageCountParams &params, std::function< void(int total_unread_message_count, SBDError *)> completion_handler)
static SBDConnectionState GetConnectionState()
static std::wstring GetPendingPushToken()
static void RemoveAllUserEventHandlers()
static int GetSubscribedTotalUnreadMessageCount()
static SBDUserListQuery * CreateUserListQuery(std::vector< std::wstring > user_ids)
static SBDUserListQuery * CreateAllUserListQuery()
static void UpdateCurrentUserInfoWithBinaryProfileImage(std::wstring nickname, std::wstring profile_image_file_path, std::wstring mime_type, std::function< void(SBDError *)> completion_handler)
static void UnregisterPushTokenAllForCurrentUser(std::function< void(SBDError *)> completion_handler)
static void RemoveChannelHandler(std::wstring identifier)
static int GetSubscribedCustomTypeUnreadMessageCount(std::wstring custom_type)
static void AddUserEventHandler(std::wstring identifier, SBDUserEventHandler *handler)
static bool Reconnect()
static std::wstring GetSdkVersion()
static bool IsInitialized()
static void SetChannelInvitationPreference(bool auto_accept, std::function< void(SBDError *)> completion_handler)
Definition: Sendbird.h:61
static bool use_member_as_message_sender
Definition: Sendbird.h:67
Definition: Sendbird.h:85
virtual void TotalUnreadMessageCountChanged(int total_count, std::map< std::wstring, int > total_count_by_custom_type)
Definition: Sendbird.h:95
Definition: SBDUser.h:20
Definition: SBDUserListQuery.h:35