agones.dev/agones@v1.53.0/examples/cpp-simple/server.cc (about) 1 // Copyright 2017 Google LLC All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // A server that starts, and then stops after 60 seconds. 16 // This example really does nothing other than show how to integrate 17 // the C++ SDK. 18 19 #include <agones/sdk.h> 20 #include <grpc++/grpc++.h> 21 #include <iostream> 22 #include <thread> 23 24 std::atomic_int stop_threads(0); 25 26 class ThreadJoiner { 27 public: 28 explicit ThreadJoiner(std::thread t) 29 : t_(std::move(t)) {} 30 31 ~ThreadJoiner() { 32 // Stop threads loop 33 stop_threads.store(1); 34 t_.join(); 35 } 36 private: 37 std::thread t_; 38 }; 39 40 // send health check pings 41 void DoHealth(std::shared_ptr<agones::SDK> sdk) { 42 while (true) { 43 bool ok = sdk->Health(); 44 std::cout << "Health ping " << (ok ? "sent" : "failed") << "\n" 45 << std::flush; 46 std::this_thread::sleep_for(std::chrono::seconds(2)); 47 if(stop_threads.load()) { 48 return ; 49 } 50 } 51 } 52 53 // watch GameServer Updates 54 void WatchUpdates(std::shared_ptr<agones::SDK> sdk) { 55 std::cout << "Starting to watch GameServer updates...\n" << std::flush; 56 sdk->WatchGameServer([](const agones::dev::sdk::GameServer& gameserver) { 57 std::cout << "GameServer Update:\n" // 58 << "\tname: " << gameserver.object_meta().name() << "\n" // 59 << "\tstate: " << gameserver.status().state() << "\n" 60 << std::flush; 61 }); 62 } 63 int main() { 64 std::cout << "C++ Game Server has started!\n" 65 << "Getting the instance of the SDK.\n" 66 << std::flush; 67 auto sdk = std::make_shared<agones::SDK>(); 68 69 std::cout << "Attempting to connect...\n" << std::flush; 70 if (!sdk->Connect()) { 71 std::cerr << "Exiting!\n"; 72 return -1; 73 } 74 std::cout << "...handshake complete.\n" << std::flush; 75 76 std::thread health(DoHealth, sdk); 77 std::thread watch(WatchUpdates, sdk); 78 ThreadJoiner h(std::move(health)); 79 ThreadJoiner w(std::move(watch)); 80 81 std::cout << "Setting a label\n" << std::flush; 82 grpc::Status status = sdk->SetLabel("test-label", "test-value"); 83 if (!status.ok()) { 84 std::cerr << "Could not run SetLabel(): " << status.error_message() 85 << ". Exiting!\n"; 86 return -1; 87 } 88 89 std::cout << "Setting an annotation\n" << std::flush; 90 status = sdk->SetAnnotation("test-annotation", "test value"); 91 if (!status.ok()) { 92 std::cerr << "Could not run SetAnnotation(): " << status.error_message() 93 << ". Exiting!\n"; 94 return -1; 95 } 96 97 std::cout << "Marking server as ready...\n" << std::flush; 98 status = sdk->Ready(); 99 if (!status.ok()) { 100 std::cerr << "Could not run Ready(): " << status.error_message() 101 << ". Exiting!\n"; 102 return -1; 103 } 104 std::cout << "...marked Ready\n" << std::flush; 105 106 std::cout << "Getting GameServer details...\n" << std::flush; 107 agones::dev::sdk::GameServer gameserver; 108 status = sdk->GameServer(&gameserver); 109 110 if (!status.ok()) { 111 std::cerr << "Could not run GameServer(): " << status.error_message() 112 << ". Exiting!\n"; 113 return -1; 114 } 115 116 std::cout << "GameServer name: " << gameserver.object_meta().name() << "\n" 117 << std::flush; 118 119 for (int i = 0; i < 10; i++) { 120 int time = i * 10; 121 std::cout << "Running for " + std::to_string(time) + " seconds !\n" 122 << std::flush; 123 124 std::this_thread::sleep_for(std::chrono::seconds(10)); 125 126 if (i == 5) { 127 std::cout << "Shutting down after 60 seconds...\n" << std::flush; 128 grpc::Status status = sdk->Shutdown(); 129 if (!status.ok()) { 130 std::cerr << "Could not run Shutdown():" << status.error_message() 131 << ". Exiting!\n"; 132 return -1; 133 } 134 std::cout << "...marked for Shutdown\n" << std::flush; 135 } 136 } 137 138 return 0; 139 }