diff --git a/conn_test.go b/conn_test.go index c9dc2e3..b73fddd 100644 --- a/conn_test.go +++ b/conn_test.go @@ -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) } diff --git a/decoder_test.go b/decoder_test.go index 665d398..2170b91 100644 --- a/decoder_test.go +++ b/decoder_test.go @@ -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) } diff --git a/message.go b/message.go index 338e5ba..dd86aff 100644 --- a/message.go +++ b/message.go @@ -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 @@ -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) { @@ -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 } @@ -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 { diff --git a/proto_test.go b/proto_test.go index 4638aa2..bfd0dfc 100644 --- a/proto_test.go +++ b/proto_test.go @@ -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) } @@ -317,7 +317,7 @@ 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) } @@ -325,7 +325,7 @@ func BenchmarkDecodeMessageSmall(b *testing.B) { 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) } @@ -338,7 +338,7 @@ 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) } @@ -346,7 +346,7 @@ func BenchmarkDecodeMessageBig(b *testing.B) { 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) } @@ -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) } @@ -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) } diff --git a/transport_generic.go b/transport_generic.go index aeb2be8..a08e281 100644 --- a/transport_generic.go +++ b/transport_generic.go @@ -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 { @@ -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) } diff --git a/transport_unix.go b/transport_unix.go index 5f73cb5..2212e7f 100644 --- a/transport_unix.go +++ b/transport_unix.go @@ -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 } @@ -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 { @@ -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 } @@ -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 } }