package sock

import (
	
	

	
)

// TCPSock is a pseudo-file representing a TCP socket.
type TCPSock interface {
	sys.File

	Accept() (TCPConn, sys.Errno)
}

// TCPConn is a pseudo-file representing a TCP connection.
type TCPConn interface {
	sys.File

	// Recvfrom only supports the flag sysfs.MSG_PEEK
	// TODO: document this like sys.File with known sys.Errno
	Recvfrom(p []byte, flags int) (n int, errno sys.Errno)

	// TODO: document this like sys.File with known sys.Errno
	Shutdown(how int) sys.Errno
}

// ConfigKey is a context.Context Value key. Its associated value should be a Config.
type ConfigKey struct{}

// Config is an internal struct meant to implement
// the interface in experimental/sock/Config.
type Config struct {
	// TCPAddresses is a slice of the configured host:port pairs.
	TCPAddresses []TCPAddress
}

// TCPAddress is a host:port pair to pre-open.
type TCPAddress struct {
	// Host is the host name for this listener.
	Host string
	// Port is the port number for this listener.
	Port int
}

// WithTCPListener implements the method of the same name in experimental/sock/Config.
//
// However, to avoid cyclic dependencies, this is returning the *Config in this scope.
// The interface is implemented in experimental/sock/Config via delegation.
func ( *Config) ( string,  int) *Config {
	 := .clone()
	.TCPAddresses = append(.TCPAddresses, TCPAddress{, })
	return &
}

// Makes a deep copy of this sockConfig.
func ( *Config) () Config {
	 := *
	.TCPAddresses = make([]TCPAddress, 0, len(.TCPAddresses))
	.TCPAddresses = append(.TCPAddresses, .TCPAddresses...)
	return 
}

// BuildTCPListeners build listeners from the current configuration.
func ( *Config) () ( []*net.TCPListener,  error) {
	for ,  := range .TCPAddresses {
		var  net.Listener
		,  = net.Listen("tcp", .String())
		if  != nil {
			break
		}
		if ,  := .(*net.TCPListener);  {
			 = append(, )
		}
	}
	if  != nil {
		// An error occurred, cleanup.
		for ,  := range  {
			_ = .Close() // Ignore errors, we are already cleaning.
		}
		 = nil
	}
	return
}

func ( TCPAddress) () string {
	return fmt.Sprintf("%s:%d", .Host, .Port)
}