{"id":13413642,"url":"https://github.com/xfxdev/xtcp","last_synced_at":"2025-03-14T19:32:56.529Z","repository":{"id":57496389,"uuid":"55167207","full_name":"xfxdev/xtcp","owner":"xfxdev","description":"A TCP Server Framework with graceful shutdown, custom protocol.","archived":false,"fork":false,"pushed_at":"2020-02-29T18:57:41.000Z","size":40,"stargazers_count":150,"open_issues_count":0,"forks_count":31,"subscribers_count":14,"default_branch":"master","last_synced_at":"2024-07-31T20:52:36.781Z","etag":null,"topics":["framework","golang","graceful","server","tcp"],"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/xfxdev.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}},"created_at":"2016-03-31T16:50:14.000Z","updated_at":"2024-07-09T07:34:26.000Z","dependencies_parsed_at":"2022-09-16T09:41:55.550Z","dependency_job_id":null,"html_url":"https://github.com/xfxdev/xtcp","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xfxdev%2Fxtcp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xfxdev%2Fxtcp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xfxdev%2Fxtcp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xfxdev%2Fxtcp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/xfxdev","download_url":"https://codeload.github.com/xfxdev/xtcp/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":221498771,"owners_count":16833059,"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":["framework","golang","graceful","server","tcp"],"created_at":"2024-07-30T20:01:45.245Z","updated_at":"2024-10-26T05:31:13.149Z","avatar_url":"https://github.com/xfxdev.png","language":"Go","readme":"# xtcp\n\nA TCP Server Framework with graceful shutdown,custom protocol.\n\n[![Build Status](https://travis-ci.org/xfxdev/xtcp.svg?branch=master)](https://travis-ci.org/xfxdev/xtcp)\n[![Go Report Card](https://goreportcard.com/badge/github.com/xfxdev/xtcp)](https://goreportcard.com/report/github.com/xfxdev/xtcp)\n[![GoDoc](https://godoc.org/github.com/xfxdev/xtcp?status.svg)](https://godoc.org/github.com/xfxdev/xtcp)\n\n\n## Usage\n\n### Define your protocol format:\nBefore create server and client, you need define the protocol format first.\n\n```go\n// Packet is the unit of data.\ntype Packet interface {\n\tfmt.Stringer\n}\n\n// Protocol use to pack/unpack Packet.\ntype Protocol interface {\n\t// return the size need for pack the Packet.\n\tPackSize(p Packet) int\n\t// PackTo pack the Packet to w.\n\t// The return value n is the number of bytes written;\n\t// Any error encountered during the write is also returned.\n\tPackTo(p Packet, w io.Writer) (int, error)\n\t// Pack pack the Packet to new created buf.\n\tPack(p Packet) ([]byte, error)\n\t// try to unpack the buf to Packet. If return len \u003e 0, then buf[:len] will be discard.\n\t// The following return conditions must be implement:\n\t// (nil, 0, nil) : buf size not enough for unpack one Packet.\n\t// (nil, len, err) : buf size enough but error encountered.\n\t// (p, len, nil) : unpack succeed.\n\tUnpack(buf []byte) (Packet, int, error)\n}\n```\n\n### Set your logger(optional):\n```go\nfunc SetLogger(l Logger)\n```\nNote: xtcp will not output any log by default unless you implement your own logger.\n\n\n### Provide event handler:\nIn xtcp, there are some events to notify the state of net conn, you can handle them according your need:\n\n```go\nconst (\n\t// EventAccept mean server accept a new connect.\n\tEventAccept EventType = iota\n\t// EventConnected mean client connected to a server.\n\tEventConnected\n\t// EventRecv mean conn recv a packet.\n\tEventRecv\n\t// EventClosed mean conn is closed.\n\tEventClosed\n)\n```\n\nTo handle the event, just implement the OnEvent interface.\n\n```go\n// Handler is the event callback.\n// p will be nil when event is EventAccept/EventConnected/EventClosed\ntype Handler interface {\n\tOnEvent(et EventType, c *Conn, p Packet)\n}\n```\n\n### Create server:\n\n```go\n// 1. create protocol and handler.\n// ...\n\n// 2. create opts.\nopts := xtcp.NewOpts(handler, protocol)\n\n// 3. create server.\nserver := xtcp.NewServer(opts)\n\n// 4. start.\ngo server.ListenAndServe(\"addr\")\n```\n\n### Create client:\n\n```go\n// 1. create protocol and handler.\n// ...\n\n// 2. create opts.\nopts := xtcp.NewOpts(handler, protocol)\n\n// 3. create client.\nclient := NewConn(opts)\n\n// 4. start\ngo client.DialAndServe(\"addr\")\n```\n\n### Send and recv packet.\nTo send data, just call the 'Send' function of Conn. You can safe call it in any goroutines.\n\n```go\nfunc (c *Conn) Send(buf []byte) error\n```\n\nTo recv a packet, implement your handler function:\n\n```go\nfunc (h *myhandler) OnEvent(et EventType, c *Conn, p Packet) {\n\tswitch et {\n\t\tcase EventRecv:\n\t\t\t...\n\t}\n}\n```\n\n### Stop\n\nxtcp have three stop modes, stop gracefully mean conn will stop until all cached data sended.\n\n```go\n// StopMode define the stop mode of server and conn.\ntype StopMode uint8\n\nconst (\n\t// StopImmediately mean stop directly, the cached data maybe will not send.\n\tStopImmediately StopMode = iota\n\t// StopGracefullyButNotWait stop and flush cached data.\n\tStopGracefullyButNotWait\n\t// StopGracefullyAndWait stop and block until cached data sended.\n\tStopGracefullyAndWait\n)\n```\n\n## Example\nThe example define a protocol format which use protobuf inner.\nYou can see how to define the protocol and how to create server and client.\n\n[example](https://github.com/xfxdev/xtcp/tree/master/_example)\n","funding_links":[],"categories":["Networking","网络相关库","网络","\u003cspan id=\"网络-networking\"\u003e网络 Networking\u003c/span\u003e","網絡","Relational Databases"],"sub_categories":["Uncategorized","Strings","暂未分类","Advanced Console UIs","交流","\u003cspan id=\"高级控制台用户界面-advanced-console-uis\"\u003e高级控制台用户界面 Advanced Console UIs\u003c/span\u003e","Transliteration","高級控制台界面","音译","高级控制台界面","暂未分类这些库被放在这里是因为其他类别似乎都不适合。"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxfxdev%2Fxtcp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxfxdev%2Fxtcp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxfxdev%2Fxtcp/lists"}