forked from tetratelabs/wazero
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sock.go
42 lines (35 loc) · 1.23 KB
/
sock.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
package sock
import (
"context"
"github.com/AR1011/wazero/internal/sock"
)
// Config configures the host to open TCP sockets and allows guest access to
// them.
//
// Instantiating a module with listeners results in pre-opened sockets
// associated with file-descriptors numerically after pre-opened files.
type Config interface {
// WithTCPListener configures the host to set up the given host:port listener.
WithTCPListener(host string, port int) Config
}
// NewConfig returns a Config for module instantiation.
func NewConfig() Config {
return &internalSockConfig{c: &sock.Config{}}
}
// internalSockConfig delegates to internal/sock.Config to avoid circular
// dependencies.
type internalSockConfig struct {
c *sock.Config
}
// WithTCPListener implements Config.WithTCPListener
func (c *internalSockConfig) WithTCPListener(host string, port int) Config {
cNew := c.c.WithTCPListener(host, port)
return &internalSockConfig{cNew}
}
// WithConfig registers the given Config into the given context.Context.
func WithConfig(ctx context.Context, config Config) context.Context {
if config, ok := config.(*internalSockConfig); ok && len(config.c.TCPAddresses) > 0 {
return context.WithValue(ctx, sock.ConfigKey{}, config.c)
}
return ctx
}