Files
go-libp2p/p2p/transport/tcpreuse/internal/sampledconn/sampledconn_unix.go
Adin Schmahmann 5a47a90938 feat(tcpreuse): add options for sharing TCP listeners amongst TCP, WS and WSS transports (#2984)
Allows the same socket to be shared amongst TCP,WS,WSS transports.

---------

Co-authored-by: sukun <sukunrt@gmail.com>
Co-authored-by: Marco Munizaga <git@marcopolo.io>
2024-11-04 09:41:32 -08:00

43 lines
669 B
Go

//go:build unix
package sampledconn
import (
"errors"
"syscall"
)
func OSPeekConn(conn syscall.Conn) (PeekedBytes, error) {
s := PeekedBytes{}
rawConn, err := conn.SyscallConn()
if err != nil {
return s, err
}
readBytes := 0
var readErr error
err = rawConn.Read(func(fd uintptr) bool {
for readBytes < peekSize {
var n int
n, _, readErr = syscall.Recvfrom(int(fd), s[readBytes:], syscall.MSG_PEEK)
if errors.Is(readErr, syscall.EAGAIN) {
return false
}
if readErr != nil {
return true
}
readBytes += n
}
return true
})
if readErr != nil {
return s, readErr
}
if err != nil {
return s, err
}
return s, nil
}