transport

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package transport provides transport layer abstractions for RPC communication.

Index

Constants

View Source
const (
	// Default timeouts for WebSocket operations.
	DefaultWriteTimeout = 15 * time.Second
	DefaultReadTimeout  = 90 * time.Second

	// Default maximum message size (512KB).
	DefaultMaxMessageSize = 512 * 1024

	// Ping interval for keepalive
	DefaultPingInterval = 30 * time.Second
	DefaultPongTimeout  = 60 * time.Second
)

Variables

View Source
var (
	ErrTransportClosed = errors.New("transport is closed")
	ErrWriteTimeout    = errors.New("write timeout")
	ErrReadTimeout     = errors.New("read timeout")
)

Common transport errors.

Functions

func GenerateID

func GenerateID() string

GenerateID generates a unique transport/client ID.

Types

type Listener

type Listener interface {
	// Accept waits for and returns the next transport connection.
	Accept(ctx context.Context) (Transport, error)

	// Close closes the listener.
	Close() error
}

Listener is a simplified interface for accepting transport connections. Unlike Server, it doesn't manage the lifecycle of a network listener.

type Server

type Server interface {
	// Start begins accepting connections.
	// For each new connection, handler is called in a new goroutine.
	// Start blocks until ctx is cancelled or an error occurs.
	Start(ctx context.Context, handler TransportHandler) error

	// Stop gracefully stops the server.
	// It waits for existing connections to complete up to the context deadline.
	Stop(ctx context.Context) error

	// Addr returns the address the server is listening on.
	// For WebSocket, this is the HTTP address (e.g., "localhost:16180").
	// For stdio, this returns "stdio".
	Addr() string
}

Server represents a transport server that accepts connections.

type StdioMode

type StdioMode int

StdioMode defines how messages are framed over stdio.

const (
	// StdioModeNewline uses newline-delimited JSON (simple, compact).
	StdioModeNewline StdioMode = iota

	// StdioModeLSP uses Content-Length headers like LSP protocol.
	// Format: Content-Length: 123\r\n\r\n{"jsonrpc":"2.0",...}
	StdioModeLSP
)

type StdioOption

type StdioOption func(*StdioTransport)

StdioOption configures a StdioTransport.

func WithStdioID

func WithStdioID(id string) StdioOption

WithStdioID sets a custom ID for the transport.

func WithStdioMode

func WithStdioMode(mode StdioMode) StdioOption

WithStdioMode sets the message framing mode.

type StdioTransport

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

StdioTransport implements Transport over stdin/stdout. This is used for VS Code extension integration and MCP compatibility.

func NewStdioTransport

func NewStdioTransport(opts ...StdioOption) *StdioTransport

NewStdioTransport creates a new stdio transport using os.Stdin and os.Stdout.

func NewStdioTransportWithIO

func NewStdioTransportWithIO(r io.Reader, w io.Writer, opts ...StdioOption) *StdioTransport

NewStdioTransportWithIO creates a new stdio transport with custom reader/writer. This is useful for testing.

func (*StdioTransport) Close

func (t *StdioTransport) Close() error

Close closes the stdio transport.

func (*StdioTransport) Done

func (t *StdioTransport) Done() <-chan struct{}

Done returns a channel that's closed when the transport is closed.

func (*StdioTransport) ID

func (t *StdioTransport) ID() string

ID returns the unique identifier for this transport.

func (*StdioTransport) Info

func (t *StdioTransport) Info() TransportInfo

Info returns metadata about the stdio transport.

func (*StdioTransport) Read

func (t *StdioTransport) Read(ctx context.Context) ([]byte, error)

Read reads the next message from stdin.

func (*StdioTransport) Write

func (t *StdioTransport) Write(ctx context.Context, data []byte) error

Write sends a message through stdout.

type Transport

type Transport interface {
	// ID returns a unique identifier for this transport instance.
	// For WebSocket, this is typically a UUID generated per connection.
	// For stdio, this is typically "stdio".
	ID() string

	// Read reads the next message from the transport.
	// It blocks until a message is available or the context is cancelled.
	// Returns io.EOF when transport is closed cleanly.
	Read(ctx context.Context) ([]byte, error)

	// Write sends a message through the transport.
	// It blocks until the message is sent or the context is cancelled.
	Write(ctx context.Context, data []byte) error

	// Close closes the transport.
	// After Close is called, Read and Write will return errors.
	// Close is safe to call multiple times.
	Close() error

	// Done returns a channel that's closed when the transport is closed.
	// This can be used to detect transport closure from another goroutine.
	Done() <-chan struct{}
}

Transport represents a bidirectional communication channel. It abstracts the underlying transport mechanism (WebSocket, stdio, etc.) to provide a uniform interface for JSON-RPC communication.

type TransportHandler

type TransportHandler func(Transport)

TransportHandler is called when a new transport connection is established.

type TransportInfo

type TransportInfo struct {
	// Type is the transport type: "websocket", "stdio", "tcp", etc.
	Type string

	// RemoteAddr is the remote address for network transports.
	// Empty for stdio transport.
	RemoteAddr string

	// LocalAddr is the local address for network transports.
	// Empty for stdio transport.
	LocalAddr string
}

TransportInfo contains metadata about a transport connection.

type WebSocketOption

type WebSocketOption func(*WebSocketTransport)

WebSocketOption configures a WebSocketTransport.

func WithReadTimeout

func WithReadTimeout(d time.Duration) WebSocketOption

WithReadTimeout sets the read timeout for the WebSocket transport.

func WithTransportID

func WithTransportID(id string) WebSocketOption

WithTransportID sets a custom ID for the transport.

func WithWriteTimeout

func WithWriteTimeout(d time.Duration) WebSocketOption

WithWriteTimeout sets the write timeout for the WebSocket transport.

type WebSocketTransport

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

WebSocketTransport implements Transport over a WebSocket connection.

func NewWebSocketTransport

func NewWebSocketTransport(conn *websocket.Conn, opts ...WebSocketOption) *WebSocketTransport

NewWebSocketTransport creates a new WebSocket transport.

func (*WebSocketTransport) Close

func (t *WebSocketTransport) Close() error

Close closes the WebSocket connection.

func (*WebSocketTransport) Conn

func (t *WebSocketTransport) Conn() *websocket.Conn

Conn returns the underlying WebSocket connection. Use with caution as direct access bypasses transport abstractions.

func (*WebSocketTransport) Done

func (t *WebSocketTransport) Done() <-chan struct{}

Done returns a channel that's closed when the transport is closed.

func (*WebSocketTransport) ID

func (t *WebSocketTransport) ID() string

ID returns the unique identifier for this transport.

func (*WebSocketTransport) Info

Info returns metadata about the WebSocket transport.

func (*WebSocketTransport) Read

func (t *WebSocketTransport) Read(ctx context.Context) ([]byte, error)

Read reads the next message from the WebSocket connection.

func (*WebSocketTransport) Write

func (t *WebSocketTransport) Write(ctx context.Context, data []byte) error

Write sends a message through the WebSocket connection.

Jump to

Keyboard shortcuts

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