| 1 |
package sock |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
|
| 6 |
"github.com/tetratelabs/wazero/internal/sock" |
| 7 |
) |
| 8 |
|
| 9 |
// Config configures the host to open TCP sockets and allows guest access to |
| 10 |
// them. |
| 11 |
// |
| 12 |
// Instantiating a module with listeners results in pre-opened sockets |
| 13 |
// associated with file-descriptors numerically after pre-opened files. |
| 14 |
type Config interface { |
| 15 |
// WithTCPListener configures the host to set up the given host:port listener. |
| 16 |
WithTCPListener(host string, port int) Config |
| 17 |
} |
| 18 |
|
| 19 |
// NewConfig returns a Config for module instantiation. |
| 20 |
func NewConfig() Config { |
| 21 |
return &internalSockConfig{c: &sock.Config{}} |
| 22 |
} |
| 23 |
|
| 24 |
// internalSockConfig delegates to internal/sock.Config to avoid circular |
| 25 |
// dependencies. |
| 26 |
type internalSockConfig struct { |
| 27 |
c *sock.Config |
| 28 |
} |
| 29 |
|
| 30 |
// WithTCPListener implements Config.WithTCPListener |
| 31 |
func (c *internalSockConfig) WithTCPListener(host string, port int) Config { |
| 32 |
cNew := c.c.WithTCPListener(host, port) |
| 33 |
return &internalSockConfig{cNew} |
| 34 |
} |
| 35 |
|
| 36 |
// WithConfig registers the given Config into the given context.Context. |
| 37 |
func WithConfig(ctx context.Context, config Config) context.Context { |
| 38 |
if config, ok := config.(*internalSockConfig); ok && len(config.c.TCPAddresses) > 0 { |
| 39 |
return context.WithValue(ctx, sock.ConfigKey{}, config.c) |
| 40 |
} |
| 41 |
return ctx |
| 42 |
} |