aboutsummaryrefslogtreecommitdiffstats
path: root/codec
diff options
context:
space:
mode:
authorOndrej Fabry <ofabry@cisco.com>2020-06-16 10:40:34 +0200
committerOndrej Fabry <ofabry@cisco.com>2020-06-16 10:40:34 +0200
commit280b1c6c83b676ef4e592f4ecf60cb5b54b6a753 (patch)
treebf9a35f020de061ba66a432411ee44866405fe76 /codec
parentf049390060630c0085fe4ad683c83a4a14a47ffb (diff)
Optimize socketclient adapter and add various code improvements
This commit includes: Features - optimized [socketclient](adapter/socketclient) adapter and add method to set client name - added list of compatible messages to `CompatibilityError` Fixes - `MsgCodec` will recover panic occurring during a message decoding - calling `Unsubscibe` will close the notification channel Other - improved log messages to provide more relevant info Examples - added more code samples of working with unions in [union example](examples/union-example) - added profiling mode to [perf bench](examples/perf-bench) example - improved [simple client](examples/simple-client) example to work properly even with multiple runs Dependencies - updated `github.com/sirupsen/logrus` dep to `v1.6.0` - updated `github.com/lunixbochs/struc` dep to `v0.0.0-20200521075829-a4cb8d33dbbe` Change-Id: I136a3968ccf9e93760d7ee2b9902fc7e6390a09d Signed-off-by: Ondrej Fabry <ofabry@cisco.com>
Diffstat (limited to 'codec')
-rw-r--r--codec/msg_codec.go20
1 files changed, 16 insertions, 4 deletions
diff --git a/codec/msg_codec.go b/codec/msg_codec.go
index 67628a4..3f60cae 100644
--- a/codec/msg_codec.go
+++ b/codec/msg_codec.go
@@ -20,8 +20,9 @@ import (
"fmt"
"reflect"
- "git.fd.io/govpp.git/api"
"github.com/lunixbochs/struc"
+
+ "git.fd.io/govpp.git/api"
)
// MsgCodec provides encoding and decoding functionality of `api.Message` structs into/from
@@ -65,7 +66,7 @@ func (*MsgCodec) EncodeMsg(msg api.Message, msgID uint16) (data []byte, err erro
if err, ok = r.(error); !ok {
err = fmt.Errorf("%v", r)
}
- err = fmt.Errorf("panic occurred: %v", err)
+ err = fmt.Errorf("panic occurred during encoding message %s: %v", msg.GetMessageName(), err)
}
}()
@@ -101,11 +102,22 @@ func (*MsgCodec) EncodeMsg(msg api.Message, msgID uint16) (data []byte, err erro
}
// DecodeMsg decodes binary-encoded data of a message into provided `Message` structure.
-func (*MsgCodec) DecodeMsg(data []byte, msg api.Message) error {
+func (*MsgCodec) DecodeMsg(data []byte, msg api.Message) (err error) {
if msg == nil {
return errors.New("nil message passed in")
}
+ // try to recover panic which might possibly occur
+ defer func() {
+ if r := recover(); r != nil {
+ var ok bool
+ if err, ok = r.(error); !ok {
+ err = fmt.Errorf("%v", r)
+ }
+ err = fmt.Errorf("panic occurred during decoding message %s: %v", msg.GetMessageName(), err)
+ }
+ }()
+
var header interface{}
// check which header is expected
@@ -123,7 +135,7 @@ func (*MsgCodec) DecodeMsg(data []byte, msg api.Message) error {
buf := bytes.NewReader(data)
// decode message header
- if err := struc.Unpack(buf, header); err != nil {
+ if err = struc.Unpack(buf, header); err != nil {
return fmt.Errorf("failed to decode message header: %+v, error: %v", header, err)
}