MCPcopy
hub / github.com/aceld/zinx

github.com/aceld/zinx @v1.2.8 sqlite

repository ↗ · DeepWiki ↗ · release v1.2.8 ↗
1,329 symbols 4,039 edges 182 files 559 documented · 42%
README

English | 简体中文

License Discord Gitter zinx tutorial Original Book of Zinx

Zinx 是一个基于Golang的轻量级并发服务器框架

开发者文档

< Zinx Wiki : English >

< Zinx 文档 : 简体中文>

说明:目前zinx已经在很多企业进行开发使用,具体使用领域包括:后端模块的消息中转、长连接游戏服务器、Web框架中的消息处理插件等。zinx的定位是代码简洁,让更多的开发者迅速的了解框架的内脏细节并且可以快速基于zinx DIY(二次开发)一款适合自己企业场景的模块。


zinx源码地址

platform Entry
Github https://github.com/aceld/zinx
Gitcode https://gitcode.com/aceld/zinx
Gitee https://gitee.com/Aceld/zinx

官网

http://zinx.me


在线开发教程

文字教程

platform Entry
Zinx Framework tutorial-Lightweight server based on Golang
《Golang轻量级并发服务器框架zinx》

视频教程

platform online video
zinx-BiliBili
zinx-BiliBili
zinx-youtube

一、写在前面

我们为什么要做Zinx,Golang目前在服务器的应用框架很多,但是应用在游戏领域或者其他长连接的领域的轻量级企业框架甚少。

设计Zinx的目的是我们可以通过Zinx框架来了解基于Golang编写一个TCP服务器的整体轮廓,让更多的Golang爱好者能深入浅出的去学习和认识这个领域。

Zinx框架的项目制作采用编码和学习教程同步进行,将开发的全部递进和迭代思维带入教程中,而不是一下子给大家一个非常完整的框架去学习,让很多人一头雾水,不知道该如何学起。

教程会一个版本一个版本迭代,每个版本的添加功能都是微小的,让一个服务框架小白,循序渐进的曲线方式了解服务器框架的领域。

当然,最后希望Zinx会有更多的人加入,给我们提出宝贵的意见,让Zinx成为真正的解决企业的服务器框架!在此感谢您的关注!

二、初探Zinx架构

Zinx框架

流程图 zinx-start

三、Zinx开发接口文档

(1)快速开始

版本 Golang 1.17+

DownLoad zinx Source

$go get github.com/aceld/zinx

note: Golang Version 1.17+

Zinx-Server

package main

import (
    "fmt"
    "github.com/aceld/zinx/ziface"
    "github.com/aceld/zinx/znet"
)

// PingRouter MsgId=1的路由
type PingRouter struct {
    znet.BaseRouter
}

//Ping Handle MsgId=1的路由处理方法
func (r *PingRouter) Handle(request ziface.IRequest) {
    //读取客户端的数据
    fmt.Println("recv from client : msgId=", request.GetMsgID(), ", data=", string(request.GetData()))
}

func main() {
    //1 创建一个server服务
    s := znet.NewServer()

    //2 配置路由
    s.AddRouter(1, &PingRouter{})

    //3 启动服务
    s.Serve()
}

Run Server

$ go run server.go

              ██                        
              ▀▀                        
 ████████   ████     ██▄████▄  ▀██  ██▀ 
     ▄█▀      ██     ██▀   ██    ████   
   ▄█▀        ██     ██    ██    ▄██▄   
 ▄██▄▄▄▄▄  ▄▄▄██▄▄▄  ██    ██   ▄█▀▀█▄  
 ▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀  ▀▀    ▀▀  ▀▀▀  ▀▀▀ 

