Documentation
¶
Overview ¶
Package transport provides transport layer abstractions for RPC communication.
Index ¶
- Constants
- Variables
- func GenerateID() string
- type Listener
- type Server
- type StdioMode
- type StdioOption
- type StdioTransport
- func (t *StdioTransport) Close() error
- func (t *StdioTransport) Done() <-chan struct{}
- func (t *StdioTransport) ID() string
- func (t *StdioTransport) Info() TransportInfo
- func (t *StdioTransport) Read(ctx context.Context) ([]byte, error)
- func (t *StdioTransport) Write(ctx context.Context, data []byte) error
- type Transport
- type TransportHandler
- type TransportInfo
- type WebSocketOption
- type WebSocketTransport
- func (t *WebSocketTransport) Close() error
- func (t *WebSocketTransport) Conn() *websocket.Conn
- func (t *WebSocketTransport) Done() <-chan struct{}
- func (t *WebSocketTransport) ID() string
- func (t *WebSocketTransport) Info() TransportInfo
- func (t *WebSocketTransport) Read(ctx context.Context) ([]byte, error)
- func (t *WebSocketTransport) Write(ctx context.Context, data []byte) error
Constants ¶
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 ¶
var ( ErrTransportClosed = errors.New("transport is closed") ErrWriteTimeout = errors.New("write timeout") ErrReadTimeout = errors.New("read timeout") )
Common transport errors.
Functions ¶
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 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.
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 ¶
func (t *WebSocketTransport) Info() TransportInfo
Info returns metadata about the WebSocket transport.