Skip to content

Commit

Permalink
Add Rust SDK async/await syntax support and minor improvements (googl…
Browse files Browse the repository at this point in the history
…eforgames#1733)

* Add Rust SDK async/await syntax support and minor improvements

* Fix comment in run-sdk-conformance-test-rust

Co-authored-by: Mark Mandel <[email protected]>
  • Loading branch information
2 people authored and ilkercelikyilmaz committed Oct 23, 2020
1 parent c9df60a commit 4e8487d
Show file tree
Hide file tree
Showing 13 changed files with 1,322 additions and 931 deletions.
6 changes: 3 additions & 3 deletions build/build-sdk-images/rust/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ RUN apt-get update && \
ENV RUSTUP_HOME=/usr/local/rustup \
CARGO_HOME=/usr/local/cargo \
PATH=/usr/local/cargo/bin:$PATH \
RUST_VERSION=1.43.1
RUST_VERSION=1.45.0
ENV RUST_ARCH=x86_64-unknown-linux-gnu \
RUSTUP_SHA256=c9837990bce0faab4f6f52604311a19bb8d2cde989bea6a7b605c8e526db6f02
RUN wget -q https://static.rust-lang.org/rustup/archive/1.11.0/${RUST_ARCH}/rustup-init && \
Expand All @@ -35,8 +35,8 @@ RUN wget -q https://static.rust-lang.org/rustup/archive/1.11.0/${RUST_ARCH}/rust
rustc --version;

# install rust tooling for SDK generation
RUN cargo install protobuf-codegen --vers 2.0.2
RUN cargo install grpcio-compiler --vers 0.3.0
RUN cargo install protobuf-codegen --vers 2.16.2
RUN cargo install grpcio-compiler --vers 0.6.0

RUN wget -q https://cmake.org/files/v3.14/cmake-3.14.1-Linux-x86_64.sh
RUN mkdir /opt/cmake
Expand Down
4 changes: 4 additions & 0 deletions build/includes/sdk.mk
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,12 @@ run-sdk-conformance-test-go:
run-sdk-conformance-test-rust:
# run without feature flags
$(MAKE) run-sdk-conformance-test SDK_FOLDER=rust GRPC_PORT=9004 HTTP_PORT=9104
# run without feature flags and with RUN_ASYNC=true
DOCKER_RUN_ARGS="$(DOCKER_RUN_ARGS) -e RUN_ASYNC=true" $(MAKE) run-sdk-conformance-test SDK_FOLDER=rust GRPC_PORT=9004 HTTP_PORT=9104
# run with feature flags enabled
$(MAKE) run-sdk-conformance-test SDK_FOLDER=rust GRPC_PORT=9004 HTTP_PORT=9104 FEATURE_GATES=PlayerTracking=true TESTS=$(DEFAULT_CONFORMANCE_TESTS),$(ALPHA_CONFORMANCE_TESTS)
# run with feature flags enabled and with RUN_ASYNC=true
DOCKER_RUN_ARGS="$(DOCKER_RUN_ARGS) -e RUN_ASYNC=true" $(MAKE) run-sdk-conformance-test SDK_FOLDER=rust GRPC_PORT=9004 HTTP_PORT=9104 FEATURE_GATES=PlayerTracking=true TESTS=$(DEFAULT_CONFORMANCE_TESTS),$(ALPHA_CONFORMANCE_TESTS)

run-sdk-conformance-test-rest:
# run without feature flags
Expand Down
15 changes: 10 additions & 5 deletions sdks/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@
[package]
name = "agones"
version = "0.1.0"
edition = "2018"

[features]
openssl = ["grpcio/openssl"]
openssl-vendored = ["grpcio/openssl-vendored"]

[dependencies]
grpcio = "0.3.0"
grpcio-proto = "0.3.0"
protobuf = "2.0.2"
futures = "^0.1.15"
error-chain = "0.11.0"
grpcio = "0.6.0"
protobuf = "2.16.2"
futures = { version = "0.3", features = ["compat"] }
futures01 = "0.1"
error-chain = "0.12.3"
101 changes: 98 additions & 3 deletions sdks/rust/src/alpha.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ use std::sync::Arc;

use grpcio;

use errors::*;
use grpc::alpha;
use grpc::alpha_grpc;
use super::errors::*;
use super::grpc::alpha;
use super::grpc::alpha_grpc;

/// Alpha is an instance of the Agones Alpha SDK
pub struct Alpha {
Expand Down Expand Up @@ -103,6 +103,101 @@ impl Alpha {
.map(|pl| pl.list.into())?;
Ok(res)
}

/// This returns the last player capacity that was set through the SDK.
/// If the player capacity is set from outside the SDK, use sdk.get_gameserver() instead.
pub async fn get_player_capacity_async(&self) -> Result<i64> {
let req = alpha::Empty::new();
let count = self
.client
.get_player_capacity_async(&req)?
.await
.map(|c| c.count)?;
Ok(count)
}

/// This changes the player capacity to a new value.
pub async fn set_player_capacity_async(&self, capacity: i64) -> Result<()> {
let mut c = alpha::Count::new();
c.set_count(capacity);
let res = self
.client
.set_player_capacity_async(&c)?
.await
.map(|_| ())?;
Ok(res)
}

/// This function increases the SDK’s stored player count by one, and appends this playerID to GameServer.status.players.ids.
/// Returns true and adds the playerID to the list of playerIDs if the playerIDs was not already in the list of connected playerIDs.
pub async fn player_connect_async<S>(&self, id: S) -> Result<bool>
where
S: Into<String>,
{
let mut p = alpha::PlayerID::new();
p.set_playerID(id.into());
let res = self
.client
.player_connect_async(&p)?
.await
.map(|b| b.bool)?;
Ok(res)
}

/// This function decreases the SDK’s stored player count by one, and removes the playerID from GameServer.status.players.ids.
/// Will return true and remove the supplied playerID from the list of connected playerIDs if the playerID value exists within the list.
pub async fn player_disconnect_async<S>(&self, id: S) -> Result<bool>
where
S: Into<String>,
{
let mut p = alpha::PlayerID::new();
p.set_playerID(id.into());
let res = self
.client
.player_disconnect_async(&p)?
.await
.map(|b| b.bool)?;
Ok(res)
}

/// Returns the current player count.
pub async fn get_player_count_async(&self) -> Result<i64> {
let req = alpha::Empty::new();
let count = self
.client
.get_player_count_async(&req)?
.await
.map(|c| c.count)?;
Ok(count)
}

/// This returns if the playerID is currently connected to the GameServer.
/// This is always accurate, even if the value hasn’t been updated to the GameServer status yet.
pub async fn is_player_connected_async<S>(&self, id: S) -> Result<bool>
where
S: Into<String>,
{
let mut p = alpha::PlayerID::new();
p.set_playerID(id.into());
let res = self
.client
.is_player_connected_async(&p)?
.await
.map(|b| b.bool)?;
Ok(res)
}

/// This returns the list of the currently connected player ids.
/// This is always accurate, even if the value has not been updated to the Game Server status yet.
pub async fn get_connected_players_async(&self) -> Result<Vec<String>> {
let req = alpha::Empty::new();
let res = self
.client
.get_connected_players_async(&req)?
.await
.map(|pl| pl.list.into())?;
Ok(res)
}
}

impl Clone for Alpha {
Expand Down
Loading

0 comments on commit 4e8487d

Please sign in to comment.