iFun Engine API  1.0.0-b2936
Great Technology for Great Games
 All Classes Files Functions Variables Typedefs Macros
dedicated_server_manager.h
Go to the documentation of this file.
1  // Copyright (C) 2016-2018 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_DEDICATED_SERVER_MANAGER_H_
10 #define INCLUDE_FUNAPI_SERVICE_DEDICATED_SERVER_MANAGER_H_
11 
12 #include <string>
13 #include <vector>
14 
15 #include <boost/function.hpp>
16 #include <boost/noncopyable.hpp>
17 
18 #include <funapi/types.h>
19 
20 
21 namespace fun {
22 
23 class DedicatedServerManager : private boost::noncopyable {
24  public:
25  typedef boost::function<void (const fun::Uuid &, /* match_id */
26  const std::vector<std::string> &, /* users */
27  bool /* success */)> SendCallback;
28 
29  typedef boost::function<void (const fun::Uuid &, /* match_id */
30  const fun::Json &, /* match_data */
31  bool /* success */)> MatchResultCallback;
32 
33  typedef boost::function<void (const fun::Uuid &, /* match_id */
34  const fun::Json &, /* game_state */
35  bool /* success */)> GetGameStateCallback;
36 
37  typedef boost::function<void (const fun::Uuid &match_id,
38  const std::string &account)> UserJoinedCallback;
39 
40  typedef boost::function<void (const fun::Uuid &match_id,
41  const std::string &account)> UserLeftCallback;
42 
43  typedef boost::function<void (const fun::Uuid &match_id,
44  const fun::Json &data)> CustomCallback;
45 
46  // NOTE: `reserved_instance_count` is a value from MANIFEST file.
47  typedef boost::function<uint32_t (uint32_t current_instance_count,
48  uint32_t idle_instance_count,
49  uint32_t min_instance_count,
50  uint32_t max_instance_count,
51  uint32_t reserved_instance_count)>
52  ConfigureReservedInstanceCountCallback;
53 
54  // Authorization callback type (for JWT enforced APIs)
55  typedef boost::function<bool (
56  const std::string &uri, const fun::Json &claim)> JwtAuthorizationCallback;
57 
58  // Pre hook for a function that sending the redirection message
59  typedef boost::function<bool (const std::string &account_id,
60  const std::string &host,
61  const uint16_t port,
62  const std::string &token)> PreRedirectionHook;
63 
64  /*
65  * Spawn a dedicated server instance on a remote server.
66  * After spwaning a instance, selected users will be redirected to the
67  * newly spawned instance.
68  * After getting response from the dedicated server, result will be
69  * passed to the `callback` function.
70  *
71  * You may send additional data in JSON format using `match_data` and
72  * `user_data.` `match_data` should contain per-match game data and
73  * `user_data` should have per-user game data.
74  *
75  * Also, you could pass arguments for a dedicated server using
76  * `dedicated_server_args' parameter. These arguments will be passed when
77  * dedicated server manager executes dedicated server excutable.
78  *
79  * Note: user_data should be given in the same order as `accounts` parameter.
80  */
81  static void Spawn(const fun::Uuid &match_id,
82  const fun::Json &match_data,
83  const std::vector<std::string> &dedicated_server_args,
84  const std::vector<std::string> &accounts,
85  const std::vector<fun::Json> &user_data,
86  const SendCallback &callback);
87 
88  // Additional parameter `region_preference` may given to prioritize certain
89  // EC2 regions.
90  static void Spawn(const fun::Uuid &match_id,
91  const fun::Json &match_data,
92  const std::vector<std::string> &dedicated_server_args,
93  const std::vector<std::string> &accounts,
94  const std::vector<fun::Json> &user_data,
95  const std::vector<std::string> &region_preference,
96  const SendCallback &callback);
97 
98  /*
99  * Cancels a spawn request corresponding to `match_id`.
100  * If the spawn request was successfully canceled, it returns true.
101  * Otherwise false.
102  * Also, you can get the parameters that was passed into Spawn() by
103  * passing non-null pointer arguments.
104  */
105  static bool Cancel(const fun::Uuid &match_id,
106  fun::Json *match_data = NULL,
107  std::vector<std::string> *dedicated_server_args = NULL,
108  std::vector<std::string> *accounts = NULL,
109  std::vector<fun::Json> *user_data = NULL);
110 
111  /*
112  * Sends users in `accounts' to a existing dedicated server instance.
113  * You may send additional per-user data in JSON format using user_data.
114  *
115  * After getting response from the dedicated server, result will be
116  * called back using `callback' function.
117  */
118  static void SendUsers(const fun::Uuid &match_id,
119  const fun::Json &match_data,
120  const std::vector<std::string> &accounts,
121  const std::vector<fun::Json> &user_data,
122  const SendCallback &callback);
123 
124  /*
125  * Registers handler function for match result.
126  *
127  * If the match result from the dedicated server instance received,
128  * register callback function `callback' would be called.
129  */
130  static void RegisterMatchResultCallback(const MatchResultCallback &callback);
131 
132  /*
133  * Registers handler function, which would be called when user enters/leaves
134  * the dedicated server.
135  */
136  static void RegisterUserEnteredCallback(const UserJoinedCallback &callback);
137  static void RegisterUserLeftCallback(const UserLeftCallback &callback);
138 
139  /*
140  * Controls desired # of reserved instance count.
141  * If set, iFunEngine will use the callback to determine how many reserved
142  * instance should be spawned.
143  *
144  * If not set, iFunEngine will use the `reserved_instances` value from
145  * the MANIFEST file(s).
146  */
147  static void RegisterConfigureReservedInstanceCountCallback(
148  const ConfigureReservedInstanceCountCallback &callback);
149 
150  /*
151  * Registers callback function. It would be called whenever a dedicated
152  * server instance sends any message to the iFunEngine game server.
153  */
154  static void RegisterCustomCallback(const CustomCallback &callback);
155 
156  /*
157  * Get state of game that is running on dedicated server
158  */
159  static void GetGameState(const fun::Uuid &match_id,
160  const GetGameStateCallback &callback);
161 
162  // Register callback function to authorize a JWT claims
163  static void RegisterJwtAuthorizationCallback(
164  const JwtAuthorizationCallback &callback);
165 
166  /*
167  * Register a pre hook for a function that sending redirection message to the
168  * client. The server will not send the message if pre hook returns 'false',
169  * you can potentially ignore it, or send the message to your application.
170  */
171  static void RegisterPreRedirectionHook(const PreRedirectionHook &hook);
172 };
173 
174 } // namespace fun
175 
176 #endif // INCLUDE_FUNAPI_SERVICE_DEDICATED_SERVER_MANAGER_H_
Definition: dedicated_server_manager.h:23
Definition: json.h:27