transport_tcp.go 797 字节
Newer Older
S
stormgbs 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
package dbus

import (
	"errors"
	"net"
)

func init() {
	transports["tcp"] = newTcpTransport
}

func tcpFamily(keys string) (string, error) {
	switch getKey(keys, "family") {
	case "":
		return "tcp", nil
	case "ipv4":
		return "tcp4", nil
	case "ipv6":
		return "tcp6", nil
	default:
		return "", errors.New("dbus: invalid tcp family (must be ipv4 or ipv6)")
	}
}

func newTcpTransport(keys string) (transport, error) {
	host := getKey(keys, "host")
	port := getKey(keys, "port")
	if host == "" || port == "" {
		return nil, errors.New("dbus: unsupported address (must set host and port)")
	}

	protocol, err := tcpFamily(keys)
	if err != nil {
		return nil, err
	}
	socket, err := net.Dial(protocol, net.JoinHostPort(host, port))
	if err != nil {
		return nil, err
	}
	return NewConn(socket)
}