In the context of the MMO game demo application, the client portion acts as a server, while the upcoming content in this chapter pertains to the server portion of the game. Prior to development, it is necessary to define the protocol between the two parties.

13.1 Protocol Definition

Based on the requirements of the client application, it is understood that the client will transmit the following types of application protocol game data to the server, as shown in Table 13–1.

Table 13–1

13.1.1 Table Explanation

The meaning of each column in the table is as follows:

MsgID: The Message ID of the current communication protocol, synonymous with the MsgID defined in Zinx.
Client: The name of the protocol for the client, and the current message is initiated by the client.
Server: The name of the protocol for the server, and the current message is initiated by the server.
Description: The description of the current protocol.

13.1.2 Protocol Meanings

In Table 13–1, there are a total of 6 protocols, and their meanings are as follows:

SyncPID Protocol: Initiated by the server to the client, Message ID 1. It informs the client of other online users that the current player has come online.

Talk Protocol: Initiated by the client to the server, Message ID 2. It represents the client-initiated world chat message, informing the server to broadcast the message.

Position Protocol: Initiated by the client to the server, Message ID 3. It carries the coordinates of the client player’s movement.

BroadCast Protocol: Initiated by the server to the client, Message ID 200. It represents server-initiated broadcast messages to all online player clients. The Tp variable indicates different types of business.

SyncPID Protocol: Initiated by the server to the client, Message ID 201. It represents server-initiated broadcast messages for player disconnections or changing viewpoints.

SyncPlayers Protocol: Initiated by the server to the client, Message ID 202. It synchronizes the positions of surrounding players, including the player’s own position, to other players.

13.2 Proto3 Protocol Definitions

This section will define the specific data protocol format for Protobuf based on the protocol definition format provided in Table 13–1.

13.2.1 Protocol One (MsgID=1)

The SyncPid protocol is used to synchronize the player’s unique ID generated by the server to the client after the player logs in. This ID serves to identify the player.
The Proto protocol definition is as follows:

message SyncPid {
int32 Pid=1;
}

Relevant Parameters are as Follows:
(1) Pid: Player ID.

13.2.2 Protocol Two (MsgID=2)

Talk Protocol, world chat, this message is initiated by the Client client.

Proto protocol definition is as follows:

message Talk {
string Content = 1;
}

Relevant parameters are as follows:
(1) Content: Chat message.

13.2.3 Protocol Three (MsgID=3)

Position Protocol, coordinates of movement, message about the movement coordinates of the client player, initiated by the Client.

Proto protocol definition is as follows:

message Position {
float X = 1;
float Y = 2;
float Z = 3;
float V = 4;
}

Relevant parameters are as follows:
(1) X: X-axis coordinate in the 3D map.
(2) Y: Height coordinate in the 3D map, not the Y-axis.
(3) Z: Y-axis coordinate in the 3D map.
(4) V: Angle of rotation for objects or tasks (0~360°).

13.2.4 Protocol Four (MsgID=200)

BroadCast Protocol, broadcast message, the server broadcasts the message to all online player clients, and the Tp variable indicates different types of businesses.

Proto protocol definition is as follows:

message BroadCast {
int32 Pid = 1;
int32 Tp = 2;
oneof Data {
string Content = 3;
Position P = 4;
int32 ActionData = 5;
}
}

Relevant parameters are as follows:

(1) Pid: Player ID.
(2) Tp: Type of broadcast message. 1 for world chat, 2 for coordinates, 3 for actions, and 4 for coordinates update after movement.

(3) Data: Specific message format for transmission. Data is decorated with the “oneof” keyword. Depending on Tp, Data will have different specific data types. Content is for Tp=1, broadcasting world chat data; Position is for Tp=2 or 4, broadcasting coordinate data; ActionData is for Tp=3, broadcasting specific player action data.

13.2.5 Protocol Five (MsgID=201)

SyncPid Protocol, broadcast message, a player goes offline or a player’s visibility disappears from the AOI region, initiated by the Server.

Proto protocol definition is as follows:

message SyncPid {
int32 Pid = 1;
}

Relevant parameters are as follows:
(1) Pid: Player ID.

13.2.6 Protocol Six (MsgID=202)

SyncPlayers Protocol, synchronizes the positions of nearby players, including the current player, to other players. Initiated by the Server.

Proto protocol definition is as follows:

message SyncPlayers {
repeated Player ps = 1;
}

message Player {
int32 Pid = 1;
Position P = 2;
}

Relevant parameters are as follows:
(1) Player: Collection of players to be synchronized, decorated with the “repeated” keyword.
(2) Pid: Player ID.
(3) Position: Position information.

--

--