Game server (Nano) login & game packet communication actual combat

series

  1. Golang develops production grade Mahjong game server – Part 1
  2. Golang develops production grade Mahjong game server – Part 2
  3. Golang development production grade mahjong game server – chapter 3
  4. Golang development production grade Mahjong game server – chapter 4

introduce

This will be a complete, fully implemented Golang game server development tutorial series for DevOps/GitOps and cloud processes on Kubernetes.

This series of tutorials is a complete teardown of the open source Nanoserver project, designed to help you get started on the Golang server backend. Understand the essence of Golang development through practice — Share memory by communication.

The project may also involve work on Linux performance tuning (BPF related tools) and system assurance (SRE).

Step By Step development Mahjong Server

  • Monomer architectureunderstandMahjong ServerBusiness – >Nano Distributed Game Server + Micro serviceTransformation.
  • Demo: go – mahjong – server

The client connects to the game server

Enable the Debug Mode on the game server

After nano-.withdebugmode () is added to the server, detailed log output will be generated.

// internal/game/game.go
nano.Listen(addr,
  nano.WithPipeline(pip),
  nano.WithHeartbeatInterval(time.Duration(heartbeat)*time.Second),
  nano.WithLogger(log.WithField("component"."nano")),
  nano.WithSerializer(json.NewSerializer()),
  nano.WithComponents(comps),
  nano.WithDebugMode(),
)
Copy the code

Viewing Login Logs

. . time="2021-02-18T23:01:16+08:00" level=info msg="New session established: Remote=192.168.31.125:62569, LastTime=1613660476" component=nano
time="2021-02-18T23:01:16+08:00" level=info msg="The Session handshake Id = 2, Remote = 192.168.31.125:62569" component=nano
time="2021-02-18T23:01:16+08:00" level=info msg="Receive handshake ACK Id=2, Remote=192.168.31.125:62569" component=nano
time="2021-02-18T23:01:16+08:00" level=info msg="UID=0, Message={Request Manager.Login (195bytes)}, Data=&{Name:G1 Uid:1 HeadUrl:http://wx.qlogo.cn/mmopen/s962LEwpLxhQSOnarDnceXjSxVGaibMRsvRM4EIWic0U6fQdkpqz4Vr8XS8D81QKfyYuwjwm2M2ibsFY8mia8i FangKa c51ww / 0 Sex: 1:10 IP: 192.168.31.125}" component=nano
time="2021-02-18T23:01:16+08:00" level=info msg="Player: 1 Login: &{Name:G1 Uid:1 HeadUrl:http://wx.qlogo.cn/mmopen/s962LEwpLxhQSOnarDnceXjSxVGaibMRsvRM4EIWic0U6fQdkpqz4Vr8XS8D81QKfyYuwjwm2M2ibsFY8mia8i FangKa c51ww / 0 Sex: 1:10 IP: 192.168.31.125}"
time="2021-02-18T23:01:16+08:00" level=info msg="Player: 1 not online, create a new player"
time="2021-02-18T23:01:16+08:00" level=info msg="Add session to group _SYSTEM_MESSAGE_BROADCAST, ID=2, UID=1" component=nano
time="2021-02-18T23:01:16+08:00" level=info msg="Type=Response, ID=2, UID=1, MID=1, Data=&{Uid:1 Nickname:G1 HeadUrl:http://wx.qlogo.cn/mmopen/s962LEwpLxhQSOnarDnceXjSxVGaibMRsvRM4EIWic0U6fQdkpqz4Vr8XS8D81QKfyYuwjwm2M2ibsFY8mia8i c51ww/0 Sex:1 FangKa:10}" component=nano
time="2021-02-18T23:01:16+08:00" level=info msg="[SQL] SELECT `id`, `algo`, `hash`, `salt`, `role`, `status`, `is_online`, `last_login_at`, `priv_key`, `pub_key`, `coin`, `register_at`, `first_recharge_at`, `debug` FROM `user` WHERE `id`=? LIMIT 1 []interface {}{1}" component=model orm=xorm
time="2021-02-18T23:01:16+08:00" level=info msg="Type=Push, ID=2, UID=1, Route=onCoinChange, Data=&{Coin:10}" component=nano
time="2021-02-18T23:01:16+08:00" level=info msg="Receive handshake ACK Id=2, Remote=192.168.31.125:62569" component=nano
time="2021-02-18T23:01:16+08:00" level=info msg="UID=1, Message={Request DeskManager.UnCompleteDesk (2bytes)}, Data=[91 93]" component=nano
time="2021-02-18T23:01:16+08:00" level=debug msg="DeskManager. UnCompleteDesk: the player is not in the room" player=1
time="2021-02-18T23:01:16+08:00" level=info msg="Type=Response, ID=2, UID=1, MID=2, Data=&{Exist:false TableInfo:{DeskNo: CreatedAt:0 Creator:0 Title: Desc: Round:0 Mode:0}}"component=nano ... .Copy the code

Analysis of the log

You can clearly see three info level logs:

  • New session established(A connection is entered, a new session is established.)
  • Session handshake(The client initiates a handshake request to the server)
  • Receive handshake ACKThe client sends a handshake to the serverACK)

In fact, this is the game client and game server (Nano framework) handshake 🤝 process.

When the underlying connection is established, the client makes a handshake request to the server, along with the necessary data. The server verifies the handshake data and returns the handshake response. If the handshake succeeds, the client sends a handshake ACK to the server, and the handshake phase ends successfully. For more, check out the official Nano communication protocol

Next we see the following log:

UID=0, Message={Request Manager.Login (195bytes)}, Data=&{Name:G1 Uid:1 HeadUrl:http://wx.qlogo.cn/mmopen/s962LEwpLxhQSOnarDnceXjSxVGaibMRsvRM4EIWic0U6fQdkpqz4Vr8XS8D81QKfyYuwjwm2M2ibsFY8mia8i FangKa c51ww / 0 Sex: 1:10 IP: 192.168.31.125}Copy the code

Indicates that the Game Client sends a packet of message type Request to the Game Server’s route manager.login.

Manager.Login

Here are the basics of Nano: 5 minutes to get started Nano Game server framework

Business code logic analysis

  • For the currentSessionBinding the playeruid
  • Querying the player list
  • Player not online, create a new player
    • Initialize the player’s game data
    • The bindingSessionTo current player
    • Asynchronously synchronize room cards from the database
    • Add players to the player list for unified management
  • The player online
    • Reset the previous session
    • Binding of new session
  • Add to broadcast channel
  • In response to the results

Communication protocols involved:

protocol/login.go

type LoginToGameServerRequest struct {
	Name    string `json:"name"`
	Uid     int64  `json:"uid"`
	HeadUrl string `json:"headUrl"`
	Sex     int    `json:"sex"` //[0] Unknown [1] male [2] female
	FangKa  int    `json:"fangka"`
	IP      string `json:"ip"`
}

type LoginToGameServerResponse struct {
	Uid      int64  `json:"acId"`
	Nickname string `json:"nickname"`
	HeadUrl  string `json:"headURL"`
	Sex      int    `json:"sex"`
	FangKa   int    `json:"fangka"`
}
Copy the code

A picture is worth a thousand words

I am weishao wechat: uuhells123 public number: hackers afternoon tea add my wechat (mutual learning exchange), pay attention to the public number (for more learning materials ~)Copy the code