-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdial.go
More file actions
189 lines (158 loc) · 3.63 KB
/
Copy pathdial.go
File metadata and controls
189 lines (158 loc) · 3.63 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package connaxis
import (
"errors"
"net"
"os"
"reflect"
"sync"
"syscall"
"unsafe"
"github.com/bernardhu/connaxis/connection"
"github.com/bernardhu/connaxis/internal/tls"
"github.com/bernardhu/connaxis/wrapper"
"golang.org/x/sys/unix"
)
type IDialWatch interface {
OnUpdate(bool)
}
type Dialer struct {
cid uint64
key string //大类
subkey string
typ string
f *os.File
network string
addr string
fd int
bufs [][]byte
lastFlush int64
lastProbe int64
bufLen int
lock sync.Mutex
local net.Addr
remote net.Addr
watch IDialWatch
underlying connection.EngineConn
closeCb func(key, addr string, id uint64)
onCb func(key, addr string, id uint64)
probeCb func(string) []byte
score int
}
func (d *Dialer) bootup(p *DialParam) error {
conn, err := d.dial(p)
if err != nil {
wrapper.Debugf("Dialer %s add err:%v", p.Addr, err)
return err
}
d.local = conn.LocalAddr()
d.remote = conn.RemoteAddr()
if err := d.system(conn); err != nil {
wrapper.Debugf("Dialer %s system err:%v", p.Addr, err)
return err
}
if err := unix.SetsockoptInt(d.fd, unix.SOL_SOCKET, unix.SO_SNDBUF, 1024*1024); err != nil {
_ = unix.Close(d.fd)
wrapper.Errorf("bootup set fd:%d SO_SNDBUF 1M err :%v", d.fd, err)
return err
}
return nil
}
func (d *Dialer) dial(p *DialParam) (net.Conn, error) {
var err error
d.network, d.addr, err = parseAddr(p.Addr)
if err != nil {
return nil, err
}
d.onCb = p.On
d.closeCb = p.Close
d.probeCb = p.Probe
var conn net.Conn
if d.network != "tcp" {
return conn, errors.New("unsupported network: only tcp is supported")
}
if p.SslType == connection.TYPE_CONN_TLS || p.SslType == connection.TYPE_CONN {
wrapper.Debugf("dial conn")
_, err := net.ResolveTCPAddr("tcp", d.addr)
if err != nil {
return conn, err
}
conn, err = net.DialTimeout("tcp", d.addr, p.Duration) //net.DialTCP("tcp", nil, ra)
if err != nil {
return conn, err
}
} else {
return conn, errors.New("not supported")
}
return conn, nil
}
// system takes the net listener and detaches it from it's parent
// event loop, grabs the file descriptor, and makes it non-blocking.
func (d *Dialer) system(con net.Conn) error {
var err error
switch conn := con.(type) {
case *net.TCPConn:
d.f, err = conn.File()
conn.Close()
case *tls.Conn:
netln := reflect.ValueOf(conn).Elem().FieldByName("conn")
netln = reflect.NewAt(netln.Type(), unsafe.Pointer(netln.UnsafeAddr())).Elem()
netconn := netln.Interface().(*net.TCPConn)
d.f, err = netconn.File()
netconn.Close()
default:
return errors.New("unsupport connection type")
}
if err != nil {
if d.fd != 0 {
d.f.Close()
d.fd = 0
}
return err
}
d.fd = int(d.f.Fd())
return syscall.SetNonblock(d.fd, true)
}
func (d *Dialer) Underlying() connection.EngineConn {
return d.underlying
}
func (d *Dialer) SetWatcher(watch IDialWatch) {
d.watch = watch
}
func (d *Dialer) GetID() uint64 {
return d.cid
}
func (d *Dialer) Addr() string {
return d.addr
}
func (d *Dialer) AddBuf(buf []byte) int {
d.lock.Lock()
d.bufs = append(d.bufs, buf)
d.bufLen = len(d.bufs)
d.lock.Unlock()
return d.bufLen
}
func (d *Dialer) Flush() error {
if d.bufLen > 1 {
return d.flushVec()
} else if d.bufLen == 1 {
return d.flushOne()
}
return nil
}
func (d *Dialer) BufLen() int {
return d.bufLen
}
func (d *Dialer) LastFlush() int64 {
return d.lastFlush
}
func (d *Dialer) Close() {
if d.watch != nil {
d.watch.OnUpdate(false)
}
if d.closeCb != nil {
d.closeCb(d.key, d.addr, d.cid)
}
d.f.Close()
d.f = nil
wrapper.Debugf("close dailer:%d fd:%d %s:%s:%s", d.cid, d.fd, d.network, d.addr, d.key)
}