A game server framework in Go (golang)

Overview

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.

Issues
  • 服务端崩溃

    服务端崩溃

    同时开几百个客户端出现奔溃,大神帮忙看看。

    goroutine 935 [IO wait]: internal/poll.runtime_pollWait(0x7fa51f17b400, 0x72, 0x0) C:/Go/src/runtime/netpoll.go:173 +0x57 internal/poll.(*pollDesc).wait(0xc420432a98, 0x72, 0xffffffffffffff00, 0xf42ba0, 0xf3c6b8) C:/Go/src/internal/poll/fd_poll_runtime.go:85 +0xae internal/poll.(*pollDesc).waitRead(0xc420432a98, 0xc42052a700, 0x2, 0x4) C:/Go/src/internal/poll/fd_poll_runtime.go:90 +0x3d internal/poll.(*FD).Read(0xc420432a80, 0xc42052a700, 0x2, 0x4, 0x0, 0x0, 0x0) C:/Go/src/internal/poll/fd_unix.go:126 +0x18a net.(*netFD).Read(0xc420432a80, 0xc42052a700, 0x2, 0x4, 0xc42145bdd8, 0xc42145bd50, 0x3) C:/Go/src/net/fd_unix.go:202 +0x52 net.(*conn).Read(0xc420532560, 0xc42052a700, 0x2, 0x4, 0x0, 0x0, 0x0) C:/Go/src/net/net.go:176 +0x6d github.com/name5566/leaf/network.(*TCPConn).Read(0xc420fb7d40, 0xc42052a700, 0x2, 0x4, 0xc42052a700, 0xc42145bdf8, 0x0) D:/goprojects/src/github.com/name5566/leaf/network/tcp_conn.go:96 +0x52 io.ReadAtLeast(0xf40720, 0xc420fb7d40, 0xc42052a700, 0x2, 0x4, 0x2, 0xa966c0, 0xc4201b4201, 0xc42052a700) C:/Go/src/io/io.go:309 +0x86 io.ReadFull(0xf40720, 0xc420fb7d40, 0xc42052a700, 0x2, 0x4, 0xc420172f60, 0xc421636fc2, 0x2b) C:/Go/src/io/io.go:327 +0x58 github.com/name5566/leaf/network.(*MsgParser).Read(0xc42040fec0, 0xc420fb7d40, 0xc421636fc0, 0x0, 0x0, 0x0, 0x0) D:/goprojects/src/github.com/name5566/leaf/network/tcp_msg.go:70 +0x85 github.com/name5566/leaf/network.(*TCPConn).ReadMsg(0xc420fb7d40, 0xb38da0, 0xc42163c510, 0xb386a0, 0xc420fb7d70, 0x0) D:/goprojects/src/github.com/name5566/leaf/network/tcp_conn.go:108 +0x34 github.com/name5566/leaf/gate.(*agent).Run(0xc420fb7d70) D:/goprojects/src/github.com/name5566/leaf/gate/gate.go:94 +0x3a github.com/name5566/leaf/network.(*TCPServer).run.func1(0xf44160, 0xc420fb7d70, 0xc420fb7d40, 0xc42051a000, 0xf4b7a0, 0xc420532560) D:/goprojects/src/github.com/name5566/leaf/network/tcp_server.go:102 +0x35 created by github.com/name5566/leaf/network.(*TCPServer).run D:/goprojects/src/github.com/name5566/leaf/network/tcp_server.go:101 +0x40a

    opened by exfun 55
  • 关于消息头len的问题

    关于消息头len的问题

    看了下源码,发现使用TCP协议时,先读取len,再读取data,而websocket协议本身做了分包,因此不需要先读len。 那么在定义C/S消息格式时,需要区分使用tcp就发len,而使用websocket就不发len吗?既然processor已经屏蔽了分包的细节,却又要使用者区分tcp/websocket协议去发不同格式的消息,这样设计是否会有一种逻辑上不连贯的感觉?

    opened by trist725 11
  • 如何解决Module间的循环import

    如何解决Module间的循环import

    比如有game和login两个Module,两个Module间会相互调用,比如login中调用game的rpc接口:game.ChanRPC.Go("xxx", agent,)。在game中也可能调用login的rpc接口,这样在import会出现循环依赖。不知道这种情况怎么解决?

    opened by xuyang3232 9
  • 定时器的功能太少,能否适当跟进增加功能?

    定时器的功能太少,能否适当跟进增加功能?

    首先感谢作者的开源精神!个人在使用leaf过程中提交点小建议。 感觉目前的定时器功能还不够,有个常见的需求不具备,说明如下: 根据指定定时器名称,可设置时间间隔,定时轮训执行某个func(),以此创建定时器触发器。 并提供相关的查询、修改时间间隔、停止、清理等相关接口。 需要注意的是,需要从整体去规划定时器的触发,而不是任意创建一个定时器就等待它自己内部按自己内部的时间触发。例如,创建一个房间,房间会按每秒50次触发房间广播的定时轮询触发器A,如果有100个房间总不能100个定时器吧?应该是100个房间都在定时轮询触发器A内触发吧。 个人拙见,见效勿怪。

    opened by Think-Jumper 9
  • 在 Leaf 中使用 Protobuf,客户端如何对接服务器

    在 Leaf 中使用 Protobuf,客户端如何对接服务器

    Wiki文档中并没有提供相关代码,只好硬着头皮自己写

    conn, err := net.Dial("tcp", "127.0.0.1:3563")
    if err != nil {
    	panic(err)
    }
    
    hello := &msg.Hello{
    	Name: proto.String("leaf"),
    }
    
    data, err := proto.Marshal(hello)
    if err != nil {
    	log.Fatal("marshaling error: ", err)
    }
    
    // len + data
    m := make([]byte, 2+len(data))
    
    // 默认使用大端序
    binary.BigEndian.PutUint16(m, uint16(len(data)))
    
    copy(m[2:], data)
    
    // 发送消息
    conn.Write(m)
    

    服务端打印: 2016/12/16 21:04:35 [debug ] unmarshal message error: message id 2564 not registered

    不知道该怎么办了

    opened by heyuanchao 9
  • 请问一下 ws_client是怎么用的,start后没有返回链接,该怎么和服务器通讯呢?是和服务器同一个进程还是另生成一个exe?

    请问一下 ws_client是怎么用的,start后没有返回链接,该怎么和服务器通讯呢?是和服务器同一个进程还是另生成一个exe?

    type WSClient struct { sync.Mutex Addr string ConnNum int ConnectInterval time.Duration PendingWriteNum int MaxMsgLen uint32 HandshakeTimeout time.Duration AutoReconnect bool NewAgent func(*WSConn) Agent dialer websocket.Dialer conns WebsocketConnSet wg sync.WaitGroup closeFlag bool }

    func (client *WSClient) Start() { client.init()

    for i := 0; i < client.ConnNum; i++ {
    	client.wg.Add(1)
    	go client.connect()
    }
    

    }

    opened by jjjjilyf 6
  • protobuf自动生成的id为什么不使用名称而用注册顺序

    protobuf自动生成的id为什么不使用名称而用注册顺序

    感觉自动生成的id用注册顺序跟前端同步很麻烦啊 用 len | id | message 这样也不用管注册的顺序了 Processor改成 msgInfo map[string]*MsgInfo msgID map[reflect.Type]string 不是会方便很多,希望大佬能给我指点一下

    opened by ldwangaqr 5
  • 请问这里的同步机制的使用是什么意思?和skeleton的使用有什么区别吗?使用标准库的timer要加同步处理吗?谢谢回答。

    请问这里的同步机制的使用是什么意思?和skeleton的使用有什么区别吗?使用标准库的timer要加同步处理吗?谢谢回答。

    Leaf timer Go 语言标准库提供了定时器的支持:

    func AfterFunc(d Duration, f func()) *Timer AfterFunc 会等待 d 时长后调用 f 函数,这里的 f 函数将在另外一个 goroutine 中执行。Leaf 提供了一个相同的 AfterFunc 函数,相比之下,f 函数在 AfterFunc 的调用 goroutine 中执行,这样就### 避免了同步机制的使用:

    skeleton.AfterFunc(5 * time.Second, func() { // ... }) 另外,Leaf timer 还支持 cron 表达式,用于实现诸如“每天 9 点执行”、“每周末 6 点执行”的逻辑。

    更加详细的用法可以参考 leaf/timer。

    opened by jjjjilyf 5
  • 请问我服务器要在Login时做验证,在Game里保存数据,跟数据库的处理是怎样的?

    请问我服务器要在Login时做验证,在Game里保存数据,跟数据库的处理是怎样的?

    1.是Login和Game都保持一个MongoDB的连接吗? Login就是登录验证和创建角色。 Game保存玩家获得的道具,分数等。 这样Login和Game两个MongoDB的连接连接一个MongoDB会不会有冲突? 还是把Login的创建角色处理放在Game里Login只做查询,不操作DB的数据?

    2.还有就是我在Login里验证过后,就通过chanrpc通知Game创建玩家实体,如果玩家实体不存在,不管收到了Gate转发到Game的任何消息,如果实体不存在就说明玩家没有验证过,就不处理该消息,用这种方式来保证消息安全,目前想到的只有这一种方法,可行吗?有没有更好的方法在Gate转发的时候就能确保消息的安全性呢?

    3.DBServer单独独立出来做一个服务器呢?还是和Game放在一起?我看大神你在Leaf的中文文档里建议,不要划分太多模块,不要太频繁使用rpc通讯。要多在模块内部调用goroutine。虽然有这个建议,还是不知道应该怎么用起来比较好。

    按照目前的设想

                 Login     (也要连接Redis)                   
           /                \                                 
    

    Gate------------------Game-----------------DBServer------------Redis

    opened by jjjjilyf 5
  • 请问mongodb的连接例子里db.Ensure.Counter(

    请问mongodb的连接例子里db.Ensure.Counter("game", "counters", "users")起的什么作用

    db.EnsureCounter("game", "counters", "users") 是创建了counters表,然后把users表名放到counter表里吗?这里的users是不是指的具体玩家名字还是users表名呢?如果是放玩家的数据,users表里不是已经存了吗?如果就放users这个表名,有什么意义吗?谢谢解答。

    // users err = db.EnsureUniqueIndex("game", "users", []string{"accid"}) if err != nil { log.Fatal("ensure index error: %v", err) } err = db.EnsureCounter("game", "counters", "users") if err != nil { log.Fatal("ensure counter error: %v", err) }

    opened by jjjjilyf 5
  • 提两个小建议,个人在项目中使用中有些的不方便的地方

    提两个小建议,个人在项目中使用中有些的不方便的地方

    1.protobuf 消息号不能自定义。一般来说我们开发会定义一个消息段,比如1000-2000是用户模块。leaf 中要强行定义得自己修改源码。 2.可以把websocket的Header 可以提到 gate.Agent下面方便获取。 不知道这两问题是我操作没对还是框架本身是没有的,我都是通过修改了下源码实现的,就比较蛋疼。

    opened by weichangyu 0
  • module 可否增加一个“启动后” 的事件通知?

    module 可否增加一个“启动后” 的事件通知?

    我有个游戏项目用了 leaf ,另加了分布式通信层和服务发现机制,需要在进程里的服务(module)全部就绪后注册到外部服务。但因为在 OnInit 阶段服务还没有全部启动完毕,所以目前我是这样做的:

    go func() {
    	leaf.Run(
    		xxx.Module,
    		yyy.Module,
    		xxx.Module,
    	)
    }()
    // 等待就绪.... 
    time.Sleep(100*time.Milisecond)
    // register xxx.Module into blahblah...  
    
    

    这样可行,但比较看脸(实际启动时间会变的,比如又加了新功能),不太好用。 故而有点新想法,比如 module 可否有个启动后的 OnXXX 调用? 延伸一下,甚至可以细分为几个阶段:启动前、启动后、停止前、停止后。

    以上个人愚见,欢迎讨论。

    opened by cupen 2
Releases(1.1.3)
Owner
Name5566
Name5566
Lightweight, facility, high performance golang based game server framework

Nano Nano is an easy to use, fast, lightweight game server networking library for Go. It provides a core network architecture and a series of tools an

Lonng 2k Jul 3, 2022
Scalable game server framework with clustering support and client libraries for iOS, Android, Unity and others through the C SDK.

pitaya Pitaya is an simple, fast and lightweight game server framework with clustering support and client libraries for iOS, Android, Unity and others

TFG Co 1.5k Jun 28, 2022
A game server side framework with both web API and realtime communication.

HAYABUSA Framework Hayabusa is a server side framework for Japan-like social games. Easy to understand and use for beginners Powerful controller, flex

はやぶさクラウド 17 May 21, 2022
Arkanoid game in Go using Ebiten game engine with ECS.

Arkanoid-go Arkanoid game in Go using Ebiten game engine with ECS. You must have Git LFS installed when cloning the repository to download assets. See

null 56 Apr 2, 2022
AircraftWar - a game powered by Go+ spx game engine

AircraftWar - a game powered by Go+ spx game engine How to run Download Go+ and build it. See https://github.com/goplus/gop#how-to-build. Download thi

GoPlus 1 Jan 5, 2022
FlappyCalf - a game powered by Go+ spx game engine

FlappyCalf - a game powered by Go+ spx game engine How to run Download Go+ and build it. See https://github.com/goplus/gop#how-to-build. Download this

GoPlus 2 Jan 10, 2022
FlappyCalf - a game powered by Go+ spx game engine

FlappyCalf - a game powered by Go+ spx game engine How to run Download Go+ and build it. See https://github.com/goplus/gop#how-to-build. Download this

GoPlus 2 Jan 10, 2022
MazePlay - a game powered by Go+ spx game engine

MazePlay - a game powered by Go+ spx game engine How to run Download Go+ and build it. See https://github.com/goplus/gop#how-to-build. Download this g

GoPlus 1 Dec 16, 2021
A simple game that I created with Ebiten game library as a way to teach myself Go. Enjoy!

galactic-asteroid-belt Overview A simple game that I created with Ebiten game library as a way to teach myself Go. Enjoy! Run To run, you will need Go

null 0 Dec 2, 2021
RundQuiz-Game - This is a Go exercise that implements and builds a quiz game from a list of math questions in a CSV file.

Go RundQuiz Game Exercise details This exercise is broken into two parts to help simplify the process of explaining it as well as to make it easier to

IkehAkinyemi 0 Jan 5, 2022
Simple 2D game to teach myself various things about game development and ECS, etc

2d-grass-game I really don't know what to name this game. Its a big grass field, and its in 2d so....2D Grass game This is a simple 2D game to teach m

James Wynne III 1 Jan 17, 2022
Fab.io is a lightweight game backend framework written in Go (Golang).

Fab.io is a lightweight real-time game backend framework written in Go (Golang).

null 20 Jun 20, 2022
A Game Server Skeleton in golang.

A game server skeleton implemented with golang. 注意(NOTICE) 欢迎加入QQ群: 459420581 (Gopher成都,讨论一切与go有关的话题) gonet/2 gonet1已停止维护(I no longer maintain this, p

xtaci 1.2k Jun 26, 2022
Scalable Distributed Game Server Engine with Hot Swapping in Golang

GoWorld Scalable Distributed Game Server Engine with Hot Reload in Golang Features Architecture Introduction Get GoWorld Manage GoWorld Servers Demos

Nan Lin 2.1k Jun 28, 2022
game server by golang

使用golang开发的框架 How to use ? 参考 main.go Features kernel使用channel 和 goroutine 模拟的Actor模式 使用channel 模拟的消息队列 kernel.Context内部实现了一个链表,用于发起call的时候,由于自身channe

liangmanlin 15 Apr 20, 2022
Simple 2D game prototyping framework.

prototype Simply prototype 2D games using an easy, minimal interface that lets you draw simple primitives and images on the screen, easily handle mous

null 71 May 25, 2022
Dedicated Game Server Hosting and Scaling for Multiplayer Games on Kubernetes

Agones is a library for hosting, running and scaling dedicated game servers on Kubernetes. Agones, is derived from the Greek word agōn which roughly t

GoogleForGames 4.6k Jun 30, 2022
This is a "simple" game server. Main functionalities are matching and establishing a connection between players

Game Server This is a "simple" game server. Main functionalities are matching and establishing a connection between players How to Run? run the server

eco 7 Feb 6, 2022
Backend server for a tic-tac-toe game. frontend is a simple .md file.

Backend server for a tic-tac-toe game. frontend is a simple .md file.

Sridhar 3 Apr 13, 2022