游戏服务器框架 Leaf/go

码农软件 · 软件分类 · 游戏开发包 · 2019-10-05 07:58:30

软件介绍

Leaf 是一个使用 Go 语言开发的开源游戏服务器框架,注重运行效率并追求极致的开发效率。Leaf 适用于几乎所有的游戏类型。其主要的特性:

  • 良好的使用体验。Leaf 总是尽可能的提供简洁和易用的接口,尽可能的提升开发的效率

  • 稳定性。Leaf 总是尽可能的恢复运行过程中的错误,避免崩溃

  • 多核支持。Leaf 通过模块机制和 leaf/go 尽可能的利用多核资源,同时又尽量避免各种副作用

  • 良好的模块支持。

一个 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 协议,可自定义消息格式,目前 Leaf 提供了基于 protobuf 和 JSON 的消息格式

  • leaf/recordfile 用于管理游戏数据

  • leaf/timer 定时器相关

  • leaf/util 辅助库

使用 Leaf 开发游戏服务器

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

获取 LeafServer:

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

设置 leafserver 目录到 GOPATH 后获取相关依赖:

go get github.com/name5566/leaf
go get github.com/golang/protobuf/proto
go get gopkg.in/mgo.v2

编译 LeafServer:

go install server

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

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

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

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

本文地址:https://codercto.com/soft/d/16068.html

Boolean Reasoning

Boolean Reasoning

Brown, Frank Markham / 2003-4 / $ 19.15

A systematic treatment of Boolean reasoning, this concise, newly revised edition combines the works of early logicians with recent investigations, including previously unpublished research results. Th......一起来看看 《Boolean Reasoning》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具