[TOC] ## 安装 ### protobuf库 `go get github.com/golang/protobuf/proto ` #### 性能测试 这里只是简单的用go test测试了一下 ``` //goprotobuf "编码":447ns/op "解码":422ns/op //gogoprotobuf-go "编码":433ns/op "解码":427ns/op //gogoprotobuf-fast "编码":112ns/op "解码":112ns/op ``` ## 存在粘包的tcp 核心代码 ``` msg := &protogo.Msg{To: "hello", From: "word"} // 序列化为 bytes bytes, e := proto.Marshal(msg) if e != nil { panic(bytes) } //反序列化为结构体 msg2 :=&protogo.Msg{} e = proto.Unmarshal(bytes, msg2) if e != nil { panic(e) } fmt.Printf("%+v\n", msg2) ``` 目录 ``` protobuf_dmeo ├── go.mod ├── proto │ ├── test.pb.go │ └── test.proto ├── server.go └──client.go ``` <details> <summary>server.go</summary> ``` package main import ( "fmt" "net" "os" stProto "protobuf_dmeo/proto" //protobuf编解码库,下面两个库是相互兼容的,可以使用其中任意一个 "github.com/golang/protobuf/proto" //"github.com/gogo/protobuf/proto" ) func main() { //监听 listener, err := net.Listen("tcp", "localhost:6600") if err != nil { panic(err) } for { conn, err := listener.Accept() if err != nil { panic(err) } fmt.Println("new connect", conn.RemoteAddr()) go readMessage(conn) } } //接收消息 func readMessage(conn net.Conn) { defer conn.Close() buf := make([]byte, 4096, 4096) for { //读消息 cnt, err := conn.Read(buf) if err != nil { panic(err) } stReceive := &stProto.UserInfo{} pData := buf[:cnt] //protobuf解码 err = proto.Unmarshal(pData, stReceive) if err != nil { panic(err) } fmt.Println("receive", conn.RemoteAddr(), stReceive) if stReceive.Message == "stop" { os.Exit(1) } } } ``` </details> <br/> <details> <summary>client.go</summary> ``` package main import ( "bufio" "fmt" "net" "os" stProto "protobuf_dmeo/proto" "time" //protobuf编解码库,下面两个库是相互兼容的,可以使用其中任意一个 "github.com/golang/protobuf/proto" //"github.com/gogo/protobuf/proto" ) func main() { strIP := "localhost:6600" var conn net.Conn var err error //连接服务器 for conn, err = net.Dial("tcp", strIP); err != nil; conn, err = net.Dial("tcp", strIP) { fmt.Println("connect", strIP, "fail") time.Sleep(time.Second) fmt.Println("reconnect...") } fmt.Println("connect", strIP, "success") defer conn.Close() //发送消息 cnt := 0 sender := bufio.NewScanner(os.Stdin) for sender.Scan() { cnt++ stSend := &stProto.UserInfo{ Message: sender.Text(), Length: *proto.Int(len(sender.Text())), Cnt: *proto.Int(cnt), } //protobuf编码 pData, err := proto.Marshal(stSend) if err != nil { panic(err) } //发送 conn.Write(pData) if sender.Text() == "stop" { return } } } ``` </details> <br/> <details> <summary>proto/test.proto</summary> ``` syntax = "proto3"; //指定版本,必须要写(proto3、proto2) package proto; enum FOO { X = 0; }; //message是固定的。UserInfo是类名,可以随意指定,符合规范即可 message UserInfo{ string message = 1; //消息 int32 length = 2; //消息大小 int32 cnt = 3; //消息计数 } ``` </details> <br/> 运行 ``` cd protoc protoc --go_out=. test.proto cd ../ go run server.go go run client.go ```