comm

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2025 License: Apache-2.0 Imports: 10 Imported by: 2

Documentation

Index

Constants

View Source
const (
	ModePrefix  = "MODE:"
	OkResponse  = "OK"
	ErrorPrefix = "ERROR"
)

Constants for handshake protocol

Variables

This section is empty.

Functions

This section is empty.

Types

type ConnWrap

type ConnWrap struct {
	Conn     net.Conn
	Reader   *bufio.Reader
	PeerName string
}

ConnWrap wraps a net.Conn and a bufio.Reader for convenient line-based communication.

func Connect

func Connect(mode string, submode string, appRunId string, domainSocketPath string, serverAddr string) (*ConnWrap, error)

Connect attempts to connect to either a domain socket or TCP server, performs the handshake with the specified mode, submode, and appRunId, and returns a new ConnWrap if successful.

If domainSocketPath is not empty, it will first try to connect to the domain socket. If that fails and serverAddr is not empty, it will fall back to TCP. If both are empty or "-", it will return nil and an error.

The function returns a new ConnWrap and error. If the connection is successful, the error will be nil. If the connection fails, the error will contain the reason.

func MakeConnWrap

func MakeConnWrap(conn net.Conn, peerName string) *ConnWrap

MakeConnWrap creates a new ConnWrap from a net.Conn.

func (*ConnWrap) ClientHandshake

func (cw *ConnWrap) ClientHandshake(modeName string, submode string, appRunId string) error

ClientHandshake performs the client side of the mode-based handshake protocol with the server. It sends a mode line in the format "MODE:[mode]:[submode] [apprunid]\n" and expects an "OK\n" response from the server. If the server responds with an error, it returns that error. The submode is optional and can be empty.

func (*ConnWrap) Close

func (cw *ConnWrap) Close() error

Close closes the underlying connection.

func (*ConnWrap) ReadLine

func (cw *ConnWrap) ReadLine() (string, error)

ReadLine reads a line from the connection.

func (*ConnWrap) ServerHandshake

func (cw *ConnWrap) ServerHandshake() (string, string, string, error)

ServerHandshake performs the server side of the mode-based handshake protocol. It reads a mode line in the format "MODE:[mode]:[submode] [apprunid]\n", validates it, and sends an "OK\n" response if valid or an error message if invalid. It returns the mode, submode, and appRunId if successful, or an error if the handshake fails. The submode is optional and can be empty.

func (*ConnWrap) WriteLine

func (cw *ConnWrap) WriteLine(line string) error

WriteLine writes a line to the connection.

Jump to

Keyboard shortcuts

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