0% found this document useful (0 votes)
0 views

game network tencent

The document is a development guide for Tencent Cloud's Game Server Elastic-scaling, detailing the integration process with the gRPC framework across multiple programming languages. It outlines steps for publishing a game server, obtaining server addresses via TencentCloud APIs, and managing game server sessions and player sessions. Additionally, it provides information on server health checks and process management within the game server environment.

Uploaded by

sherbina.pro
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

game network tencent

The document is a development guide for Tencent Cloud's Game Server Elastic-scaling, detailing the integration process with the gRPC framework across multiple programming languages. It outlines steps for publishing a game server, obtaining server addresses via TencentCloud APIs, and managing game server sessions and player sessions. Additionally, it provides information on server health checks and process management within the game server environment.

Uploaded by

sherbina.pro
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 105

Game Server Elastic-scaling

Game Server Elastic-scaling

Development Guide
Product Documentation

©2013-2019 Tencent Cloud. All rights reserved. Page 1 of 105


Game Server Elastic-scaling

Copyright Notice

©2013-2019 Tencent Cloud. All rights reserved.

Copyright in this document is exclusively owned by Tencent Cloud. You must not reproduce, modify,
copy or distribute in any way, in whole or in part, the contents of this document without Tencent
Cloud's the prior written consent.

Trademark Notice

All trademarks associated with Tencent Cloud and its services are owned by Tencent Cloud
Computing (Beijing) Company Limited and its affiliated companies. Trademarks of third parties
referred to in this document are owned by their respective proprietors.

Service Statement

This document is intended to provide users with general information about Tencent Cloud's products
and services only and does not form part of Tencent Cloud's terms and conditions. Tencent Cloud's
products or services are subject to change. Specific products and services and the standards
applicable to them are exclusively provided for in Tencent Cloud's applicable terms and conditions.

©2013-2019 Tencent Cloud. All rights reserved. Page 2 of 105


Game Server Elastic-scaling

Contents

Development Guide
Overall Process
Integrating Server with gRPC Framework
gRPC C++ Tutorial
gRPC C# Tutorial
gRPC Go Tutorial
gRPC Java Tutorial
gRPC Lua Tutorial
gRPC Node.js Tutorial
gRPC Unity Tutorial
Getting Server Address
TencentCloud API Calling Method
Creating Game Server Session
Placing Game Server Session
GSE Local
Latency Test Tool
Game Process Launch Configuration

©2013-2019 Tencent Cloud. All rights reserved. Page 3 of 105


Game Server Elastic-scaling

Development Guide
Overall Process
Last updated:2020-09-08 15:27:29

Overall Flowchart

Integration Steps

©2013-2019 Tencent Cloud. All rights reserved. Page 4 of 105


Game Server Elastic-scaling

Step 1. Integrate the server with the gRPC framework


The game server communicates with GSE over gRPC. The gRPC framework can be integrated with
the game server program in various programming languages to generate game server executable
files. For more information on how to integrate GSE with the server in different languages, please see
gRPC - C++ Tutorial, gRPC - C# Tutorial, gRPC - Go Tutorial, gRPC - Java Tutorial, gRPC - Lua Tutorial,
and gRPC - Node.js Tutorial. For other languages, please see the gRPC official documentation.

Step 2. Publish the program


1. Upload an asset package
An asset package contains the executable files, dependencies, and installation script of the game
server. You need to package them as a ZIP file before upload. For more information, please see
Creating Code Packages.
2. Create a server fleet
Deploy the uploaded asset package on the created server fleet and complete process
management, deployment configuration, scaling configuration, etc. For more information, please
see Creating Server Fleets.

Step 3. Call a TencentCloud API to get the server address (IP:port)


You can get the server address (IP:port) by creating or placing a game server session.

Method 1. Create a game server session

Call a TencentCloud API:


The client TencentCloud API call process varies by supporting mode of the game server session.

When a game server session only supports one game:


Create a game server session (CreateGameServerSession);
Join a game server session (JoinGameServerSession).
When a game server session supports multiple games or one service (such as login):
Query the game server session list (DescribeGameServerSessions) or search in the game server
session list (SearchGameServerSessions);
If there is a game server session, join it (JoinGameServerSession);
If there is no game server session, create one (CreateGameServerSession) and join it
(JoinGameServerSession).

For more information on how to call TencentCloud APIs, please see Creating Game Server Session.

Method 2. Place a game server session

Call a TencentCloud API:

©2013-2019 Tencent Cloud. All rights reserved. Page 5 of 105


Game Server Elastic-scaling

Start placing a game server session (StartGameServerSessionPlacement);


Query game server session placement (DescribeGameServerSessionPlacement);
Stop placing a game server session (StopGameServerSessionPlacement).

For more information on how to call the TencentCloud APIs, please see Placing Game Server Session.

Step 4. The client uses the IP:port to access the server


The client can connect to the target server through the IP:port returned in step 3.

Workflow

©2013-2019 Tencent Cloud. All rights reserved. Page 6 of 105


Game Server Elastic-scaling

Integrating Server with gRPC


Framework
gRPC C++ Tutorial
Last updated:2021-11-17 18:06:09

Installing gRPC
1. Prerequisites: install CMake.
Linux

$ sudo apt install -y cmake

MAC OS

$ brew install cmake

2. Install gRPC and Protocol Buffers locally.

Note
For more information on the installation process, see Installing CMake, Installing gRPC C++,
and Installing Protocol Buffers.

Defining Service
gRPC uses Protocol Buffers to define a service: an RPC service specifies methods that can be called
remotely by using parameters and return types.

Note
We provide the .proto files for service definition. You can directly download them with no need
to generate them by yourself.

©2013-2019 Tencent Cloud. All rights reserved. Page 7 of 105


Game Server Elastic-scaling

Generating gRPC Code


1. After defining the service, you can use protoc (protocol buffer compiler) to generate the client and
server code (in any language supported by gRPC).
2. The generated code includes the client stub and the abstract APIs to be implemented by the
server.
3. Steps for generating gRPC code:
In the proto directory, run:

protoc --cpp_out=. *.proto

to generate the pb.cc and pb.h files.

protoc --grpc_out=. --plugin=protoc-gen-grpc=`which grpc_cpp_plugin` *.proto

to generate the corresponding gRPC code.


Move the eight generated files to an appropriate location in the project.

Game Process Integration Process

©2013-2019 Tencent Cloud. All rights reserved. Page 8 of 105


Game Server Elastic-scaling

Game server callback API list

API Name API Description

OnHealthCheck Runs health check

OnStartGameServerSession Receives game server session

OnProcessTerminate Ends game process

Game server active API list

©2013-2019 Tencent Cloud. All rights reserved. Page 9 of 105


Game Server Elastic-scaling

API Name API Description

ProcessReady Gets process ready

ActivateGameServerSession Activates game server session

AcceptPlayerSession Receives player session

RemovePlayerSession Removes player session

DescribePlayerSessions Gets player session list

UpdatePlayerSessionCreationPolicy Updates player session creation policy

TerminateGameServerSession Ends game server session

ProcessEnding Ends process

ReportCustomData Reports custom data

Others

When the game process uses gRPC to call a game server active API, you need to add two fields to
meta of the gRPC request.

Field Description Type

pid pid of the current game process string

requestId of the current request, which is used to uniquely identify a


requestId string
request

1. Generally, after the server is initialized, the process will check itself to see whether it can provide
services, and the game server will call the ProcessReady API to notify GSE that the process is
ready to host a game server session. After receiving the notification, GSE will change the status of
the server instance to "Active".

Status GseManager::ProcessReady(std::vector<std::string> &amp;logPath, int clientPort, int grp


cPort, GseResponse&amp; reply)
{
ProcessReadyRequest request;
// Log path
for (auto iter = logPath.begin(); iter != logPath.end(); iter++)
{
request.add_logpathstoupload(*iter);
}

©2013-2019 Tencent Cloud. All rights reserved. Page 10 of 105


Game Server Elastic-scaling

GConsoleLog-&gt;PrintOut(true, "ProcessReady clientPort is %d\n", clientPort);


GConsoleLog-&gt;PrintOut(true, "ProcessReady grpcPort is %d\n", grpcPort);

// Set the ports


request.set_clientport(clientPort);
request.set_grpcport(grpcPort);

ClientContext context;
AddMetadata(context);

// Ready to provide services


return stub_-&gt;ProcessReady(&amp;context, request, &amp;reply);
}

2. After the process is ready, GSE will call the OnHealthCheck API to perform a health check on the
game server every minute. If the health check fails three consecutive times, the process will be
considered to be unhealthy, and no game server sessions will be assigned to it.

Status GameServerGrpcSdkServiceImpl::OnHealthCheck(ServerContext* context, const HealthCheckRe


quest* request, HealthCheckResponse* reply)
{
reply->set_healthstatus(healthStatus);
return Status::OK;
}

3. Because the client calls the CreateGameServerSession API to create a game server session and
assigns it to a process, GSE will be triggered to call the onStartGameServerSession API for the
process and change the status of GameServerSession to "Activating".

Status GameServerGrpcSdkServiceImpl::OnStartGameServerSession(ServerContext* context, const St


artGameServerSessionRequest* request, GseResponse* reply)
{
auto gameServerSession = request->gameserversession();
GGseManager->SetGameServerSession(gameServerSession);
GseResponse processReadyReply;
Status status = GGseManager->ActivateGameServerSession(gameServerSession.gameserversessionid()
, gameServerSession.maxplayers(), processReadyReply);
// Determine whether the activation has succeeded based on `status` and `replay`

©2013-2019 Tencent Cloud. All rights reserved. Page 11 of 105


Game Server Elastic-scaling

return Status::OK;
}

4. After the game server receives onStartGameServerSession , you need to handle the logic or
resource allocation by yourself. After everything is ready, the game server will call the
ActivateGameServerSession API to notify GSE that the game server session has been assigned to a
process and is ready to receive player requests and will change the server status to "Active".

Status GseManager::ActivateGameServerSession(std::string gameServerSessionId, int maxPlayers, Gse


Response& reply)
{
GConsoleLog->PrintOut(true, "ActivateGameServerSession gameServerSessionId is %s\n", gameServerSe
ssionId.c_str());
GConsoleLog->PrintOut(true, "ActivateGameServerSession maxPlayers is %d\n", maxPlayers);
ActivateGameServerSessionRequest request;
request.set_gameserversessionid(gameServerSessionId);
request.set_maxplayers(maxPlayers);

ClientContext context;
AddMetadata(context);

return stub_->ActivateGameServerSession(&context, request, &reply);


}

5. After the client calls the JoinGameServerSession API for the player to join, the game server will call
the AcceptPlayerSession API to verify the validity of the player. If the connection is accepted, the
status of PlayerSession will be set to "Active". If the client receives no response within 60
seconds after calling the JoinGameServerSession API, it will change the status of PlayerSession to
"Timeout" and then call JoinGameServerSession again.

Status GseManager::AcceptPlayerSession(std::string playerSessionId, GseResponse& reply)


{
AcceptPlayerSessionRequest request;
request.set_gameserversessionid(gameServerSession.gameserversessionid());
request.set_playersessionid(playerSessionId);
ClientContext context;
AddMetadata(context);

©2013-2019 Tencent Cloud. All rights reserved. Page 12 of 105


Game Server Elastic-scaling

return stub_->AcceptPlayerSession(&context, request, &reply);


}

6. After the game ends or the player exits, the game server will call the RemovePlayerSession API to
remove the player, change the status of playersession to "Completed", and reserve the player slot
in the game server session.

Status GseManager::RemovePlayerSession(std::string playerSessionId, GseResponse& reply)


{
GConsoleLog->PrintOut(true, "RemovePlayerSession playerSessionId is %s\n", playerSessionId.c_str
());
RemovePlayerSessionRequest request;
request.set_gameserversessionid(gameServerSession.gameserversessionid());
request.set_playersessionid(playerSessionId);
ClientContext context;
AddMetadata(context);

return stub_->RemovePlayerSession(&context, request, &reply);


}

7. After a game server session (a game battle or a service) ends, the game server will call the
TerminateGameServerSession API to end the GameServerSession and change its status to
Terminated .

Status GseManager::TerminateGameServerSession(GseResponse& reply)


{
GConsoleLog->PrintOut(true, "start to TerminateGameServerSession\n");
TerminateGameServerSessionRequest request;
request.set_gameserversessionid(gameServerSession.gameserversessionid());
ClientContext context;
AddMetadata(context);

return stub_->TerminateGameServerSession(&context, request, &reply);


}

8. In case of health check failure or reduction, GSE will call the OnProcessTerminate API to end the
game process. The reduction will be triggered according to the protection policy configured in the
GSE console.

©2013-2019 Tencent Cloud. All rights reserved. Page 13 of 105


Game Server Elastic-scaling

Status GameServerGrpcSdkServiceImpl::OnProcessTerminate(ServerContext* context, const ProcessT


erminateRequest* request, GseResponse* reply)
{
auto terminationTime = request->terminationtime();
GGseManager->SetTerminationTime(terminationTime);
// If the following two APIs are called, the game server session will be ended immediately. We
recommend you call `ProcessEnding` to end the process only when there are no players or game s
erver sessions
// If the following two APIs are not called, `ProcessEnding` will be called to end the process
according to the protection policy. We recommend you configure time-period protection

//End the game server sessions


GseResponse terminateGameServerSessionReply;
GGseManager->TerminateGameServerSession(terminateGameServerSessionReply);

// End the processes


GseResponse processEndingReply;
GGseManager->ProcessEnding(processEndingReply);

return Status::OK;
}

9. The game server calls the ProcessEnding API to end the process immediately, change the server
process status to "Terminated", and repossess the resources.

