6 Star 39 Fork 12

xiaoyi445 / NFShmServer

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

简体中文

当前版本: v1.0.0

nfshmserver

NFShmServer 是一个使用C++开发的轻量级,敏捷型,弹性的,分布式的插件开发框架, 共有三种主要使用模式:1.传统使用STL的纯C++开发;2.使用共享内存作为内存池进行热更的纯C++开发(腾讯系公司大量使用这种模式开发);3.使用lua,python脚本进行游戏逻辑开发。已经用共享内存模式和lua脚本模式各自实现一款unity3d捕鱼游戏,有兴趣的可以加群762414765学习 ---

License GitHub stars GitHub issues GitHub code size in bytes
SonarCloud codecov QQ Group Discord 996.icu

Copyright © [NFShmServer]
GitHub: https://github.com/yigao/NFShmServer

码云: https://gitee.com/xiaoyi445_admin/NFShmServer

QQ 群: 点击加群762414765

CI master分支 develop分支
Github Actions Github Action Github Action

SAST Tools

PVS-Studio - static analyzer for C, C++, C#, and Java code.

Use case

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

  • 中青龙图(网友联系的,貌似项目后来被砍了)

为什么要写NFShmFrame开源服务器

  • 最近参加了一下面试,面试官问为什么要写开源服务器,你写的和别人的开源有什么区别。我说因为兴趣所以写,要写世界最屌的服务器.
  • 这个服务器和别人的服务器有什么区别呢!如果写的东西,别人也可以立马写出来,或者和别人的差不多,那就只是练练手脚,价值不大。

NFShmFrame开源服务器的与众不同之处

  • 由游戏服务器开发过程中遇到的问题来展开NFShmFrame不同于别的服务器之处,在近10年的游戏开发中,常常遇到一些开发中的问题,为了解决遇到的这些服务器难题,我往往会在NFShmFrame上先实现解决方案,久而久之,6年的积累,慢慢就形成了NFShmFrame这个开源服务器
  1. 问题一:C++服务器开发谁都避不过去的问题,C++服务器崩溃。不管是指针导致的,还是memcpy,memset等函数导致的,还是数组越界,栈空间越界导致的,最终C++服务器崩溃,内存数据全部丢失。
  2. 问题二:C++难以实现像LUA那样的热更功能
  3. 问题三:一般的分布式游戏服务器,基本都是由很多不同功能的进程组成,这些进程使用Tcp或共享内存相互链接,难以配置。
  4. 问题四:多进程的分布式服务器,需要同时开启多个进程。即使在windows系统上使用vs调试,也需要开启很多窗口,太多的话,关闭或启动服务器麻烦。在linux,如果使用gdb调试多个服务器的话,那简直就是人道灾难
  5. 问题五:在实现游戏服务器系统的DB存储过程中,大量手写sql语句。写的蛋痛,看的蛋痛,维护蛋痛,特别容易写错,还难写。玩转mysql服务器,storeserver利用protobuf反射,自动创建数据库,创建表,新加列, 通过加载pb, 动态更新storeserver服务,无需你手写任何mysql语句,统一实现游戏服务器的数据拉去,存取,更新
  6. 问题六:C++多线程,不管是要实现tcp网络的多线程,还是db系统的多线程,并不好写。
  7. 问题七:有些游戏服务器,比如从腾讯等公司拿出来的,同时使用共享内存和TCP来实现服务器与服务器之间的通讯以及服务器和客户端的通讯。TCP和共享内存之间接口难以统一,相互替换麻烦。
  8. 问题八:大部分游戏公司的服务器代码各个模块耦合度太高,难以分开,难以替换升级。
  9. 问题九:部分公司,主要是腾讯出来创业搞出来的公司,使用共享内存来解决C++服务器崩溃后数据丢失和C++热更问题。但是使用共享内存创建类,缺乏stl之类的容器支持。导致使用共享内存成本太高。

