Leaf

Go(golang)中的游戏服务器框架。「A game server framework in Go (golang)」

Github stars Tracking Chart

Leaf 游戏服务器框架简介

文档

Leaf 是一个由 Go 语言(golang)编写的开发效率和执行效率并重的开源游戏服务器框架。Leaf 适用于各类游戏服务器的开发,包括 H5(HTML5)游戏服务器。

Leaf 的关注点:

  • 良好的使用体验。Leaf 总是尽可能的提供简洁和易用的接口,尽可能的提升开发的效率
  • 稳定性。Leaf 总是尽可能的恢复运行过程中的错误,避免崩溃
  • 多核支持。Leaf 通过模块机制和 leaf/go 尽可能的利用多核资源,同时又尽量避免各种副作用
  • 模块机制。

Leaf 的模块机制

一个 Leaf 开发的游戏服务器由多个模块组成(例如 LeafServer),模块有以下特点:

  • 每个模块运行在一个单独的 goroutine 中
  • 模块间通过一套轻量的 RPC 机制通讯(leaf/chanrpc

Leaf 不建议在游戏服务器中设计过多的模块。

游戏服务器在启动时进行模块的注册,例如:

leaf.Run(
    game.Module,
    gate.Module,
    login.Module,
)

这里按顺序注册了 game、gate、login 三个模块。每个模块都需要实现接口:

type Module interface {
    OnInit()
    OnDestroy()
    Run(closeSig chan bool)
}

Leaf 首先会在同一个 goroutine 中按模块注册顺序执行模块的 OnInit 方法,等到所有模块 OnInit 方法执行完成后则为每一个模块启动一个 goroutine 并执行模块的 Run 方法。最后,游戏服务器关闭时(Ctrl + C 关闭游戏服务器)将按模块注册相反顺序在同一个 goroutine 中执行模块的 OnDestroy 方法。

Leaf 源码概览

  • leaf/chanrpc 提供了一套基于 channel 的 RPC 机制,用于游戏服务器模块间通讯
  • leaf/db 数据库相关,目前支持 MongoDB
  • leaf/gate 网关模块,负责游戏客户端的接入
  • leaf/go 用于创建能够被 Leaf 管理的 goroutine
  • leaf/log 日志相关
  • leaf/network 网络相关,使用 TCP 和 WebSocket 协议,可自定义消息格式,默认 Leaf 提供了基于 protobuf 和 JSON 的消息格式
  • leaf/recordfile 用于管理游戏数据
  • leaf/timer 定时器相关
  • leaf/util 辅助库

使用 Leaf 开发游戏服务器

LeafServer 是一个基于 Leaf 开发的游戏服务器,我们以 LeafServer 作为起点。

获取 LeafServer:

git clone https://github.com/name5566/leafserver

设置 leafserver 目录到 GOPATH 环境变量后获取 Leaf:

go get github.com/name5566/leaf

编译 LeafServer:

go install server

如果一切顺利,运行 server 你可以获得以下输出:

2015/08/26 22:11:27 [release] Leaf 1.1.2 starting up

敲击 Ctrl + C 关闭游戏服务器,服务器正常关闭输出:

2015/08/26 22:12:30 [release] Leaf closing down (signal: interrupt)

Hello Leaf

现在,在 LeafServer 的基础上,我们来看看游戏服务器如何接收和处理网络消息。

首先定义一个 JSON 格式的消息(protobuf 类似)。打开 LeafServer msg/msg.go 文件可以看到如下代码:

package msg
import (
    "github.com/name5566/leaf/network"
)
var Processor network.Processor
func init() {
}

Processor 为消息的处理器(可由用户自定义),这里我们使用 Leaf 默认提供的 JSON 消息处理器并尝试添加一个名字为 Hello 的消息:

package msg
import (
    "github.com/name5566/leaf/network/json"
)
// 使用默认的 JSON 消息处理器(默认还提供了 protobuf 消息处理器)
var Processor = json.NewProcessor()
func init() {
    // 这里我们注册了一个 JSON 消息 Hello
    Processor.Register(&Hello{})
}
// 一个结构体定义了一个 JSON 消息的格式
// 消息名为 Hello
type Hello struct {
    Name string
}

客户端发送到游戏服务器的消息需要通过 gate 模块路由,简而言之,gate 模块决定了某个消息具体交给内部的哪个模块来处理。这里,我们将 Hello 消息路由到 game 模块中。打开 LeafServer gate/router.go,敲入如下代码:

package gate
import (
    "server/game"
    "server/msg"
)
func init() {
    // 这里指定消息 Hello 路由到 game 模块
    // 模块间使用 ChanRPC 通讯,消息路由也不例外
    msg.Processor.SetRouter(&msg.Hello{}, game.ChanRPC)
}

一切就绪,我们现在可以在 game 模块中处理 Hello 消息了。打开 LeafServer game/internal/handler.go,敲入如下代码:

package internal
import (
    "github.com/name5566/leaf/log"
    "github.com/name5566/leaf/gate"
    "reflect"
    "server/msg"
)
func init() {
    // 向当前模块(game 模块)注册 Hello 消息的消息处理函数 handleHello
    handler(&msg.Hello{}, handleHello)
}
func handler(m interface{}, h interface{}) {
    skeleton.RegisterChanRPC(reflect.TypeOf(m), h)
}
func handleHello(args []interface{}) {
    // 收到的 Hello 消息
    m := args[0].(*msg.Hello)
    // 消息的发送者
    a := args[1].(gate.Agent)
    // 输出收到的消息的内容
    log.Debug("hello %v", m.Name)
    // 给发送者回应一个 Hello 消息
    a.WriteMsg(&msg.Hello{
        Name: "client",
    })
}

到这里,一个简单的范例就完成了。为了更加清楚的了解消息的格式,我们从 0 编写一个最简单的测试客户端。

了解更多

阅读 Wiki 获取更多的帮助:https://github.com/name5566/leaf/wiki

Overview

Name With Ownername5566/leaf
Primary LanguageGo
Program languageGo (Language Count: 1)
PlatformLinux, Mac, Windows
License:Apache License 2.0
Release Count5
Last Release Name1.1.3 (Posted on )
First Release Name1.0.0 (Posted on )
Created At2014-08-04 12:40:08
Pushed At2022-10-21 10:50:39
Last Commit At2022-10-21 18:50:39
Stargazers Count5.1k
Watchers Count321
Fork Count1.3k
Commits Count330
Has Issues Enabled
Issues Count184
Issue Open Count22
Pull Requests Count8
Pull Requests Open Count0
Pull Requests Close Count10
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private

Leaf

A pragmatic game server framework in Go (golang).

Features

  • Extremely easy to use
  • Reliable
  • Multicore support
  • Modularity

Community

  • QQ 群:376389675

Documentation

Licensing

Leaf is licensed under the Apache License, Version 2.0. See LICENSE for the full license text.

To the top