iFun Engine API  1.0.0-b2601
Great Technology for Great Games
 All Classes Files Functions Variables Typedefs Macros
account_manager.h
Go to the documentation of this file.
1 // Copyright (C) 2013-2017 iFunFactory Inc. All Rights Reserved.
2 //
3 // This work is confidential and proprietary to iFunFactory Inc. and
4 // must not be used, disclosed, copied, or distributed without the prior
5 // consent of iFunFactory Inc.
6 
9 #ifndef INCLUDE_FUNAPI_SERVICE_ACCOUNT_MANAGER_H_
10 #define INCLUDE_FUNAPI_SERVICE_ACCOUNT_MANAGER_H_
11 
12 #include <boost/noncopyable.hpp>
14 #include <funapi/network/session.h>
15 
16 #include <utility>
17 #include <vector>
18 
19 
20 namespace fun {
21 
22 class AccountManager : private boost::noncopyable {
23  public:
24  typedef function<void(const string & /*account_id*/, bool /*ret*/)> Callback;
25  typedef function<void(const string & /*account_id*/,
26  const Ptr<Session> &/*session*/,
27  bool /*ret*/)> LoginCallback;
28  typedef function<void(const string &/*account_id*/,
29  const Ptr<Session> &/*session*/,
30  bool /*ret*/)> LogoutCallback;
31  typedef function<void(const string &/*account_id*/,
32  const Ptr<Session> &/*session*/)> RemoteLogoutCallback;
33  typedef function<void(const string &/*account_id*/,
34  const Rpc::PeerId &/*peer_id*/)> LocateCallback;
35 
36  // Called after authenticating session as "account_id"
37  typedef function<void (const string & /*account_id*/,
38  const Ptr<Session> & /*session*/,
39  bool /*success*/,
40  const string & /*extra_data*/)> RedirectionCallback;
41 
42  static const Callback kNullCallback;
43 
44  static void RegisterRemoteLogoutHandler(const RemoteLogoutCallback &handler);
45  static void RegisterRedirectionHandler(const RedirectionCallback &handler);
46 
56  static bool CheckAndSetLoggedIn(
57  const string &account_id,
58  const Ptr<Session> &session) ASSERT_NO_ROLLBACK;
59 
68  static void CheckAndSetLoggedInAsync(
69  const string &account_id,
70  const Ptr<Session> &session,
71  const LoginCallback &callback) ASSERT_NO_ROLLBACK;
72 
78  static bool SetLoggedOut(const string &account_id) ASSERT_NO_ROLLBACK;
79 
84  static void SetLoggedOutAsync(
85  const string &account_id,
86  const LogoutCallback &callback) ASSERT_NO_ROLLBACK;
87 
93  static bool SetLoggedOutGlobal(const string &account_id) ASSERT_NO_ROLLBACK;
94 
99  static void SetLoggedOutGlobalAsync(
100  const string &account_id,
101  const LogoutCallback &callback) ASSERT_NO_ROLLBACK;
102 
105  static void GetAllLocalAccounts(std::vector<string> *accounts);
106 
109  static void GetAllLocalSessions(std::vector<Ptr<Session> > *sessions);
110 
113  static void GetAllLocalAccountsAndSessions(
114  std::vector<std::pair<string, Ptr<Session> > > *accounts);
115 
118  static size_t GetLocalAccountsNumber();
119 
122  static size_t GetGlobalAccountsNumber();
123 
128  static Rpc::PeerId Locate(const string &account_id) ASSERT_NO_ROLLBACK;
129 
134  static void LocateAsync(
135  const string &account_id,
136  const LocateCallback &callback) ASSERT_NO_ROLLBACK;
137 
141  static Ptr<Session> FindLocalSession(const string &account_id);
142 
147  static string FindLocalAccount(const Ptr<Session> &session);
148 
156  static void SendMessage(
157  const string &msg_type, const Ptr<FunMessage> &msg,
158  const string &account_id, const Encryption &enc = kDefaultEncryption,
159  const TransportProtocol &protocol = kDefaultProtocol,
160  const Callback &cb = kNullCallback) ASSERT_NO_ROLLBACK;
161 
169  static void SendMessage(
170  int32_t msg_type, const Ptr<FunMessage> &msg,
171  const string &account_id, const Encryption &enc = kDefaultEncryption,
172  const TransportProtocol &protocol = kDefaultProtocol,
173  const Callback &cb = kNullCallback) ASSERT_NO_ROLLBACK;
174 
182  static void SendMessage(
183  const ProtobufExtensionIdentifier &msg_type, const Ptr<FunMessage> &msg,
184  const string &account_id, const Encryption &enc = kDefaultEncryption,
185  const TransportProtocol &protocol = kDefaultProtocol,
186  const Callback &cb = kNullCallback) ASSERT_NO_ROLLBACK;
187 
195  static void SendMessage(
196  const string &msg_type, const Json &msg, const string &account_id,
197  const Encryption &enc = kDefaultEncryption,
198  const TransportProtocol &protocol = kDefaultProtocol,
199  const Callback &cb = kNullCallback) ASSERT_NO_ROLLBACK;
200 
208  static bool SendMessageSync(
209  const string &msg_type, const Ptr<FunMessage> &msg,
210  const string &account_id, const Encryption &enc = kDefaultEncryption,
211  const TransportProtocol &protocol = kDefaultProtocol) ASSERT_NO_ROLLBACK;
212 
220  static bool SendMessageSync(
221  int32_t msg_type, const Ptr<FunMessage> &msg,
222  const string &account_id, const Encryption &enc = kDefaultEncryption,
223  const TransportProtocol &protocol = kDefaultProtocol) ASSERT_NO_ROLLBACK;
224 
232  static bool SendMessageSync(
233  const ProtobufExtensionIdentifier &msg_type, const Ptr<FunMessage> &msg,
234  const string &account_id, const Encryption &enc = kDefaultEncryption,
235  const TransportProtocol &protocol = kDefaultProtocol) ASSERT_NO_ROLLBACK;
236 
244  static bool SendMessageSync(
245  const string &msg_type, const Json &msg,
246  const string &account_id, const Encryption &enc = kDefaultEncryption,
247  const TransportProtocol &protocl = kDefaultProtocol) ASSERT_NO_ROLLBACK;
248 
254  static void BroadcastLocally(
255  const string &msg_type, const Ptr<FunMessage> &msg,
256  const Encryption &enc,
257  const TransportProtocol &protocol) ASSERT_NO_ROLLBACK;
258 
264  static void BroadcastLocally(
265  int32_t msg_type, const Ptr<FunMessage> &msg,
266  const Encryption &enc,
267  const TransportProtocol &protocol) ASSERT_NO_ROLLBACK;
268 
274  static void BroadcastLocally(
275  const ProtobufExtensionIdentifier &msg_type, const Ptr<FunMessage> &msg,
276  const Encryption &enc,
277  const TransportProtocol &protocol) ASSERT_NO_ROLLBACK;
278 
284  static void BroadcastLocally(
285  const string &msg_type, const Json &msg,
286  const Encryption &enc,
287  const TransportProtocol &protocol) ASSERT_NO_ROLLBACK;
288 
296  static void BroadcastGlobally(
297  const string &msg_type, const Ptr<FunMessage> &msg,
298  const Rpc::Tag &server_tag,
299  const Encryption &enc,
300  const TransportProtocol &protocol) ASSERT_NO_ROLLBACK;
301 
309  static void BroadcastGlobally(
310  int32_t msg_type, const Ptr<FunMessage> &msg,
311  const Rpc::Tag &server_tag,
312  const Encryption &enc,
313  const TransportProtocol &protocol) ASSERT_NO_ROLLBACK;
314 
322  static void BroadcastGlobally(
323  const ProtobufExtensionIdentifier &msg_type, const Ptr<FunMessage> &msg,
324  const Rpc::Tag &server_tag,
325  const Encryption &enc,
326  const TransportProtocol &protocol) ASSERT_NO_ROLLBACK;
327 
335  static void BroadcastGlobally(
336  const string &msg_type, const Json &msg,
337  const Rpc::Tag &server_tag,
338  const Encryption &enc,
339  const TransportProtocol &protocol) ASSERT_NO_ROLLBACK;
340 
348  static void BroadcastGlobally(
349  const string &msg_type, const Ptr<FunMessage> &msg,
350  const Rpc::PeerMap &servers,
351  const Encryption &enc,
352  const TransportProtocol &protocol) ASSERT_NO_ROLLBACK;
353 
361  static void BroadcastGlobally(
362  int32_t msg_type, const Ptr<FunMessage> &msg,
363  const Rpc::PeerMap &servers,
364  const Encryption &enc,
365  const TransportProtocol &protocol) ASSERT_NO_ROLLBACK;
366 
374  static void BroadcastGlobally(
375  const ProtobufExtensionIdentifier &msg_type, const Ptr<FunMessage> &msg,
376  const Rpc::PeerMap &servers,
377  const Encryption &enc,
378  const TransportProtocol &protocol) ASSERT_NO_ROLLBACK;
379 
387  static void BroadcastGlobally(
388  const string &msg_type, const Json &msg,
389  const Rpc::PeerMap &servers,
390  const Encryption &enc,
391  const TransportProtocol &protocol) ASSERT_NO_ROLLBACK;
392 
397  static bool RedirectClient(
398  const Ptr<Session> &session, const Rpc::PeerId &peer_id,
399  const string &extra_data) ASSERT_NO_ROLLBACK;
400 };
401 
402 } // namespace fun
403 
404 #endif // INCLUDE_FUNAPI_SERVICE_ACCOUNT_MANAGER_H_
static Rpc::PeerId Locate(const string &account_id) ASSERT_NO_ROLLBACK
Locates a server node hosting the given account.
static void SendMessage(const string &msg_type, const Ptr< FunMessage > &msg, const string &account_id, const Encryption &enc=kDefaultEncryption, const TransportProtocol &protocol=kDefaultProtocol, const Callback &cb=kNullCallback) ASSERT_NO_ROLLBACK
Asynchronously sends a protobuf message to the given account.
static void SetLoggedOutGlobalAsync(const string &account_id, const LogoutCallback &callback) ASSERT_NO_ROLLBACK
Marks the given account logged out of any server node.
static bool CheckAndSetLoggedIn(const string &account_id, const Ptr< Session > &session) ASSERT_NO_ROLLBACK
Marks the given account logged in this server node.
static void BroadcastLocally(const string &msg_type, const Ptr< FunMessage > &msg, const Encryption &enc, const TransportProtocol &protocol) ASSERT_NO_ROLLBACK
Broadcasts a protobuf message to all the logged-in local sessions.
static bool SendMessageSync(const string &msg_type, const Ptr< FunMessage > &msg, const string &account_id, const Encryption &enc=kDefaultEncryption, const TransportProtocol &protocol=kDefaultProtocol) ASSERT_NO_ROLLBACK
Synchronously sends a protobuf message to the given account.
Definition: session.h:561
static string FindLocalAccount(const Ptr< Session > &session)
Returns an account string associated with the local session.
static size_t GetGlobalAccountsNumber()
Returns the total number of logged-in accounts.
static void LocateAsync(const string &account_id, const LocateCallback &callback) ASSERT_NO_ROLLBACK
Locates a server node hosting the given account.
Definition: account_manager.h:22
static void BroadcastGlobally(const string &msg_type, const Ptr< FunMessage > &msg, const Rpc::Tag &server_tag, const Encryption &enc, const TransportProtocol &protocol) ASSERT_NO_ROLLBACK
Broadcasts a protobuf message to all the logged-in sessions of the given RPC servers.
static bool SetLoggedOut(const string &account_id) ASSERT_NO_ROLLBACK
Marks the given account logged out of this server node.
static void CheckAndSetLoggedInAsync(const string &account_id, const Ptr< Session > &session, const LoginCallback &callback) ASSERT_NO_ROLLBACK
Marks the given account logged in this server node.
Definition: json.h:27
static bool RedirectClient(const Ptr< Session > &session, const Rpc::PeerId &peer_id, const string &extra_data) ASSERT_NO_ROLLBACK
Cooperatively moves client to another server.
static Ptr< Session > FindLocalSession(const string &account_id)
Returns a session pointer if the account is attached to a local session.
static bool SetLoggedOutGlobal(const string &account_id) ASSERT_NO_ROLLBACK
Marks the given account logged out of any server node.
static void GetAllLocalAccountsAndSessions(std::vector< std::pair< string, Ptr< Session > > > *accounts)
Returns all the accounts attached to the server node and sessions.
static void SetLoggedOutAsync(const string &account_id, const LogoutCallback &callback) ASSERT_NO_ROLLBACK
Marks the given account logged out of this server node.
static void GetAllLocalAccounts(std::vector< string > *accounts)
Returns all the accounts attached to the server node.
static size_t GetLocalAccountsNumber()
Returns the number of accounts attached to the server node.
static void GetAllLocalSessions(std::vector< Ptr< Session > > *sessions)
Returns all the sessions of logged-in accounts.