Skip to content

Commit

Permalink
Use protobuf to encode/decode session metadata (breaking change)
Browse files Browse the repository at this point in the history
Signed-off-by: Yilun <[email protected]>
  • Loading branch information
yilunzhang committed Oct 17, 2020
1 parent 307be55 commit 086809e
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 2 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.PHONY: pb
pb:
protoc --go_out=. pb/*.proto
24 changes: 22 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ import (
"sync"
"time"

"github.com/golang/protobuf/proto"
ncp "github.com/nknorg/ncp-go"
nkn "github.com/nknorg/nkn-sdk-go"
"github.com/nknorg/nkn-tuna-session/pb"
"github.com/nknorg/tuna"
gocache "github.com/patrickmn/go-cache"
)
Expand Down Expand Up @@ -288,12 +290,20 @@ func (c *TunaSessionClient) listenNet(i int) {
return
}

sessionID, err := c.decode(buf, remoteAddr)
metadataRaw, err := c.decode(buf, remoteAddr)
if err != nil {
log.Printf("Decode message error: %v", err)
return
}

metadata := &pb.SessionMetadata{}
err = proto.Unmarshal(metadataRaw, metadata)
if err != nil {
log.Printf("Decode session metadata error: %v", err)
return
}

sessionID := metadata.Id
sessKey := sessionKey(remoteAddr, sessionID)

c.Lock()
Expand Down Expand Up @@ -456,7 +466,17 @@ func (c *TunaSessionClient) DialWithConfig(remoteAddr string, config *DialConfig
return
}

buf, err := c.encode(sessionID, remoteAddr)
metadata := &pb.SessionMetadata{
Id: sessionID,
}
metadataRaw, err := proto.Marshal(metadata)
if err != nil {
log.Printf("Encode session metadata error: %v", err)
conn.Close()
return
}

buf, err := c.encode(metadataRaw, remoteAddr)
if err != nil {
log.Printf("Encode message error: %v", err)
conn.Close()
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/nknorg/nkn-tuna-session
go 1.13

require (
github.com/golang/protobuf v1.4.1
github.com/imdario/mergo v0.3.9
github.com/nknorg/ncp-go v1.0.3
github.com/nknorg/nkn-sdk-go v1.3.3
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ github.com/golang/mock v1.3.1/go.mod h1:sBzyDLLjw3U8JLTeZvSv8jJB+tU5PVekmnlKIyFU
github.com/golang/mock v1.4.0/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw=
github.com/golang/mock v1.4.1/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.1 h1:YF8+flBXS5eO826T4nzqPrxfhQThhXl0YzfuUPu4SBg=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
Expand Down Expand Up @@ -523,6 +524,8 @@ golang.org/x/tools v0.0.0-20200304193943-95d2e580d8eb/go.mod h1:o4KQGtdN14AW+yjs
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/api v0.3.1/go.mod h1:6wY9I6uQWHQ8EM57III9mq/AjF+i8G65rmVagqKMtkk=
google.golang.org/api v0.4.0/go.mod h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEtwE=
Expand Down
73 changes: 73 additions & 0 deletions pb/session.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions pb/session.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
syntax = "proto3";

package pb;

message SessionMetadata {
bytes id = 1;
}

0 comments on commit 086809e

Please sign in to comment.