Skip to content

Commit

Permalink
encoder: Add message.EncodeToWithFDs and DecodeMessageWithFDs
Browse files Browse the repository at this point in the history
Also, restore functionality to message.EncodeTo and DecodeMessage
This maintains compatibility with API v5
  • Loading branch information
wdouglass authored and jsouthworth committed Sep 8, 2021
1 parent e5edbf7 commit 84a2dfb
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 27 deletions.
2 changes: 1 addition & 1 deletion conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ func TestCloseBeforeSignal(t *testing.T) {
FieldPath: MakeVariant(ObjectPath("/baz")),
},
}
_, err = msg.EncodeTo(pipewriter, binary.LittleEndian)
err = msg.EncodeTo(pipewriter, binary.LittleEndian)
if err != nil {
t.Fatal(err)
}
Expand Down
4 changes: 2 additions & 2 deletions decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ func TestDecodeArrayEmptyStruct(t *testing.T) {
},
serial: 0x00000003,
}
_, err := msg.EncodeTo(buf, binary.LittleEndian)
err := msg.EncodeTo(buf, binary.LittleEndian)
if err != nil {
t.Fatal(err)
}
msg, err = DecodeMessage(buf, make([]int, 0))
msg, err = DecodeMessage(buf)
if err != nil {
t.Fatal(err)
}
Expand Down
27 changes: 18 additions & 9 deletions message.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,7 @@ type header struct {
Variant
}