// Active call: a game battle corresponds to a process. The `ProcessEnding` API will be active
ly called after the game battle ends
// Passive call: in case of reduction, process exception, or health check failure, the `Proces
sEnding` API will be called passively according to the protection policy. If a full protection
or time-period protection policy is configured, it is required to determine whether there are
any players in the game server session before the passive call can be made
Status GseManager::ProcessEnding(GseResponse& reply)
{
GConsoleLog->PrintOut(true, "start to ProcessEnding\n");
ProcessEndingRequest request;
ClientContext context;
AddMetadata(context);

©2013-2019 Tencent Cloud. All rights reserved. Page 14 of 105


Game Server Elastic-scaling

return stub_->ProcessEnding(&context, request, &reply);


}

10. The game server calls the DescribePlayerSessions API to get the information of the player in the
game server session (which is optional based on your actual business needs).

Status GseManager::DescribePlayerSessions(std::string gameServerSessionId, std::string playerId,


std::string playerSessionId,std::string playerSessionStatusFilter, std::string nextToken, int lim
it, DescribePlayerSessionsResponse& reply)
{
GConsoleLog->PrintOut(true, "start to DescribePlayerSessions\n");
DescribePlayerSessionsRequest request;
request.set_gameserversessionid(gameServerSessionId);
request.set_playerid(playerId);
request.set_playersessionid(playerSessionId);
request.set_playersessionstatusfilter(playerSessionStatusFilter);
request.set_nexttoken(nextToken);
request.set_limit(limit);
ClientContext context;
AddMetadata(context);

return stub_->DescribePlayerSessions(&context, request, &reply);


}

1. The game server calls the UpdatePlayerSessionCreationPolicy API to update the player session
creation policy and set whether to accept new players, i.e., whether to allow new players to join a
game session (which is optional based on your actual business needs).

Status GseManager::UpdatePlayerSessionCreationPolicy(std::string newpolicy, GseResponse& reply


)
{
GConsoleLog->PrintOut(true, "UpdatePlayerSessionCreationPolicy, newpolicy is %s\n", newpolicy.
c_str());
UpdatePlayerSessionCreationPolicyRequest request;
request.set_gameserversessionid(gameServerSession.gameserversessionid());
request.set_newplayersessioncreationpolicy(newpolicy);
ClientContext context;
AddMetadata(context);

©2013-2019 Tencent Cloud. All rights reserved. Page 15 of 105


Game Server Elastic-scaling

return stub_->UpdatePlayerSessionCreationPolicy(&context, request, &reply);


}

12. The game server calls the ReportCustomData API to notify GSE of the custom data that can be
viewed during game server session query (which is optional based on your actual business needs).

Status GseManager::ReportCustomData(int currentCustomCount, int maxCustomCount, GseResponse& repl


y)
{
GConsoleLog->PrintOut(true, "ReportCustomData, currentCustomCount is %d\n", currentCustomCount);
GConsoleLog->PrintOut(true, "ReportCustomData, maxCustomCount is %d\n", maxCustomCount);
ReportCustomDataRequest request;
request.set_currentcustomcount(currentCustomCount);
request.set_maxcustomcount(maxCustomCount);

ClientContext context;
AddMetadata(context);

return stub_->ReportCustomData(&context, request, &reply);


}

Launching Server for GSE to Call


Server running: launch GrpcServer .

GameServerGrpcSdkServiceImpl::GameServerGrpcSdkServiceImpl() : serverAddress("127.0.0.1:0"), heal


thStatus(true)
{
sem_init(&sem, 0, 0);
}
void GameServerGrpcSdkServiceImpl::StartGrpcServer()
{
ServerBuilder builder;
builder.AddListeningPort(serverAddress, grpc::InsecureServerCredentials(), &grpcPort);
builder.RegisterService(this);
std::unique_ptr<Server> server(builder.BuildAndStart());
sem_post(&sem);
server->Wait();
}

©2013-2019 Tencent Cloud. All rights reserved. Page 16 of 105


Game Server Elastic-scaling

Connecting Client to gRPC Server of GSE


Server connecting: create a gRPC channel, specify the host name and server port to connect to, and
use this channel to create a stub instance.

void GseManager::InitStub()
{
auto channel = grpc::CreateChannel("127.0.0.1:5758", grpc::InsecureChannelCredentials());
stub_ = GseGrpcSdkService::NewStub(channel);
}

Demo for C++


1. Click here to download the code of the Demo for C++.
2. Generate the gRPC code.
As the gRPC code has already been generated in the cpp-demo/source/grpcsdk directory of the
Demo for C++, you do not need to generate it again.
3. Launch the server for GSE to call.
Implement the server.
grpcserver.cpp in the cpp-demo/source/api directory implements three server APIs.
Run the server.
grpcserver.cpp in the cpp-demo/source/api directory launches GrpcServer .
4. Connect the client to the gRPC server of GSE.
Implement the client.
gsemanager.cpp in the cpp-demo/source/gsemanager directory implements nine client APIs.
Connect to the server.
Create a gRPC channel, specify the host name and server port to connect to, and use this
channel to create a stub instance.
5. Compile and run the project.
i. Install CMake.
ii. Install GCC v4.9 or above.
iii. Download the code and run the following command in the cpp-demo directory:

mkdir build
cmake ..
make

The corresponding cpp-demo executable file will be generated.

©2013-2019 Tencent Cloud. All rights reserved. Page 17 of 105


Game Server Elastic-scaling

iv. Package the cpp-demo executable file as an asset package and configure the launch path as
cpp-demo with no launch parameter needed.
v. Create a server fleet and deploy the asset package on it. After that, you can perform various
operations such as scaling.

©2013-2019 Tencent Cloud. All rights reserved. Page 18 of 105


Game Server Elastic-scaling

gRPC C# Tutorial
Last updated:2021-11-17 18:06:09

Installing gRPC
1. To use gRPC C#, you need to install .Net Core 3.1 SDK first. Taking CentOS as an example, the
version must be v7, v8 or above.
Add the signature key

sudo rpm -Uvh https://packages.microsoft.com/config/centos/7/packages-microsoft-prod.rpm

Install .NET Core SDK

sudo yum install dotnet-sdk-3.1

2. In addition, you can also use gRPC C# in the following runtime environments/IDEs:
Windows: .NET Framework 4.5 or higher, Visual Studio 2013 or higher, Visual Studio Code.
Linux: Mono 4 or higher, Visual Studio Code.
macOS X: Mono 4 or higher, Visual Studio Code, Visual Studio for Mac.

Note
For more information on the installation process, please see Installing gRPC C#.

Defining Service
gRPC uses Protocol Buffers to define a service: an RPC service specifies methods that can be called
remotely by using parameters and return types.

Note:
We provide the .proto files for service definition. You can click here to directly download them
with no need to generate them by yourself.

Generating gRPC Code

©2013-2019 Tencent Cloud. All rights reserved. Page 19 of 105


Game Server Elastic-scaling

1. After defining the service, you can use protoc (protocol buffer compiler) to generate the client and
server code (in any language supported by gRPC).
2. The generated code includes the client stub and the abstract APIs to be implemented by the
server.
3. Steps for generating gRPC code:
Download the code. In the csharp-demo directory, run

dotnet run

to automatically compile and run the service.


After the program is compiled and run correctly, the project's dependent libraries and binary
files, and the .cs files created by compiling the proto file will be generated in the csharp-
demo/obj/Debug/netcoreapp3.1 folder.
The proto file is imported in csharp-demo/csharpdemo.csproj :

<Protobuf Include="..\proto\csharp-demo\GameServerGrpcSdkService.proto" Link="GameServerGrpc


SdkService.proto"/>
<Protobuf Include="..\proto\csharp-demo\GseGrpcSdkService.proto" Link="GseGrpcSdkService.pro
to" />

The project relies on the two proto files GameServerGrpcSdkService.proto and


GseGrpcSdkService.proto in the proto/csharp-demo folder.

Game Process Integration Process

©2013-2019 Tencent Cloud. All rights reserved. Page 20 of 105


Game Server Elastic-scaling

Game server callback API list

API Name API Description

OnHealthCheck Runs health check

OnStartGameServerSession Receives game server session

OnProcessTerminate Ends game process

Game server active API list

©2013-2019 Tencent Cloud. All rights reserved. Page 21 of 105


Game Server Elastic-scaling

API Name API Description

ProcessReady Gets process ready

ActivateGameServerSession Activates game server session

AcceptPlayerSession Receives player session

RemovePlayerSession Removes player session

DescribePlayerSessions Gets player session list

UpdatePlayerSessionCreationPolicy Updates player session creation policy

TerminateGameServerSession Ends game server session

ProcessEnding Ends process

ReportCustomData Reports custom data

Others

When the game process uses gRPC to call a game server active API, you need to add two fields to
meta of the gRPC request.

Field Description Type

pid pid of the current game process string

requestId of the current request, which is used to uniquely identify a


requestId string
request

1. Generally, after the server is initialized, the process will check itself to see whether it can provide
services, and the game server will call the ProcessReady API to notify GSE that the process is
ready to host a game server session. After receiving the notification, GSE will change the status of
the server instance to "Active".

public static GseResponse ProcessReady(string[] logPath, int clientPort, int grpcPort)


{
logger.Println($"Getting process ready, LogPath: {logPath}, ClientPort: {clientPort}, GrpcPor
t: {grpcPort}");
// Set the ports
var req = new ProcessReadyRequest{
ClientPort = clientPort,
GrpcPort = grpcPort,
};

©2013-2019 Tencent Cloud. All rights reserved. Page 22 of 105


Game Server Elastic-scaling

// Log path
req.LogPathsToUpload.Add(logPath); // After being parsed by `pb`, the `repeated` type is read-
only and needs to be added by running `Add`
// Ready to provide services
return GrpcClient.GseClient.ProcessReady(req, meta);
}

2. After the process is ready, GSE will call the OnHealthCheck API to perform a health check on the
game server every minute. If the health check fails three consecutive times, the process will be
considered to be unhealthy, and no game server sessions will be assigned to it.

public override Task<HealthCheckResponse> OnHealthCheck(HealthCheckRequest request, ServerCall


Context context)
{
logger.Println($"OnHealthCheck, HealthStatus: {GseManager.HealthStatus}");
return Task.FromResult(new HealthCheckResponse{
HealthStatus = GseManager.HealthStatus
});
}

3. Because the client calls the CreateGameServerSession API to create a game server session and
assigns it to a process, GSE will be triggered to call the onStartGameServerSession API for the
process and change the status of GameServerSession to "Activating".

public override Task<GseResponse> OnStartGameServerSession(StartGameServerSessionRequest reque


st, ServerCallContext context)
{
logger.Println($"OnStartGameServerSession, request: {request}");
GseManager.SetGameServerSession(request.GameServerSession);
var resp = GseManager.ActivateGameServerSession(request.GameServerSession.GameServerSessionId,
request.GameServerSession.MaxPlayers);
return Task.FromResult(resp);
}

4. After the game server receives onStartGameServerSession , you need to handle the logic or
resource allocation by yourself. After everything is ready, the game server will call the
ActivateGameServerSession API to notify GSE that the game server session has been assigned to a
process and is ready to receive player requests and will change the server status to "Active".

public static GseResponse ActivateGameServerSession(string gameServerSessionId, int maxPlayer


s)

©2013-2019 Tencent Cloud. All rights reserved. Page 23 of 105


Game Server Elastic-scaling

{
logger.Println($"Activating game server session, GameServerSessionId: {gameServerSessionId}, M
axPlayers: {maxPlayers}");
var req = new ActivateGameServerSessionRequest{
GameServerSessionId = gameServerSessionId,
MaxPlayers = maxPlayers,
};
return GrpcClient.GseClient.ActivateGameServerSession(req, meta);
}

5. After the client calls the JoinGameServerSession API for the player to join, the game server will call
the AcceptPlayerSession API to verify the validity of the player. If the connection is accepted, the
status of PlayerSession will be set to "Active". If the client receives no response within 60
seconds after calling the JoinGameServerSession API, it will change the status of PlayerSession to
"Timeout" and then call JoinGameServerSession again.

public static GseResponse AcceptPlayerSession(string playerSessionId)


{
logger.Println($"Accepting player session, PlayerSessionId: {playerSessionId}");
var req = new AcceptPlayerSessionRequest{
GameServerSessionId = gameServerSession.GameServerSessionId,
PlayerSessionId = playerSessionId,
};
return GrpcClient.GseClient.AcceptPlayerSession(req, meta);
}

6. After the game ends or the player exits, the game server will call the RemovePlayerSession API to
remove the player, change the status of playersession to "Completed", and reserve the player
slot in the game server session.

public static GseResponse RemovePlayerSession(string playerSessionId)


{
logger.Println($"Removing player session, PlayerSessionId: {playerSessionId}");
var req = new RemovePlayerSessionRequest{
GameServerSessionId = gameServerSession.GameServerSessionId,
PlayerSessionId = playerSessionId,
};
return GrpcClient.GseClient.RemovePlayerSession(req, meta);
}

©2013-2019 Tencent Cloud. All rights reserved. Page 24 of 105


Game Server Elastic-scaling

7. After a game server session (a game battle or a service) ends, the game server will call the
TerminateGameServerSession API to end the GameServerSession and change its status to
Terminated .

public static GseResponse TerminateGameServerSession()


{
logger.Println($"Terminating game server session, GameServerSessionId: {gameServerSession.Game
ServerSessionId}");
var req = new TerminateGameServerSessionRequest{
GameServerSessionId = gameServerSession.GameServerSessionId
};
return GrpcClient.GseClient.TerminateGameServerSession(req, meta);
}

8. In case of health check failure or reduction, GSE will call the OnProcessTerminate API to end the
game process. The reduction will be triggered according to the protection policy configured in the
GSE console.

public override Task<gseresponse> OnProcessTerminate(ProcessTerminateRequest request, ServerCa


llContext context)
{
logger.Println($"OnProcessTerminate, request: {request}");
// Set the process termination time
GseManager.SetTerminationTime(request.TerminationTime);
// If the following two APIs are called, the game server session will be ended immediately. We
recommend you call `ProcessEnding` to end the process only when there are no players or game s
erver sessions
// If the following two APIs are not called, `ProcessEnding` will be called to end the process
according to the protection policy. We recommend you configure time-period protection
// Terminate game server sessions
GseManager.TerminateGameServerSession();
// Exit the process
GseManager.ProcessEnding();
return Task.FromResult(new GseResponse{
Status = GseResponse.Types.Status.Ok,
ResponseData = "SUCCESS",
});
}

9. The game server calls the ProcessEnding API to end the process immediately, change the server
process status to "Terminated", and repossess the resources.

©2013-2019 Tencent Cloud. All rights reserved. Page 25 of 105


Game Server Elastic-scaling

// Active call: a game battle corresponds to a process. The `ProcessEnding` API will be active
ly called after the game battle ends
// Passive call: in case of reduction, process exception, or health check failure, the `Proces
sEnding` API will be called passively according to the protection policy. If a full protection
or time-period protection policy is configured, it is required to determine whether there are
any players in the game server session before the passive call can be made
public static GseResponse ProcessEnding()
{
logger.Println($"Process ending, pid: {pid}");
var req = new ProcessEndingRequest();
return GrpcClient.GseClient.ProcessEnding(req, meta);
}

0. The game server calls the DescribePlayerSessions API to get the information of the player in the
game server session (which is optional based on your actual business needs).

public static DescribePlayerSessionsResponse DescribePlayerSessions(string gameServerSessionI


d, string playerId, string playerSessionId, string playerSessionStatusFilter, string nextToke
n, int limit)
{
logger.Println($"Describing player session, GameServerSessionId: {gameServerSessionId}, Player
Id: {playerId}, PlayerSessionId: {playerSessionId}, PlayerSessionStatusFilter: {playerSessionS
tatusFilter}, NextToken: {nextToken}, Limit: {limit}");
var req = new DescribePlayerSessionsRequest{
GameServerSessionId = gameServerSessionId,
PlayerId = playerId,
PlayerSessionId = playerSessionId,
PlayerSessionStatusFilter = playerSessionStatusFilter,
NextToken = nextToken,
Limit = limit,
};
return GrpcClient.GseClient.DescribePlayerSessions(req, meta);
}

1. The game server calls the UpdatePlayerSessionCreationPolicy API to update the player session
creation policy and set whether to accept new players, i.e., whether to allow new players to join a
game session (which is optional based on your actual business needs).

public static GseResponse UpdatePlayerSessionCreationPolicy(string newPolicy)


{
logger.Println($"Updating player session creation policy, newPolicy: {newPolicy}");
var req = new UpdatePlayerSessionCreationPolicyRequest{
GameServerSessionId = gameServerSession.GameServerSessionId,

©2013-2019 Tencent Cloud. All rights reserved. Page 26 of 105


Game Server Elastic-scaling

NewPlayerSessionCreationPolicy = newPolicy,
};
return GrpcClient.GseClient.UpdatePlayerSessionCreationPolicy(req, meta);
}

2. The game server calls the ReportCustomData API to notify GSE of the custom data (which is
optional based on your actual business needs).

public static GseResponse ReportCustomData(int currentCustomCount, int maxCustomCount)


{
logger.Println($"Reporting custom data, CurrentCustomCount: {currentCustomCount}, MaxCustomCou
nt: {maxCustomCount}");
var req = new ReportCustomDataRequest{
CurrentCustomCount = currentCustomCount,
MaxCustomCount = maxCustomCount,
};
return GrpcClient.GseClient.ReportCustomData(req, meta);
}

Launching Server for GSE to Call


Server running: launch GrpcServer .

public class Program


{
public static int ClientPort = PortServer.GenerateRandomPort(2000, 6000);
public static int GrpcPort = PortServer.GenerateRandomPort(6001, 10000);

public static void Main(string[] args)


{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =&gt;
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =&gt;
{
webBuilder.ConfigureKestrel(options =&gt;
{
// gRPC port (set the HTTP/2 endpoint without TLS certificate)
options.ListenAnyIP(GrpcPort, o =&gt; o.Protocols =
HttpProtocols.Http2);

// HTTP port

©2013-2019 Tencent Cloud. All rights reserved. Page 27 of 105


Game Server Elastic-scaling

options.ListenAnyIP(ClientPort);
});

webBuilder.UseStartup<startup>();
});
}

Connecting Client to gRPC Server of GSE


Server connecting: create a gRPC channel, specify the host name and server port to connect to, and
use this channel to create a stub instance.

public class GrpcClient


{
private static string agentAdress = "127.0.0.1:5758";
public static GameServerGrpcSdkService.GameServerGrpcSdkServiceClient GameServerClient
{
get
{
Channel channel = new Channel(agentAdress, ChannelCredentials.Insecure);
return new GameServerGrpcSdkService.GameServerGrpcSdkServiceClient(channel);
}
}
public static GseGrpcSdkService.GseGrpcSdkServiceClient GseClient
{
get
{
Channel channel = new Channel(agentAdress, ChannelCredentials.Insecure);
return new GseGrpcSdkService.GseGrpcSdkServiceClient(channel);
}
}
}

Demo for C#
1. Click here to download the code of the Demo for C#.
2. Generate the gRPC code.
As the gRPC code has already been generated in the proto/csharp-demo directory of the Demo for
C#, you do not need to generate it again.
3. Launch the server for GSE to call.

©2013-2019 Tencent Cloud. All rights reserved. Page 28 of 105


Game Server Elastic-scaling

Implement the server.


gameserversdk.cs in the csharp-demo/api directory implements three server APIs.
Run the server.
Program.cs in the csharp-demo directory launches GrpcServer .
4. Connect the client to the gRPC server of GSE.
Implement the client.
GseManager.cs in the csharp-demo/Models directory implements nine client APIs.
Connect to the server.
Create a gRPC channel, specify the host name and server port to connect to, and use this
channel to create a stub instance.
5. Compile and run the program.
i. Generate the executable file and dependencies

dotnet publish -c Release -r linux-x64 --self-contained true

The above operation will generate all the dependent files needed to generate and package the
asset package in the csharp-demo/bin/Release/netcoreapp3.1/linux-x64 directory, which contains
the executable file csharpdemo used to run the service.
Copy the pre-request script install.sh

chmod u+x install.sh


cp install.sh bin/Release/netcoreapp3.1/linux-x64

Package the GSE asset package

cd csharp-demo/bin/Release/netcoreapp3.1/linux-x64
zip -r csharpdemo.zip *

The packaged csharpdemo.zip is the asset package needed by GSE. Configure the launch path
as csharpdemo with no launch parameter needed.
Create a server fleet and deploy the asset package on it. After that, you can perform various
operations such as scaling.

©2013-2019 Tencent Cloud. All rights reserved. Page 29 of 105


Game Server Elastic-scaling

gRPC Go Tutorial
Last updated:2021-11-17 18:06:09

Installing gRPC
1. To use gRPC Go, you need to install the latest major release of Go first.
2. Install the protocol buffer compiler protoc3.
3. Install the Go plugin in the protocol buffer compiler.
Run the following command to install the protocol buffer compiler plugin for Go (protoc-gen-go):

$ export GO111MODULE=on # Enable module mode


$ go get github.com/golang/protobuf/protoc-gen-go

Update the path so that the protocol buffer compiler can find the Go plugin:

$ export PATH="$PATH:$(go env GOPATH)/bin"

Note
For more information on the installation process, see Installing Go and Installing Protocol Buffer
Compiler.

Defining Service
gRPC uses Protocol Buffers to define a service: an RPC service specifies methods that can be called
remotely by using parameters and return types.

Note
We provide the .proto files for service definition. You can click here to directly download them
with no need to generate them by yourself.

Generating gRPC Code

©2013-2019 Tencent Cloud. All rights reserved. Page 30 of 105


Game Server Elastic-scaling

1. After defining the service, you can use protoc (protocol buffer compiler) to generate the client and
server code (in any language supported by gRPC).

2. The generated code includes the client stub and the abstract APIs to be implemented by the
server.

3. Steps for generating gRPC code:


In the proto directory, run:

protoc --go_out=plugins=grpc:. *.proto

to automatically generate the go_package path that contains proto. You can modify the
go_package path as needed but not the package.

## Game Process Integration Process


![](https://main.qcloudimg.com/raw/96018551bc88c71a02333b1f197b3111.png)
#### Game server callback API list
| API Name | API Description |
|-----|----|
|[OnHealthCheck](https://intl.cloud.tencent.com/document/product/1055/37422)| Runs health chec
k |
|[OnStartGameServerSession](https://intl.cloud.tencent.com/document/product/1055/37423)| Recei
ves game server session |
|[OnProcessTerminate](https://intl.cloud.tencent.com/document/product/1055/37424)| Ends game p
rocess |
#### Game server active API list
| API Name | API Description |
|-----|----|
|[ProcessReady](https://intl.cloud.tencent.com/document/product/1055/37426)| Gets process read
y |
|[ActivateGameServerSession](https://intl.cloud.tencent.com/document/product/1055/37427)| Acti
vates game server session |
|[AcceptPlayerSession](https://intl.cloud.tencent.com/document/product/1055/37428)| Receives p
layer session |
|[RemovePlayerSession](https://intl.cloud.tencent.com/document/product/1055/37429)| Removes pl
ayer session |
|[DescribePlayerSessions](https://intl.cloud.tencent.com/document/product/1055/37430)| Gets pl
ayer session list |
|[UpdatePlayerSessionCreationPolicy](https://intl.cloud.tencent.com/document/product/1055/3743
1)| Updates player session creation policy |
|[TerminateGameServerSession](https://intl.cloud.tencent.com/document/product/1055/37432)| End
s game server session |
|[ProcessEnding](https://intl.cloud.tencent.com/document/product/1055/37434)| Ends process |
|[ReportCustomData](https://intl.cloud.tencent.com/document/product/1055/37435)| Reports custo

©2013-2019 Tencent Cloud. All rights reserved. Page 31 of 105


Game Server Elastic-scaling

m data |
#### Others
When the game process uses gRPC to call a game server active API, you need to add two fields t
o `meta` of the gRPC request.
| Field | Description | Type |
| --------- | ----------------------------------------- | ------ |
| pid | `pid` of the current game process | string |
| requestId | `requestId` of the current request, which is used to uniquely identify a request
| string |
1. Generally, after the server is initialized, the process will check itself to see whether it
can provide services, and the game server will call the `ProcessReady` API to notify GSE that
the process is ready to host a game server session. After receiving the notification, GSE will
change the status of the server instance to "Active".

Go

func (g *gsemanager) ProcessReady(logPath []string, clientPort int32, grpcPort int32) error {


logger.Info("start to processready", zap.Any("logPath", logPath), zap.Int32("clientPort", clientPort),

zap.Int32("grpcPort", grpcPort))

req := &grpcsdk.ProcessReadyRequest{

// Log path
LogPathsToUpload: logPath,
// Set the ports
ClientPort: clientPort,
GrpcPort: grpcPort,

_, err := g.rpcClient.ProcessReady(g.getContext(), req)


if err != nil {

logger.Info("ProcessReady fail", zap.Error(err))


return err

// Ready to provide services


logger.Info("ProcessReady success")

©2013-2019 Tencent Cloud. All rights reserved. Page 32 of 105


Game Server Elastic-scaling

return nil
}

2. After the process is ready, GSE will call the `OnHealthCheck` API to perform a health check
on the game server every minute. If the health check fails three consecutive times, the proces
s will be considered to be unhealthy, and no game server sessions will be assigned to it.

Go

func _GameServerGrpcSdkService_OnHealthCheck_Handler(srv interface{}, ctx context.Context,


dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(HealthCheckRequest)
if err := dec(in); err != nil {

return nil, err

}
if interceptor == nil {

return srv.(GameServerGrpcSdkServiceServer).OnHealthCheck(ctx, in)

}
info := &grpc.UnaryServerInfo{

Server: srv,
FullMethod: "/tencentcloud.gse.grpcsdk.GameServerGrpcSdkService/OnHealthCheck",

}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {

return srv.(GameServerGrpcSdkServiceServer).OnHealthCheck(ctx, req.(*HealthCheckRequest))

}
return interceptor(ctx, in, info, handler)
}

3. Because the client calls the [CreateGameServerSession](https://intl.cloud.tencent.com/docum


ent/product/1055/37139) API to create a game server session and assigns it to a process, GSE w

©2013-2019 Tencent Cloud. All rights reserved. Page 33 of 105


Game Server Elastic-scaling

ill be triggered to call the `onStartGameServerSession` API for the process and change the sta
tus of `GameServerSession` to "Activating".

Go

func _GameServerGrpcSdkService_OnStartGameServerSession_Handler(srv interface{}, ctx


context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor)
(interface{}, error) {
in := new(StartGameServerSessionRequest)
if err := dec(in); err != nil {

return nil, err

}
if interceptor == nil {

return srv.(GameServerGrpcSdkServiceServer).OnStartGameServerSession(ctx, in)

}
info := &grpc.UnaryServerInfo{

Server: srv,
FullMethod: "/tencentcloud.gse.grpcsdk.GameServerGrpcSdkService/OnStartGameServerSession",

}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {

return srv.(GameServerGrpcSdkServiceServer).OnStartGameServerSession(ctx, req.(*StartGameServe


rSessionRequest))

}
return interceptor(ctx, in, info, handler)
}

4. After the game server receives `onStartGameServerSession`, you need to handle the logic or
resource allocation by yourself. After everything is ready, the game server will call the `Act
ivateGameServerSession` API to notify GSE that the game server session has been assigned to a
process and is ready to receive player requests and will change the server status to "Active".

©2013-2019 Tencent Cloud. All rights reserved. Page 34 of 105


Game Server Elastic-scaling

Go

func (g *gsemanager) ActivateGameServerSession(gameServerSessionId string, maxPlayers int32)


error {
logger.Info("start to ActivateGameServerSession", zap.String("gameServerSessionId",
gameServerSessionId),

zap.Int32("maxPlayers", maxPlayers))

req := &grpcsdk.ActivateGameServerSessionRequest{

GameServerSessionId: gameServerSessionId,
MaxPlayers: maxPlayers,

_, err := g.rpcClient.ActivateGameServerSession(g.getContext(), req)


if err != nil {

logger.Error("ActivateGameServerSession fail", zap.Error(err))


return err

logger.Info("ActivateGameServerSession success")
return nil
}

5. After the client calls the [JoinGameServerSession](https://intl.cloud.tencent.com/document/


product/1055/39130) API for the player to join, the game server will call the `AcceptPlayerSes
sion` API to verify the validity of the player. If the connection is accepted, the status of `
PlayerSession` will be set to "Active". If the client receives no response within 60 seconds a
fter calling the `JoinGameServerSession` API, it will change the status of `PlayerSession` to
"Timeout" and then call `JoinGameServerSession` again.

func (g gsemanager) AcceptPlayerSession(playerSessionId string) (grpcsdk.GseResponse, error) {


logger.Info("start to AcceptPlayerSession", zap.String("playerSessionId", playerSessionId))
req := &grpcsdk.AcceptPlayerSessionRequest{

©2013-2019 Tencent Cloud. All rights reserved. Page 35 of 105


Game Server Elastic-scaling

GameServerSessionId: g.gameServerSession.GameServerSessionId,
PlayerSessionId: playerSessionId,

return g.rpcClient.AcceptPlayerSession(g.getContext(), req)


}

6. After the game ends or the player exits, the game server will call the `RemovePlayerSession
` API to remove the player, change the status of `playersession` to "Completed", and reserve t
he player slot in the game server session.

Go

func (g gsemanager) RemovePlayerSession(playerSessionId string) (grpcsdk.GseResponse, error)


{
logger.Info("start to RemovePlayerSession", zap.String("playerSessionId", playerSessionId))
req := &grpcsdk.RemovePlayerSessionRequest{

GameServerSessionId: g.gameServerSession.GameServerSessionId,
PlayerSessionId: playerSessionId,

return g.rpcClient.RemovePlayerSession(g.getContext(), req)


}

7. After a game server session (a game battle or a service) ends, the game server will call th
e `TerminateGameServerSession` API to end the `GameServerSession` and change its status to `Te
rminated`.

Go

func (g gsemanager) TerminateGameServerSession() (grpcsdk.GseResponse, error) {


logger.Info("start to TerminateGameServerSession")
req := &grpcsdk.TerminateGameServerSessionRequest{

GameServerSessionId: g.gameServerSession.GameServerSessionId,

©2013-2019 Tencent Cloud. All rights reserved. Page 36 of 105


Game Server Elastic-scaling

return g.rpcClient.TerminateGameServerSession(g.getContext(), req)


}

8. In case of health check failure or reduction, GSE will call the `OnProcessTerminate` API to
end the game process. The reduction will be triggered according to the [protection policy](htt
ps://intl.cloud.tencent.com/document/product/1055/36675) configured in the GSE console.

Go

func _GameServerGrpcSdkService_OnProcessTerminate_Handler(srv interface{}, ctx


context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor)
(interface{}, error) {
in := new(ProcessTerminateRequest)
if err := dec(in); err != nil {

return nil, err

}
if interceptor == nil {

return srv.(GameServerGrpcSdkServiceServer).OnProcessTerminate(ctx, in)

}
info := &grpc.UnaryServerInfo{

Server: srv,
FullMethod: "/tencentcloud.gse.grpcsdk.GameServerGrpcSdkService/OnProcessTerminate",

}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {

return srv.(GameServerGrpcSdkServiceServer).OnProcessTerminate(ctx, req.(*ProcessTerminateRequ


est))

}
return interceptor(ctx, in, info, handler)

©2013-2019 Tencent Cloud. All rights reserved. Page 37 of 105


Game Server Elastic-scaling

9. The game server calls the `ProcessEnding` API to end the process immediately, change the se
rver process status to "Terminated", and repossess the resources.

Go

// Active call: a game battle corresponds to a process. The ProcessEnding API will be actively
called after the game battle ends
// Passive call: in case of reduction, process exception, or health check failure, the ProcessEnding
API will be called passively according to the protection policy. If a full protection or time-period
protection policy is configured, it is required to determine whether there are any players in the
game server session before the passive call can be made
func (g gsemanager) ProcessEnding() (grpcsdk.GseResponse, error) {
logger.Info("start to ProcessEnding")
req := &grpcsdk.ProcessEndingRequest{
}

return g.rpcClient.ProcessEnding(g.getContext(), req)


}

10. The game server calls the `DescribePlayerSessions` API to get the information of the playe
r in the game server session (which is optional based on your actual business needs).

Go

func (g gsemanager) DescribePlayerSessions(gameServerSessionId, playerId, playerSessionId,


playerSessionStatusFilter, nextToken string,limit int32) (grpcsdk.DescribePlayerSessionsResponse,
error) {
logger.Info("start to DescribePlayerSessions", zap.String("gameServerSessionId",
gameServerSessionId),

zap.String("playerId", playerId), zap.String("playerSessionId", playerSessionId),


zap.String("playerSessionStatusFilter", playerSessionStatusFilter), zap.String("nextToken", ne
xtToken),
zap.Int32("limit", limit))

req := &grpcsdk.DescribePlayerSessionsRequest{

©2013-2019 Tencent Cloud. All rights reserved. Page 38 of 105


Game Server Elastic-scaling

GameServerSessionId: gameServerSessionId,
PlayerId: playerId,
PlayerSessionId: playerSessionId,
PlayerSessionStatusFilter: playerSessionStatusFilter,
NextToken: nextToken,
Limit: limit,

return g.rpcClient.DescribePlayerSessions(g.getContext(), req)


}

11. The game server calls the `UpdatePlayerSessionCreationPolicy` API to update the player ses
sion creation policy and set whether to accept new players, i.e., whether to allow new players
to join a game session (which is optional based on your actual business needs).

Go

func (g gsemanager) UpdatePlayerSessionCreationPolicy(newPolicy string) (grpcsdk.GseResponse,


error) {
logger.Info("start to UpdatePlayerSessionCreationPolicy", zap.String("newPolicy", newPolicy))
req := &grpcsdk.UpdatePlayerSessionCreationPolicyRequest{

GameServerSessionId: g.gameServerSession.GameServerSessionId,
NewPlayerSessionCreationPolicy: newPolicy,

return g.rpcClient.UpdatePlayerSessionCreationPolicy(g.getContext(), req)


}

12. The game server calls the `ReportCustomData` API to notify GSE of the custom data (which i
s optional based on your actual business needs).

Go

func (g gsemanager) ReportCustomData(currentCustomCount, maxCustomCount int32)


(grpcsdk.GseResponse, error) {
logger.Info("start to UpdatePlayerSessionCreationPolicy", zap.Int32("currentCustomCount",
currentCustomCount),

©2013-2019 Tencent Cloud. All rights reserved. Page 39 of 105


Game Server Elastic-scaling

zap.Int32("maxCustomCount", maxCustomCount))

req := &grpcsdk.ReportCustomDataRequest{

CurrentCustomCount: currentCustomCount,
MaxCustomCount: maxCustomCount,

return g.rpcClient.ReportCustomData(g.getContext(), req)


}

## Launching Server for GSE to Call


Server running: launch `GrpcServer`.

Go

func (s *rpcService) StartGrpcServer() {


listen, err := net.Listen("tcp", "127.0.0.1:")
if err != nil {

logger.Fatal("grpc fail to listen", zap.Error(err))

addr := listen.Addr().String()
portStr := strings.Split(addr, ":")[1]
s.grpcPort, err = strconv.Atoi(portStr)
if err != nil {

logger.Fatal("grpc fail to get port",zap.Error(err))

logger.Info("grpc listen port is", zap.Int("port", s.grpcPort))

©2013-2019 Tencent Cloud. All rights reserved. Page 40 of 105


Game Server Elastic-scaling

grpcServer := grpc.NewServer()
grpcsdk.RegisterGameServerGrpcSdkServiceServer(grpcServer, s)
logger.Info("start grpc server success")
go grpcServer.Serve(listen)
}

## Connecting Client to gRPC Server of GSE


Server connecting: create a gRPC channel, specify the host name and server port to connect to,
and use this channel to create a stub instance.

Go

const (

localhost = "127.0.0.1"
agentPort = 5758

)
type gsemanager struct {

pid string
gameServerSession *grpcsdk.GameServerSession
terminationTime int64
rpcClient grpcsdk.GseGrpcSdkServiceClient

## Demo for Go
1. [Click here](https://gsegrpcdemo-1301007756.cos.ap-guangzhou.myqcloud.com/go-demo.zip) to d
ownload the code of the Demo for Go.
2. Generate the gRPC code.
As the gRPC code has already been generated in the `go-demo/grpcsdk` directory of the Demo for
Go, you do not need to generate it again.
3. Launch the server for GSE to call.
- Implement the server.
`grpcserver.go` in the `go-demo/api` directory implements three server APIs.
- Run the server.
`grpcserver.go` in the `go-demo/api` directory launches `GrpcServer`.
4. Connect the client to the gRPC server of GSE.
- Implement the client.
`gsemanager.go` in the `go-demo/gsemanager` directory implements nine client APIs.
- Connect to the server.

©2013-2019 Tencent Cloud. All rights reserved. Page 41 of 105


Game Server Elastic-scaling

Create a gRPC channel, specify the host name and server port to connect to, and use this chann
el to create a stub instance.
5. Compile and run the project.
1. In the `go-demo` directory, run

go mod vendor

to generate the vendor directory.

Run the compile command:

go build -mod=vendor main.go

to generate the corresponding go-demo executable file main.go.

4. Package the executable file main.go as an [asset package]


(https://intl.cloud.tencent.com/document/product/1055/36674) and configure the launch path as
main with no launch parameter needed.
5. [Create a server fleet](https://intl.cloud.tencent.com/document/product/1055/36675) and deploy the
asset package on it. After that, you can perform various operations such as [scaling]
(https://intl.cloud.tencent.com/document/product/1055/37445).

©2013-2019 Tencent Cloud. All rights reserved. Page 42 of 105


Game Server Elastic-scaling

gRPC Java Tutorial


Last updated:2021-11-17 18:06:09

Installing gRPC
1. gRPC Java does not require other tools except JDK.
2. Install the gRPC Java SNAPSHOT library locally, including the code generation plugin.

Note:
For more information on the installation process, please see Installing gRPC Java.

Defining Service
gRPC uses Protocol Buffers to define a service: an RPC service specifies methods that can be
called remotely by using parameters and return types.

Note
We provide the .proto files for service definition. You can click here to directly download them
with no need to generate them by yourself.

Generating gRPC Code


1. After defining the service, you can use protoc (protocol buffer compiler) to generate the client and
server code (in any language supported by gRPC).
2. The generated code includes the client stub and the abstract APIs to be implemented by the
server.
3. Methods for generating gRPC code:
Method 1. Execute the script under java-demo/src/main/proto . You need to download protoc
and protoc-gen-grpc-java generation tools from the gRPC website:

sh gen_pb.sh

©2013-2019 Tencent Cloud. All rights reserved. Page 43 of 105


Game Server Elastic-scaling

protoc --java_out=../java --proto_path=. GameServerGrpcSdkService.proto


protoc --plugin=protoc-gen-grpc-java=`which protoc-gen-grpc-java` --grpc-java_out=../java --
proto_path=. GameServerGrpcSdkService.proto
protoc --java_out=../java --proto_path=. GseGrpcSdkService.proto
protoc --plugin=protoc-gen-grpc-java=`which protoc-gen-grpc-java` --grpc-java_out=../java --
proto_path=. GseGrpcSdkService.proto

Method 2. Use the Maven tool to generate gRPC code by adding a Maven plugin for compiling
gRPC code to Maven. For more information, please see here.

<build>
<extensions>
<extension>
<groupid>kr.motd.maven</groupid>
<artifactid>os-maven-plugin</artifactid>
<version>1.6.2</version>
</extension>
</extensions>
<plugins>
<plugin>
<groupid>org.xolstice.maven.plugins</groupid>
<artifactid>protobuf-maven-plugin</artifactid>
<version>0.6.1</version>
<configuration>
<protocartifact>com.google.protobuf:protoc:3.12.0:exe:${os.detected.classifier}</protocartif
act>
<pluginid>grpc-java</pluginid>
<pluginartifact>io.grpc:protoc-gen-grpc-java:1.30.2:exe:${os.detected.classifier}</pluginart
ifact>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compile-custom</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

Game Process Integration Process

©2013-2019 Tencent Cloud. All rights reserved. Page 44 of 105


Game Server Elastic-scaling

Game server callback API list

API Name API Description

OnHealthCheck Runs health check

OnStartGameServerSession Receives game server session

OnProcessTerminate Ends game process

Game server active API list

©2013-2019 Tencent Cloud. All rights reserved. Page 45 of 105


Game Server Elastic-scaling

API Name API Description

ProcessReady Gets process ready

ActivateGameServerSession Activates game server session

AcceptPlayerSession Receives player session

RemovePlayerSession Removes player session

DescribePlayerSessions Gets player session list

UpdatePlayerSessionCreationPolicy Updates player session creation policy

TerminateGameServerSession Ends game server session

ProcessEnding Ends process

ReportCustomData Reports custom data

Others

When the game process uses gRPC to call a game server active API, you need to add two fields to
meta of the gRPC request.

Field Description Type

pid pid of the current game process string

requestId of the current request, which is used to uniquely identify a


requestId string
request

1. Generally, after the server is initialized, the process will check itself to see whether it can provide
services, and the game server will call the ProcessReady API to notify GSE that the process is
ready to host a game server session. After receiving the notification, GSE will change the status of
the server instance to "Active".

public GseResponseBo processReady(ProcessReadyRequestBo request) {


logger.info("processReady request=" + new Gson().toJson(request));
GseResponseBo responseBo = new GseResponseBo();
GseGrpcSdkServiceOuterClass.ProcessReadyRequest rpcRequest = GseGrpcSdkServiceOuterClass.Proce
ssReadyRequest
// Set the ports.
.newBuilder().setClientPort(request.getClientPort())
.setGrpcPort(request.getGrpcPort())
// Log path.

©2013-2019 Tencent Cloud. All rights reserved. Page 46 of 105


Game Server Elastic-scaling

.addAllLogPathsToUpload(request.getLogPathsToUploadList()).build();
GseGrpcSdkServiceOuterClass.GseResponse rpcResponse;
try {
rpcResponse = getGseGrpcSdkServiceClient().processReady(rpcRequest);
} catch (StatusRuntimeException e) {
logger.log(Level.WARNING, "RPC failed: {0}", e.getStatus());
return createRpcFailedResponseBo(e.getStatus());
}
// Ready to provide services.
logger.info("processReady response=" + rpcResponse.toString());
return createResponseBoByRpcResponse(rpcResponse);
}

2. After the process is ready, GSE will call the OnHealthCheck API to perform a health check on the
game server every minute. If the health check fails three consecutive times, the process will be
considered to be unhealthy, and no game server sessions will be assigned to it.

public boolean onHealthCheck() {


// Add game server logic for health check.
boolean res = getGrpcServiceConfig().getGseGrpcSdkServiceClient().isProcessHealth();
logger.info("onHealthCheck status=" + res);
return res;
}

3. Because the client calls the CreateGameServerSession API to create a game server session and
assigns it to a process, GSE will be triggered to call the onStartGameServerSession API for the
process and change the status of GameServerSession to "Activating".

public GseResponseBo onStartGameServerSession(GameServerSessionBo gameServerSessionBo) {


logger.info("onStartGameServerSession gameServerSession=" + new Gson().toJson(gameServerSessio
nBo));
// Add the game server logic used to launch the game server session.
// Save the game server sessions
getGrpcServiceConfig().getGseGrpcSdkServiceClient().onStartGameServerSession(gameServerSession
Bo);
// Activate the game server sessions
ActivateGameServerSessionRequestBo activateRequest = new ActivateGameServerSessionRequestBo();
activateRequest.setGameServerSessionId(gameServerSessionBo.getGameServerSessionId());
activateRequest.setMaxPlayers(gameServerSessionBo.getMaxPlayers());
getGrpcServiceConfig().getGseGrpcSdkServiceClient().activateGameServerSession(activateReques
t);
// Add the final logic here.
return createResponseBo(0, "SUCCESS");
}

©2013-2019 Tencent Cloud. All rights reserved. Page 47 of 105


Game Server Elastic-scaling

4. After the game server receives onStartGameServerSession , you need to handle the logic or
resource allocation by yourself. After everything is ready, the game server will call the
ActivateGameServerSession API to notify GSE that the game server session has been assigned to a
process and is ready to receive player requests and will change the server status to "Active".

public GseResponseBo activateGameServerSession(ActivateGameServerSessionRequestBo request) {


logger.info("activateGameServerSession request=" + new Gson().toJson(request));
if (gameServerSessionBo == null) {
return createResponseBo(Constants.gameServerSessionExpectCode, "no game server session found."
);
}
GseResponseBo responseBo = new GseResponseBo();
GseGrpcSdkServiceOuterClass.ActivateGameServerSessionRequest rpcRequest = GseGrpcSdkServiceOut
erClass.ActivateGameServerSessionRequest
.newBuilder().setMaxPlayers(request.getMaxPlayers())
.setGameServerSessionId(gameServerSessionBo.getGameServerSessionId()).build();
GseGrpcSdkServiceOuterClass.GseResponse rpcResponse;
try {
rpcResponse = getGseGrpcSdkServiceClient().activateGameServerSession(rpcRequest);
} catch (StatusRuntimeException e) {
logger.log(Level.WARNING, "RPC failed: {0}", e.getStatus());
return createRpcFailedResponseBo(e.getStatus());
}
logger.info("activateGameServerSession response=" + rpcResponse.toString());
return createResponseBoByRpcResponse(rpcResponse);
}

5. After the client calls the JoinGameServerSession API for the player to join, the game server will call
the AcceptPlayerSession API to verify the validity of the player. If the connection is accepted, the
status of PlayerSession will be set to "Active". If the client receives no response within 60
seconds after calling the JoinGameServerSession API, it will change the status of PlayerSession to
"Timeout" and then call JoinGameServerSession again.

public GseResponseBo acceptPlayerSession(PlayerSessionRequestBo request) {


logger.info("acceptPlayerSession request=" + new Gson().toJson(request));
if (gameServerSessionBo == null) {
return createResponseBo(Constants.gameServerSessionExpectCode, "no game server session found."
);
}
GseResponseBo responseBo = new GseResponseBo();
GseGrpcSdkServiceOuterClass.AcceptPlayerSessionRequest rpcRequest = GseGrpcSdkServiceOuterClas
s.AcceptPlayerSessionRequest

©2013-2019 Tencent Cloud. All rights reserved. Page 48 of 105


Game Server Elastic-scaling

.newBuilder().setGameServerSessionId(gameServerSessionBo.getGameServerSessionId())
.setPlayerSessionId(request.getPlayerSessionId()).build();
GseGrpcSdkServiceOuterClass.GseResponse rpcResponse;
try {
rpcResponse = getGseGrpcSdkServiceClient().acceptPlayerSession(rpcRequest);
} catch (StatusRuntimeException e) {
logger.log(Level.WARNING, "RPC failed: {0}", e.getStatus());
return createRpcFailedResponseBo(e.getStatus());
}
logger.info("acceptPlayerSession response=" + rpcResponse.toString());
return createResponseBoByRpcResponse(rpcResponse);
}

6. After the game ends or the player exits, the game server will call the RemovePlayerSession API to
remove the player, change the status of playersession to "Completed", and reserve the player
slot in the game server session.

public GseResponseBo removePlayerSession(PlayerSessionRequestBo request) {


logger.info("removePlayerSession request=" + new Gson().toJson(request));
if (gameServerSessionBo == null) {
return createResponseBo(Constants.gameServerSessionExpectCode, "no game server session found."
);
}
GseResponseBo responseBo = new GseResponseBo();
GseGrpcSdkServiceOuterClass.RemovePlayerSessionRequest rpcRequest = GseGrpcSdkServiceOuterClas
s.RemovePlayerSessionRequest
.newBuilder().setGameServerSessionId(gameServerSessionBo.getGameServerSessionId())
.setPlayerSessionId(request.getPlayerSessionId()).build();
GseGrpcSdkServiceOuterClass.GseResponse rpcResponse;
try {
rpcResponse = getGseGrpcSdkServiceClient().removePlayerSession(rpcRequest);
} catch (StatusRuntimeException e) {
logger.log(Level.WARNING, "RPC failed: {0}", e.getStatus());
return createRpcFailedResponseBo(e.getStatus());
}
logger.info("removePlayerSession response=" + rpcResponse.toString());
return createResponseBoByRpcResponse(rpcResponse);
}

7. After a game server session (a game battle or a service) ends, the game server will call the
TerminateGameServerSession API to end the GameServerSession and change its status to
Terminated .

©2013-2019 Tencent Cloud. All rights reserved. Page 49 of 105


Game Server Elastic-scaling

public GseResponseBo terminateGameServerSession(String gameServerSessionId) {


logger.info("terminateGameServerSession request=" + gameServerSessionId);
if (StringUtils.isEmpty(gameServerSessionId) && gameServerSessionBo != null
&& !StringUtils.isEmpty(gameServerSessionBo.getGameServerSessionId())) {
gameServerSessionId = gameServerSessionBo.getGameServerSessionId();
}
if (StringUtils.isEmpty(gameServerSessionId)) {
return createResponseBo(Constants.gameServerSessionExpectCode, "no game server session found."
);
}
GseResponseBo responseBo = new GseResponseBo();
GseGrpcSdkServiceOuterClass.TerminateGameServerSessionRequest rpcRequest = GseGrpcSdkServiceOu
terClass.TerminateGameServerSessionRequest
.newBuilder().setGameServerSessionId(gameServerSessionId).build();
GseGrpcSdkServiceOuterClass.GseResponse rpcResponse;
try {
rpcResponse = getGseGrpcSdkServiceClient().terminateGameServerSession(rpcRequest);
} catch (StatusRuntimeException e) {
logger.log(Level.WARNING, "RPC failed: {0}", e.getStatus());
return createRpcFailedResponseBo(e.getStatus());
}
logger.info("terminateGameServerSession response=" + rpcResponse.toString());
return createResponseBoByRpcResponse(rpcResponse);
}

8. In case of health check failure or reduction, GSE will call the OnProcessTerminate API to end the
game process. The reduction will be triggered according to the protection policy configured in the
GSE console.

public GseResponseBo onProcessTerminate(long terminationTime) {


logger.info("onProcessTerminate terminationTime=" + terminationTime);
// It is possible to end the game server now.
return createResponseBo(0, "SUCCESS");
}

9. The game server calls the ProcessEnding API to end the process immediately, change the server
process status to "Terminated", and repossess the resources.

// Active call: a game battle corresponds to a process. The `ProcessEnding` API will be active
ly called after the game battle ends
// Passive call: in case of reduction, process exception, or health check failure, the `Proces
sEnding` API will be called passively according to the protection policy. If a full protection
or time-period protection policy is configured, it is required to determine whether there are

©2013-2019 Tencent Cloud. All rights reserved. Page 50 of 105


Game Server Elastic-scaling

any players in the game server session before the passive call can be made
public GseResponseBo processEnding() {
logger.info("processEnding begin");
GseResponseBo responseBo = new GseResponseBo();
GseGrpcSdkServiceOuterClass.ProcessEndingRequest rpcRequest = GseGrpcSdkServiceOuterClass.Proc
essEndingRequest
.newBuilder().build();
GseGrpcSdkServiceOuterClass.GseResponse rpcResponse;
try {
rpcResponse = getGseGrpcSdkServiceClient().processEnding(rpcRequest);
} catch (StatusRuntimeException e) {
logger.log(Level.WARNING, "RPC failed: {0}", e.getStatus());
return createRpcFailedResponseBo(e.getStatus());
}
logger.info("processEnding response=" + rpcResponse.toString());
return createResponseBoByRpcResponse(rpcResponse);
}

0. The game server calls the DescribePlayerSessions API to get the information of the player in the
game server session (which is optional based on your actual business needs).

public DescribePlayerSessionsResponseBo describePlayerSessions(DescribePlayerSessionsRequestBo


request) {
logger.info("describePlayerSessions request=" + new Gson().toJson(request));
if (StringUtils.isEmpty(request.getGameServerSessionId()) &&
gameServerSessionBo != null && !StringUtils.isEmpty(gameServerSessionBo.getGameServerSessionId
())) {
request.setGameServerSessionId(gameServerSessionBo.getGameServerSessionId());
}
GseGrpcSdkServiceOuterClass.DescribePlayerSessionsRequest rpcRequest = GseGrpcSdkServiceOuterC
lass.DescribePlayerSessionsRequest
.newBuilder().setGameServerSessionId(request.getGameServerSessionId())
.setLimit(request.getLimit())
.setNextToken(request.getNextToken())
.setPlayerId(request.getPlayerId())
.setPlayerSessionId(request.getPlayerSessionId())
.setPlayerSessionStatusFilter(request.getPlayerSessionStatusFilter()).build();
GseGrpcSdkServiceOuterClass.DescribePlayerSessionsResponse rpcResponse;
try {
rpcResponse = getGseGrpcSdkServiceClient().describePlayerSessions(rpcRequest);
} catch (StatusRuntimeException e) {
logger.log(Level.WARNING, "RPC failed: {0}", e.getStatus());
return null;
}
logger.info("describePlayerSessions response=" + rpcResponse.toString());

©2013-2019 Tencent Cloud. All rights reserved. Page 51 of 105


Game Server Elastic-scaling

return toPlayerSessionsResponseBo(rpcResponse);
}

1. The game server calls the UpdatePlayerSessionCreationPolicy API to update the player session
creation policy and set whether to accept new players, i.e., whether to allow new players to join a
game session (which is optional based on your actual business needs).

public GseResponseBo updatePlayerSessionCreationPolicy(UpdatePlayerSessionCreationPolicyReques


tBo request) {
logger.info("updatePlayerSessionCreationPolicy request=" + new Gson().toJson(request));
if (gameServerSessionBo == null) {
return createResponseBo(Constants.gameServerSessionExpectCode, "no game server session found."
);
}
GseResponseBo responseBo = new GseResponseBo();
GseGrpcSdkServiceOuterClass.UpdatePlayerSessionCreationPolicyRequest rpcRequest = GseGrpcSdkSe
rviceOuterClass.UpdatePlayerSessionCreationPolicyRequest
.newBuilder().setGameServerSessionId(gameServerSessionBo.getGameServerSessionId())
.setNewPlayerSessionCreationPolicy(request.getNewPlayerSessionCreationPolicy()).build();
GseGrpcSdkServiceOuterClass.GseResponse rpcResponse;
try {
rpcResponse = getGseGrpcSdkServiceClient().updatePlayerSessionCreationPolicy(rpcRequest);
} catch (StatusRuntimeException e) {
logger.log(Level.WARNING, "RPC failed: {0}", e.getStatus());
return createRpcFailedResponseBo(e.getStatus());
}
logger.info("updatePlayerSessionCreationPolicy response=" + rpcResponse.toString());
return createResponseBoByRpcResponse(rpcResponse);
}

2. The game server calls the ReportCustomData API to notify GSE of the custom data (which is
optional based on your actual business needs).

public GseResponseBo reportCustomData(ReportCustomDataRequestBo request) {


logger.info("reportCustomData request=" + new Gson().toJson(request));
GseResponseBo responseBo = new GseResponseBo();
GseGrpcSdkServiceOuterClass.ReportCustomDataRequest rpcRequest = GseGrpcSdkServiceOuterClass.R
eportCustomDataRequest
.newBuilder()
.setCurrentCustomCount(request.getCurrentCustomCount())
.setMaxCustomCount(request.getMaxCustomCount()).build();
GseGrpcSdkServiceOuterClass.GseResponse rpcResponse;
try {
rpcResponse = getGseGrpcSdkServiceClient().reportCustomData(rpcRequest);

©2013-2019 Tencent Cloud. All rights reserved. Page 52 of 105


Game Server Elastic-scaling

} catch (StatusRuntimeException e) {
logger.log(Level.WARNING, "RPC failed: {0}", e.getStatus());
return createRpcFailedResponseBo(e.getStatus());
}
logger.info("reportCustomData response=" + rpcResponse.toString());
return createResponseBoByRpcResponse(rpcResponse);
}

Launching Server for GSE to Call


Server running: launch GrpcServer .

@Bean(name = "grpcService", initMethod = "startup", destroyMethod = "shutdown")


public GrpcService getGrpcService() {
GrpcServiceConfig grpcServiceConfig = new GrpcServiceConfig();
grpcServiceConfig.setGseGrpcSdkServiceClient(gseGrpcSdkServiceClient);
grpcServiceConfig.setGameServerGrpcPort(gameServerGrpcPort);
grpcServiceConfig.setGameServerToClientPort(gameServerToClientPort);
grpcServiceConfig.setTargetAddress(targetAddress);
grpcServiceConfig.setGameServerUploadLogPath(gameServerUploadLogPath);
GrpcService grpcService = new GrpcService(grpcServiceConfig);
return grpcService;
}

Connecting Client to gRPC Server of GSE


Server connecting: create a gRPC channel, specify the host name and server port to connect to, and
use this channel to create a stub instance.

public GseGrpcSdkServiceGrpc.GseGrpcSdkServiceBlockingStub getGseGrpcSdkServiceClient() {


// The "channel" here is a channel instead of a `ManagedChannel`; therefore, it is not the respon
sibility of the code to shut it down.
// Pass the channel to the code to make it easier for the code to test and reuse the channel.
if (blockingStub == null) {
managedChannel = getGrpcChannel(targetAddress);
blockingStub = GseGrpcSdkServiceGrpc.newBlockingStub(managedChannel);
}
if (managedChannel.isShutdown() || managedChannel.isTerminated()) {
managedChannel.shutdownNow();
managedChannel = getGrpcChannel(targetAddress);
blockingStub = GseGrpcSdkServiceGrpc.newBlockingStub(managedChannel);

©2013-2019 Tencent Cloud. All rights reserved. Page 53 of 105


Game Server Elastic-scaling

}
return blockingStub;
}

Demo for Java


1. Click here to download the code of the Demo for Java.
2. Generate the gRPC code.
As the gRPC code has already been generated in the java-demo/src/main/java/tencentcloud
directory of the Demo for Java, you do not need to generate it again.
3. Launch the server for GSE to call.
Implement the server.
GameServerGrpcCallbackImpl.java in the java-
demo/src/main/java/com/tencentcloud/gse/gameserver/service/gamelogic/impl directory implements
three server APIs.
Run the server.
GameServerConfig.java in the java-demo/src/main/java/com/tencentcloud/gse/gameserver/config
directory launches GrpcServer .
4. Connect the client to the gRPC server of GSE.
Implement the client.
GseGrpcSdkServiceClientImpl.java in the java-
demo/src/main/java/com/tencentcloud/gse/gameserver/service/gsegrpc/impl directory implements
nine client APIs.
Connect to the server.
Create a gRPC channel, specify the host name and server port to connect to, and use this
channel to create a stub instance.
5. Compile and run the project.
i. Java v1.8 or above is required. You can use yum to install openjdk on Linux:

yum install -y java-1.8.0-openjdk

Download the code, use Maven to build and generate gse-gameserver-demo.jar in the java-
demo directory, and run the following command to launch it:

java -jar gse-gameserver-demo.jar

Package the executable file gse-gameserver-demo.jar as an asset package and configure the
launch path as java and the launch parameter as jar gse-gameserver-demo.jar .

©2013-2019 Tencent Cloud. All rights reserved. Page 54 of 105


Game Server Elastic-scaling

Create a server fleet and deploy the asset package on it. After that, you can perform various
operations such as scaling.

©2013-2019 Tencent Cloud. All rights reserved. Page 55 of 105


Game Server Elastic-scaling

gRPC Lua Tutorial


Last updated:2021-11-17 18:06:10

Installing gRPC
1. Install gRPC. The installation will generate an executable program grpc_cpp_plugin , which will be
needed for generating gRPC code.
2. Install protocol buffers.

Note
For more information on the installation process, see Installing gRPC Lua and Installing Protocol
Buffers.

Defining Service
gRPC uses Protocol Buffers to define a service: an RPC service specifies methods that can be called
remotely by using parameters and return types.

Note
We provide the .proto files for service definition. You can click here to directly download them
with no need to generate them by yourself.

Generating gRPC Code


1. After defining the service, you can use protoc (protocol buffer compiler) to generate the client and
server code (in any language supported by gRPC).
2. The generated code includes the client stub and the abstract APIs to be implemented by the
server.
3. Steps for generating gRPC code:
The Demo for Lua relies on the C++ framework. Just like with the Demo for C++, in the proto
directory, run:

©2013-2019 Tencent Cloud. All rights reserved. Page 56 of 105


Game Server Elastic-scaling

protoc --cpp_out=. *.proto

to generate the pb.cc and pb.h files.

protoc --grpc_out=. --plugin=protoc-gen-grpc=`which grpc_cpp_plugin` *.proto

to generate the corresponding gRPC code.


Move the eight generated files to an appropriate location in the project.

Game Process Integration Process

©2013-2019 Tencent Cloud. All rights reserved. Page 57 of 105


Game Server Elastic-scaling

Game server callback API list

API Name API Description

OnHealthCheck runs health check

OnStartGameServerSession Receives game server session

OnProcessTerminate Ends game process

Game server callback API list

©2013-2019 Tencent Cloud. All rights reserved. Page 58 of 105


Game Server Elastic-scaling

API Name API Description

ProcessReady Gets process ready

ActivateGameServerSession Activates game server session

AcceptPlayerSession Receives player session

RemovePlayerSession Removes player session

DescribePlayerSessions Gets player session list

UpdatePlayerSessionCreationPolicy Updates player session creation policy

TerminateGameServerSession Ends game server session

ProcessEnding Ends process

ReportCustomData Reports custom data

Others

When the game process uses gRPC to call a game server active API, you need to add two fields to
meta of the gRPC request.

Field Description Type

pid pid of the current game process string

requestId of the current request, which is used to uniquely identify a


requestId string
request

1. Generally, after the server is initialized, the process will check itself to see whether it can provide
services, and the game server will call the ProcessReady API to notify GSE that the process is
ready to host a game server session. After receiving the notification, GSE will change the status of
the server instance to "Active".

static bool luaProcessReady(std::vector <std::string> &logPath, int clientPort, int grpcPort)


{
GseResponse reply;
// Log path. Set the ports.
Status status = GGseManager->ProcessReady(logPath, clientPort, grpcPort, reply);
// Ready to provide services
GSESDK()->setReplyStatus(status);
if (!status.ok()) {
return false;

©2013-2019 Tencent Cloud. All rights reserved. Page 59 of 105


Game Server Elastic-scaling

}
return true;
}

2. After the process is ready, GSE will call the OnHealthCheck API to perform a health check on the
game server every minute. If the health check fails three consecutive times, the process will be
considered to be unhealthy, and no game server sessions will be assigned to it.

Status GameServerGrpcSdkServiceImpl::OnHealthCheck(ServerContext* context, const HealthCheckRe


quest* request, HealthCheckResponse* reply)
{
healthStatus = GSESDK()->exec("return OnHealthCheck()");
std::cout << "healthStatus=" << healthStatus << std::endl;
reply->set_healthstatus(healthStatus);
return Status::OK;
}

3. Because the client calls the CreateGameServerSession API to create a game server session and
assigns it to a process, GSE will be triggered to call the onStartGameServerSession API for the
process and change the status of GameServerSession to "Activating".

Status GameServerGrpcSdkServiceImpl::OnStartGameServerSession(ServerContext* context, const St


artGameServerSessionRequest* request, GseResponse* reply)
{
auto gameServerSession = request->gameserversession();
GGseManager->SetGameServerSession(gameServerSession);
std::ostringstream o;
o << "return OnStartGameServerSession('" << gameServerSession.gameserversessionid() << "'," <<
gameServerSession.maxplayers() << ")";
std::string luaCmd = o.str();
bool res = GSESDK()->exec(luaCmd);
return Status::OK;
}

4. After the game server receives onStartGameServerSession , you need to handle the logic or
resource allocation by yourself. After everything is ready, the game server will call the
ActivateGameServerSession API to notify GSE that the game server session has been assigned to a
process and is ready to receive player requests and will change the server status to "Active".

static bool luaActivateGameServerSession(const std::string &gameServerSessionId, int maxPlayer


s) {

©2013-2019 Tencent Cloud. All rights reserved. Page 60 of 105


Game Server Elastic-scaling

GseResponse reply;
Status status = GGseManager->ActivateGameServerSession(gameServerSessionId, maxPlayers, repl
y);
GSESDK()->setReplyStatus(status);
if (!status.ok()) {
return false;
}
return true;
}

5. After the client calls the JoinGameServerSession API for the player to join, the game server will call
the AcceptPlayerSession API to verify the validity of the player. If the connection is accepted, the
status of PlayerSession will be set to "Active". If the client receives no response within 60
seconds after calling the JoinGameServerSession API, it will change the status of PlayerSession to
"Timeout" and then call JoinGameServerSession again.

static bool luaAcceptPlayerSession(const std::string &gameServerSessionId, const std::string &


playerSessionId) {
GseResponse reply;
Status status = GGseManager->AcceptPlayerSession(gameServerSessionId, playerSessionId, reply);
GSESDK()->setReplyStatus(status);
if (!status.ok()) {
return false;
}
return true;
}

6. After the game ends or the player exits, the game server will call the RemovePlayerSession API to
remove the player, change the status of playersession to "Completed", and reserve the player
slot in the game server session.

static bool luaRemovePlayerSession(const std::string &gameServerSessionId, const std::string &


playerSessionId) {
GseResponse reply;
Status status = GGseManager->RemovePlayerSession(gameServerSessionId, playerSessionId, reply);
GSESDK()->setReplyStatus(status);
if (!status.ok()) {
return false;
}
return true;
}

©2013-2019 Tencent Cloud. All rights reserved. Page 61 of 105


Game Server Elastic-scaling

7. After a game server session (a game battle or a service) ends, the game server will call the
TerminateGameServerSession API to end the GameServerSession and change its status to
Terminated .

static bool luaTerminateGameServerSession(const std::string &gameServerSessionId) {


GseResponse reply;
Status status = GGseManager->TerminateGameServerSession(gameServerSessionId, reply);
GSESDK()->setReplyStatus(status);
if (!status.ok()) {
return false;
}
return true;
}

8. In case of health check failure or reduction, GSE will call the OnProcessTerminate API to end the
game process. The reduction will be triggered according to the protection policy configured in the
GSE console.

Status GameServerGrpcSdkServiceImpl::OnProcessTerminate(ServerContext* context, const ProcessT


erminateRequest* request, GseResponse* reply)
{
auto terminationTime = request->terminationtime();
std::to_string(terminationTime));
std::ostringstream o;
o << "OnProcessTerminate(" << terminationTime << ")";
std::string luaCmd = o.str();
GSESDK()->execWithNilResult(luaCmd);
return Status::OK;
}

9. The game server calls the ProcessEnding API to end the process immediately, change the server
process status to "Terminated", and repossess the resources.

// Active call: a game battle corresponds to a process. The `ProcessEnding` API will be active
ly called after the game battle ends
// Passive call: in case of reduction, process exception, or health check failure, the `Proces
sEnding` API will be called passively according to the protection policy. If a full protection
or time-period protection policy is configured, it is required to determine whether there are
any players in the game server session before the passive call can be made
static bool luaProcessEnding() {
GseResponse reply;
Status status = GGseManager->ProcessEnding(reply);
GSESDK()->setReplyStatus(status);

©2013-2019 Tencent Cloud. All rights reserved. Page 62 of 105


Game Server Elastic-scaling

if (!status.ok()) {
return false;
}
return true;
}

0. The game server calls the DescribePlayerSessions API to get the information of the player in the
game server session (which is optional based on your actual business needs).

static bool luaDescribePlayerSessions(const std::string &gameServerSessionId,


const std::string &playerId,
const std::string &playerSessionId,
const std::string &playerSessionStatusFilter, const std::string &nextToken,
int limit) {
DescribePlayerSessionsResponse reply;
Status status = GGseManager->DescribePlayerSessions(gameServerSessionId,playerId, playerSessio
nId, playerSessionStatusFilter, nextToken, limit, reply);
GSESDK()->setDescribePlayerSessionsResponse(reply);
if (!status.ok()) {
return false;
}
return true;
}

1. The game server calls the UpdatePlayerSessionCreationPolicy API to update the player session
creation policy and set whether to accept new players, i.e., whether to allow new players to join a
game session (which is optional based on your actual business needs).

static bool luaUpdatePlayerSessionCreationPolicy(const std::string &newpolicy) {


GseResponse reply;
Status status = GGseManager->UpdatePlayerSessionCreationPolicy(newpolicy, reply);
GSESDK()->setReplyStatus(status);
if (!status.ok()) {
return false;
}
return true;
}

2. The game server calls the ReportCustomData API to notify GSE of the custom data (which is
optional based on your actual business needs).

©2013-2019 Tencent Cloud. All rights reserved. Page 63 of 105


Game Server Elastic-scaling

static bool luaReportCustomData(int currentCustomCount, int maxCustomCount) {


GseResponse reply;
Status status = GGseManager->ReportCustomData(currentCustomCount, maxCustomCount, reply);
GSESDK()->setReplyStatus(status);
if (!status.ok()) {
return false;
}
return true;
}

Launching Server for GSE to Call


Server running: launch GrpcServer .

// Launch the gRPC server


std::thread tGrpc(&GameServerGrpcSdkServiceImpl::StartGrpcServer, GGameServerGrpcSdkService);
sem_wait(&(GGameServerGrpcSdkService->sem));
auto grpcPort = GGameServerGrpcSdkService->GetGrpcPort();

Connecting Client to gRPC Server of GSE


Server connecting: create a gRPC channel, specify the host name and server port to connect to, and
use this channel to create a stub instance.

void GseManager::InitStub() {
auto channel = grpc::CreateChannel("127.0.0.1:5758", grpc::InsecureChannelCredentials());
stub_ = GseGrpcSdkService::NewStub(channel);
}

Demo for Lua


1. Click here to download the code of the Demo for Lua.
2. Generate the gRPC code.
The Demo for Lua relies on the C++ framework, with gRPC code generated in the cpp-
demo/source/grpcsdk directory, so there is no need to generate it again.
3. Launch the server for GSE to call.

©2013-2019 Tencent Cloud. All rights reserved. Page 64 of 105


Game Server Elastic-scaling

Implement the server.


grpcserver.cpp in the lua-demo/source/api directory implements three server APIs.
Run the server.
main.cpp in the lua-demo directory launches GrpcServer .
4. Connect the client to the gRPC server of GSE.
Implement the client.
GSESdkHandleWrapper.cpp in the lua-demo/source/lua directory implements nine client APIs.
Connect to the server.
Create a gRPC channel, specify the host name and server port to connect to, and use this
channel to create a stub instance.
5. Compile and run the project.
i. Install CMake.
Install GCC v4.9 or above.
Install the LuaJIT and Boost development kits:

yum install -y luajit-devel


yum install -y boost-devel
yum install -y cmake

Download the code and run the following command in the lua-demo directory:

mkdir build
cd build
cmake ..
make
cp ../source/lua/gse.lua .

The corresponding lua-demo executable file will be generated. Run ./lua-demo to launch it.
Package the executable file lua-demo.cpp as an asset package and configure the launch path
as lua-demo with no launch parameter needed.
Create a server fleet and deploy the asset package on it. After that, you can perform various
operations such as scaling.

©2013-2019 Tencent Cloud. All rights reserved. Page 65 of 105


Game Server Elastic-scaling

gRPC Node.js Tutorial


Last updated:2022-01-21 11:48:44

Installing gRPC
1. Install gRPC. The installation will generate an executable program grpc_cpp_plugin , which will be
needed for generating gRPC code.
2. Install protocol buffers.

Note
For more information on the installation process, see Installing gRPC Lua and Installing Protocol
Buffers.

Defining Service
gRPC uses Protocol Buffers to define a service: an RPC service specifies methods that can be called
remotely by using parameters and return types.

Note
We provide the .proto files for service definition. You can click here to directly download them
with no need to generate them by yourself.

Generating gRPC Code

1. After defining the service, you can use protoc (protocol buffer compiler) to generate the client and
server code (in any language supported by gRPC).

2. The generated code includes the client stub and the abstract APIs to be implemented by the
server.

3. Steps for generating gRPC code:

©2013-2019 Tencent Cloud. All rights reserved. Page 66 of 105


Game Server Elastic-scaling

i. The Demo for Lua relies on the C++ framework. Just like with the Demo for C++, in the proto
directory, run:

protoc --cpp_out=. *.proto

ii. Generate the pb.cc and pb.h files.

protoc --grpc_out=. --plugin=protoc-gen-grpc=`which grpc_cpp_plugin` *.proto

iii. Generate the corresponding gRPC code.

iv. Move the eight generated files to an appropriate location in the project.

Game Process Integration Process

©2013-2019 Tencent Cloud. All rights reserved. Page 67 of 105


Game Server Elastic-scaling

Game server callback API list

API Name API Description

OnHealthCheck Runs health check

OnStartGameServerSession Receives game server session

OnProcessTerminate Ends game process

Game server active API list

©2013-2019 Tencent Cloud. All rights reserved. Page 68 of 105


Game Server Elastic-scaling

API Name API Description

ProcessReady Gets process ready

ActivateGameServerSession Activates game server session

AcceptPlayerSession Receives player session

RemovePlayerSession Removes player session

DescribePlayerSessions Gets player session list

UpdatePlayerSessionCreationPolicy Updates player session creation policy

TerminateGameServerSession Ends game server session

ProcessEnding Ends process

ReportCustomData Reports custom data

Others

When the game process uses gRPC to call a game server active API, you need to add two fields to
meta of the gRPC request.

Field Description Type

pid pid of the current game process string

requestId of the current request, which is used to uniquely identify a


requestId string
request

1. Generally, after the server is initialized, the process will check itself to see whether it can provide
services, and the game server will call the ProcessReady API to notify GSE that the process is
ready to host a game server session. After receiving the notification, GSE will change the status of
the server instance to "Active".

static bool luaProcessReady(std::vector <std::string> &logPath, int clientPort, int grpcPort)


{
GseResponse reply;
// Log path. Set the ports.
Status status = GGseManager->ProcessReady(logPath, clientPort, grpcPort, reply);
// Ready to provide services
GSESDK()->setReplyStatus(status);
if (!status.ok()) {
return false;

©2013-2019 Tencent Cloud. All rights reserved. Page 69 of 105


Game Server Elastic-scaling

}
return true;
}

2. After the process is ready, GSE will call the OnHealthCheck API to perform a health check on the
game server every minute. If the health check fails three consecutive times, the process will be
considered to be unhealthy, and no game server sessions will be assigned to it.

Status GameServerGrpcSdkServiceImpl::OnHealthCheck(ServerContext* context, const HealthCheckRe


quest* request, HealthCheckResponse* reply)
{
healthStatus = GSESDK()->exec("return OnHealthCheck()");
std::cout << "healthStatus=" << healthStatus << std::endl;
reply->set_healthstatus(healthStatus);
return Status::OK;
}

3. Because the client calls the CreateGameServerSession API to create a game server session and
assigns it to a process, GSE will be triggered to call the onStartGameServerSession API for the
process and change the status of GameServerSession to "Activating".

Status GameServerGrpcSdkServiceImpl::OnStartGameServerSession(ServerContext* context, const St


artGameServerSessionRequest* request, GseResponse* reply)
{
auto gameServerSession = request->gameserversession();
GGseManager->SetGameServerSession(gameServerSession);
std::ostringstream o;
o << "return OnStartGameServerSession('" << gameServerSession.gameserversessionid() << "'," <<
gameServerSession.maxplayers() << ")";
std::string luaCmd = o.str();
bool res = GSESDK()->exec(luaCmd);
return Status::OK;
}

4. After the game server receives onStartGameServerSession , you need to handle the logic or
resource allocation by yourself. After everything is ready, the game server will call the
ActivateGameServerSession API to notify GSE that the game server session has been assigned to a
process and is ready to receive player requests and will change the server status to "Active".

static bool luaActivateGameServerSession(const std::string &gameServerSessionId, int maxPlayer


s) {

©2013-2019 Tencent Cloud. All rights reserved. Page 70 of 105


Game Server Elastic-scaling

GseResponse reply;
Status status = GGseManager->ActivateGameServerSession(gameServerSessionId, maxPlayers, repl
y);
GSESDK()->setReplyStatus(status);
if (!status.ok()) {
return false;
}
return true;
}

5. After the client calls the JoinGameServerSession API for the player to join, the game server will call
the AcceptPlayerSession API to verify the validity of the player. If the connection is accepted, the
status of PlayerSession will be set to "Active". If the client receives no response within 60
seconds after calling the JoinGameServerSession API, it will change the status of PlayerSession to
"Timeout" and then call JoinGameServerSession again.

static bool luaAcceptPlayerSession(const std::string &gameServerSessionId, const std::string &


playerSessionId) {
GseResponse reply;
Status status = GGseManager->AcceptPlayerSession(gameServerSessionId, playerSessionId, reply);
GSESDK()->setReplyStatus(status);
if (!status.ok()) {
return false;
}
return true;
}

6. After the game ends or the player exits, the game server will call the RemovePlayerSession API to
remove the player, change the status of playersession to "Completed", and reserve the player
slot in the game server session.

static bool luaRemovePlayerSession(const std::string &gameServerSessionId, const std::string &


playerSessionId) {
GseResponse reply;
Status status = GGseManager->RemovePlayerSession(gameServerSessionId, playerSessionId, reply);
GSESDK()->setReplyStatus(status);
if (!status.ok()) {
return false;
}
return true;
}

©2013-2019 Tencent Cloud. All rights reserved. Page 71 of 105


Game Server Elastic-scaling

7. After a game server session (a game battle or a service) ends, the game server will call the
TerminateGameServerSession API to end the GameServerSession and change its status to
Terminated .

static bool luaTerminateGameServerSession(const std::string &gameServerSessionId) {


GseResponse reply;
Status status = GGseManager->TerminateGameServerSession(gameServerSessionId, reply);
GSESDK()->setReplyStatus(status);
if (!status.ok()) {
return false;
}
return true;
}

8. In case of health check failure or reduction, GSE will call the OnProcessTerminate API to end the
game process. The reduction will be triggered according to the protection policy configured in the
GSE console.

Status GameServerGrpcSdkServiceImpl::OnProcessTerminate(ServerContext* context, const ProcessT


erminateRequest* request, GseResponse* reply)
{
auto terminationTime = request->terminationtime();
std::to_string(terminationTime));
std::ostringstream o;
o << "OnProcessTerminate(" << terminationTime << ")";
std::string luaCmd = o.str();
GSESDK()->execWithNilResult(luaCmd);
return Status::OK;
}

9. The game server calls the ProcessEnding API to end the process immediately, change the server
process status to "Terminated", and repossess the resources.

// Active call: a game battle corresponds to a process. The `ProcessEnding` API will be active
ly called after the game battle ends
// Passive call: in case of reduction, process exception, or health check failure, the `Proces
sEnding` API will be called passively according to the protection policy. If a full protection
or time-period protection policy is configured, it is required to determine whether there are
any players in the game server session before the passive call can be made
static bool luaProcessEnding() {
GseResponse reply;
Status status = GGseManager->ProcessEnding(reply);
GSESDK()->setReplyStatus(status);

©2013-2019 Tencent Cloud. All rights reserved. Page 72 of 105


Game Server Elastic-scaling

if (!status.ok()) {
return false;
}
return true;
}

0. The game server calls the DescribePlayerSessions API to get the information of the player in the
game server session (which is optional based on your actual business needs).

static bool luaDescribePlayerSessions(const std::string &gameServerSessionId,


const std::string &playerId,
const std::string &playerSessionId,
const std::string &playerSessionStatusFilter, const std::string &nextToken,
int limit) {
DescribePlayerSessionsResponse reply;
Status status = GGseManager->DescribePlayerSessions(gameServerSessionId,playerId, playerSessio
nId, playerSessionStatusFilter, nextToken, limit, reply);
GSESDK()->setDescribePlayerSessionsResponse(reply);
if (!status.ok()) {
return false;
}
return true;
}

1. The game server calls the UpdatePlayerSessionCreationPolicy API to update the player session
creation policy and set whether to accept new players, i.e., whether to allow new players to join a
game session (which is optional based on your actual business needs).

static bool luaUpdatePlayerSessionCreationPolicy(const std::string &newpolicy) {


GseResponse reply;
Status status = GGseManager->UpdatePlayerSessionCreationPolicy(newpolicy, reply);
GSESDK()->setReplyStatus(status);
if (!status.ok()) {
return false;
}
return true;
}

2. The game server calls the ReportCustomData API to notify GSE of the custom data (which is
optional based on your actual business needs).

©2013-2019 Tencent Cloud. All rights reserved. Page 73 of 105


Game Server Elastic-scaling

static bool luaReportCustomData(int currentCustomCount, int maxCustomCount) {


GseResponse reply;
Status status = GGseManager->ReportCustomData(currentCustomCount, maxCustomCount, reply);
GSESDK()->setReplyStatus(status);
if (!status.ok()) {
return false;
}
return true;
}

Launching Server for GSE to Call


Server running: launch GrpcServer .

// Launch the gRPC server


std::thread tGrpc(&GameServerGrpcSdkServiceImpl::StartGrpcServer, GGameServerGrpcSdkService);
sem_wait(&(GGameServerGrpcSdkService->sem));
auto grpcPort = GGameServerGrpcSdkService->GetGrpcPort();

Connecting Client to gRPC Server of GSE


Server connecting: create a gRPC channel, specify the host name and server port to connect to, and
use this channel to create a stub instance.

void GseManager::InitStub() {
auto channel = grpc::CreateChannel("127.0.0.1:5758", grpc::InsecureChannelCredentials());
stub_ = GseGrpcSdkService::NewStub(channel);
}

Demo for Lua


1. Click here to download the code of the Demo for Lua.
2. Generate the gRPC code.
The Demo for Lua relies on the C++ framework, with gRPC code generated in the cpp-
demo/source/grpcsdk directory, so there is no need to generate it again.
3. Launch the server for GSE to call.

©2013-2019 Tencent Cloud. All rights reserved. Page 74 of 105


Game Server Elastic-scaling

Implement the server.


grpcserver.cpp in the lua-demo/source/api directory implements three server APIs.
Run the server.
main.cpp in the lua-demo directory launches GrpcServer .
4. Connect the client to the gRPC server of GSE.
Implement the client.
GSESdkHandleWrapper.cpp in the lua-demo/source/lua directory implements nine client APIs.
Connect to the server.
Create a gRPC channel, specify the host name and server port to connect to, and use this
channel to create a stub instance.
5. Compile and run the project.
i. Install CMake.
Install GCC v4.9 or above.
Install the LuaJIT and Boost development kits:

yum install -y luajit-devel


yum install -y boost-devel
yum install -y cmake

Download the code and run the following command in the lua-demo directory:

mkdir build
cd build
cmake ..
make
cp ../source/lua/gse.lua .

The corresponding lua-demo executable file will be generated. Run ./lua-demo to launch it.
Package the executable file lua-demo.cpp as an asset package and configure the launch path
as lua-demo with no launch parameter needed.
Create a server fleet and deploy the asset package on it. After that, you can perform various
operations such as scaling.

©2013-2019 Tencent Cloud. All rights reserved. Page 75 of 105


Game Server Elastic-scaling

gRPC Unity Tutorial


Last updated:2021-08-30 15:52:50

This document describes how to integrate Unity with GSE SDK. The overall process mainly consists of
two tasks:

1. Integrating Unity with gRPC


2. Integrating Unity with GSE SDK

Prerequisites
You have already installed Unity Hub and Unity IDE.

Note:
This document uses 2018.3.5f1 or 2019.4.9f1 Unity engine and MacOS as an example.

Integrating Unity with gRPC


gRPC has experimental support for Unity. For more information, see README. Perform the following
steps to integrate Unity with gRPC:

Step 1: create a Unity project

Because gRPC APIs are only available for .NET 4.5+ , it is necessary to create a Unity project
equivalent to .NET 4.x at Edit > Project Setting > Player > Configuration > Scripting

©2013-2019 Tencent Cloud. All rights reserved. Page 76 of 105


Game Server Elastic-scaling

Runtime Version.

Step 2: download grpc_unity_package

Download the latest development version of grpc_unity_package.VERSION.zip here. Click Buidld ID


to redirect to the download page.

©2013-2019 Tencent Cloud. All rights reserved. Page 77 of 105


Game Server Elastic-scaling

Click to download grpc_unity_package.VERSION.zip under the c# directory.

©2013-2019 Tencent Cloud. All rights reserved. Page 78 of 105


Game Server Elastic-scaling

Step 3: decompress the package


Decompress the downloaded .zip package to the Assets directory of the Unity project, as shown
below:

Step 4: test the package

Unity Editor will fetch files and automatically add them to the project for your use of gRPC and
Protobuf in codes. If Unity Editor prompts an error, see FAQs for troubleshooting.

Integrating Unity with GSE SDK


Complete the following steps to integrate Unity with GSE SDK:

Step 1: obtain the GSE SDK Protobuf files

Obtain the GameServerGrpcSdkService.proto and GseGrpcSdkService.proto files of GSE SDK Protobuf.


For more information, see proto File

Step 2: generate C# codes based on Protobuf

1. Access the grpc_unity_package.VERSION.zip page again to download the gRPC protoc Plugin
package compatible with your operating system.

©2013-2019 Tencent Cloud. All rights reserved. Page 79 of 105


Game Server Elastic-scaling

©2013-2019 Tencent Cloud. All rights reserved. Page 80 of 105


Game Server Elastic-scaling

2. Decompress the package to obtain the protoc and grpc_csharp_plugin executable programs.

3. Copy protoc and grpc_csharp_plugin executable programs to the same directory as the Protobuf
file. Run the following two commands according to the operating system to generate C# codes:

For MAC and Linux OS:


protoc -I ./ --csharp_out=. GseGrpcSdkService.proto --grpc_out=. --plugin=protoc-gen-
grpc=grpc_csharp_plugin
protoc -I ./ --csharp_out=. GameServerGrpcSdkService.proto --grpc_out=. --plugin=protoc-gen-
grpc=grpc_csharp_plugin
For Windows OS:
./protoc -I ./ --csharp_out=. GseGrpcSdkService.proto --grpc_out=. --plugin=protoc-gen-
grpc=grpc_csharp_plugin.exe
./protoc -I ./ --csharp_out=. GameServerGrpcSdkService.proto --grpc_out=. --plugin=protoc-
gen-grpc=grpc_csharp_plugin.exe

©2013-2019 Tencent Cloud. All rights reserved. Page 81 of 105


Game Server Elastic-scaling

Four .cs code files are generated as shown in the following figure.

Step 3: develop and use GSE SDK on the Unity server

Copy the four .cs files generated in the Step 2 to the Unity project (to a separate folder under the
Assets/Scripts/ directory) and use GSE SDK for the development. For more information, see Unity
DEMO.

1. Implement the OnHealthCheck , OnStartGameServerSession and OnProcessTerminate APIs defined by


gameserver_grpcsdk_service.proto .

public class GrpcServer : GameServerGrpcSdkService.GameServerGrpcSdkServiceBase


{
private static Logs logger
{
get

©2013-2019 Tencent Cloud. All rights reserved. Page 82 of 105


Game Server Elastic-scaling

{
return new Logs();
}
}
// Health checks
public override Task<HealthCheckResponse> OnHealthCheck(HealthCheckRequest request, ServerCall
Context context)
{
logger.Println($"OnHealthCheck, HealthStatus: {GseManager.HealthStatus}");
logger.Println($"OnHealthCheck, GameServerSession: {GseManager.GetGameServerSession()}");
return Task.FromResult(new HealthCheckResponse
{
HealthStatus = GseManager.HealthStatus
});
}
// Receive game sessions
public override Task<GseResponse> OnStartGameServerSession(StartGameServerSessionRequest reque
st, ServerCallContext context)
{
logger.Println($"OnStartGameServerSession, request: {request}");
GseManager.SetGameServerSession(request.GameServerSession);
var resp = GseManager.ActivateGameServerSession(request.GameServerSession.GameServerSessionId,
request.GameServerSession.MaxPlayers);
logger.Println($"OnStartGameServerSession, resp: {resp}");
return Task.FromResult(resp);
}
// End the game process
public override Task<GseResponse> OnProcessTerminate(ProcessTerminateRequest request, ServerCa
llContext context)
{
logger.Println($"OnProcessTerminate, request: {request}");
// Set the process termination time
GseManager.SetTerminationTime(request.TerminationTime);
// Terminate game server sessions
GseManager.TerminateGameServerSession();
// Exit the process
GseManager.ProcessEnding();
return Task.FromResult(new GseResponse());
}
}

2. Develop Unity server programs (taking ChatServer as an example).

public static void StartChatServer(int clientPort)


{
RegisterHandlers();

©2013-2019 Tencent Cloud. All rights reserved. Page 83 of 105


Game Server Elastic-scaling

logger.Println("ChatServer Listen at " + clientPort);


NetworkServer.Listen(clientPort);
}

3. Develop the gRPC server.

public static void StartGrpcServer(int clientPort, int grpcPort, string logPath)


{
try
{
Server server = new Server
{
Services = { GameServerGrpcSdkService.BindService(new GrpcServer()) },
Ports = { new ServerPort("127.0.0.1", grpcPort, ServerCredentials.Insecure) },
};
server.Start();
logger.Println("GrpcServer Start On localhost:" + grpcPort);
GseManager.ProcessReady(new string[] { logPath }, clientPort, grpcPort);
}
catch (System.Exception e)
{
logger.Println("error: " + e.Message);
}
}

4. Launch the implemented server and the gRPC server.

public class StartServers : MonoBehaviour


{
private int grpcPort = PortServer.GenerateRandomPort(2000, 6000);
private int chatPort = PortServer.GenerateRandomPort(6001, 10000);
private const string logPath = "./log/log.txt";
// Start is called before the first frame update
[Obsolete]
void Start()
{
// Start ChatServer By UNet's NetWorkServer, Listen on UDP protocol
MyChatServer.StartChatServer(chatPort);
// Start GrpcServer By Grpc, Listen on TCP protocol
MyGrpcServer.StartGrpcServer(chatPort, grpcPort, logPath);
}
[Obsolete]
void OnGUI()
{

©2013-2019 Tencent Cloud. All rights reserved. Page 84 of 105


Game Server Elastic-scaling

}
}

Unity DEMO
1. Click here to download the code of the Demo for Unity.
2. Import grpc unity package.
Decompress grpc_unity_package in the Step 2 to the unity-demo/Assets directory of the Demo
project.
3. Generate C# codes based on the Protobuf file.
4. Launch the server for GSE to call.
Implement the server: implement the three server APIs in the GrpcServer.cs file under the
unity-demo/Assets/Scripts/Api directory.
Run the server: create gRPC Server and StartServers.cs in the MyGrpcServer.cs file under the
unity-demo/Assets/Scripts directory to launch gRPC Server .
5. Connect the client to the gRPC server of GSE.
Implement the client: implement the nine client APIs in the Gsemanager.cs file under the unity-
demo/Assets/Scripts/Gsemanager directory.
Connect to the server: create a gRPC channel, specify the host name and server port to connect
to, and use this channel to create a stub instance.
6. Compile and run the program
Use Unity Editor to encapsulate the executable program of the target system into an asset
package, and configure the actual name of the executable program at the launch path.

©2013-2019 Tencent Cloud. All rights reserved. Page 85 of 105


Game Server Elastic-scaling

Getting Server Address


TencentCloud API Calling Method
Last updated:2021-03-30 10:13:29

A client API is provided as a TencentCloud API and can be called in the following ways:

1. SDK Call

You can use Tencent Cloud Software Development Kit (SDK) v3.0 to call a client TencentCloud API.
The SDK supports various programming languages such as PHP, Python, Java, Go, .NET, Node.js, and
C++.

Note:
Currently, GSE supports SDK v3.0. For detailed directions, please see the SDK overview.

2. Online Debugging

You can use API Explorer to call a client TencentCloud API. This tool provides various capabilities such
as online call, signature verification, SDK code generation, and quick API search.

Note:
In API 3.0 Explorer, select "GSE" and then select a TencentCloud API under "Console APIs" or
"Service Management APIs" for online debugging.

3. Direct Encapsulation
You can use the HTTP request method of a domain name or an API name to call a client TencentCloud
API.

Note:
TencentCloud APIs of GSE have been upgraded to v3.0. For detailed directions, please see
TencentCloud API calling methods.

©2013-2019 Tencent Cloud. All rights reserved. Page 86 of 105


Game Server Elastic-scaling

Creating Game Server Session


Last updated:2020-07-27 10:26:38

Overview
You can use a client TencentCloud API to create a game server session in the following two ways:
Create in a server fleet to implement auto scaling and health check.
Create through an alias to implement zero downtime update.
One game server session is placed in one server process, but the client API calling process varies
by supporting mode of the game server session.

Client API Calling Process


One game server session supports one game

If one game server session supports only one game, you can call a client API in the following steps:

1. Create a game server session through a server fleet or alias. For detailed directions, please see
the API document CreateGameServerSession.

Note:
The following sample code is based on Java:

public class CreateGameServerSession


{
public static void main(String [] args) {
try{

Credential cred = new Credential("", "");

HttpProfile httpProfile = new HttpProfile();


httpProfile.setEndpoint("gse.tencentcloudapi.com");

ClientProfile clientProfile = new ClientProfile();


clientProfile.setHttpProfile(httpProfile);

GseClient client = new GseClient(cred, "", clientProfile);

©2013-2019 Tencent Cloud. All rights reserved. Page 87 of 105


Game Server Elastic-scaling

String params = "{}";


CreateGameServerSessionRequest req = CreateGameServerSessionRequest.fromJsonString(params, Cre
ateGameServerSessionRequest.class);

CreateGameServerSessionResponse resp = client.CreateGameServerSession(req);

System.out.println(CreateGameServerSessionRequest.toJsonString(resp));
} catch (TencentCloudSDKException e) {
System.out.println(e.toString());
}
}
}

2. Join the created game server session. For detailed directions, please see the API document
JoinGameServerSession.

public class JoinGameServerSession


{
public static void main(String [] args) {
try{

Credential cred = new Credential("", "");

HttpProfile httpProfile = new HttpProfile();


httpProfile.setEndpoint("gse.tencentcloudapi.com");

ClientProfile clientProfile = new ClientProfile();


clientProfile.setHttpProfile(httpProfile);

GseClient client = new GseClient(cred, "", clientProfile);

String params = "{}";


JoinGameServerSessionRequest req = JoinGameServerSessionRequest.fromJsonString(params, JoinGameSe
rverSessionRequest.class);

JoinGameServerSessionResponse resp = client.JoinGameServerSession(req);

System.out.println(JoinGameServerSessionRequest.toJsonString(resp));
} catch (TencentCloudSDKException e) {
System.out.println(e.toString());
}
}
}

©2013-2019 Tencent Cloud. All rights reserved. Page 88 of 105


Game Server Elastic-scaling

One game server session supports multiple games or one service


If one game server session supports multiple games or one service (such as login), you can all a
client API in the following steps:

1. Query the game server session list to check whether there is any game server session. For
detailed directions, please see the API document DescribeGameServerSessions.

public class DescribeGameServerSessions


{
public static void main(String [] args) {
try{

Credential cred = new Credential("", "");

HttpProfile httpProfile = new HttpProfile();


httpProfile.setEndpoint("gse.tencentcloudapi.com");

ClientProfile clientProfile = new ClientProfile();


clientProfile.setHttpProfile(httpProfile);

GseClient client = new GseClient(cred, "", clientProfile);

String params = "{}";


DescribeGameServerSessionsRequest req = DescribeGameServerSessionsRequest.fromJsonString(param
s, DescribeGameServerSessionsRequest.class);

DescribeGameServerSessionsResponse resp = client.DescribeGameServerSessions(req);

System.out.println(DescribeGameServerSessionsRequest.toJsonString(resp));
} catch (TencentCloudSDKException e) {
System.out.println(e.toString());
}
}
}

You can also search for existing sessions in the game server session list. For detailed directions,
please see the API document SearchGameServerSessions.

public class SearchGameServerSessions


{
public static void main(String [] args) {
try{

Credential cred = new Credential("", "");

©2013-2019 Tencent Cloud. All rights reserved. Page 89 of 105


Game Server Elastic-scaling

HttpProfile httpProfile = new HttpProfile();


httpProfile.setEndpoint("gse.tencentcloudapi.com");

ClientProfile clientProfile = new ClientProfile();


clientProfile.setHttpProfile(httpProfile);

GseClient client = new GseClient(cred, "", clientProfile);

String params = "{}";


SearchGameServerSessionsRequest req = SearchGameServerSessionsRequest.fromJsonString(params, S
earchGameServerSessionsRequest.class);

SearchGameServerSessionsResponse resp = client.SearchGameServerSessions(req);

System.out.println(SearchGameServerSessionsRequest.toJsonString(resp));
} catch (TencentCloudSDKException e) {
System.out.println(e.toString());
}
}
}

2. If a game server session exists, you can directly join it. For detailed directions, please see the API
document JoinGameServerSession or the sample code in this document.

3. If no game server sessions exist, you need to create one first. For detailed directions, please see
the API document CreateGameServerSession or the sample code in this document. Then, join the
created session. For detailed directions, please see the API document JoinGameServerSession or
the sample code in this document.

Note:
You can use API 3.0 Explorer for online debugging. You can select TencentCloud APIs under
"Game Server Engine" > "Service Management APIs" on the left sidebar and perform
operations such as "Code Generation" and "Online Call".

©2013-2019 Tencent Cloud. All rights reserved. Page 90 of 105


Game Server Elastic-scaling

Placing Game Server Session


Last updated:2021-04-20 15:06:54

Overview
You can use a client TencentCloud API to place a game server session, that is, you can implement
nearby resource scheduling and cross-region disaster recovery through a game server queue.

Client API Calling Process


1. First, check whether a game server session has been placed in a process. For detailed directions,
please see the API document DescribeGameServerSessionPlacement.

Note:
The following sample code is based on Java:

public class DescribeGameServerSessionPlacement


{
public static void main(String [] args) {
try{

Credential cred = new Credential("", "");

HttpProfile httpProfile = new HttpProfile();


httpProfile.setEndpoint("gse.tencentcloudapi.com");

ClientProfile clientProfile = new ClientProfile();


clientProfile.setHttpProfile(httpProfile);

GseClient client = new GseClient(cred, "", clientProfile);

String params = "{}";


DescribeGameServerSessionPlacementRequest req = DescribeGameServerSessionPlacementRequest.fromJso
nString(params, DescribeGameServerSessionPlacementRequest.class);

DescribeGameServerSessionPlacementResponse resp = client.DescribeGameServerSessionPlacement(req);

System.out.println(DescribeGameServerSessionPlacementRequest.toJsonString(resp));
} catch (TencentCloudSDKException e) {

©2013-2019 Tencent Cloud. All rights reserved. Page 91 of 105


Game Server Elastic-scaling

System.out.println(e.toString());
}
}
}

2. Start placing the game server session. For detailed directions, please see the API document
StartGameServerSessionPlacement.

public class StartGameServerSessionPlacement


{
public static void main(String [] args) {
try{

Credential cred = new Credential("", "");

HttpProfile httpProfile = new HttpProfile();


httpProfile.setEndpoint("gse.tencentcloudapi.com");

ClientProfile clientProfile = new ClientProfile();


clientProfile.setHttpProfile(httpProfile);

GseClient client = new GseClient(cred, "", clientProfile);

String params = "{}";


StartGameServerSessionPlacementRequest req = StartGameServerSessionPlacementRequest.fromJsonStrin
g(params, StartGameServerSessionPlacementRequest.class);

StartGameServerSessionPlacementResponse resp = client.StartGameServerSessionPlacement(req);

System.out.println(StartGameServerSessionPlacementRequest.toJsonString(resp));
} catch (TencentCloudSDKException e) {
System.out.println(e.toString());
}
}
}

3. Stop placing the game server session. For detailed directions, please see the API document
StopGameServerSessionPlacement.

public class StopGameServerSessionPlacement


{
public static void main(String [] args) {
try{

Credential cred = new Credential("", "");

©2013-2019 Tencent Cloud. All rights reserved. Page 92 of 105


Game Server Elastic-scaling

HttpProfile httpProfile = new HttpProfile();


httpProfile.setEndpoint("gse.tencentcloudapi.com");

ClientProfile clientProfile = new ClientProfile();


clientProfile.setHttpProfile(httpProfile);

GseClient client = new GseClient(cred, "", clientProfile);

String params = "{}";


StopGameServerSessionPlacementRequest req = StopGameServerSessionPlacementRequest.fromJsonString(
params, StopGameServerSessionPlacementRequest.class);

StopGameServerSessionPlacementResponse resp = client.StopGameServerSessionPlacement(req);

System.out.println(StopGameServerSessionPlacementRequest.toJsonString(resp));
} catch (TencentCloudSDKException e) {
System.out.println(e.toString());
}
}
}

Note:
You can use API 3.0 Explorer for online debugging. You can select TencentCloud APIs under
"Game Server Elastic-scaling" > "Service Management APIs" on the left sidebar, and perform
operations such as "Code Generation" and "Online Call".

©2013-2019 Tencent Cloud. All rights reserved. Page 93 of 105


Game Server Elastic-scaling

GSE Local
Last updated:2021-06-28 10:09:36

GSE Local
GSE Local is a command line tool that can independently launch the game server hosting service
GSE. This tool also provides runtime logs including the server initialization, health check, and API
calls and responses.

GSE Local is limited to launch GSE hosting services and test your game integration on a local device,
which will shorten the debugging time and improve efficiency at the iterative development of games.
Otherwise, you have to upload each new game package to GSE and configure the server fleet to host
games.

With GSE Local, you can test that:

Your game server correctly integrates the GSE server development kit, properly communicates
with GSE service, and is able to launch new game sessions, accept new players and report the
running status.
Your game client correctly integrates the GSE-related TencentCloud APIs to retrieve existing game
sessions, launch new game sessions, and allow players to join and connect to the game session.

Setting Up GSE Local


GSE Local can run on Windows, Linux and Mac in any GSE-supported languages. You can download
the installation package according to the operating system:

GSE Local for Windows


GSE Local for Linux
GSE Local for Mac

Note:
The following sample code is applicable to Linux and MacOS. For Windows, we recommend to
use the gitbash command line tool to run curl command.

©2013-2019 Tencent Cloud. All rights reserved. Page 94 of 105


Game Server Elastic-scaling

Testing Game Server


If you only need to test your game server, directly use curl to simulate the game client calls to
GSE Local and verify that your game server can complete the following operations as expected:

1. During launch, the game server will call the ProcessReady API to inform GSE that the server is
ready to host a game server session.
2. During runtime, the game server will use the onHealthCheck callback to send its running status to
GSE every minute.
3. The game server will respond to requests and trigger the onStartGameServerSession callback (call
the activateGameServerSession API in this process) to launch a new game session.

Step 1: launch GSE Local

Open the command prompt window, navigate to the directory of gselocal_windows , gselocal_linux
or gselocal_mac , and run the program. This document uses the Mac program ./gselocal_mac as an
example. After the program is launched, it will automatically connect to GSE Local.
Enter the following command in a terminal window:

./gselocal_mac

If the following information appears in the command prompt window, the launch is successful:

{"level":"info","ts":"2020-10-20T09:16:09.364+0800","msg":"start grpc v3 server success"}

Step 2: launch the game server

Launch the game process in a programming tool or command line tool. The game process then will
call the ProcessReady API to prepare for hosting a session and print the following logs:

Getting process ready, LogPath: System.String[], ClientPort: 3237, GrpcPort: 6224


Process ready succeed, resp: { }
Server Start On Locolhost:6224

After receiving the ProcessReady request, GSE Local will also print logs and start the health check:

{"level":"info","ts":"2020-10-20T09:27:03.172+0800","msg":"ProcessReady Info is","pid":"41688","r


equestId":"3b38495b38bc4ef8a59ae8****a8256d","info":"clientPort:3237 grpcPort:6224 "}
{"level":"info","ts":"2020-10-20T09:27:03.172+0800","msg":"set runner success","pid":"41688","pro
cessUUID":"527bf89b-d128-4b5d-bfea-****3d22ede7"}
{"level":"info","ts":"2020-10-20T09:28:03.276+0800","msg":"onHealthCheck received","pid":"41688",
"health":true}

©2013-2019 Tencent Cloud. All rights reserved. Page 95 of 105


Game Server Elastic-scaling

{"level":"info","ts":"2020-10-20T09:29:03.256+0800","msg":"onHealthCheck received","pid":"41688",
"health":true}
{"level":"info","ts":"2020-10-20T09:30:03.261+0800","msg":"onHealthCheck received","pid":"41688",
"health":true}

Step 3: use curl to create a game server session and a player session

Use curl to simulate the client calls. For specific parameters, see APIs.

Create a game server session

Run the following command to configure the FleetId parameter. You can set it to any valid strings
(^fleet-\S+) in GSE Local.

curl -d '{"Action":"CreateGameServerSession", "FleetId":"fleet-1235", "MaximumPlayerSessionCount"


:5}' http://127.0.0.1:8080/capi

The following log message displayed in the command prompt window indicates that GSE Local has
sent the onStartGameServerSession callback to your game server. If a game server session is
successfully created, your game server will call the ActivateGameServerSession API to respond to the
callback. The logs are as follows:

{"level":"info","ts":"2020-10-20T09:37:08.580+0800","msg":"API to use: GSE.CreateGameServerSessio


n, with input","req":"FleetId:<value:\"fleet-1235\" > MaximumPlayerSessionCount:<value:5 > "}
{"level":"info","ts":"2020-10-20T09:37:08.580+0800","msg":"Reserved process: 41688 for GameServer
Session: qcs::gse:local::gameserversession/fleet-1235/gssess-c648654a-293b-4f1f-b71f-****6a09bff
e"}
{"level":"info","ts":"2020-10-20T09:37:08.580+0800","msg":"start to call StartGameSessionByGrpc t
o game server","gameServerSessionId":"qcs::gse:local::gameserversession/fleet-1235/gssess-c648654
a-293b-4f1f-b71f-****6a09bffe"}
{"level":"info","ts":"2020-10-20T09:37:08.597+0800","msg":"onGameSessionActivate received","pid":
"4****","gameServerSessionId":"qcs::gse:local::gameserversession/fleet-1235/gssess-c648654a-293b-
4f1f-b71f-****6a09bffe","requestId":"de1a678dea364db4b487ff84ad****31"}
{"level":"info","ts":"2020-10-20T09:37:08.598+0800","msg":"call StartGameSessionByGrpc to game se
rver success","gameServerSessionId":"qcs::gse:local::gameserversession/fleet-1235/gssess-c648654a
-293b-4f1f-b71f-****6a09bffe"}

Querying a game server session

GSE Local uses curl to pass the game server session ID and object. Please note that the status of a
new server session will change from “Activating” to “Active” after the game server calls the
ActivateGameServerSession API. To view the status, run the following curl command to call the
DescribeGameServerSessions API:

©2013-2019 Tencent Cloud. All rights reserved. Page 96 of 105


Game Server Elastic-scaling

curl -d '{"Action":"DescribeGameServerSessions", "FleetId":"fleet-1235"}' http://127.0.0.1:8080/c


api

The output is as shown below:

{"Response":{"GameServerSessions":[{"AvailabilityStatus":"Enable","CreationTime":"2020-10-20T01:3
7:08Z","CreatorId":"","CurrentCustomCount":0,"CurrentPlayerSessionCount":0,"DnsName":"","FleetId"
:"fleet-1235","GameProperties":[],"GameServerSessionData":"","GameServerSessionId":"qcs::gse:loca
l::gameserversession/fleet-1235/gssess-c648654a-293b-4f1f-b71f-2fa56a09bffe","InstanceType":"loca
lhost","IpAddress":"127.0.0.1","MatchmakerData":"","MaxCustomCount":0,"MaximumPlayerSessionCount"
:5,"Name":"","PlayerSessionCreationPolicy":"ACCEPT_ALL","Port":3237,"Status":"ACTIVE","StatusReas
on":"","TerminationTime":null,"Weight":0}],"NextToken":"","RequestId":"s1603158295201357000"}}

Testing Game Server and Client


Prerequisites

You have completed the game server tests.

Step 1: add players


Run the following command to add players. The GameServerSessionId parameter is obtained in the
response of the API used in creating a game server session

curl -d '{"Action":"JoinGameServerSession", "GameServerSessionId":"qcs::gse:local::gameserversess


ion/fleet-1235/gssess-c648654a-293b-4f1f-b71f-****6a09bffe", "PlayerId":"k****111"}' http://127.
0.0.1:8080/capi

The GSE Local Command Prompt displays the following logs, indicating that the game server has
sent the AcceptPlayerSession request to verify a new player connection.

{"level":"info","ts":"2020-10-20T10:03:43.096+0800","msg":"API to use: GSE.JoinGameServerSession,


with input","req":"GameServerSessionId:\"qcs::gse:local::gameserversession/fleet-****/gssess-c648
654a-293b-4f1f-b71f-****6a09bffe\" PlayerId:\"ka****11\" "}
{"level":"info","ts":"2020-10-20T10:03:43.096+0800","msg":"Creating player session with id: kadin
111 for gameServersessionId: qcs::gse:local::gameserversession/fleet-****/gssess-c648654a-293b-4f
1f-b71f-****6a09bffe"}
{"level":"info","ts":"2020-10-20T10:03:43.096+0800","msg":"Created player session with PlayerId:
kadin111 and PlayerSessionId: psess-56dd6f48-08d4-4a11-9330-****09784977"}

Step 2: query a player session

©2013-2019 Tencent Cloud. All rights reserved. Page 97 of 105


Game Server Elastic-scaling

Call the DescribePlayerSessions to query a player session. The initial status of the player session is
“Reserved”:

If the client successfully connects to the game server within 1 minute, the player session status
will become “Active”.
If the client fails to connect to the game server within 1 minute, the player session status will
become “TIMEDOUT”.

curl -d '{"Action":"DescribePlayerSessions", "GameServerSessionId":"qcs::gse:local::gameserverses


sion/fleet-1235/gssess-c648654a-293b-4f1f-b71f-2fa56a09bffe", "PlayerId":"kadin111"}' http://127.
0.0.1:8080/capi

The output is as shown below:

{"Response":{"NextToken":"","PlayerSessions":[{"CreationTime":"2020-10-20T02:03:43Z","DnsName":""
,"FleetId":"fleet-****","GameServerSessionId":"qcs::gse:local::gameserversession/fleet-1235/gsses
s-c648654a-293b-4f1f-b71f-****6a09bffe","IpAddress":"127.*.*.1","PlayerData":"","PlayerId":"ka***
*11","PlayerSessionId":"psess-56dd6f48-08d4-4a11-9330-****09784977","Port":3237,"Status":"TIMEDOU
T","TerminationTime":"1970-01-01T00:00:00Z"}],"RequestId":"s16031596094****2000"}}%

Step 3: connect the client player to the server

After creating a game session and player session, you can directly use localhost:port to join a
client player to the game session.

The GSE Local Command Prompt will display logs, indicating that the game server has sent the
AcceptPlayerSession request to verify the new player connection. If you use curl to call the
DescribePlayerSessions API, the player session status should be changed from “Reserved” to
“Active”.

Step 4: send the test report to GSE

To verify that your game server sends the game and player statuses to GSE, ensure your game
server always send these statuses to GSE Local to help GSE Local manage player needs and
correctly report metrics. GSE Local will record the following actions. You may also need curl to
track the status change.

A player disconnects from the game session


The GSE Local logs should display that the game server called the RemovePlayerSession API. The
status in the response of the DescribePlayerSessions() API changed from “Active” to “Completed”.
You can also call the DescribeGameServerSessions API to check that the current number of players
in the game session has decreased by one.

©2013-2019 Tencent Cloud. All rights reserved. Page 98 of 105


Game Server Elastic-scaling

The game session ends


The GSL Local logs should display that the game server called the TerminateGameServerSession API.
The status in the response of the DescribeGameServerSessions API changed from “Active” to
“Terminated” or “Terminating”.
The server process stops
The GSE Local logs should display that the game server called the ProcessEnding API.

Testing Game Client Calls to GSE


All game session and player session APIs used in game server tests and game server and client tests
use curl to call GSE Local. You can use codes to call the following APIs in the game service to verify
whether your game server is running properly. For the local debugging, you need to call
http://127.0.0.1:8080/capi .

CreateGameServerSession
DescribeGameServerSessions
JoinGameServerSession
JoinGameServerSessionBatch
DescribePlayerSessions

The GSE Local Command Prompt only displays the logs of the CreateGameServerSession API calls. As
shown in the log message, GSE Local prompts the time when your game server launches a game
session (using the onStartGameServerSession callback). After your game server uses the callback,
GSE Local will obtain the ActivateGameServerSession response. You can use curl to view the calling
of other APIs.

Notes
Take notice of the following points when using GSE Local:

1. Different from the GSE Web service, GSE Local does not track the running status or the
onProcessTerminate callback triggering of the server. GSE Local only records the runtime report of
the game server.
2. The FleetId will not be verified during the calling of Tencent Cloud development kid, because this
parameter can be set to any valid strings (^fleet-\S+) .
3. The game session created using GSE Local has a distinct ID structure, which contains local as
shown below:

©2013-2019 Tencent Cloud. All rights reserved. Page 99 of 105


Game Server Elastic-scaling

arn:gse:local::gamesession/fleet-****/gsess-56961f8e-db9c-4173-97e7-****82f0daa6

©2013-2019 Tencent Cloud. All rights reserved. Page 100 of 105


Game Server Elastic-scaling

Latency Test Tool


Last updated:2021-04-12 14:22:16

This document provides the addresses and examples for latency test in different regions. Both HTTPS
and UDP addresses are supported.

HTTPS and UDP addresses for latency test in regions

Region HTTPS Address UDP Address

https://ap-
Beijing ap-beijing.speed.tencentgse.com
beijing.speed.tencentgse.com

https://ap- ap-
Shanghai
shanghai.speed.tencentgse.com shanghai.speed.tencentgse.com

Hong Kong https://ap- ap-


(China) hongkong.speed.tencentgse.com hongkong.speed.tencentgse.com

https://ap- ap-
Guangzhou
guangzhou.speed.tencentgse.com guangzhou.speed.tencentgse.com

https://ap-
Chengdu ap-chengdu.speed.tencentgse.com
chengdu.speed.tencentgse.com

https://ap- ap-
Singapore
singapore.speed.tencentgse.com singapore.speed.tencentgse.com

https://ap-
Mumbai ap-mumbai.speed.tencentgse.com
mumbai.speed.tencentgse.com

https://na- na-
Silicon Valley
siliconvalley.speed.tencentgse.com siliconvalley.speed.tencentgse.com

https://na-
Virginia na-ashburn.speed.tencentgse.com
ashburn.speed.tencentgse.com

https://eu-
Frankfurt eu-frankfurt.speed.tencentgse.com
frankfurt.speed.tencentgse.com

Seoul https://ap-seoul.speed.tencentgse.com ap-seoul.speed.tencentgse.com

Tokyo https://ap-tokyo.speed.tencentgse.com ap-tokyo.speed.tencentgse.com

Example

©2013-2019 Tencent Cloud. All rights reserved. Page 101 of 105


Game Server Elastic-scaling

Let’s take Guangzhou as an example.

HTTPS

ping ap-guangzhou.speed.tencentgse.com
curl https://ap-guangzhou.speed.tencentgse.com/v1/ping

UDP

Domain name + PORT (8888)


ap-guangzhou.speed.tencentgse.com + PORT (8888)

©2013-2019 Tencent Cloud. All rights reserved. Page 102 of 105


Game Server Elastic-scaling

Game Process Launch Configuration


Last updated:2021-04-12 14:22:16

Launching game process as a root user or user_00 in Linux


environment
In Linux environment, the game process should be launched by a root user by default. If you want to
launch the game process as a non-root user, please do the following:

1. Add the file gse.yaml to the root directory of the game’s asset package, which means the
decompressed file path will be /local/game/gse.yaml on the game server fleet instance;

2. The content of the file gse.yaml is shown below, indicating that user_00 is added to the users
user group. You cannot configure other users and user groups currently;

User: user_00:users

When the file gse.yaml is added to the asset package, GSE will launch the game process with
user_00:users and set the users and user groups of all files under /local/game as
user_00:users .

©2013-2019 Tencent Cloud. All rights reserved. Page 103 of 105


Game Server Elastic-scaling

See below for the example:

Executing install.sh before launching game process in


Linux environment
Before a game process is launched, you may need to install some software or configure some
environment variables on the CVM instance with the following steps:

1. Create the install.sh script and write the operations to be conducted before launching the game
process in this script;
2. Add the file install.sh under the root directory of the game’s asset package, which means the
decompressed path will be /local/game/install.sh on the game server fleet instance.

Launch configuration for Java game process


In Linux environment, a command like java -jar XXXX.jar can be used to launch Java programs.
The following configurations are required to ensure the Java game process is successfully launched:

1. Write the install.sh script

#!/bin/bash

©2013-2019 Tencent Cloud. All rights reserved. Page 104 of 105


Game Server Elastic-scaling

# Install the JDK 1.8 environment - y indicates answer yes for all questions
yum install java-1.8.0-openjdk.x86_64 -y
# Put the java command under `/local/game` with a soft link
ln -s /usr/bin/java /local/game/java

2. Put install.sh script under the root directory of the game’s asset package, which means the
decompressed path will be /local/game/install.sh on the game server fleet instance.

3. When creating the game server fleet, enter /local/game/java as the launch path, and enter -jar
jar package specified by user as the launch parameter.

4. After the game process is successfully launched, the content of the path /local/game is shown
below:

©2013-2019 Tencent Cloud. All rights reserved. Page 105 of 105

You might also like