┌──────────────────────────────────────────────────────┐
│ [Github] https://github.com/aceld                    │
│ [tutorial] https://www.yuque.com/aceld/npyr8s/bgftov │
└──────────────────────────────────────────────────────┘
[Zinx] Version: V1.0, MaxConn: 12000, MaxPacketSize: 4096
===== Zinx Global Config =====
TCPServer: <nil>
Host: 0.0.0.0
TCPPort: 8999
Name: ZinxServerApp
Version: V1.0
MaxPacketSize: 4096
MaxConn: 12000
WorkerPoolSize: 10
MaxWorkerTaskLen: 1024
MaxMsgChanLen: 1024
ConfFilePath: /Users/Aceld/go/src/zinx-usage/quick_start/conf/zinx.json
LogDir: /Users/Aceld/go/src/zinx-usage/quick_start/log
LogFile: 
LogIsolationLevel: 0
HeartbeatMax: 10
==============================
2023/03/09 18:39:49 [INFO]msghandler.go:61: Add api msgID = 1
2023/03/09 18:39:49 [INFO]server.go:112: [START] Server name: ZinxServerApp,listenner at IP: 0.0.0.0, Port 8999 is starting
2023/03/09 18:39:49 [INFO]msghandler.go:66: Worker ID = 0 is started.
2023/03/09 18:39:49 [INFO]msghandler.go:66: Worker ID = 1 is started.
2023/03/09 18:39:49 [INFO]msghandler.go:66: Worker ID = 3 is started.
2023/03/09 18:39:49 [INFO]msghandler.go:66: Worker ID = 2 is started.
2023/03/09 18:39:49 [INFO]msghandler.go:66: Worker ID = 4 is started.
2023/03/09 18:39:49 [INFO]msghandler.go:66: Worker ID = 6 is started.
2023/03/09 18:39:49 [INFO]msghandler.go:66: Worker ID = 7 is started.
2023/03/09 18:39:49 [INFO]msghandler.go:66: Worker ID = 8 is started.
2023/03/09 18:39:49 [INFO]msghandler.go:66: Worker ID = 9 is started.
2023/03/09 18:39:49 [INFO]msghandler.go:66: Worker ID = 5 is started.
2023/03/09 18:39:49 [INFO]server.go:134: [START] start Zinx server  ZinxServerApp succ, now listenning...

Zinx-Client

package main

import (
    "fmt"
    "github.com/aceld/zinx/ziface"
    "github.com/aceld/zinx/znet"
    "time"
)

//客户端自定义业务
func pingLoop(conn ziface.IConnection) {
    for {
        err := conn.SendMsg(1, []byte("Ping...Ping...Ping...[FromClient]"))
        if err != nil {
            fmt.Println(err)
            break
        }

        time.Sleep(1 * time.Second)
    }
}

//创建连接的时候执行
func onClientStart(conn ziface.IConnection) {
    fmt.Println("onClientStart is Called ... ")
    go pingLoop(conn)
}

func main() {
    //创建Client客户端
    client := znet.NewClient("127.0.0.1", 8999)

    //设置连接建立成功后的钩子函数
    client.SetOnConnStart(onClientStart)

    //启动客户端
    client.Start()

    //防止进程退出,等待中断信号
    select {}
}

Run Client

$ go run client.go 
2023/03/09 19:04:54 [INFO]client.go:73: [START] Zinx Client LocalAddr: 127.0.0.1:55294, RemoteAddr: 127.0.0.1:8999
2023/03/09 19:04:54 [INFO]connection.go:354: ZINX CallOnConnStart....

Terminal of Zinx Print:

recv from client : msgId= 1 , data= Ping...Ping...Ping...[FromClient]
recv from client : msgId= 1 , data= Ping...Ping...Ping...[FromClient]
recv from client : msgId= 1 , data= Ping...Ping...Ping...[FromClient]
recv from client : msgId= 1 , data= Ping...Ping...Ping...[FromClient]
recv from client : msgId= 1 , data= Ping...Ping...Ping...[FromClient]
recv from client : msgId= 1 , data= Ping...Ping...Ping...[FromClient]
...

(2)Zinx配置文件

{
  "Name":"zinx v-0.10 demoApp",
  "Host":"0.0.0.0",
  "TCPPort":9090,
  "MaxConn":3,
  "WorkerPoolSize":10,
  "LogDir": "./mylog",
  "LogFile":"app.log",
  "LogSaveDays":15,
  "LogCons": true,
  "LogIsolationLevel":0
}

Name:服务器应用名称

Host:服务器IP

TcpPort:服务器监听端口

