-
Notifications
You must be signed in to change notification settings - Fork 229
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #47 from pck/dial_tcp_transport
Add support for Dial("tcp:host=..,port=..")
- Loading branch information
Showing
4 changed files
with
88 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
//+build !windows | ||
|
||
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package dbus | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"testing" | ||
) | ||
|
||
func TestTcpConnection(t *testing.T) { | ||
listener, err := net.Listen("tcp", ":0") | ||
if err != nil { | ||
t.Fatal("Failed to create listener") | ||
} | ||
host, port, err := net.SplitHostPort(listener.Addr().String()) | ||
if err != nil { | ||
t.Fatal("Failed to parse host/port") | ||
} | ||
|
||
conn, err := Dial(fmt.Sprintf("tcp:host=%s,port=%s", host, port)) | ||
if err != nil { | ||
t.Error("Expected no error, got", err) | ||
} | ||
if conn == nil { | ||
t.Error("Expected connection, got nil") | ||
} | ||
} |