Skip to content

Commit

Permalink
feat(codec): message compressed;routing codes
Browse files Browse the repository at this point in the history
  • Loading branch information
pipiaha committed May 30, 2023
1 parent c5789e7 commit 7eb5e41
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 12 deletions.
12 changes: 10 additions & 2 deletions conn/message/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ type Message struct {
ID uint // unique id, zero while notify mode
Route string // route for locating service
Data []byte // payload
compressed bool // is message compressed
Compressed bool // is message Compressed
Err bool // is an error message
}

Expand All @@ -92,7 +92,7 @@ func (m *Message) String() string {
types[m.Type],
m.ID,
m.Route,
m.compressed,
m.Compressed,
m.Err,
m.Data,
len(m.Data))
Expand Down Expand Up @@ -146,6 +146,14 @@ func GetDictionary() map[string]uint16 {
return dict
}

func GetDictCode(route string) uint16 {
return routes[route]
}

func GetDictRoute(code uint16) string {
return codes[code]
}

func (t *Type) String() string {
return types[*t]
}
4 changes: 2 additions & 2 deletions conn/message/message_decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (md *MessagesDecoder) Decode(data []byte) (*Message, error) {

if routable(m.Type) {
if flag&msgRouteCompressMask == 1 {
m.compressed = true
m.Compressed = true
code := binary.BigEndian.Uint16(data[offset:(offset + 2)])
routesCodesMutex.RLock()
route, ok := codes[code]
Expand All @@ -92,7 +92,7 @@ func (md *MessagesDecoder) Decode(data []byte) (*Message, error) {
m.Route = route
offset += 2
} else {
m.compressed = false
m.Compressed = false
rl := data[offset]
offset++
m.Route = string(data[offset:(offset + int(rl))])
Expand Down
4 changes: 2 additions & 2 deletions conn/message/message_encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func Decode(data []byte) (*Message, error) {

if routable(m.Type) {
if flag&msgRouteCompressMask == 1 {
m.compressed = true
m.Compressed = true
code := binary.BigEndian.Uint16(data[offset:(offset + 2)])
routesCodesMutex.RLock()
route, ok := codes[code]
Expand All @@ -174,7 +174,7 @@ func Decode(data []byte) (*Message, error) {
m.Route = route
offset += 2
} else {
m.compressed = false
m.Compressed = false
rl := data[offset]
offset++
m.Route = string(data[offset:(offset + int(rl))])
Expand Down
12 changes: 6 additions & 6 deletions conn/message/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ var encodeTables = map[string]struct {
"test_wrong_type": {&Message{Type: 0xff, Data: []byte{}}, nil, false, 0x0, ErrWrongMessageType},

"test_request_type": {&Message{Type: Request, Route: "a", Data: []uint8{}}, nil, false, 0x0, nil},
"test_request_type_compressed": {&Message{Type: Request, Route: "a", Data: []byte{}, compressed: true},
"test_request_type_compressed": {&Message{Type: Request, Route: "a", Data: []byte{}, Compressed: true},
map[string]uint16{"a": 1}, false, 0x0, nil},

"test_notify_type": {&Message{Type: Notify, Route: "a", Data: []byte{}}, nil, false, 0x0, nil},
"test_notify_type_compressed": {&Message{Type: Notify, Route: "a", Data: []byte{}, compressed: true},
"test_notify_type_compressed": {&Message{Type: Notify, Route: "a", Data: []byte{}, Compressed: true},
map[string]uint16{"a": 1}, false, 0x0, nil},

"test_push_type": {&Message{Type: Push, Route: "a", Data: []byte{}}, nil, false, 0x0, nil},
"test_push_type_compressed": {&Message{Type: Push, Route: "a", Data: []byte{}, compressed: true},
"test_push_type_compressed": {&Message{Type: Push, Route: "a", Data: []byte{}, Compressed: true},
map[string]uint16{"a": 1}, false, 0x0, nil},

"test_reponse_type": {&Message{Type: Response, Data: []byte{}}, nil, false, 0x0, nil},
Expand Down Expand Up @@ -100,15 +100,15 @@ var decodeTables = map[string]struct {
"test_wrong_type": {&Message{Type: 0xff, Data: []byte{}}, nil, false, 0x0, ErrWrongMessageType},

"test_request_type": {&Message{Type: Request, Route: "a", Data: []uint8{}}, nil, false, 0x0, nil},
"test_request_type_compressed": {&Message{Type: Request, Route: "a", Data: []byte{}, compressed: true},
"test_request_type_compressed": {&Message{Type: Request, Route: "a", Data: []byte{}, Compressed: true},
map[string]uint16{"a": 1}, false, 0x0, nil},

"test_notify_type": {&Message{Type: Notify, Route: "a", Data: []byte{}}, nil, false, 0x0, nil},
"test_notify_type_compressed": {&Message{Type: Notify, Route: "a", Data: []byte{}, compressed: true},
"test_notify_type_compressed": {&Message{Type: Notify, Route: "a", Data: []byte{}, Compressed: true},
map[string]uint16{"a": 1}, false, 0x0, nil},

"test_push_type": {&Message{Type: Push, Route: "a", Data: []byte{}}, nil, false, 0x0, nil},
"test_push_type_compressed": {&Message{Type: Push, Route: "a", Data: []byte{}, compressed: true},
"test_push_type_compressed": {&Message{Type: Push, Route: "a", Data: []byte{}, Compressed: true},
map[string]uint16{"a": 1}, false, 0x0, nil},

"test_reponse_type": {&Message{Type: Response, Data: []byte{}}, nil, false, 0x0, nil},
Expand Down

0 comments on commit 7eb5e41

Please sign in to comment.