Skip to content

Commit 18c531d

Browse files
committed
Implement NewConn
Step 1 for fixing #18.
1 parent 4cc2ba7 commit 18c531d

File tree

2 files changed

+49
-5
lines changed

2 files changed

+49
-5
lines changed

conn.go

+15-5
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,23 @@ func SystemBus() (conn *Conn, err error) {
104104

105105
// Dial establishes a new connection to the message bus specified by address.
106106
func Dial(address string) (*Conn, error) {
107-
var err error
108-
conn := new(Conn)
109-
conn.transport, err = getTransport(address)
107+
tr, err := getTransport(address)
110108
if err != nil {
111109
return nil, err
112110
}
113-
if err = conn.auth(); err != nil {
111+
return newConn(tr)
112+
}
113+
114+
// NewConn creates a new *Conn from an already established connection.
115+
func NewConn(conn io.ReadWriteCloser) (*Conn, error) {
116+
return newConn(genericTransport{conn})
117+
}
118+
119+
// newConn creates a new *Conn from a transport.
120+
func newConn(tr transport) (*Conn, error) {
121+
conn := new(Conn)
122+
conn.transport = tr
123+
if err := conn.auth(); err != nil {
114124
conn.transport.Close()
115125
return nil, err
116126
}
@@ -123,7 +133,7 @@ func Dial(address string) (*Conn, error) {
123133
go conn.inWorker()
124134
go conn.outWorker()
125135
go conn.serials()
126-
if err = conn.hello(); err != nil {
136+
if err := conn.hello(); err != nil {
127137
conn.transport.Close()
128138
return nil, err
129139
}

transport_generic.go

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package dbus
2+
3+
import (
4+
"errors"
5+
"io"
6+
)
7+
8+
type genericTransport struct {
9+
io.ReadWriteCloser
10+
}
11+
12+
func (t genericTransport) SendNullByte() error {
13+
_, err := t.Write([]byte{0})
14+
return err
15+
}
16+
17+
func (t genericTransport) SupportsUnixFDs() bool {
18+
return false
19+
}
20+
21+
func (t genericTransport) EnableUnixFDs() {}
22+
23+
func (t genericTransport) ReadMessage() (*Message, error) {
24+
return DecodeMessage(t)
25+
}
26+
27+
func (t genericTransport) SendMessage(msg *Message) error {
28+
for _, v := range msg.Body {
29+
if _, ok := v.(UnixFD); ok {
30+
return errors.New("unix fd passing not enabled")
31+
}
32+
}
33+
return msg.EncodeTo(t)
34+
}

0 commit comments

Comments
 (0)