特性

  • 可以做到分布式架构不需要修改任何源码的情况下,做到单进程运行整套分布式系统(对于滚服服务器,可同时运行多个服务器,便于调试跨服),加快平时的服务器开发。 同时还能做到在开发运行的时候最大可能的节约内存, 方便调试开发。分布式运行只需要程序启动时参数修改即可

  • C++热更,服务器实现了共享内存C++热更(对于纯粹C++来说,也是唯一热更的有效可用的方案,当然热更的前提是没有修改共享内存类的结构大小)。

  • 服务器崩溃不丢失数据,对于热门的赚钱的游戏这点还是很重要的。 服务器崩溃后重启,不丢任何物理数据,甚至连内网协议数据都不会有丢失。

  • 游戏数据均放在共享内存里,共享内存架构经过很多MMO大项目验证无问题,内网通讯采用共享内存通讯的话,服务器异常时,玩家协议数据不会丢失。

  • (2022.9-2023.9已完成)实现一套可以放心用在共享内存里的仿sgi-stl数据结构, 极大的解决了使用共享内存写代码难度搞得问题(见过好几套从腾讯流出来的游戏的共享内存架构,大部分仅仅是实现了一个简单的哈希表,其余的业务就需要使用C语言的原始数组啥的了)。除了事先需要定义好需要的内存大小外,其余使用方法都与stl一样. NFShmVector -- std::vecotr 使用:NFShmVector<int, 5> -- std::vector 除了要定义5这个最大内存容量外,其余使用方法NFShmVector和std::vector完全一样,包过使用他们的迭代器以及stl算法

  • (2022.9-2023.9已完成)服务器游戏配置数据,从excel到共享内存代码,到sql语句,只需定义一个protobuf结构,就可以生成大量有用代码,你可以用这个protobuf结构去读取excel数据,生成对应的共享内存结构类,来存放数据,也可以生成sql数据,把excel表导入到数据库,不需要修改源码,可以从本地文件读取excel数据,可以直接从数据库读取配置。

  • (2022.9-2023.9已完成)玩家个人数据,只需定义一个protobuf结构,自动生成共享内存数据,sql数据,通过protobuf发射来完成mysql数据的存取,无需你自己写sql语句。

  • 类似腾讯内部服务器,每一个服务器进程都有一个独立的类似网络IP的ID,来代表这个服务器,你不需要知道任何一个服务器进程部署在哪里,只要知道这个服务器的独立ID,就可以相互通讯

  • (2022.9-2023.9已完成)多进程单线程lua热更,实现了lua插件。可以用lua写业务代码,热更服务器。

  • (未完成)多进程actor多线程lua热更, 目前还是构思中。有点类似skynet, 但是C++底层还是nf, 架构主C++,业务主lua.

  • (2022.9-2023.9已完成)友好的协程RPC系统,实现了一个使用游戏服务器的RPC系统,简单好用,同时非常适配游戏服务器的协议系统。

  • 高并发和高性能的通信模块, 跨物理机器以及对外采用libevent+evpp实现的多线程网络tcp,udp,http, 同一个物理机通信可采用单线程共享内存bus系统,也可采用前面的多线程网络。同时实现了网络和bus系统的接口配置统一, 只需要稍微修改配置,就可方便切换.

  • 高可用的系统架构。架构采用分成架构,系统分为架构层,服务器层,具体的游戏业务层。具体的层次具体的目录,上层不会依赖下层,结构清晰。架构层,服务器层通用,不同的游戏分不同的目录.

  • (2020.1-2023.9已完成)可复用的db系统,db系统采用protobuf反射机制来实现。不需要手动撸sql语句,不需要定义mysql表。只需要一个protobuf结构体,db系统会自动创建数据库,创建表格,新加列。数据库的查询,插入,保存,也仅需要使用这个protobuf结构体调用系统结构即可。

  • (2022.9-2023.9已完成)redis缓存系统,使用protobuf反射来实现,不需要手写任何代码。只需要配置好storeserver系统的简单的缓存机制即可。

  • (2022.9-2023.9已完成)统一的配置加载机制,无论你是从excel加载配置,还是从数据库表格里加载配置。无需大量修改代码,仅仅该一个标志即可。

  • (2022.9-2023.9已完成)邮箱以及企业微信通知机制,实现了启动服务器以及服务器崩溃,服务器结构会把启动的信息以及服务器崩溃的dump信息发送至邮箱或你的企业微信里。

  • 友好的日志控制,你可以单独控制,一个小模块的日志,甚至一个玩家的日志

  • 配套的 U3D 客户端,一个捕鱼项目 https://github.com/yigao/NFShmFrame_FishClient.git

  • 跨平台(Windows, Linux)

