You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

291 lines
8.2 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
5 years ago
5 years ago
6 years ago
5 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
5 years ago
5 years ago
6 years ago
6 years ago
5 years ago
5 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. # Zinx
  2. [![License](https://img.shields.io/badge/License-GPL%203.0-blue.svg)](LICENSE) [![Gitter](https://img.shields.io/badge/在线交流-Gitter-green.svg)](https://gitter.im/zinx_go/community) [![zinx详细教程](https://img.shields.io/badge/zinx详细教程-简书-red.svg)](https://www.jianshu.com/p/23d07c0a28e5) [![zinx原创书籍下载](https://img.shields.io/badge/原创书籍下载-Gitbook-black.svg)](https://legacy.gitbook.com/book/aceld/zinx/details)
  3. Zinx 是一个基于Golang的轻量级并发服务器框架
  4. ### 开发者
  5. - 刘丹冰([@aceld](https://github.com/aceld))
  6. - 张超([@zhngcho](https://github.com/zhngcho))
  7. [zinx(C++版本)](https://github.com/marklion/zinx)
  8. ### 开发者
  9. - 刘洋([@marklion](https://github.com/marklion))
  10. ---
  11. ### Github
  12. Git: https://github.com/aceld/zinx
  13. ### 码云(Gitee)
  14. Git: https://gitee.com/Aceld/zinx
  15. ---
  16. ## 一、写在前面
  17. 我们为什么要做Zinx,Golang目前在服务器的应用框架很多,但是应用在游戏领域或者其他长链接的领域的轻量级企业框架甚少。
  18. 设计Zinx的目的是我们可以通过Zinx框架来了解基于Golang编写一个TCP服务器的整体轮廓,让更多的Golang爱好者能深入浅出的去学习和认识这个领域。
  19. Zinx框架的项目制作采用编码和学习教程同步进行,将开发的全部递进和迭代思维带入教程中,而不是一下子给大家一个非常完整的框架去学习,让很多人一头雾水,不知道该如何学起。
  20. 教程会一个版本一个版本迭代,每个版本的添加功能都是微小的,让一个服务框架小白,循序渐进的曲线方式了解服务器框架的领域。
  21. 当然,最后希望Zinx会有更多的人加入,给我们提出宝贵的意见,让Zinx成为真正的解决企业的服务器框架!在此感谢您的关注!
  22. ## 二、初探Zinx架构
  23. ![1-Zinx框架.png](https://camo.githubusercontent.com/903d1431358fa6f4634ebaae3b49a28d97e23d77/68747470733a2f2f75706c6f61642d696d616765732e6a69616e7368752e696f2f75706c6f61645f696d616765732f31313039333230352d633735666636383232333362323533362e706e673f696d6167654d6f6772322f6175746f2d6f7269656e742f7374726970253743696d61676556696577322f322f772f31323430)
  24. ![zinx-start.gif](https://camo.githubusercontent.com/16afabf00523abd556d918203d11f413c178472a/68747470733a2f2f75706c6f61642d696d616765732e6a69616e7368752e696f2f75706c6f61645f696d616765732f31313039333230352d343930623439303938616336336166362e6769663f696d6167654d6f6772322f6175746f2d6f7269656e742f7374726970)
  25. ## 三、Zinx详细教程及文档
  26. [《Zinx框架教程-基于Golang的轻量级并发服务器》](https://www.jianshu.com/p/23d07c0a28e5)
  27. ## 四、Zinx开发API文档
  28. ### 快速开始
  29. #### server
  30. 基于Zinx框架开发的服务器应用,主函数步骤比较精简,最多主需要3步即可。
  31. 1. 创建server句柄
  32. 2. 配置自定义路由及业务
  33. 3. 启动服务
  34. ```go
  35. func main() {
  36. //1 创建一个server句柄
  37. s := znet.NewServer()
  38. //2 配置路由
  39. s.AddRouter(0, &PingRouter{})
  40. //3 开启服务
  41. s.Serve()
  42. }
  43. ```
  44. 其中自定义路由及业务配置方式如下:
  45. ```go
  46. import (
  47. "fmt"
  48. "zinx/ziface"
  49. "zinx/znet"
  50. )
  51. //ping test 自定义路由
  52. type PingRouter struct {
  53. znet.BaseRouter
  54. }
  55. //Ping Handle
  56. func (this *PingRouter) Handle(request ziface.IRequest) {
  57. //先读取客户端的数据
  58. fmt.Println("recv from client : msgId=", request.GetMsgID(), ", data=", string(request.GetData()))
  59. //再回写ping...ping...ping
  60. err := request.GetConnection().SendBuffMsg(0, []byte("ping...ping...ping"))
  61. if err != nil {
  62. fmt.Println(err)
  63. }
  64. }
  65. ```
  66. #### client
  67. Zinx的消息处理采用,`[MsgLength]|[MsgID]|[Data]`的封包格式
  68. ```go
  69. package main
  70. import (
  71. "fmt"
  72. "io"
  73. "net"
  74. "time"
  75. "zinx/znet"
  76. )
  77. /*
  78. 模拟客户端
  79. */
  80. func main() {
  81. fmt.Println("Client Test ... start")
  82. //3秒之后发起测试请求,给服务端开启服务的机会
  83. time.Sleep(3 * time.Second)
  84. conn,err := net.Dial("tcp", "127.0.0.1:7777")
  85. if err != nil {
  86. fmt.Println("client start err, exit!")
  87. return
  88. }
  89. for n := 3; n >= 0; n-- {
  90. //发封包message消息
  91. dp := znet.NewDataPack()
  92. msg, _ := dp.Pack(znet.NewMsgPackage(0,[]byte("Zinx Client Test Message")))
  93. _, err := conn.Write(msg)
  94. if err !=nil {
  95. fmt.Println("write error err ", err)
  96. return
  97. }
  98. //先读出流中的head部分
  99. headData := make([]byte, dp.GetHeadLen())
  100. _, err = io.ReadFull(conn, headData) //ReadFull 会把msg填充满为止
  101. if err != nil {
  102. fmt.Println("read head error")
  103. break
  104. }
  105. //将headData字节流 拆包到msg中
  106. msgHead, err := dp.Unpack(headData)
  107. if err != nil {
  108. fmt.Println("server unpack err:", err)
  109. return
  110. }
  111. if msgHead.GetDataLen() > 0 {
  112. //msg 是有data数据的,需要再次读取data数据
  113. msg := msgHead.(*znet.Message)
  114. msg.Data = make([]byte, msg.GetDataLen())
  115. //根据dataLen从io中读取字节流
  116. _, err := io.ReadFull(conn, msg.Data)
  117. if err != nil {
  118. fmt.Println("server unpack data err:", err)
  119. return
  120. }
  121. fmt.Println("==> Recv Msg: ID=", msg.Id, ", len=", msg.DataLen, ", data=", string(msg.Data))
  122. }
  123. time.Sleep(1*time.Second)
  124. }
  125. }
  126. ```
  127. ### Zinx配置文件
  128. ```json
  129. {
  130. "Name":"zinx v-0.10 demoApp",
  131. "Host":"127.0.0.1",
  132. "TcpPort":7777,
  133. "MaxConn":3,
  134. "WorkerPoolSize":10,
  135. "LogDir": "./mylog",
  136. "LogFile":"zinx.log"
  137. }
  138. ```
  139. `Name`:服务器应用名称
  140. `Host`:服务器IP
  141. `TcpPort`:服务器监听端口
  142. `MaxConn`:允许的客户端链接最大数量
  143. `WorkerPoolSize`:工作任务池最大工作Goroutine数量
  144. `LogDir`: 日志文件夹
  145. `LogFile`: 日志文件名称(如果不提供,则日志信息打印到Stderr)
  146. ### I.服务器模块Server
  147. ```go
  148. func NewServer () ziface.IServer
  149. ```
  150. 创建一个Zinx服务器句柄,该句柄作为当前服务器应用程序的主枢纽,包括如下功能:
  151. #### 1)开启服务
  152. ```go
  153. func (s *Server) Start()
  154. ```
  155. #### 2)停止服务
  156. ```go
  157. func (s *Server) Stop()
  158. ```
  159. #### 3)运行服务
  160. ```go
  161. func (s *Server) Serve()
  162. ```
  163. #### 4)注册路由
  164. ```go
  165. func (s *Server) AddRouter (msgId uint32, router ziface.IRouter)
  166. ```
  167. #### 5)注册链接创建Hook函数
  168. ```go
  169. func (s *Server) SetOnConnStart(hookFunc func (ziface.IConnection))
  170. ```
  171. #### 6)注册链接销毁Hook函数
  172. ```go
  173. func (s *Server) SetOnConnStop(hookFunc func (ziface.IConnection))
  174. ```
  175. ### II.路由模块
  176. ```go
  177. //实现router时,先嵌入这个基类,然后根据需要对这个基类的方法进行重写
  178. type BaseRouter struct {}
  179. //这里之所以BaseRouter的方法都为空,
  180. // 是因为有的Router不希望有PreHandle或PostHandle
  181. // 所以Router全部继承BaseRouter的好处是,不需要实现PreHandle和PostHandle也可以实例化
  182. func (br *BaseRouter)PreHandle(req ziface.IRequest){}
  183. func (br *BaseRouter)Handle(req ziface.IRequest){}
  184. func (br *BaseRouter)PostHandle(req ziface.IRequest){}
  185. ```
  186. ### III.链接模块
  187. #### 1)获取原始的socket TCPConn
  188. ```go
  189. func (c *Connection) GetTCPConnection() *net.TCPConn
  190. ```
  191. #### 2)获取链接ID
  192. ```go
  193. func (c *Connection) GetConnID() uint32
  194. ```
  195. #### 3)获取远程客户端地址信息
  196. ```go
  197. func (c *Connection) RemoteAddr() net.Addr
  198. ```
  199. #### 4)发送消息
  200. ```go
  201. func (c *Connection) SendMsg(msgId uint32, data []byte) error
  202. func (c *Connection) SendBuffMsg(msgId uint32, data []byte) error
  203. ```
  204. #### 5)链接属性
  205. ```go
  206. //设置链接属性
  207. func (c *Connection) SetProperty(key string, value interface{})
  208. //获取链接属性
  209. func (c *Connection) GetProperty(key string) (interface{}, error)
  210. //移除链接属性
  211. func (c *Connection) RemoveProperty(key string)
  212. ```
  213. ---
  214. ### 关于作者:
  215. 作者:`Aceld(刘丹冰)`
  216. 简书号:`IT无崖子`
  217. `mail`:
  218. [danbing.at@gmail.com](mailto:danbing.at@gmail.com)
  219. `github`:
  220. [https://github.com/aceld](https://github.com/aceld)
  221. `原创书籍gitbook`:
  222. [http://legacy.gitbook.com/@aceld](http://legacy.gitbook.com/@aceld)
  223. ### Zinx技术讨论社区
  224. QQ技术讨论群:
  225. ![gopool5.jpeg](https://camo.githubusercontent.com/847acab8b3d25cce70f7eaa242120727aebe9a1c/68747470733a2f2f75706c6f61642d696d616765732e6a69616e7368752e696f2f75706c6f61645f696d616765732f31313039333230352d366364666433383165386666613132372e6a7065673f696d6167654d6f6772322f6175746f2d6f7269656e742f7374726970253743696d61676556696577322f322f772f31323430)
  226. 欢迎大家加入,获取更多相关学习资料