55 lines
1.0 KiB
Go
Raw Normal View History

2021-12-04 16:42:11 +00:00
// 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()
2022-03-03 10:49:59 +00:00
// err = syscall.SetNonblock(newFd, true)
// if err != nil {
// syscall.Close(newFd)
// return nil, err
// }
2021-12-04 16:42:11 +00:00
return &Conn{
fd: newFd,
lAddr: conn.LocalAddr(),
rAddr: conn.RemoteAddr(),
}, nil
}