{"id":13601267,"url":"https://github.com/davyxu/cellnet","last_synced_at":"2025-05-14T07:09:57.685Z","repository":{"id":36657934,"uuid":"40964260","full_name":"davyxu/cellnet","owner":"davyxu","description":"High performance, simple, extensible golang open source network library","archived":false,"fork":false,"pushed_at":"2024-03-14T09:01:56.000Z","size":10099,"stargazers_count":4061,"open_issues_count":7,"forks_count":884,"subscribers_count":306,"default_branch":"master","last_synced_at":"2025-04-11T02:51:36.449Z","etag":null,"topics":["game","golang","networking","protobuf","rpc","server","socket","tcp-server"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/davyxu.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGES.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2015-08-18T09:54:01.000Z","updated_at":"2025-04-09T09:33:15.000Z","dependencies_parsed_at":"2024-06-18T11:27:32.412Z","dependency_job_id":null,"html_url":"https://github.com/davyxu/cellnet","commit_stats":{"total_commits":512,"total_committers":15,"mean_commits":34.13333333333333,"dds":0.083984375,"last_synced_commit":"916a7eaabb30c7f29d1f69b977b3ba143162e5c9"},"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davyxu%2Fcellnet","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davyxu%2Fcellnet/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davyxu%2Fcellnet/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davyxu%2Fcellnet/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/davyxu","download_url":"https://codeload.github.com/davyxu/cellnet/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254092789,"owners_count":22013290,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["game","golang","networking","protobuf","rpc","server","socket","tcp-server"],"created_at":"2024-08-01T18:00:59.634Z","updated_at":"2025-05-14T07:09:52.667Z","avatar_url":"https://github.com/davyxu.png","language":"Go","readme":"![cellnetlogo](doc/logo.png)\n\n [![Build Status][3]][4]  [![MIT licensed][11]][12] [![GoDoc][1]][2]\n\n[1]: https://godoc.org/github.com/davyxu/cellnet?status.svg\n[2]: https://godoc.org/github.com/davyxu/cellnet\n[3]: https://travis-ci.org/davyxu/cellnet.svg?branch=master\n[4]: https://travis-ci.org/davyxu/cellnet\n[11]: https://img.shields.io/badge/license-MIT-blue.svg\n[12]: LICENSE\n\ncellnet是一个组件化、高扩展性、高性能的开源服务器网络库\n\n# 使用领域\n\ncellnet经过多个版本的迭代，无论是作为初学者学习的范例，还是作为私用、商用项目的基础构建乃至核心技术层已经在业内广受了解及使用。\n\n主要使用领域：\n\n- 游戏服务器\n\n  方便定制私有协议，快速构建逻辑服务器、网关服务器、服务器间互联互通、对接第三方SDK、转换编码协议等\n\n- ARM设备\n\n  设备间网络通讯\n\n- 证券软件\n\n  内部RPC\n\n# 特性 (Features)\n\n## 传输协议支持\n- TCP\n\n    TCP连接器的重连，侦听器的优雅重启。\n\n- UDP\n\n    纯UDP裸包收发\n\n- HTTP(测试中)\n\n    侦听器的优雅重启, 支持json及form的收发及封装。编写游戏服务器时，不再需要使用第三方HTTP服务器对接SDK。\n\n    注: 如需要对接大规模网页, 请使用第三方专业网络库,如(https://github.com/gin-gonic/gin), cellnet的http支持主要目的在用统一的地址及peer管理\n\n- WebSocket\n\n    采用(github.com/gorilla/websocket)实现\n\n## 编码(Codec)\n\n* cellnet内建支持以下数据编码:\n    - Google Protobuf (https://github.com/google/protobuf)\n\n    - json\n        适合与第三方服务器通信\n\n    - 二进制协议(https://github.com/davyxu/goobjfmt)\n\n       内存流直接序列化, 适用于服务器内网传输\n\n    - ProtoPlus(https://github.com/davyxu/protoplus)\n\n       增加并优化过的Protobuf的编码格式\n\n    可以通过codec包自行添加新的编码格式\n\n* 支持混合编码收发\n\n    无需改动代码，只需调整消息注册方式，即可达成运行期同时收发不同编码的封包\n\n    - 与其他语言编写的服务器使用protobuf\n\n    - 与web服务器使用json通信\n    \n    - 与Unity3D(C#)使用ProtoPlus(github.com/davyxu/protoplus)协议通信\n\n    优点：\n\n    - 享受不同通信协议格式的优点，避免缺点。如Protobuf适合服务器间的传输，封包小，适应面广。\n\n    - 私有的二进制协议方便加密和客户端处理，低GC和实现复杂度。\n\n## 队列实现各种单线程/多线程，异步/同步业务处理模型\n\n  使用队列消息处理结构，方便实现以下效果：\n\n* 单线程异步逻辑，适用于MMORPG复杂交互，免加锁处理共享数据。\n\n* 多线程同步逻辑，适用于机器人逻辑，每个机器人使用独立的goroutine收发处理，机器人间互不干扰。\n\n* 多线程并发逻辑，适用于网关，消息转发，HTTP服务器，每条消息处理都会在完全并发下。\n\n## 远程过程调用（RPC）\n\n* 支持同步RPC，适用于后台服务器向其他服务器请求数据后再顺处理事务。\n\n* 支持异步RPC，适用于单线程服务器逻辑。\n\n## 消息日志\n* 可以方便的通过日志查看收发消息的每一个字段消息\n\n![cellnetlogo](doc/chatlogger.png)\n\n# 获取+编译\n\n编译和使用cellnet，请下载Go1.10以上版本\n\n```\n  go get -u -v github.com/davyxu/cellnet\n\n  go get -u -v github.com/davyxu/golog\n\n  go get -u -v github.com/davyxu/goobjfmt\n\n  go get -u -v github.com/davyxu/protoplus\n```\n\n# 第三方库\n\ncellnet 使用Protobuf时，需要使用附带的pb插件生成一个绑定代码，下面的链接可以处理这个问题\n[Google Protobuf 编码安装](https://github.com/davyxu/cellnet/blob/master/doc/pbcodec.md)\n\n[WebSocket实现](https://github.com/gorilla/websocket)\n\n# 架构\n\ncellnet架构层次由如下图所示：\n\n![architecture](doc/architecture.png)\n\ncellnet对Processor和Peer进行高度抽象，用户可以从这两个层面横向扩展以满足不同的网络封装需求\n\n红色框内的部分为cellnet实现部分\n\n# 消息处理流程\n\ncellnet的消息收发处理流程如下图所示：\n\n![procflow](doc/procflow.png)\n\n本图对应的接口为cellnet/processor.go\n\n\n# 样例\n```golang\n\nconst peerAddress = \"127.0.0.1:17701\"\n\n// 服务器逻辑\nfunc server() {\n\n    // 创建服务器的事件队列，所有的消息，事件都会被投入这个队列处理\n\tqueue := cellnet.NewEventQueue()\n\n    // 创建一个服务器的接受器(Acceptor)，接受客户端的连接\n\tpeerIns := peer.NewGenericPeer(\"tcp.Acceptor\", \"server\", peerAddress, queue)\n\n    // 将接受器Peer与tcp.ltv的处理器绑定，并设置事件处理回调\n    // tcp.ltv处理器负责处理消息收发，使用私有的封包格式以及日志，RPC等处理\n\tproc.BindProcessorHandler(peerIns, \"tcp.ltv\", func(ev cellnet.Event) {\n\n        // 处理Peer收到的各种事件\n\t\tswitch msg := ev.Message().(type) {\n\t\tcase *cellnet.SessionAccepted: // 接受一个连接\n\t\t\tfmt.Println(\"server accepted\")\n\t\tcase *TestEchoACK: // 收到连接发送的消息\n\n\t\t\tfmt.Printf(\"server recv %+v\\n\", msg)\n\n            // 发送回应消息\n\t\t\tev.Session().Send(\u0026TestEchoACK{\n\t\t\t\tMsg:   msg.Msg,\n\t\t\t\tValue: msg.Value,\n\t\t\t})\n\n\t\tcase *cellnet.SessionClosed: // 会话连接断开\n\t\t\tfmt.Println(\"session closed: \", ev.Session().ID())\n\t\t}\n\n\t})\n\n    // 启动Peer，服务器开始侦听\n\tpeerIns.Start()\n\n    // 开启事件队列，开始处理事件，此函数不阻塞\n\tqueue.StartLoop()\n}\n\n// 模拟客户端逻辑\nfunc client() {\n\n    // 例子专用的完成标记\n\tdone := make(chan struct{})\n\n    // 创建客户端的事件处理队列\n\tqueue := cellnet.NewEventQueue()\n\n    // 创建客户端的连接器\n\tpeerIns := peer.NewGenericPeer(\"tcp.Connector\", \"client\", peerAddress, queue)\n\n    // 将客户端连接器Peer与tcp.ltv处理器绑定，并设置接收事件回调\n\tproc.BindProcessorHandler(peerIns, \"tcp.ltv\", func(ev cellnet.Event) {\n\n\t\tswitch msg := ev.Message().(type) {\n\t\tcase *cellnet.SessionConnected: // 已经连接上\n\t\t\tfmt.Println(\"client connected\")\n\t\t\tev.Session().Send(\u0026TestEchoACK{\n\t\t\t\tMsg:   \"hello\",\n\t\t\t\tValue: 1234,\n\t\t\t})\n\t\tcase *TestEchoACK: //收到服务器发送的消息\n\n\t\t\tfmt.Printf(\"client recv %+v\\n\", msg)\n\n\t\t\t// 完成操作\n\t\t\tdone \u003c- struct{}{}\n\n\t\tcase *cellnet.SessionClosed:\n\t\t\tfmt.Println(\"client closed\")\n\t\t}\n\t})\n\n    // 开启客户端Peer\n\tpeerIns.Start()\n\n    // 开启客户端队列处理\n\tqueue.StartLoop()\n\n\t// 等待客户端收到消息\n\t\u003c-done\n}\n\n```\n\n# 目录功能\n\n[目录及功能一览](https://github.com/davyxu/cellnet/blob/master/doc/dirstruct.md)\n\n# 运行聊天例子\n\n## 运行 服务器\n\n```bash\ncd examples/chat/server\n\ngo run main.go\n```\n\n## 运行 客户端\n\n```bash\ncd examples/chat/client\n\ngo run main.go\n```\n\n随后, 在命令行中输入hello后打回车, 就可以看到服务器返回\n\n```\n\nsid1 say: hello\n\n```\n\n# 基本概念及使用说明\n\n理解下面链接中的概念，可以迅速使用cellnet做基本的网络通讯及消息处理\n\n* [队列](https://github.com/davyxu/cellnet/blob/master/doc/queue.md)\n\n* [端(Peer)](https://github.com/davyxu/cellnet/blob/master/doc/peer.md)\n\n* [收发处理消息](https://github.com/davyxu/cellnet/blob/master/doc/procmsg.md)\n\n\n# 扩展及定制\n\n若cellnet内建的Peer, Codec及Processor流程不能满足你的需求，可以阅读下面链接内容，添加并扩展cellnet功能\n\n* [定制封包编码(Codec)](https://github.com/davyxu/cellnet/blob/master/doc/customcodec.md)\n\n    封包编码可以是JSON，Protobuf等\n\n* [定制端(Peer)](https://github.com/davyxu/cellnet/blob/master/doc/custompeer.md)\n\n    新增Redis连接器，与cellnet自带的TCP/UDP的Peer都不同。\n\n* [定制处理器(Processor)](https://github.com/davyxu/cellnet/blob/master/doc/customproc.md)\n\n    需要在收发包时有统一的处理行为\n\n# FAQ\n\n[常见问题及回答](https://github.com/davyxu/cellnet/blob/master/doc/faq.md)\n\n这里应该有你想知道的答案\n\n\n# 贡献者\n\n按贡献时间排序，越靠前表示越新的贡献\n\nsuperikw(https://github.com/superikw), 在v3中测试出一个websocket接口并发发送问题，wss支持，修复会话管理。\n\nbruce.hu(https://github.com/hxdhero), 在v3中测试出一个竞态冲突的bug\n\nM4tou(https://github.com/mutousay), 在v3中协助解决RPC异步超时回调处理\n\nchuan.li(https://github.com/blade-226), 在v3中提供一个没有在io线程编码的bug\n\nChris Lonng(https://github.com/lonnng), 在v3中提供一个最大封包约束造成服务器间连接断开的bug\n\nIronsDu(https://github.com/IronsDu), 在v2中大幅度性能优化\n\nviwii(viwii@sina.cn), 在v2中，提供一个可能造成死锁的bug\n\n# 版本历史\n2018.5  v4版本 [详细请查看](https://github.com/davyxu/cellnet/blob/v4/CHANGES.md)\n\n2017.8  v3版本 [详细请查看](https://github.com/davyxu/cellnet/blob/v3/CHANGES.md)\n\n2017.1  v2版本 [详细请查看](https://github.com/davyxu/cellnet/blob/v2/CHANGES.md)\n\n2015.8\tv1版本\n\n\n# 备注\n\n感觉不错请star, 谢谢!\n\n知乎: http://www.zhihu.com/people/sunicdavy\n\n提交bug及特性: https://github.com/davyxu/cellnet/issues\n","funding_links":[],"categories":["Go"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdavyxu%2Fcellnet","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdavyxu%2Fcellnet","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdavyxu%2Fcellnet/lists"}