{"id":18615378,"url":"https://github.com/8treenet/jaguar","last_synced_at":"2025-04-11T00:31:10.948Z","repository":{"id":92413600,"uuid":"197310206","full_name":"8treenet/jaguar","owner":"8treenet","description":"jaguar is a scalable, efficient network library.","archived":false,"fork":false,"pushed_at":"2021-04-28T14:22:52.000Z","size":38,"stargazers_count":21,"open_issues_count":0,"forks_count":8,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-03-25T06:51:17.083Z","etag":null,"topics":["network","socket","tcp","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/8treenet.png","metadata":{"files":{"readme":"readme.md","changelog":null,"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":"2019-07-17T03:44:10.000Z","updated_at":"2022-01-17T02:58:19.000Z","dependencies_parsed_at":null,"dependency_job_id":"999b1c41-7b78-47b1-b210-e27e2d13e1d1","html_url":"https://github.com/8treenet/jaguar","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/8treenet%2Fjaguar","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/8treenet%2Fjaguar/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/8treenet%2Fjaguar/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/8treenet%2Fjaguar/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/8treenet","download_url":"https://codeload.github.com/8treenet/jaguar/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248322247,"owners_count":21084334,"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":["network","socket","tcp","tcp-server"],"created_at":"2024-11-07T03:29:08.618Z","updated_at":"2025-04-11T00:31:09.221Z","avatar_url":"https://github.com/8treenet.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# jaguar\n[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://github.com/8treenet/gotree/blob/master/LICENSE) [![Go Report Card](https://goreportcard.com/badge/github.com/8treenet/tcp)](https://goreportcard.com/report/github.com/8treenet/tcp) [![Build Status](https://travis-ci.org/8treenet/gotree.svg?branch=master)](https://travis-ci.org/8treenet/gotree) [![GoDoc](https://godoc.org/github.com/8treenet/gotree?status.svg)](https://godoc.org/github.com/8treenet/gotree)\n\n###### jaguar 是一个可扩展、高效的tcp网络库。\n\n## Overview\n- Tcp Server\n- Tcp Connect\n- Request Handle\n- Push Handle\n- Middleware\n- Inversion of Control\n- Examples\n\n#### 安装\n```sh\n$ go get -u github.com/8treenet/jaguar\n```\n\n## Tcp Server\n```go\n\ntype TcpServer interface {\n    Listen(*Opt)\n    Accept(func(*TcpConn, *Middleware))\n}\n\n//创建 server\nserver := jaguar.NewServer()\n\n//创建配置\nopt := \u0026jaguar.Opt{\n    Addr: \"0.0.0.0:9000\",       //绑定地址和端口\n    PacketMaxLength: 6000,       //收发最大包体字节，超过该字节主动断开连接。\n    PacketHeaderLength: 2,       //包头占位长度 1,2,4,8\n    IdleCheckFrequency: time.Second * 120, //心跳检测\n    ByteOrder: binary.BigEndian //网络字节序\n}\n\n// 新连接回调\n// conn : 新连接\n// middleware : 中间件\nserver.Accept(func(conn *jaguar.TcpConn, middleware *jaguar.Middleware) {\n    //session 是一个自定义的插件\n    session := plugins.NewSession()\n    //conn 附加插件\n    conn.Attach(session)\n    //使用自定义的 session.CloseEvent 注册连接关闭事件\n    middleware.Closed(session.CloseEvent)\n})\n\n//开启监听\nserver.Listen(opt)\n```\n\n## Tcp Connect\n```go\n\ntype TcpConn interface {\n    //附加插件对象，可以通过依赖注入获取 type Test struct {obj object `inject:\"\"`}\n    Attach(object interface{})\n    //附加插件对象接口形式，可以通过依赖注入获取 type Test struct {obj inteface `inject:\"impl\"`}\n    AttachImpl(impl string, object interface{})\n    //关闭连接\n    Close()\n    //获取远程地址\n    RemoteAddr() net.Addr\n    //推送 推送处理器\n    Push(pushHandle Encode) error\n}\n```\n\n\n## Request Handle\n```go\n// 请求处理器，读写io数据和返回响应，继承该接口可使用。\ntype ReqHandle interface {\n    //读取流数据\n    ReadStream(...interface{}) error\n    //读取流数据中的字符串\n    ReadStreamByString(int, *string) error\n    //写入流数据\n    WriteStream(...interface{})\n    //回执响应\n    Respone() error\n}\n\nfunc init() {\n    // 注册请求处理器 对应请求数据包的协议id 101\n    // 请求处理器chat必须实现 Execute 方法\n    jaguar.AddRequest(101, new(chat))\n}\n\n//定义一个聊天请求处理器\ntype chat struct {\n    //继承 ReqHandle\n    jaguar.ReqHandle `inject:\"req_handle\"`\n    //通过依赖注入获取插件-实体方式\n    Session *plugins.Session `inject:\"\"`\n    //使用 jaguar.TcpConn 插件, 该插件负责连接相关\n    Conn jaguar.TcpConn `inject:\"tcp_conn\"`\n}\n\n// Execute - 必须实现\nfunc (c *chat) Execute() {\n    //c.ReadStream()\n    //c.WriteStream()\n    //c.Respone()\n}\n```\n\n## Push Handle\n```go\n// 推送处理器，写io数据，继承该接口可使用。\ntype PushHandle interface {\n    //Write byte stream data\n    WriteStream(values ...interface{})\n}\n\n// 定义个推送处理器\ntype chat struct {\n    jaguar.PushHandle `inject:\"push_handle\"`\n    //要推送的数据列 三项\n    sender            string\n    content           string\n    row               uint32\n}\n\n// ProtocolId - 必须实现\nfunc (c *chat) ProtocolId() uint16 {\n    //推送数据包的协议id\n    //jaguar.TcpConn 插件会调用 chat.ProtocolId()\n    return 300\n}\n\n// Encode() - 必须实现\nfunc (c *chat) Encode() {\n    //jaguar.TcpConn 插件会调用 chat.Encode()\n    c.WriteStream(c.row)\n    c.WriteStream(uint8(len(c.sender)), c.sender)\n    c.WriteStream(uint16(len(c.content)), c.content)\n}\n\nTcpConn.Push(\u0026chat{sender:\"lucifer\", content:\"fuck\"})\n```\n\n\n## Middleware\n```go\n// 拦截器\nserver.Accept(func(conn jaguar.TcpConn, middleware *jaguar.Middleware) {\n    middleware.Closed(f func(){\n        // Closed - Close registration for callbacks\n    })\n\n    middleware.Recover(f func(e error,s string){\n        // Recover - Request a panic in the code\n    })\n\n    middleware.Reader(func(id uint16, b *bytes.Buffer) *bytes.Buffer {\n        // Reader - Read data\n        return b\n    })\n\n    middleware.Writer(func(id uint16, b *bytes.Buffer) *bytes.Buffer {\n        // Writer - Write data\n        return b\n    })\n\n    middleware.Request(func(id uint16, reqHandle interface{}) {\n        //Request - Callback at request\n    })\n\n    middleware.Respone(func(id uint16, reqHandle interface{}) {\n        //Respone - Callback when requesting a reply\n    })\n\n    middleware.Push(func(uint16, interface{}){\n        //Push - Callbacks on push\n    })\n})\n```\n\n## Examples\n##### 一个聊天室示例\n```sh\n# 启动示例程序\n$ cd jaguar/chat_examples\n# 启动server\n$ go run server/main.go \n\n# 开启新窗口启动客户端 1\n$ command + t\n$ go run mock_client/main.go \n\n# 开启新窗口启动客户端 2\n$ command + t\n$ go run mock_client/main.go \n```\n\n```\n报文格式, 可变长数据需要指明 [长度, 数据]\n+-------------------+--------------+---------------------------------------------------+\n|               2 Bytes            |  2 Bytes  |       N Bytes                         |\n+-------------------+--------------+---------------------------------------------------+\n|\u003c=         length of body       =\u003e|     id    | \u003c======= data =======================\u003e|\n|\u003c============= header ===========\u003e|\u003c==================== body =======================\u003e|\n\n协议 ：请求登录\nid : 100 (2 Bytes)\ntoken :[] (1 Bytes, N Bytes)\n\n协议 ：请求登录回执\nid : 100 (2 Bytes)\nok : 1 (1 Bytes)\nuid : [](4 Bytes)\nuname : [](1 Bytes, N Bytes)\n\n\n协议 ：请求聊天消息\nid : 101 (2 Bytes)\nrow : [] (4 Bytes)\ncontent : [](2 Bytes, N Bytes)\n\n协议 ：请求聊天消息回执\nid : 101 (2 Bytes)\nok : 1 (1 Bytes)\n\n协议 ：聊天信息推送\nid : 300 (2 Bytes)\nsender : [] (1 Bytes, N Bytes)\ncontent : [](2 Bytes, N Bytes)\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F8treenet%2Fjaguar","html_url":"https://awesome.ecosyste.ms/projects/github.com%2F8treenet%2Fjaguar","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F8treenet%2Fjaguar/lists"}