tcpx
tcpx copied to clipboard
a cross-language tcp framwork in golang
A very convenient tcp framework in golang.
Start
go get github.com/fwhezfwhez/tcpx
Dependency
if you want to run program in this repo,you should prepare protoc,proto-gen-go environment.
It's good to compile yourself from these repos,but there is already release versions referring to their doc.
Make sure run protoc --version available.
protoc: https://github.com/golang/protobuf
proto-gen-go:https://github.com/golang/protobuf/tree/master/protoc-gen-go
Benchmark
https://github.com/fwhezfwhez/tcpx/blob/master/benchmark_test.go
| cases | exec times | cost time per loop | cost mem per loop | cost object num per loop | url |
|---|---|---|---|---|---|
| OnMessage | 2000000 | 643 ns/op | 1368 B/op | 5 allocs/op | click to location |
| Mux without middleware | 2000000 | 761 ns/op | 1368 B/op | 5 allocs/op | click to location |
| Mux with middleware | 2000000 | 768 ns/op | 1368 B/op | 5 allocs/op | click to location |
Pack
Tcpx has its well-designed pack. To focus on detail, you can refer to: https://github.com/fwhezfwhez/tcpx/tree/master/examples/modules/pack-detail
[4]byte -- length fixed_size,binary big endian encode
[4]byte -- messageID fixed_size,binary big endian encode
[4]byte -- headerLength fixed_size,binary big endian encode
[4]byte -- bodyLength fixed_size,binary big endian encode
[]byte -- header marshal by json
[]byte -- body marshal by marshaller
According to this pack rule, tcpx has 2 well-designed routing ways and their pack structure:
messageID type pack
header:
{
"Router-Type": "MESSAGE_ID"
}
urlPattern pack
header:
{
"Router-Type": "URL_PATTERN"
"Router-Pattern-Value": "/login/"
}
Chat
https://github.com/fwhezfwhez/tcpx/tree/master/examples/modules/chat
It examples a chat using tcpx.
Raw
https://github.com/fwhezfwhez/tcpx/tree/master/examples/modules/raw
It examples how to send stream without rule, nothing to do with messageID/urlPattern system. You can send all stream you want. Global middleware and anchor middleware are still working as the example said.
IM
Here is an example of IM system using tcpx.
https://github.com/q1n9-jair/tcpx-demo
Product practice