MaxConn:允许的客户端连接最大数量

WorkerPoolSize:工作任务池最大工作Goroutine数量

LogDir: 日志文件夹

LogFile: 日志文件名称(如果不提供,则日志信息打印到Stderr)

LogIsolationLevel: 日志隔离级别 0:全开, 1:关debug, 2:关debug/info, 3:关debug/info/warn


开发者

Zinx 开发者
zinx 刘丹冰(@aceld) 张超(@zhngcho) 高智辉Roger(@adsian) 胡贵建(@huguijian) 张继瑀(@kstwoak) 夏小力(@xxl6097) 李志成(@clukboy)姚承政(@hcraM41)李国杰(@LI-GUOJIE)余喆宁(@YanHeDoki
moke-kit(微服务框架) GStones(@GStones)
zinx(C++) 刘洋(@marklion)
zinx(Lua) 胡琪(@huqitt)
ginx(Java) ModuleCode(@ModuleCode)

感谢所有为zinx贡献的开发者


关于作者:

作者:Aceld(刘丹冰)

mail: danbing.at@gmail.com

github: https://github.com/aceld

原创书籍: https://www.yuque.com/aceld

加入Zinx技术社区

platform Entry
https://discord.gg/xQ8Xxfyfcz
加微信: ace_ld 或扫二维码,备注zinx即可。

| || WeChat Public Account | || QQ Group |

Extension points exported contracts — how you extend this code

IRouter (Interface)
IRouter is the interface for message routing. The route is the processing business method set by the framework user for [9 …
ziface/irouter.go
IInterceptor (Interface)
Interceptor (拦截器) [5 implementers]
ziface/iinterceptor.go
IDecoder (Interface)
(no doc) [5 implementers]
ziface/idecoder.go
IConnection (Interface)
IConnection Define connection interface [3 implementers]
ziface/iconnection.go
IRequest (Interface)
IRequest interface: It actually packages the connection information and request data of the client request into Request [2 …
ziface/irequest.go
IDataPack (Interface)
IDataPack Package and unpack data. Operating on the data stream of TCP connections, add header information to transfer d [2 …
ziface/idatapack.go
IServer (Interface)
Defines the server interface [1 implementers]
ziface/iserver.go
IConnManager (Interface)
IConnManager Connection Management Abstract Layer [1 implementers]
ziface/iconnmanager.go

Core symbols most depended-on inside this repo

Ins
called by 146
zlog/default.go
Write
called by 69
zutils/witer.go
Debug
called by 66
zlog/stdzlog.go
Error
called by 66
zlog/logger_core.go
GetConnection
called by 66
ziface/irequest.go
ErrorF
called by 63
ziface/ilogger.go
SendMsg
called by 57
ziface/iconnection.go
InfoF
called by 52
ziface/ilogger.go

Shape

Method 776
Function 397
Struct 121
Interface 23
FuncType 10
TypeAlias 2

Languages

Go100%
TypeScript1%

Modules by API surface

zinx_app_demo/mmo_game/pb/msg.pb.go85 symbols
zutils/shard_lock_map.go42 symbols
znet/connection.go41 symbols
znet/kcp_connection.go38 symbols
znet/ws_connection.go37 symbols
zutils/shard_lock_map_bench_test.go35 symbols
znet/server.go35 symbols
ziface/irequest.go35 symbols
zlog/logger_core.go31 symbols
zutils/shard_lock_map_test.go30 symbols
znet/client.go30 symbols
examples/zinx_version_ex/protoDemo/pb/Person.pb.go30 symbols

Dependencies from manifests, versioned

github.com/davecgh/go-spewv1.1.1 · 1×
github.com/klauspost/cpuid/v2v2.1.1 · 1×
github.com/klauspost/reedsolomonv1.11.8 · 1×
github.com/pmezard/go-difflibv1.0.0 · 1×
github.com/templexxx/cpufeatv0.0.0-2018072401212 · 1×
github.com/templexxx/xorv0.0.0-2019121715381 · 1×
github.com/xtaci/kcp-gov5.4.20+incompatible · 1×

For agents

$ claude mcp add zinx \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact