Skip to content
This repository has been archived by the owner on Jan 6, 2022. It is now read-only.

Commit

Permalink
Implement NewConn
Browse files Browse the repository at this point in the history
Step 1 for fixing #18.
  • Loading branch information
guelfey committed May 10, 2013
1 parent 4cc2ba7 commit 18c531d
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 5 deletions.
20 changes: 15 additions & 5 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,23 @@ func SystemBus() (conn *Conn, err error) {

// Dial establishes a new connection to the message bus specified by address.
func Dial(address string) (*Conn, error) {
var err error
conn := new(Conn)
conn.transport, err = getTransport(address)
tr, err := getTransport(address)
if err != nil {
return nil, err
}
if err = conn.auth(); err != nil {
return newConn(tr)
}

// NewConn creates a new *Conn from an already established connection.
func NewConn(conn io.ReadWriteCloser) (*Conn, error) {
return newConn(genericTransport{conn})
}

// newConn creates a new *Conn from a transport.
func newConn(tr transport) (*Conn, error) {
conn := new(Conn)
conn.transport = tr
if err := conn.auth(); err != nil {
conn.transport.Close()
return nil, err
}
Expand All @@ -123,7 +133,7 @@ func Dial(address string) (*Conn, error) {
go conn.inWorker()
go conn.outWorker()
go conn.serials()
if err = conn.hello(); err != nil {
if err := conn.hello(); err != nil {
conn.transport.Close()
return nil, err
}
Expand Down
34 changes: 34 additions & 0 deletions transport_generic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package dbus

import (
"errors"
"io"
)

type genericTransport struct {
io.ReadWriteCloser
}

func (t genericTransport) SendNullByte() error {
_, err := t.Write([]byte{0})
return err
}

func (t genericTransport) SupportsUnixFDs() bool {
return false
}

func (t genericTransport) EnableUnixFDs() {}

func (t genericTransport) ReadMessage() (*Message, error) {
return DecodeMessage(t)
}

func (t genericTransport) SendMessage(msg *Message) error {
for _, v := range msg.Body {
if _, ok := v.(UnixFD); ok {
return errors.New("unix fd passing not enabled")
}
}
return msg.EncodeTo(t)
}

0 comments on commit 18c531d

Please sign in to comment.