Coder Social home page Coder Social logo

sniper00 / moon Goto Github PK

View Code? Open in Web Editor NEW
693.0 37.0 160.0 7.97 MB

A lightweight game server framework implemented with Actor Model

License: MIT License

C++ 73.57% C 21.81% Lua 3.98% HTML 0.02% Batchfile 0.01% CMake 0.06% Makefile 0.55% Shell 0.01%
lua game game-server multithreading cpp17 network unity actor-model coroutine cpp

moon's Introduction

Current building status

linux-gcc

linux-clang

macos-clang

windows-vs2022

Quality Gate Status

Moon

Moon is a lightweight game server framework based on the actor model. One worker thread can have one or more actors (services), which communicate with each other through message queues. There are many features for game server development:

  • The architecture is simple, and the source code is concise and easy to understand.
  • Cross-platform (Windows, Linux, MacOS)
  • Uses ASIO for scheduling
  • Uses Lua for logic scripting
  • Optimized networking
    • Tcp
    • Udp/Kcp
    • Websockets
    • Http
  • Asynchronous based on Lua coroutines
    • Coroutine-socket
    • Timer
    • Inter-service communication
    • Inter-process communication
    • Redis/PostgreSQL/Mongodb/Mysql async client driver
  • High performance and optimized Lua Json library
  • Lua protobuf library
  • Lua filesystem
  • Recast Navigation
  • Lua zset library for ranklist

OverView

Framework

image

Community

  • Gitter

  • QQ group: 543833695

Documents

https://github.com/sniper00/moon/wiki

Quick Start

Download Pre-Built Binaries

# Run test
./moon test/main_test.lua

# Run script in the example directory, e:
./moon example/example_timer.lua

# This is a guessing game, example/GuessGame.md
./moon example/main_game.lua

Demo

Build

  1. Make sure your compiler(vs2022 17.5+, gcc 9.3+, clang 9.0+) support C++17 or Install C++17 Compiler.

  2. Clone source code

    git clone https://github.com/sniper00/moon.git
  1. Build
    • windows run build.bat
    • linux run ./build.sh
    • macOS run ./build.sh

If you want modify premake5 script, See detail build steps

Use case

若您的项目使用了moon,欢迎联系作者,作者很乐意把您的作品展示出来.

g1 g2

Age of kita

战神遗迹

Friend Open Source

moon's People

Contributors

lchannng avatar sniper00 avatar wanghenshui avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

moon's Issues

Lua table json decode/encode support integer key

Lua table经过json encode后,整型key会被编码成字符串,造成decode后只根据json数据类型无法还原整型key,现在想到一个方案: lua变量不能是 -,0-9 开头, json key decode时,先检测第一个字符是否是-,0-9 开头,如果是,就说明这是一个整型key。

这种方案有一些限制

  1. 不能用double类型做key,这里只处理integer类型。用浮点数做key本身就是错误的行为。
  2. 避免用 -,0-9 开头的非纯数字字符串做key

if (c == '-' || (c >= '0' && c <= '9'))

vs2022编译最新版本代码失败,mimalloc(看着是2017版编译的)解析失败了。

BUILDVCTOOLS: C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE
fatal: not a git repository (or any of the parent directories): .git
fatal: not a git repository (or any of the parent directories): .git
fatal: not a git repository (or any of the parent directories): .git

Microsoft Visual Studio 2022 版本 17.6.4.
版权所有(C) Microsoft Corp.。保留所有权利。
已启动重新生成...
1>------ 已启动全部重新生成: 项目: sharetable, 配置: Release x64 ------
2>------ 已启动全部重新生成: 项目: pb, 配置: Release x64 ------
3>------ 已启动全部重新生成: 项目: mongo, 配置: Release x64 ------
4>------ 已启动全部重新生成: 项目: mimalloc, 配置: Release x64 ------
5>------ 已启动全部重新生成: 项目: lualib, 配置: Release x64 ------
6>------ 已启动全部重新生成: 项目: lua, 配置: Release x64 ------
7>------ 已启动全部重新生成: 项目: crypt, 配置: Release x64 ------
1>lua-sharetable.c
2>pb.c
3>lua-bson.c
1>sharetable.vcxproj -> G:\unityProj\2D\KnightsLike\Moon\moon-master\build\bin\Release\sharetable.lib
4>static.c
4>c1 : fatal error C1083: 无法打开源文件: “......\third\mimalloc\src\static.c”: No such file or directory
4>已完成生成项目“mimalloc.vcxproj”的操作 - 失败。

moon.new_service是怎么返回服务id的?

