-
Notifications
You must be signed in to change notification settings - Fork 1
/
socket.go
68 lines (60 loc) · 1.84 KB
/
socket.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package water
import (
"fmt"
"net"
"os"
)
// InsertConn implements Core.
func (c *core) InsertConn(conn net.Conn) (fd int32, err error) {
if c.instance == nil {
return 0, fmt.Errorf("water: cannot insert TCPConn before instantiation")
}
switch conn := conn.(type) {
case *net.TCPConn:
key, ok := c.instance.InsertTCPConn(conn)
if !ok {
return 0, fmt.Errorf("water: (*wazero.Module).InsertTCPConn returned false")
}
if key <= 0 {
return key, fmt.Errorf("water: (*wazero.Module).InsertTCPConn returned invalid key")
}
return key, nil
default:
// TODO: support other types of connections as much as possible
return 0, fmt.Errorf("water: unsupported connection type: %T", conn)
}
}
// InsertListener implements Core.
func (c *core) InsertListener(listener net.Listener) (fd int32, err error) {
if c.instance == nil {
return 0, fmt.Errorf("water: cannot insert TCPListener before instantiation")
}
switch listener := listener.(type) {
case *net.TCPListener:
key, ok := c.instance.InsertTCPListener(listener)
if !ok {
return 0, fmt.Errorf("water: (*wazero.Module).InsertTCPListener returned false")
}
if key <= 0 {
return key, fmt.Errorf("water: (*wazero.Module).InsertTCPListener returned invalid key")
}
return key, nil
default:
// TODO: support other types of listeners as much as possible
return 0, fmt.Errorf("water: unsupported listener type: %T", listener)
}
}
// InsertFile implements Core.
func (c *core) InsertFile(osFile *os.File) (fd int32, err error) {
if c.instance == nil {
return 0, fmt.Errorf("water: cannot insert File before instantiation")
}
key, ok := c.instance.InsertOSFile(osFile)
if !ok {
return 0, fmt.Errorf("water: (*wazero.Module).InsertFile returned false")
}
if key <= 0 {
return key, fmt.Errorf("water: (*wazero.Module).InsertFile returned invalid key")
}
return key, nil
}