forked from lesismal/nbio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
net_unix.go
54 lines (44 loc) · 1.01 KB
/
net_unix.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
// Copyright 2020 lesismal. All rights reserved.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
//go:build linux || darwin || netbsd || freebsd || openbsd || dragonfly
// +build linux darwin netbsd freebsd openbsd dragonfly
package nbio
import (
"errors"
"net"
"syscall"
)
func dupStdConn(conn net.Conn) (*Conn, error) {
sc, ok := conn.(interface {
SyscallConn() (syscall.RawConn, error)
})
if !ok {
return nil, errors.New("RawConn Unsupported")
}
rc, err := sc.SyscallConn()
if err != nil {
return nil, errors.New("RawConn Unsupported")
}
var newFd int
errCtrl := rc.Control(func(fd uintptr) {
newFd, err = syscall.Dup(int(fd))
})
if errCtrl != nil {
return nil, errCtrl
}
if err != nil {
return nil, err
}
conn.Close()
// err = syscall.SetNonblock(newFd, true)
// if err != nil {
// syscall.Close(newFd)
// return nil, err
// }
return &Conn{
fd: newFd,
lAddr: conn.LocalAddr(),
rAddr: conn.RemoteAddr(),
}, nil
}