Skip to content

Commit bfe82d6

Browse files
committed
net: add FileListener for WASI
Signed-off-by: Roman Volosatovs <roman@profian.com>
1 parent 28c56ec commit bfe82d6

File tree

2 files changed

+55
-3
lines changed

2 files changed

+55
-3
lines changed

src/net/fd_wasi.go

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package net
55

66
import (
7-
"io"
87
"os"
98
"syscall"
109
"time"
@@ -21,13 +20,13 @@ type netFD struct {
2120

2221
// Read implements the Conn Read method.
2322
func (c *netFD) Read(b []byte) (int, error) {
24-
// TODO: Handle EAGAIN and perform poll
23+
// TODO: Handle EAGAIN and perform poll
2524
return c.File.Read(b)
2625
}
2726

2827
// Write implements the Conn Write method.
2928
func (c *netFD) Write(b []byte) (int, error) {
30-
// TODO: Handle EAGAIN and perform poll
29+
// TODO: Handle EAGAIN and perform poll
3130
return c.File.Write(b)
3231
}
3332

@@ -55,3 +54,25 @@ func (*netFD) SetReadDeadline(t time.Time) error {
5554
func (*netFD) SetWriteDeadline(t time.Time) error {
5655
return ErrNotImplemented
5756
}
57+
58+
type listener struct {
59+
*os.File
60+
}
61+
62+
// Accept implements the Listener Accept method.
63+
func (l *listener) Accept() (Conn, error) {
64+
fd, err := syscall.SockAccept(int(l.File.Fd()), syscall.O_NONBLOCK)
65+
if err != nil {
66+
return nil, wrapSyscallError("sock_accept", err)
67+
}
68+
return &netFD{File: os.NewFile(uintptr(fd), "conn"), net: "file+net", laddr: nil, raddr: nil}, nil
69+
}
70+
71+
// Addr implements the Listener Addr method.
72+
func (*listener) Addr() Addr {
73+
return nil
74+
}
75+
76+
func fileListener(f *os.File) (Listener, error) {
77+
return &listener{File: f}, nil
78+
}

src/net/file.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//go:build wasi
2+
// +build wasi
3+
4+
// The following is copied from Go 1.18 official implementation.
5+
6+
// Copyright 2015 The Go Authors. All rights reserved.
7+
// Use of this source code is governed by a BSD-style
8+
// license that can be found in the LICENSE file.
9+
10+
package net
11+
12+
import (
13+
"os"
14+
)
15+
16+
type fileAddr string
17+
18+
func (fileAddr) Network() string { return "file+net" }
19+
func (f fileAddr) String() string { return string(f) }
20+
21+
// FileListener returns a copy of the network listener corresponding
22+
// to the open file f.
23+
// It is the caller's responsibility to close ln when finished.
24+
// Closing ln does not affect f, and closing f does not affect ln.
25+
func FileListener(f *os.File) (ln Listener, err error) {
26+
ln, err = fileListener(f)
27+
if err != nil {
28+
err = &OpError{Op: "file", Net: "file+net", Source: nil, Addr: fileAddr(f.Name()), Err: err}
29+
}
30+
return
31+
}

0 commit comments

Comments
 (0)