package sysfs
import (
"net"
"os"
experimentalsys "github.com/tetratelabs/wazero/experimental/sys"
"github.com/tetratelabs/wazero/internal/fsapi"
socketapi "github.com/tetratelabs/wazero/internal/sock"
"github.com/tetratelabs/wazero/sys"
)
func NewTCPListenerFile (tl *net .TCPListener ) socketapi .TCPSock {
return newTCPListenerFile (tl )
}
type baseSockFile struct {
experimentalsys .UnimplementedFile
}
var _ experimentalsys .File = (*baseSockFile )(nil )
func (*baseSockFile ) IsDir () (bool , experimentalsys .Errno ) {
return false , 0
}
func (f *baseSockFile ) Stat () (fs sys .Stat_t , errno experimentalsys .Errno ) {
fs .Mode = os .ModeIrregular
return
}
var _ socketapi .TCPSock = (*tcpListenerFile )(nil )
type tcpListenerFile struct {
baseSockFile
tl *net .TCPListener
closed bool
nonblock bool
}
func newDefaultTCPListenerFile(tl *net .TCPListener ) socketapi .TCPSock {
return &tcpListenerFile {tl : tl }
}
func (f *tcpListenerFile ) Close () experimentalsys .Errno {
if !f .closed {
return experimentalsys .UnwrapOSError (f .tl .Close ())
}
return 0
}
func (f *tcpListenerFile ) Addr () *net .TCPAddr {
return f .tl .Addr ().(*net .TCPAddr )
}
func (f *tcpListenerFile ) IsNonblock () bool {
return f .nonblock
}
func (f *tcpListenerFile ) Poll (flag fsapi .Pflag , timeoutMillis int32 ) (ready bool , errno experimentalsys .Errno ) {
return false , experimentalsys .ENOSYS
}
var _ socketapi .TCPConn = (*tcpConnFile )(nil )
type tcpConnFile struct {
baseSockFile
tc *net .TCPConn
nonblock bool
closed bool
}
func newTcpConn(tc *net .TCPConn ) socketapi .TCPConn {
return &tcpConnFile {tc : tc }
}
func (f *tcpConnFile ) Read (buf []byte ) (n int , errno experimentalsys .Errno ) {
if len (buf ) == 0 {
return 0 , 0
}
if nonBlockingFileReadSupported && f .IsNonblock () {
n , errno = syscallConnControl (f .tc , func (fd uintptr ) (int , experimentalsys .Errno ) {
n , err := readSocket (fd , buf )
errno = experimentalsys .UnwrapOSError (err )
errno = fileError (f , f .closed , errno )
return n , errno
})
} else {
n , errno = read (f .tc , buf )
}
if errno != 0 {
errno = fileError (f , f .closed , errno )
}
return
}
func (f *tcpConnFile ) Write (buf []byte ) (n int , errno experimentalsys .Errno ) {
if nonBlockingFileWriteSupported && f .IsNonblock () {
return syscallConnControl (f .tc , func (fd uintptr ) (int , experimentalsys .Errno ) {
n , err := writeSocket (fd , buf )
errno = experimentalsys .UnwrapOSError (err )
errno = fileError (f , f .closed , errno )
return n , errno
})
} else {
n , errno = write (f .tc , buf )
}
if errno != 0 {
errno = fileError (f , f .closed , errno )
}
return
}
func (f *tcpConnFile ) Recvfrom (p []byte , flags int ) (n int , errno experimentalsys .Errno ) {
if flags != MSG_PEEK {
errno = experimentalsys .EINVAL
return
}
return syscallConnControl (f .tc , func (fd uintptr ) (int , experimentalsys .Errno ) {
n , err := recvfrom (fd , p , MSG_PEEK )
errno = experimentalsys .UnwrapOSError (err )
errno = fileError (f , f .closed , errno )
return n , errno
})
}
func (f *tcpConnFile ) Close () experimentalsys .Errno {
return f .close ()
}
func (f *tcpConnFile ) close () experimentalsys .Errno {
if f .closed {
return 0
}
f .closed = true
return f .Shutdown (socketapi .SHUT_RDWR )
}
func (f *tcpConnFile ) SetNonblock (enabled bool ) (errno experimentalsys .Errno ) {
f .nonblock = enabled
_, errno = syscallConnControl (f .tc , func (fd uintptr ) (int , experimentalsys .Errno ) {
return 0 , experimentalsys .UnwrapOSError (setNonblockSocket (fd , enabled ))
})
return
}
func (f *tcpConnFile ) IsNonblock () bool {
return f .nonblock
}
func (f *tcpConnFile ) Poll (flag fsapi .Pflag , timeoutMillis int32 ) (ready bool , errno experimentalsys .Errno ) {
return false , experimentalsys .ENOSYS
}
The pages are generated with Golds v0.8.2 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .