-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.go
53 lines (47 loc) · 1.11 KB
/
client.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
package netx
import (
"context"
"fmt"
"net"
"time"
)
//Client net client interface
type Client interface {
GetConnContext() ConnContext
Connect(timeout time.Duration) error
Close() error
}
//Client net Client
type _Client struct {
host string
netType string
connContext ConnContext
bootstrap *_bootstrap
}
//GetConnContext 获取该客户端的上下文
func (client *_Client) GetConnContext() ConnContext {
return client.connContext
}
//Connect 使用指定的方式连接指定的地址
func (client *_Client) Connect(timeout time.Duration) error {
var d net.Dialer
if timeout != 0 {
d = net.Dialer{Timeout: timeout}
}
conn, err := d.Dial(client.netType, client.host)
if err != nil {
return fmt.Errorf("connect出现错误:%s", err)
}
//TODO Timeout set
client.connContext, err = client.bootstrap.connection(conn)
return err
}
//Close 关闭 Client
func (client *_Client) Close() error {
if client.connContext != nil {
timeoutCxt, cancleFunc := context.WithTimeout(context.Background(), time.Second*30)
defer cancleFunc()
return client.connContext.Close(timeoutCxt)
}
return nil
}