Skip to content

Commit

Permalink
Implements SDK callback for GameServer updates
Browse files Browse the repository at this point in the history
This implements the gRPC system to send messages up to a unidirectional
stream from the sdk-server to the SDK.

This has been implemented in the Go, C++ and REST SDK.

Closes googleforgames#277
  • Loading branch information
markmandel authored and Victor Prodan committed Sep 3, 2018
1 parent 3144bfc commit 4c409bb
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
19 changes: 19 additions & 0 deletions sdks/cpp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,25 @@ sdk->WatchGameServer([](stable::agones::dev::sdk::GameServer gameserver){
For more information, you can also read the [SDK Overview](../), check out [sdk.h](sdk.h) and also look at the
[C++ example](../../examples/cpp-simple).

To get updates on the [backing `GameServer`](../README.md#gameserver) as they happen,
call `sdk->WatchGameServer([](stable::agones::dev::sdk::GameServer gameserver){...})`.

⚠️⚠️⚠️ **`WatchGameServer` is currently a development feature and has not been released** ⚠️⚠️️⚠️

This will call the passed in `std::function`
synchronously (this is a blocking function, so you may want to run it in its own thread) whenever the backing `GameServer`
is updated.

```cpp
sdk->WatchGameServer([](stable::agones::dev::sdk::GameServer gameserver){
std::cout << "GameServer Update, name: " << gameserver.object_meta().name() << std::endl;
std::cout << "GameServer Update, state: " << gameserver.status().state() << std::endl;
});
```
For more information, you can also read the [SDK Overview](../), check out [sdk.h](sdk.h) and also look at the
[C++ example](../../examples/cpp-simple).
### Failure
When running on Agones, the above functions should only fail under exceptional circumstances, so please
file a bug if it occurs.
Expand Down
30 changes: 30 additions & 0 deletions sdks/go/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,33 @@ func (s *SDK) WatchGameServer(f GameServerCallback) error {
}()
return nil
}

// WatchGameServer asynchronously calls the given GameServerCallback with the current GameServer
// configuration when the backing GameServer configuration is updated.
// This function can be called multiple times to add more than one GameServerCallback.
func (s *SDK) WatchGameServer(f GameServerCallback) error {
stream, err := s.client.WatchGameServer(s.ctx, &sdk.Empty{})
if err != nil {
return errors.Wrap(err, "could not watch gameserver")
}

go func() {
for {
var gs *sdk.GameServer
gs, err = stream.Recv()
if err != nil {
if err != io.EOF {
fmt.Printf("error watching GameServer: %s\n", err.Error())
} else {
fmt.Println("gameserver event stream EOF received")
return
}
// This is to wait for the reconnection, and not peg the CPU at 100%
time.Sleep(time.Second)
continue
}
f(gs)
}
}()
return nil
}

0 comments on commit 4c409bb

Please sign in to comment.