aws_gamelift_server_sdk_rs/
api.rs

1use crate::{
2    entity::{
3        DescribePlayerSessionsResult, GetInstanceCertificateResult, StartMatchBackfillResult,
4    },
5    error::GameLiftErrorType,
6};
7
8pub const SDK_VERSION: &str = "4.0.2";
9
10#[derive(Default)]
11pub struct Api {
12    state: crate::server_state::ServerState,
13}
14
15impl Api {
16    /// Returns the current version number of the SDK built into the server
17    /// process.
18    pub fn get_sdk_version() -> &'static str {
19        SDK_VERSION
20    }
21
22    /// Initializes the GameLift SDK. This method should be called on launch,
23    /// before any other GameLift-related initialization occurs.
24    pub async fn init_sdk(&mut self) -> Result<(), GameLiftErrorType> {
25        self.state.initialize_networking().await
26    }
27
28    /// Notifies the GameLift service that the server process is ready to host
29    /// game sessions. Call this method after successfully invoking
30    /// [init_sdk](crate::api::Api::init_sdk) and completing setup tasks
31    /// that are required before the server process can host a game session.
32    /// This method should be called only once per process.
33    pub async fn process_ready(
34        &mut self,
35        process_parameters: crate::process_parameters::ProcessParameters,
36    ) -> Result<(), GameLiftErrorType> {
37        self.state.process_ready(process_parameters).await
38    }
39
40    /// Notifies the GameLift service that the server process is shutting down.
41    /// This method should be called after all other cleanup tasks, including
42    /// shutting down all active game sessions. This method should exit with an
43    /// exit code of 0; a non-zero exit code results in an event message that
44    /// the process did not exit cleanly.
45    pub async fn process_ending(&mut self) -> Result<(), GameLiftErrorType> {
46        self.state.process_ending().await
47    }
48
49    /// Notifies the GameLift service that the server process has activated a
50    /// game session and is now ready to receive player connections. This action
51    /// should be called as part of the on_start_game_session() callback
52    /// function, after all game session initialization has been completed.
53    pub async fn activate_game_session(&self) -> Result<(), GameLiftErrorType> {
54        self.state.activate_game_session().await
55    }
56
57    /// Notifies the GameLift service that the server process has ended the
58    /// current game session. This action is called when the server process will
59    /// remain active and ready to host a new game session. It should be called
60    /// only after your game session termination procedure is complete, because
61    /// it signals to GameLift that the server process is immediately available
62    /// to host a new game session.
63    ///
64    /// This action is not called if the server process will be shut down after
65    /// the game session stops. Instead, call
66    /// [process_ending](crate::api::Api::process_ending) to signal that
67    /// both the game session and the server process are ending.
68    #[deprecated(
69        since = "4.0.1",
70        note = "Instead, the server process should call process_ending() after a game session has \
71                ended"
72    )]
73    pub async fn terminate_game_session(&self) -> Result<(), GameLiftErrorType> {
74        self.state.terminate_game_session().await
75    }
76
77    /// Updates the current game session's ability to accept new player
78    /// sessions. A game session can be set to either accept or deny all new
79    /// player sessions. (See also the update_game_session() action in the
80    /// GameLift Service API Reference).
81    pub async fn update_player_session_creation_policy(
82        &self,
83        player_session_policy: crate::entity::PlayerSessionCreationPolicy,
84    ) -> Result<(), GameLiftErrorType> {
85        self.state.update_player_session_creation_policy(player_session_policy).await
86    }
87
88    /// Retrieves the ID of the game session currently being hosted by the
89    /// server process, if the server process is active.
90    pub async fn get_game_session_id(
91        &self,
92    ) -> Result<crate::entity::GameSessionId, crate::error::GameLiftErrorType> {
93        self.state.get_game_session_id().await
94    }
95
96    /// Returns the time that a server process is scheduled to be shut down, if
97    /// a termination time is available. A server process takes this action
98    /// after receiving an on_process_terminate() callback from the GameLift
99    /// service. GameLift may call on_process_terminate() for the following
100    /// reasons: (1) for poor health (the server process has reported port
101    /// health or has not responded to GameLift, (2) when terminating the
102    /// instance during a scale-down event, or (3) when an instance is being
103    /// terminated due to a spot-instance interruption.
104    ///
105    /// If the process has received an on_process_terminate() callback, the
106    /// value returned is the estimated termination time. If the process has
107    /// not received an on_process_terminate() callback, an error message is
108    /// returned. Learn more about shutting down a server process.
109    pub async fn get_termination_time(
110        &self,
111    ) -> Result<crate::entity::TerminationTimeType, crate::error::GameLiftErrorType> {
112        self.state.get_termination_time().await
113    }
114
115    /// Notifies the GameLift service that a player with the specified player
116    /// session ID has connected to the server process and needs validation.
117    /// GameLift verifies that the player session ID is valid—that is, that the
118    /// player ID has reserved a player slot in the game session. Once
119    /// validated, GameLift changes the status of the player slot from RESERVED
120    /// to ACTIVE.
121    pub async fn accept_player_session(
122        &self,
123        player_session_id: crate::entity::PlayerSessionId,
124    ) -> Result<(), GameLiftErrorType> {
125        self.state.accept_player_session(player_session_id).await
126    }
127
128    /// Notifies the GameLift service that a player with the specified player
129    /// session ID has disconnected from the server process. In response,
130    /// GameLift changes the player slot to available, which allows it to be
131    /// assigned to a new player.
132    pub async fn remove_player_session(
133        &self,
134        player_session_id: crate::entity::PlayerSessionId,
135    ) -> Result<(), GameLiftErrorType> {
136        self.state.remove_player_session(player_session_id).await
137    }
138
139    /// Retrieves player session data, including settings, session metadata, and
140    /// player data. Use this action to get information for a single player
141    /// session, for all player sessions in a game session, or for all player
142    /// sessions associated with a single player ID.
143    pub async fn describe_player_sessions(
144        &self,
145        describe_player_sessions_request: crate::entity::DescribePlayerSessionsRequest,
146    ) -> Result<DescribePlayerSessionsResult, GameLiftErrorType> {
147        self.state.describe_player_sessions(describe_player_sessions_request).await
148    }
149
150    /// Sends a request to find new players for open slots in a game session
151    /// created with FlexMatch. See also the AWS SDK action
152    /// [start_match_backfill](crate::api::Api::start_match_backfill). With this
153    /// action, match backfill requests can be initiated by a game server
154    /// process that is hosting the game session. Learn more about the
155    /// FlexMatch backfill feature.
156    ///
157    /// This action is asynchronous. If new players are successfully matched,
158    /// the GameLift service delivers updated matchmaker data using the callback
159    /// function on_update_game_session().
160    ///
161    /// A server process can have only one active match backfill request at a
162    /// time. To send a new request, first call
163    /// [stop_match_backfill](crate::api::Api::stop_match_backfill) to cancel
164    /// the original request.
165    pub async fn start_match_backfill(
166        &self,
167        request: crate::entity::StartMatchBackfillRequest,
168    ) -> Result<StartMatchBackfillResult, GameLiftErrorType> {
169        self.state.backfill_matchmaking(request).await
170    }
171
172    /// Cancels an active match backfill request that was created with
173    /// [start_match_backfill](crate::api::Api::start_match_backfill). See also
174    /// the AWS SDK action StopMatchmaking(). Learn more about the FlexMatch
175    /// backfill feature.
176    pub async fn stop_match_backfill(
177        &self,
178        request: crate::entity::StopMatchBackfillRequest,
179    ) -> Result<(), GameLiftErrorType> {
180        self.state.stop_matchmaking(request).await
181    }
182
183    /// Retrieves the file location of a pem-encoded TLS certificate that is
184    /// associated with the fleet and its instances. This certificate is
185    /// generated when a new fleet is created with the certificate configuration
186    /// set to GENERATED. Use this certificate to establish a secure connection
187    /// with a game client and to encrypt client/server communication.
188    pub async fn get_instance_certificate(
189        &self,
190    ) -> Result<GetInstanceCertificateResult, GameLiftErrorType> {
191        self.state.get_instance_certificate().await
192    }
193
194    pub async fn destroy(&self) -> bool {
195        self.state.shutdown().await
196    }
197}