diff --git a/sdks/cpp/include/agones/sdk.h b/sdks/cpp/include/agones/sdk.h index 96b9513fdc..45e936901f 100644 --- a/sdks/cpp/include/agones/sdk.h +++ b/sdks/cpp/include/agones/sdk.h @@ -20,8 +20,6 @@ namespace agones { -class SDKImpl; - // The Agones SDK class SDK { public: @@ -68,6 +66,7 @@ class SDK { callback); private: + struct SDKImpl; std::unique_ptr pimpl_; }; diff --git a/sdks/cpp/src/agones/sdk.cc b/sdks/cpp/src/agones/sdk.cc index bf074ce5d4..86c1c06be7 100644 --- a/sdks/cpp/src/agones/sdk.cc +++ b/sdks/cpp/src/agones/sdk.cc @@ -15,22 +15,18 @@ #include "agones/sdk.h" #include - -namespace { -const int kPort = 59357; -} +#include namespace agones { -class SDKImpl final { - public: +struct SDK::SDKImpl { std::shared_ptr channel_; std::unique_ptr stub_; std::unique_ptr> health_; }; SDK::SDK() : pimpl_{std::make_unique()} { - pimpl_->channel_ = grpc::CreateChannel("localhost:" + std::to_string(kPort), + pimpl_->channel_ = grpc::CreateChannel("localhost:59357", grpc::InsecureChannelCredentials()); } @@ -47,19 +43,20 @@ bool SDK::Connect() { // make the health connection stable::agones::dev::sdk::Empty response; - pimpl_->health_ = pimpl_->stub_->Health(new grpc::ClientContext(), &response); + grpc::ClientContext context; + pimpl_->health_ = pimpl_->stub_->Health(&context, &response); return true; } grpc::Status SDK::Ready() { - grpc::ClientContext *context = new grpc::ClientContext(); - context->set_deadline(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), - gpr_time_from_seconds(30, GPR_TIMESPAN))); + grpc::ClientContext context; + context.set_deadline(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), + gpr_time_from_seconds(30, GPR_TIMESPAN))); stable::agones::dev::sdk::Empty request; stable::agones::dev::sdk::Empty response; - return pimpl_->stub_->Ready(context, request, &response); + return pimpl_->stub_->Ready(&context, request, &response); } bool SDK::Health() { @@ -67,23 +64,23 @@ bool SDK::Health() { return pimpl_->health_->Write(request); } -grpc::Status SDK::GameServer(stable::agones::dev::sdk::GameServer *response) { - grpc::ClientContext *context = new grpc::ClientContext(); - context->set_deadline(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), - gpr_time_from_seconds(30, GPR_TIMESPAN))); +grpc::Status SDK::GameServer(stable::agones::dev::sdk::GameServer* response) { + grpc::ClientContext context; + context.set_deadline(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), + gpr_time_from_seconds(30, GPR_TIMESPAN))); stable::agones::dev::sdk::Empty request; - return pimpl_->stub_->GetGameServer(context, request, response); + return pimpl_->stub_->GetGameServer(&context, request, response); } grpc::Status SDK::WatchGameServer( - const std::function &callback) { - grpc::ClientContext *context = new grpc::ClientContext(); + const std::function& callback) { + grpc::ClientContext context; stable::agones::dev::sdk::Empty request; stable::agones::dev::sdk::GameServer gameServer; std::unique_ptr> - reader = pimpl_->stub_->WatchGameServer(context, request); + reader = pimpl_->stub_->WatchGameServer(&context, request); while (reader->Read(&gameServer)) { callback(gameServer); } @@ -91,40 +88,40 @@ grpc::Status SDK::WatchGameServer( } grpc::Status SDK::Shutdown() { - grpc::ClientContext *context = new grpc::ClientContext(); - context->set_deadline(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), - gpr_time_from_seconds(30, GPR_TIMESPAN))); + grpc::ClientContext context; + context.set_deadline(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), + gpr_time_from_seconds(30, GPR_TIMESPAN))); stable::agones::dev::sdk::Empty request; stable::agones::dev::sdk::Empty response; - return pimpl_->stub_->Shutdown(context, request, &response); + return pimpl_->stub_->Shutdown(&context, request, &response); } grpc::Status SDK::SetLabel(std::string key, std::string value) { - grpc::ClientContext *context = new grpc::ClientContext(); - context->set_deadline(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), - gpr_time_from_seconds(30, GPR_TIMESPAN))); + grpc::ClientContext context; + context.set_deadline(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), + gpr_time_from_seconds(30, GPR_TIMESPAN))); stable::agones::dev::sdk::KeyValue request; - request.set_key(key); - request.set_value(value); + request.set_key(std::move(key)); + request.set_value(std::move(value)); stable::agones::dev::sdk::Empty response; - return pimpl_->stub_->SetLabel(context, request, &response); + return pimpl_->stub_->SetLabel(&context, request, &response); } grpc::Status SDK::SetAnnotation(std::string key, std::string value) { - grpc::ClientContext *context = new grpc::ClientContext(); - context->set_deadline(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), - gpr_time_from_seconds(30, GPR_TIMESPAN))); + grpc::ClientContext context; + context.set_deadline(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), + gpr_time_from_seconds(30, GPR_TIMESPAN))); stable::agones::dev::sdk::KeyValue request; - request.set_key(key); - request.set_value(value); + request.set_key(std::move(key)); + request.set_value(std::move(value)); stable::agones::dev::sdk::Empty response; - return pimpl_->stub_->SetAnnotation(context, request, &response); + return pimpl_->stub_->SetAnnotation(&context, request, &response); } } // namespace agones