// 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 }