-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathnet_unix.go
44 lines (37 loc) · 875 Bytes
/
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
// Copyright (c) 2023 Remember
// 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
package easyio
import (
"context"
"errors"
"net"
"syscall"
)
func dupStdConn(netconn net.Conn) (Conn, error) {
defer netconn.Close()
sc, ok := netconn.(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
if err = rc.Control(func(fd uintptr) {
newFd, err = syscall.Dup(int(fd))
}); err != nil {
return nil, err
}
c := &conn{
fd: newFd,
localAddr: netconn.LocalAddr(),
rAddr: netconn.RemoteAddr(),
ctx: context.Background(),
}
return c, nil
}