{"id":20355891,"url":"https://github.com/hedzr/go-socketlib","last_synced_at":"2025-04-12T02:50:45.451Z","repository":{"id":57538842,"uuid":"287880963","full_name":"hedzr/go-socketlib","owner":"hedzr","description":"A simple, fast approach to implement your communication protocol.","archived":false,"fork":false,"pushed_at":"2025-03-23T11:55:36.000Z","size":487,"stargazers_count":3,"open_issues_count":1,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-23T12:28:48.237Z","etag":null,"topics":["cmdr","communication-library","communication-protocol","golang","golang-library","interceptor","protocol","protocol-interceptor","socket-programming","tcp-server","udp-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/hedzr.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"custom":"https://paypal.me/hezr/3"}},"created_at":"2020-08-16T05:36:06.000Z","updated_at":"2025-03-23T11:54:39.000Z","dependencies_parsed_at":"2024-03-09T07:22:12.706Z","dependency_job_id":"452a4f14-aeb0-4576-ad2c-f83ad368535e","html_url":"https://github.com/hedzr/go-socketlib","commit_stats":{"total_commits":152,"total_committers":2,"mean_commits":76.0,"dds":0.03289473684210531,"last_synced_commit":"9f3430aa7c773c4e2b097482dfd506f9f4f37222"},"previous_names":[],"tags_count":17,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hedzr%2Fgo-socketlib","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hedzr%2Fgo-socketlib/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hedzr%2Fgo-socketlib/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hedzr%2Fgo-socketlib/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hedzr","download_url":"https://codeload.github.com/hedzr/go-socketlib/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248509187,"owners_count":21115957,"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":["cmdr","communication-library","communication-protocol","golang","golang-library","interceptor","protocol","protocol-interceptor","socket-programming","tcp-server","udp-server"],"created_at":"2024-11-14T23:14:23.893Z","updated_at":"2025-04-12T02:50:45.444Z","avatar_url":"https://github.com/hedzr.png","language":"Go","funding_links":["https://paypal.me/hezr/3"],"categories":[],"sub_categories":[],"readme":"# go-socketlib\n\n![Go](https://github.com/hedzr/go-socketlib/workflows/Go/badge.svg)\n![GitHub go.mod Go version](https://img.shields.io/github/go-mod/go-version/hedzr/store)\n[![GitHub tag (latest SemVer)](https://img.shields.io/github/tag/hedzr/go-socketlib.svg?label=release)](https://github.com/hedzr/go-socketlib/releases)\n[![Go Dev](https://img.shields.io/badge/go-dev-green)](https://pkg.go.dev/github.com/hedzr/go-socketlib)\n[![GoDoc](https://img.shields.io/badge/godoc-reference-blue.svg?style=flat)](https://godoc.org/github.com/hedzr/go-socketlib)\n[![Go Report Card](https://goreportcard.com/badge/github.com/hedzr/go-socketlib)](https://goreportcard.com/report/github.com/hedzr/go-socketlib)\u003c!--\n[![Coverage Status](https://coveralls.io/repos/github/hedzr/go-socketlib/badge.svg?branch=master\u0026.9)](https://coveralls.io/github/hedzr/go-socketlib?branch=master) --\u003e\n\n`go-socketlib` provides a simple, fast approach to implement your communication protocol.\n\n## Features\n\n- supports TCP, UDP, and Unix socket server/client developing\n- Wrap the dirty jobs in programming socket server and client so that you can devote into protocol design and coding.\n- Write your business logical\n  with [api.Interceptor](https://github.com/hedzr/go-socketlib/blob/master/net/api/protocol.go#L22) and [api.UdpInterceptor](https://github.com/hedzr/go-socketlib/blob/master/net/api/protocol.go#L22), also [api.ServerInterceptor] and [api.ServerUDPInterceptor].\n\n## History\n\nFor full list, have a see at [CHANGELOG](https://github.com/hedzr/go-socketlib/blob/master/CHANGELOG).\n\n## Getting Start\n\n### Import\n\n```go\nimport \"github.com/hedzr/go-socketlib\"\n```\n\n### Write a TCP server\n\n#### With [hedzr/cmdr](https://github.com/hedzr/cmdr)\n\n`go-socketlib` has been integrated with `cmdr`. Here is a full app:\n\n```go\npackage main\n\nimport (\n\t\"github.com/hedzr/cmdr\"\n\t\"github.com/hedzr/go-socketlib/tcp/client\"\n\t\"github.com/hedzr/go-socketlib/tcp/server\"\n\t\"github.com/hedzr/log\"\n\t\"github.com/hedzr/logex/build\"\n)\n\nfunc main() {\n\tif err := cmdr.Exec(buildRootCmd(),\n\t\tcmdr.WithLogx(build.New(log.NewLoggerConfigWith(true, \"sugar\", \"debug\"))),\n\t\t//cmdr.WithUnknownOptionHandler(onUnknownOptionHandler),\n\t\t//cmdr.WithUnhandledErrorHandler(onUnhandledErrorHandler),\n\t); err != nil {\n\t\tcmdr.Logger.Fatalf(\"error: %+v\", err)\n\t}\n}\n\nfunc buildRootCmd() (rootCmd *cmdr.RootCommand) {\n\troot := cmdr.Root(appName, \"1.0.1\").\n\t\tHeader(\"fluent - test for cmdr - no version - hedzr\").\n\t\tDescription(desc, longDesc).\n\t\tExamples(examples)\n\trootCmd = root.RootCommand()\n\n\tsocketlibCmd(root)\n\treturn\n}\n\nfunc socketlibCmd(root cmdr.OptCmd) {\n\t// for TCP server/client\n  \n\taCmd := root.NewSubCommand(\"tcp\", \"tcp\", \"socket\", \"socketlib\").\n\t\tDescription(\"go-socketlib operations...\", \"\").\n\t\tGroup(\"TCP\")\n\tserver.AttachToCmdr(aCmd, server.WithPort(1983))\n\tclient.AttachToCmdr(aCmd, client.WithCmdrPort(1983))\n  \n\t// for UDP server/client\n  \n\tudpCmd := root.NewSubCommand(\"udp\", \"udp\").\n\t\tDescription(\"go-socketlib UDP operations...\", \"\").\n\t\tGroup(\"UDP\")\n\n\tserver.AttachToCmdr(udpCmd, server.WithCmdrUDPMode(true), server.WithCmdrPort(1984))\n\tclient.AttachToCmdr(udpCmd, client.WithCmdrUDPMode(true), client.WithCmdrPort(1984))\n}\n\nconst (\n\tappName   = \"tcp-tool\"\n\tcopyright = \"tcp-tool is an effective devops tool\"\n\tdesc      = \"tcp-tool is an effective devops tool. It make an demo application for `cmdr`.\"\n\tlongDesc  = \"tcp-tool is an effective devops tool. It make an demo application for `cmdr`.\"\n\texamples  = `\n$ {{.AppName}} --help\n  show help screen.\n`\n)\n```\n\nRun it:\n\n```bash\n$ go run ./cli tcp --help\ntcp-tool is an effective devops tool by hedzr - v1.0.1\n\nUsages:\n    tcp-tool tcp [Sub-Commands] [tail args...] [Options] [Parent/Global Options]\n\nDescription:\n    go-socketlib TCO operations...\n\nSub-Commands:\n  c, client                                       TCP/UDP/Unix client operations\n  s, server                                       TCP/UDP/Unix Server Operations\n\nGlobal Options:\n  [Misc]\n          --config=[Locations of config files]    load config files from where you specified (default [Locations of config files]=)\n  -q,     --quiet                                 No more screen output. [env: QUITE] (default=false)\n  -trace, --trace,--tr                            enable trace mode for tcp/mqtt send/recv data dump [env: TRACE] (default=false)\n  -v,     --verbose                               Show this help screen [env: VERBOSE] (default=false)\n\nType '-h'/'-?' or '--help' to get command help screen.\nMore: '-D'/'--debug'['--env'|'--raw'|'--more'], '-V'/'--version', '-#'/'--build-info', '--no-color', '--strict-mode', '--no-env-overrides'...\n\n```\n\nThe server options:\n\n```bash\n❯ go run ./cli tcp server --help\ntcp-tool is an effective devops tool by hedzr - v1.0.1\n\nUsages:\n    tcp-tool tcp server [tail args...] [Options] [Parent/Global Options]\n\nDescription:\n    TCP/UDP/Unix Server Operations\n\nOptions:\n     --network                                    network: tcp, tcp4, tcp6, unix, unixpacket, and udp, udp4, udp6 (default='tcp')\n  [TCP/UDP]\n  -a, --addr=HOST-or-IP,--adr,--address           The address to listen to (default HOST-or-IP=)\n  -p, --port=PORT                                 The port to listen on (default PORT=1983)\n  [TLS]\n  -tls, --enable-tls                              enable TLS mode (default=false)\n  -ca,  --cacert=PATH,--ca-cert                   CA cert path (.cer,.crt,.pem) if it's standalone (default PATH='root.pem')\n  -c,   --cert=PATH                               server public-cert path (.cer,.crt,.pem) (default PATH='cert.pem')\n  -k,   --key=PATH                                server private-key path (.cer,.crt,.pem) (default PATH='cert.key')\n        --client-auth                             enable client cert authentication (default=false)\n        --tls-version                             tls-version: 0,1,2,3 (default=2)\n  [Tool]\n  -pp, --pid-path=PATH                            The pid filepath (default PATH='/var/run/$APPNAME/$APPNAME.pid')\n  -s,  --stop,--shutdown                          stop/shutdown the running server (default=false)\n\nGlobal Options:\n  [Misc]\n          --config=[Locations of config files]    load config files from where you specified (default [Locations of config files]=)\n  -q,     --quiet                                 No more screen output. [env: QUITE] (default=false)\n  -trace, --trace,--tr                            enable trace mode for tcp/mqtt send/recv data dump [env: TRACE] (default=false)\n  -v,     --verbose                               Show this help screen [env: VERBOSE] (default=false)\n\nType '-h'/'-?' or '--help' to get command help screen.\nMore: '-D'/'--debug'['--env'|'--raw'|'--more'], '-V'/'--version', '-#'/'--build-info', '--no-color', '--strict-mode', '--no-env-overrides'...\n\n```\n\nstart the server:\n\n```bash\n❯ go run ./cli tcp server -tls\n2020-08-14T17:16:53.782+0800\tINFO\tgo-socketlib/tcp/server/server.go:38\tStarting server (tcp)... cmdr.InDebugging = false\n2020-08-14T17:16:53.793+0800\tINFO\tgo-socketlib/tcp/server/server.go:70\tPID (79656) file created at: /var/run/tcp-tool/tcp-tool.pid\n2020-08-14T17:16:53.793+0800\tINFO\tgo-socketlib/tcp/server/server.go:106\tListening on :1983 with TLS enabled.\n```\n\nStart the client:\n\n```bash\n$ go run ./cli/c2 tcp client -tls -k\n...\n2020-08-14T17:32:25.279+0800\tDEBUG\tgo-socketlib/tcp/client/client.go:129\t #99 sent\n```\n\n### Write the server/client protocol interceptor with yours\n\nBy using [Interceptor](https://github.com/hedzr/go-socketlib/blob/master/tcp/protocol/protocol.go#L22) and\nClientInterceptor, you can attach a protocol interceptor onto bare metal socketlib server/client.\n\nFor example (from our CoAP impl):\n\n```go\nfunc AttachToCmdr(cmd cmdr.OptCmd, opts ...server.CmdrOpt) {\n\n\t// server\n\n\tvar pis = pi.NewCoAPInterceptor()\n\t// var pisOpt server.Opt\n\t// pisOpt = server.WithServerProtocolInterceptor(pis)\n\t// opt1 := server.WithCmdrServerOptions(pisOpt)\n\topt1 := server.WithCmdrServerProtocolInterceptor(pis)\n\topt2 := server.WithCmdrPort(0)\n\topt3 := server.WithCmdrCommandAction(server.DefaultLooper)\n\topt4 := server.WithCmdrUDPMode(true)\n\topt5 := server.WithCmdrPrefixPrefix(\"coap\")\n\n\tserver.AttachToCmdr(cmd, append(opts, opt1, opt2, opt3, opt4, opt5)...)\n\n\tserverCmdrOpt := cmdr.NewCmdFrom(cmd.ToCommand().FindSubCommand(\"server\"))\n\tcmdr.NewBool().\n\t\tTitles(\"dry-run\", \"dr\", \"dryrun\").\n\t\tDescription(\"In dry-run mode, arguments will be parsed, tcp listener will not be stared.\").\n\t\tGroup(\"zzz1.Dry Run\").\n\t\tAttachTo(serverCmdrOpt)\n\n\t// client\n\n\tvar pic = pi.NewCoAPClientInterceptor()\n\t//optCx1 := client.WithClientProtocolInterceptor(pic)\n\t//ox1 := client.WithCmdrClientOptions(optCx1)\n\tox1 := client.WithCmdrClientProtocolInterceptor(pic)\n\tox2 := client.WithCmdrPort(0)                                        // get ports configs from config file\n\tox3 := client.WithCmdrUDPMode(true)                                  // enable udp mode and loop (udpLoop)\n\tox4 := client.WithCmdrCommandAction(client.DefaultLooper)            // default internal looper\n\tox5 := client.WithCmdrMainLoop(pic.(client.MainLoopHolder).MainLoop) // coapMainLoop will block the main thread to exit to OS\n\tox6 := client.WithCmdrPrefixPrefix(\"coap\")                           // prefix of prefix is used for loading the coap section from config file\n\n\tclient.AttachToCmdr(cmd, ox1, ox2, ox3, ox4, ox5, ox6)\n\n\tclientCmdrOpt := cmdr.NewCmdFrom(cmd.ToCommand().FindSubCommand(\"client\"))\n\tcmdr.NewBool().\n\t\tTitles(\"dry-run\", \"dr\", \"dryrun\").\n\t\tDescription(\"In dry-run mode, arguments will be parsed, tcp listener will not be stared.\").\n\t\tGroup(\"zzz1.Testers\").\n\t\tAttachTo(clientCmdrOpt)\n\tcmdr.NewBool().\n\t\tTitles(\"try-debug\", \"try\").\n\t\tDescription(\"In try-debug mode, A continuous send/recv (to coap.me) processing will be started automatically.\").\n\t\tGroup(\"zzz1.Testers\").\n\t\tAttachTo(clientCmdrOpt)\n\tclientCmdrOpt.ToCommand().FindFlag(\"host\").DefaultValue = remoteCaliforniumEclipseOrg // remoteCaliforniumEclipseOrg\n}\n```\n\n## Contrib\n\nWelcome\n\n## LICENSE\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhedzr%2Fgo-socketlib","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhedzr%2Fgo-socketlib","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhedzr%2Fgo-socketlib/lists"}