unity3d捕鱼demo游戏 点击加群762414765,群里提供apk下载

}OXI`%2KDC3509YEVE Z00K 0 ZK~FM1IBM0{E%LI8 %)AJ unity3d捕鱼demo游戏:

后期开发目标

  • 使用C++重写excel的加载,验证,导出代码机制(python,lua的导出,在大型项目里太慢)(2023.8-2023.9已完成)
  • redis缓存系统(2023.9.18已完成)
  • 完善excel导出sql语句,从db系统加载配置(2023.9.1已完成)
  • 实现actor的多线程Lua系统(类似skynet, 区别lua只负责逻辑,系统架构主要由C++实现)
  • 实现单线程python系统
  • 实现单线程js系统
  • 实现单线程php系统
  • 实现单线程c#系统
  • 实现actor的多线程python系统(类似skynet, 区别python只负责逻辑,系统架构主要由C++实现)
  • 继续完善excel到db系统机制,自动导出代码, 实现网页端的配置更新
  • 将平台升级到C++17
  • 将db系统由mysql到libzdb, 兼容市场上大部分数据库(MySQL, PostgreSQL, SQLite and Oracle)
  • 完善捕鱼逻辑(9.23已完成)
  • 实现一个MMO游戏的逻辑
  • 重写最后一战的服务器
  • 重写魔兽世界的服务器

Dependencies

  • libevent
  • spdlog
  • google protobuf
  • hiredis
  • rapidjson
  • concurrentqueue
  • RapidXML
  • LuaIntf
  • navigation
  • lua
  • mysql
  • mysqlpp
  • curl
  • evpp
  • openssl
  • theron

教程文档 Tutorial Chapters:

unity3d捕鱼demo && Documents

unity3d捕鱼demo游戏:

Concept Chapters:

服务器架构

进程架构:

App Architecture

单物理机服务器架构:

单物理机服务器架构

多物理机服务器架构:

多物理机服务器架构

服务器架构说明:

  • 所有的服务器都要链接master服务器,可以选择使用master服务器作为命名服务器,只需修改下配置即可
  • 每一个服务器都有一个类似IP地址的ID,作为唯一ID,比如master服务器ID是1.1.1.1, worldserver服务器ID是15.100.3.1, 服务器之间相互通讯,不需要知道对方部署在哪一台物理机上,只需要知道对方的唯一ID,就可以相互通讯
  • 每一个单独的物理机上都有一个NFRouteAgentServer路由代理服务器,用来在这个物理机上实现内网通讯以及和别的物理机通讯,一个NFProxyAgentServer网关代理服务器,用来链接网关,实现对外部(客户端)通讯
  • 物理机
  • 逻辑服务器比如LoginServer,LogicServer,GameServer,WorldServer,SnsServer,StoreServer, 相互之间不连接,同一个物理机上全部链接同一个NFRouteAgentServer路由代理服务器,并把自己的唯一ID注册到这个NFRouteAgentServer上, 来实现相互通讯,比如LoginServer发消息给WorldServer, LoginServer和WorldServer之间相互不连接,LoginServer需要把消息先发给NFRouteAgentServer, NFRouteAgentServer再把消息转发给WorldServer。
  • NFRouteAgentServer路由代理服务器通过链接NFRouteServer服务器来实现相互链接,NFRouteAgentServer路由代理服务器会把自己下面的逻辑服务器信息发给NFRouteServer服务器,来实现分布跨物理机通讯。在不同物理机之间的服务器通讯需要NFRouteServer服务器,比如LoginServer给不在同一个物理机上的WorldServer发消息,LoginServer需要把消息先发给同一台物理机上的NFRouteAgentServer, NFRouteAgentServer再把消息转发给NFRouteServer, NFRouteServer在吧消息转发给和WorldServer同一个物理机的NFRouteAgentServer, NFRouteAgentServer再把消息转发给WorldServer
  • 客户端只链接NFProxyServer, 发消息给逻辑服务器,NFProxyServer会把消息转发给逻辑服务器链接的NFProxyAgentServer代理服务器,NFProxyAgentServer代理服务器在吧消息转发给逻辑服务器, 同理逻辑服务器发消息给客户端,需要先通过NFProxyAgentServer代理服务器, NFProxyAgentServer代理服务器在发消息转发给NFProxyServer, NFProxyServer在发给客户端

一些很赞的项目

PSS

  • 作者: freeeyes
  • 描述: 基于插件的跨平台网络服务器框架

ARK

  • 作者: NickYang1988
  • 描述: 基于插件的跨平台网络服务器框架

NoahGameFrame

  • 作者: ketoo
  • 描述: 基于插件的跨平台网络服务器框架
Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

简介

NFShmServer 是一个使用C++开发的轻量级,敏捷型,弹性的,分布式的共享内存(C++热更)/多进程lua(lua热更)的插件开发框架, 让你更快更简单的开发服务端应用. 部分思路来自UE4和Ogre.(当前主要用在游戏领域) . 最近加了rpc服务,数据库自动建库建表新加列服务,单线程lua脚本系统,后续可能会加actor多线程lua脚本系统,想法有点类似skynet的 展开 收起
Apache-2.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
C++
1
https://gitee.com/xiaoyi445_admin/NFShmServer.git
git@gitee.com:xiaoyi445_admin/NFShmServer.git
xiaoyi445_admin
NFShmServer
NFShmServer
master

搜索帮助