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