// DecodeMessage tries to decode a single message in the D-Bus wire format
// from the given reader. The byte order is figured out from the first byte.
// The possibly returned error can be an error of the underlying reader, an
// InvalidMessageError or a FormatError.
func DecodeMessage(rd io.Reader, fds []int) (msg *Message, err error) {
func DecodeMessageWithFDs(rd io.Reader, fds []int) (msg *Message, err error) {
var order binary.ByteOrder
var hlength, length uint32
var typ, flags, proto byte
Expand Down Expand Up @@ -207,6 +203,14 @@ func DecodeMessage(rd io.Reader, fds []int) (msg *Message, err error) {
return
}

// DecodeMessage tries to decode a single message in the D-Bus wire format
// from the given reader. The byte order is figured out from the first byte.
// The possibly returned error can be an error of the underlying reader, an
// InvalidMessageError or a FormatError.
func DecodeMessage(rd io.Reader) (msg *Message, err error) {
return DecodeMessageWithFDs(rd, make([]int, 0));
}

type nullwriter struct{}

func (nullwriter) Write(p []byte) (cnt int, err error) {
Expand All @@ -222,10 +226,7 @@ func (msg *Message) CountFds() (int, error) {
return len(enc.fds), err
}

// EncodeTo encodes and sends a message to the given writer. The byte order must
// be either binary.LittleEndian or binary.BigEndian. If the message is not
// valid or an error occurs when writing, an error is returned.
func (msg *Message) EncodeTo(out io.Writer, order binary.ByteOrder) (fds []int, err error) {
func (msg *Message) EncodeToWithFDs(out io.Writer, order binary.ByteOrder) (fds []int, err error) {
if err := msg.IsValid(); err != nil {
return make([]int, 0), err
}
Expand Down Expand Up @@ -274,6 +275,14 @@ func (msg *Message) EncodeTo(out io.Writer, order binary.ByteOrder) (fds []int,
return enc.fds, nil
}

// EncodeTo encodes and sends a message to the given writer. The byte order must
// be either binary.LittleEndian or binary.BigEndian. If the message is not
// valid or an error occurs when writing, an error is returned.
func (msg *Message) EncodeTo(out io.Writer, order binary.ByteOrder) (err error) {
_, err = msg.EncodeToWithFDs(out, order);
return err;
}

// IsValid checks whether msg is a valid message and returns an
// InvalidMessageError if it is not.
func (msg *Message) IsValid() error {
Expand Down
16 changes: 8 additions & 8 deletions proto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,11 +253,11 @@ func TestMessage(t *testing.T) {
FieldMember: MakeVariant("baz"),
}
message.Body = make([]interface{}, 0)
_, err := message.EncodeTo(buf, binary.LittleEndian)
err := message.EncodeTo(buf, binary.LittleEndian)
if err != nil {
t.Error(err)
}
_, err = DecodeMessage(buf, make([]int, 0))
_, err = DecodeMessage(buf)
if err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -317,15 +317,15 @@ func BenchmarkDecodeMessageSmall(b *testing.B) {

b.StopTimer()
buf := new(bytes.Buffer)
_, err = smallMessage.EncodeTo(buf, binary.LittleEndian)
err = smallMessage.EncodeTo(buf, binary.LittleEndian)
if err != nil {
b.Fatal(err)
}
decoded := buf.Bytes()
b.StartTimer()
for i := 0; i < b.N; i++ {
rd = bytes.NewReader(decoded)
_, err = DecodeMessage(rd, make([]int, 0))
_, err = DecodeMessage(rd)
if err != nil {
b.Fatal(err)
}
Expand All @@ -338,15 +338,15 @@ func BenchmarkDecodeMessageBig(b *testing.B) {

b.StopTimer()
buf := new(bytes.Buffer)
_, err = bigMessage.EncodeTo(buf, binary.LittleEndian)
err = bigMessage.EncodeTo(buf, binary.LittleEndian)
if err != nil {
b.Fatal(err)
}
decoded := buf.Bytes()
b.StartTimer()
for i := 0; i < b.N; i++ {
rd = bytes.NewReader(decoded)
_, err = DecodeMessage(rd, make([]int, 0))
_, err = DecodeMessage(rd)
if err != nil {
b.Fatal(err)
}
Expand All @@ -356,7 +356,7 @@ func BenchmarkDecodeMessageBig(b *testing.B) {
func BenchmarkEncodeMessageSmall(b *testing.B) {
var err error
for i := 0; i < b.N; i++ {
_, err = smallMessage.EncodeTo(ioutil.Discard, binary.LittleEndian)
err = smallMessage.EncodeTo(ioutil.Discard, binary.LittleEndian)
if err != nil {
b.Fatal(err)
}
Expand All @@ -366,7 +366,7 @@ func BenchmarkEncodeMessageSmall(b *testing.B) {
func BenchmarkEncodeMessageBig(b *testing.B) {
var err error
for i := 0; i < b.N; i++ {
_, err = bigMessage.EncodeTo(ioutil.Discard, binary.LittleEndian)
err = bigMessage.EncodeTo(ioutil.Discard, binary.LittleEndian)
if err != nil {
b.Fatal(err)
}
Expand Down
5 changes: 2 additions & 3 deletions transport_generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (t genericTransport) SupportsUnixFDs() bool {
func (t genericTransport) EnableUnixFDs() {}

func (t genericTransport) ReadMessage() (*Message, error) {
return DecodeMessage(t, make([]int, 0))
return DecodeMessage(t)
}

func (t genericTransport) SendMessage(msg *Message) error {
Expand All @@ -48,6 +48,5 @@ func (t genericTransport) SendMessage(msg *Message) error {
if fds != 0 {
return errors.New("dbus: unix fd passing not enabled")
}
_, err = msg.EncodeTo(t, nativeEndian)
return err
return msg.EncodeTo(t, nativeEndian)
}
8 changes: 4 additions & 4 deletions transport_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func (t *unixTransport) ReadMessage() (*Message, error) {
if err != nil {
return nil, err
}
msg, err := DecodeMessage(bytes.NewBuffer(all), fds)
msg, err := DecodeMessageWithFDs(bytes.NewBuffer(all), fds)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -175,7 +175,7 @@ func (t *unixTransport) ReadMessage() (*Message, error) {
}
return msg, nil
}
return DecodeMessage(bytes.NewBuffer(all), make([]int, 0))
return DecodeMessage(bytes.NewBuffer(all))
}

func (t *unixTransport) SendMessage(msg *Message) error {
Expand All @@ -189,7 +189,7 @@ func (t *unixTransport) SendMessage(msg *Message) error {
}
msg.Headers[FieldUnixFDs] = MakeVariant(uint32(fdcnt))
buf := new(bytes.Buffer)
fds, err := msg.EncodeTo(buf, nativeEndian)
fds, err := msg.EncodeToWithFDs(buf, nativeEndian)
if err != nil {
return err
}
Expand All @@ -202,7 +202,7 @@ func (t *unixTransport) SendMessage(msg *Message) error {
return io.ErrShortWrite
}
} else {
if _, err := msg.EncodeTo(t, nativeEndian); err != nil {
if err := msg.EncodeTo(t, nativeEndian); err != nil {
return err
}
}
Expand Down

0 comments on commit 84a2dfb

Please sign in to comment.