socket

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: May 17, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package socket provides a Unix domain socket transport for the compose protocol. It implements framed read/write of protocol.Header + payload messages over a single net.Conn, along with server-side listening and client-side dialing.

Conn

Conn wraps a net.Conn with framed I/O. Each frame on the wire is a 64-byte header (see protocol.HeaderSize) followed by PayloadSize bytes of payload. Reads and writes are independently locked, so concurrent producers and consumers are safe on the same connection.

Listener

Listener binds a Unix domain socket (AF_UNIX) and accepts incoming connections. On startup it removes any stale socket file left by a previous crash, preventing "address already in use" errors.

Dialer

Dialer connects to a compositor's Unix domain socket. It is intentionally simple — reconnection with backoff belongs in higher layers (internal/conn.Manager), keeping the transport layer focused on establishing a single connection.

Platform support

Unix domain sockets are supported on Linux, macOS, and Windows 10 1803+ (AF_UNIX). No CGO required on any platform.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Conn

type Conn struct {
	// contains filtered or unexported fields
}

Conn wraps a net.Conn with framed header+payload read/write. Concurrent reads and writes are safe (separate locks). Concurrent reads from multiple goroutines are serialized by readMu. Concurrent writes from multiple goroutines are serialized by writeMu.

func NewConn

func NewConn(c net.Conn) *Conn

NewConn wraps an existing network connection with framed I/O.

func (*Conn) Close

func (c *Conn) Close() error

Close closes the underlying network connection.

func (*Conn) ReadFrame

func (c *Conn) ReadFrame() (protocol.Header, []byte, error)

ReadFrame reads a header + payload from the connection. The returned payload slice is freshly allocated if PayloadSize > 0.

func (*Conn) ReadFrameInto

func (c *Conn) ReadFrameInto(buf []byte) (protocol.Header, []byte, error)

ReadFrameInto reads a header + payload into a caller-provided buffer. If buf is nil or too small for the header's PayloadSize, a new buffer is allocated. The returned slice may be a sub-slice of buf or a new allocation.

func (*Conn) ReadHandshakeHello

func (c *Conn) ReadHandshakeHello() (protocol.HelloMsg, error)

ReadHandshakeHello reads a HelloMsg from the connection.

func (*Conn) ReadHandshakeWelcome

func (c *Conn) ReadHandshakeWelcome() (protocol.WelcomeMsg, error)

ReadHandshakeWelcome reads a WelcomeMsg from the connection.

func (*Conn) RemoteAddr

func (c *Conn) RemoteAddr() net.Addr

RemoteAddr returns the remote address of the underlying connection.

func (*Conn) WriteFrame

func (c *Conn) WriteFrame(hdr *protocol.Header, payload []byte) error

WriteFrame sends a header + payload atomically. The header is encoded into an internal buffer and written together with payload in a single locked section to prevent interleaving.

func (*Conn) WriteHandshakeHello

func (c *Conn) WriteHandshakeHello(msg *protocol.HelloMsg) error

WriteHandshakeHello sends a HelloMsg on the connection.

func (*Conn) WriteHandshakeWelcome

func (c *Conn) WriteHandshakeWelcome(msg *protocol.WelcomeMsg) error

WriteHandshakeWelcome sends a WelcomeMsg on the connection.

type Dialer

type Dialer struct {
	// contains filtered or unexported fields
}

Dialer connects to a compositor's Unix domain socket. Reconnection with backoff belongs in higher layers (internal/conn.Manager); the dialer is a simple one-shot connect helper.

func NewDialer

func NewDialer(addr string) *Dialer

NewDialer creates a dialer for the given Unix domain socket address. The default timeout is 5 seconds.

func (*Dialer) Dial

func (d *Dialer) Dial() (*Conn, error)

Dial connects to the compositor using the default timeout. The returned Conn is ready for handshake (WriteHandshakeHello / ReadHandshakeWelcome).

func (*Dialer) DialWithTimeout

func (d *Dialer) DialWithTimeout(timeout time.Duration) (*Conn, error)

DialWithTimeout connects to the compositor with a custom timeout. A zero or negative timeout means no deadline.

type Listener

type Listener struct {
	// contains filtered or unexported fields
}

Listener accepts module connections on a Unix domain socket (AF_UNIX). On startup it removes any stale socket file left by a previous crash.

func Listen

func Listen(addr string) (*Listener, error)

Listen creates a Unix domain socket listener at the given path.

If a socket file already exists at addr, it is removed first. This is the standard Unix pattern to avoid "address already in use" after an unclean shutdown. On Windows, AF_UNIX is supported since Windows 10 version 1803.

func (*Listener) Accept

func (l *Listener) Accept() (*Conn, error)

Accept waits for and returns the next module connection. The returned Conn is ready for handshake (WriteHandshakeWelcome / ReadHandshakeHello).

func (*Listener) Addr

func (l *Listener) Addr() net.Addr

Addr returns the listener's network address.

func (*Listener) Close

func (l *Listener) Close() error

Close stops accepting connections and removes the socket file.

Jump to

Keyboard shortcuts

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