Skip to content
This repository has been archived by the owner on Jun 5, 2021. It is now read-only.

Commit

Permalink
message: implement binary and gob encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
ernado committed Nov 1, 2020
1 parent 2abc5aa commit a2af8aa
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
26 changes: 26 additions & 0 deletions message.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,32 @@ type Message struct {
Raw []byte
}

// MarshalBinary implements the encoding.BinaryMarshaler interface.
func (m Message) MarshalBinary() (data []byte, err error) {
// We can't return m.Raw, allocation is expected by implicit interface
// contract induced by other implementations.
b := make([]byte, len(m.Raw))
copy(b, m.Raw)
return b, nil
}

// UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.
func (m *Message) UnmarshalBinary(data []byte) error {
// We can't retain data, copy is expected by interface contract.
m.Raw = append(m.Raw[:0], data...)
return m.Decode()
}

// GobEncode implements the gob.GobEncoder interface.
func (m Message) GobEncode() ([]byte, error) {
return m.MarshalBinary()
}

// GobDecode implements the gob.GobDecoder interface.
func (m *Message) GobDecode(data []byte) error {
return m.UnmarshalBinary(data)
}

// AddTo sets b.TransactionID to m.TransactionID.
//
// Implements Setter to aid in crafting responses.
Expand Down
29 changes: 29 additions & 0 deletions message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1008,3 +1008,32 @@ func BenchmarkDecode(b *testing.B) {
}
}
}

func TestMessage_MarshalBinary(t *testing.T) {
m := MustBuild(
NewSoftware("software"),
&XORMappedAddress{
IP: net.IPv4(213, 1, 223, 5),
},
)
data, err := m.MarshalBinary()
if err != nil {
t.Fatal(err)
}

// Reset m.Raw to check retention.
for i := range m.Raw {
m.Raw[i] = 0
}
if err := m.UnmarshalBinary(data); err != nil {
t.Fatal(err)
}

// Reset data to check retention.
for i := range data {
data[i] = 0
}
if err := m.Decode(); err != nil {
t.Fatal(err)
}
}

0 comments on commit a2af8aa

Please sign in to comment.