/
tcp.go
54 lines (44 loc) · 1.13 KB
/
tcp.go
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
42
43
44
45
46
47
48
49
50
51
52
53
54
package urlconnection
import (
"net"
"net/url"
"time"
"golang.org/x/net/context"
)
type tcpConnection struct{}
/*
Connect creates a new TCP connection.
Expected format for the URL is tcp://[::1]:123 or tcp://[::1]/123.
*/
func (tcpConnection) Connect(ctx context.Context, dest *url.URL) (net.Conn, error) {
var host, port, hostport string
host = dest.Host
hostport = host
if len(dest.Path) > 0 {
port = dest.Path[1:]
hostport = net.JoinHostPort(host, port)
}
return net.Dial("tcp", hostport)
}
/*
ConnectTimeout creates a new TCP connection with a timeout for establishing.
Expected format for the URL is tcp://[::1]:123 or tcp://[::1]/123.
Abort attempt after "timeout" has expired.
*/
func (tcpConnection) ConnectTimeout(ctx context.Context, dest *url.URL,
timeout time.Duration) (net.Conn, error) {
var host, port, hostport string
host = dest.Host
hostport = host
if len(dest.Path) > 0 {
port = dest.Path[1:]
hostport = net.JoinHostPort(host, port)
}
return net.DialTimeout("tcp", hostport, timeout)
}
/*
Register the connection handler for TCP.
*/
func init() {
RegisterConnectionHandler("tcp", tcpConnection{})
}