moon.new_service这个api是怎么获得服务id并返回的?
看了好久也没有捋清楚. math.tointeger(co_yield()) 不是应该挂起当前协程么?

我写了个例子, 我理解的是moon.new_service 执行完都需要再次执行coroutine.resume才能把for循环跑完. 因为new_service最后yield的了? 但是实际是for循环是一次都执行完了,并没有中途挂起, 而且返回了服务id. 一直也没有看懂这服务id怎么来的.
求帮忙讲解一下.

moon.async(function()
    local conf = {
        base_name = "gate",
        file = "service_gate.lua",
        host = "0.0.0.0",
        base_port = 20520,
        num = 5,
        unique = true, -- 通过name做唯一区分,name不能相同
    }

    for i = 1, conf.num do
        conf.port = conf.base_port + i - 1
        conf.name = conf.base_name .. i
        local r = moon.new_service("lua", conf)
    end
end)

编译报错

gcc version 13.2.1 20230801 (GCC)

In file included from ../../../moon-src/core/worker.h:4,
                 from ../../../moon-src/core/server.h:6,
                 from ../../../lualib-src/lua_moon.cpp:8:
../../../moon-src/core/network/socket.h:95:18: error: declaration of ‘uint32_t moon::socket_server::udp(uint32_t, std::string_view, uint16_t)’ changes meaning of ‘udp’ [-Wchanges-meaning]
   95 |         uint32_t udp(uint32_t owner, std::string_view host, uint16_t port);
      |                  ^~~
../../../moon-src/core/network/socket.h:77:13: note: used here to mean ‘class asio::ip::udp’
   77 |             udp::endpoint from_ep;
      |             ^~~
In file included from ../../../third/asio.hpp:143,
                 from ../../../moon-src/core/network/socket.h:4:
../../../third/asio/ip/udp.hpp:42:7: note: declared here
   42 | class udp
      |       ^~~

lua-json添加选项: 数组是稀疏数组时, encode table为 json object

编写逻辑时很容易用到table key1起始的lua table, 如果写出下面代码

json.options('enable_sparse_array', true)
local t = {}
t[1] = "111"
t[2] = "222"
t[3] = "333"
t[4] = "444"

t[3] = nil

local str = json.encode(t)
print(str)
print_r(json.decode(str))

对于 json 中的 null 会使用 json.null lua lightuserdata 表示:

encode:   ["111","222",null,"444"] 
decode:   {
    [1] = "111",
    [2] = "222",
    [3] = userdata: 0000000000000000,
    [4] = "444",
}

这样在编写lua逻辑时判断元素是否存在时会比较麻烦,需要多一层对json.null的判断。
对于lua 大多数情况下并不关系数据是array还是object, 为了简化,现在提供了选项 json options enable_sparse_array , 这个选项是默认为false。效果如下:

encode:   {"1":"111","2":"222","4":"444"}
decode:   {
    [1] = "111",
    [2] = "222",
    [4] = "444",
}

注意,如果要对接其它语言,需要区分arrayobject时, 按object操作方式的table 不建议定义 key=1的元素

求助:查询mysql遇到问题.

我结合hello world例程和mysql例程写了一个测试使用mysql例程.
功能简单, 若接收到客户端发来的指定字符串'dbtest' 就从mydb中查询所有表名, 其结果直接发送给客户端作为回复.
代码如下
image
image
报错如下:
image

  1. 在on message里直接写mysql连接查询 就会报错, 所以只能在协程里写.
  2. 在协程中写, 本框架不能使用全局变量, 所以函数必须传参.
    这种情况下该怎么传参到匿名函数中?
    卡了很久了, 网上搜不到, lua的pdf我也下了看, 但我还是不知道这种该怎么写, 求助一下.

windows部署

作者你好,线上环境是windows server,moon也能部署在windows server吧?还是说windows上仅仅是调试开发?

VS2022 17.5+ build error about stdatomic.h

原因:

VS2022的bug, 存在多个版本VCToolsversion时, 不会默认使用最新版
具体可见这个目录存在多个版本:
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC

解决方案:

删除这个目录下 C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build 这两个文件
Microsoft.VCToolsVersion.v143.default.props
Microsoft.VCToolsVersion.v143.default.txt

参考资料:
actions/runner-images#9670
https://developercommunity.visualstudio.com/t/Latest-Visual-Studio-version-1791-ca/10598722#T-N10605771

如何调试这个工程的Lua部分?

作者您好!
非常钦佩您的这个工程,我在学习中。
有个问题就是:我看到其中不少lua代码,我用visual studio 2019 无法进入lua中调试,不知道您有什么好的建议?
我是个C++程序员。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.