socket

package
v0.41.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 21, 2022 License: GPL-3.0 Imports: 6 Imported by: 4

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsUdpSocket

func IsUdpSocket(address string) bool

func IsUnixSocket

func IsUnixSocket(address string) bool

Types

type Client

type Client interface {
	Connect() error
	Disconnect() error
	Command(command string, process Processor) error
}

Client is the interface that wraps the basic socket client operations and hides the implementation details from the users.

Connect should prepare the connection.

Disconnect should stop any in-flight connections.

Command should send the actual data to the wire and pass any results to the processor function.

Implementations should return TCP, UDP or Unix ready sockets.

type Config

type Config struct {
	Address        string
	ConnectTimeout time.Duration
	ReadTimeout    time.Duration
	WriteTimeout   time.Duration
	TLSConf        *tls.Config
}

Config holds the network ip v4 or v6 address, port, Socket type(ip, tcp, udp, unix), timeout and TLS configuration for a Socket

type Processor

type Processor func([]byte) bool

Processor function passed to the Socket.Command function. It is passed by the caller to process a command's response line by line.

type Socket

type Socket struct {
	Config
	// contains filtered or unexported fields
}

Socket is the implementation of a socket client.

func New

func New(config Config) *Socket

New returns a new pointer to a socket client given the socket type (IP, TCP, UDP, UNIX), a network address (IP/domain:port), a timeout and a TLS config. It supports both IPv4 and IPv6 address and reuses connection where possible.

func (*Socket) Command

func (s *Socket) Command(command string, process Processor) error

Command writes the command string to the connection and passed the response bytes line by line to the process function. It uses the timeout value from the Socket config and returns read, write and timeout errors if any. If a timeout occurs during the processing of the responses this function will stop processing and return a timeout error.

func (*Socket) Connect

func (s *Socket) Connect() (err error)

Connect connects to the Socket address on the named network. If the address is a domain name it will also perform the DNS resolution. Address like :80 will attempt to connect to the localhost. The config timeout and TLS config will be used.

func (*Socket) Disconnect

func (s *Socket) Disconnect() (err error)

Disconnect closes the connection. Any in-flight commands will be cancelled and return errors.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL