class ACP::StdioTransport

Overview

Implements the ACP transport over a pair of IO objects. In the typical case these are the stdin (for writing) and stdout (for reading) of a spawned agent child process.

Incoming messages are read in a background fiber and placed into a buffered channel so that #receive never blocks the writer.

Thread safety: Crystal fibers are cooperatively scheduled on a single thread, so we don't need mutexes — just channels.

Direct Known Subclasses

Defined in:

acp/transport.cr

Constructors

Instance Method Summary

Instance methods inherited from class ACP::Transport

close : Nil close, closed? : Bool closed?, receive : JSON::Any | Nil receive, send(message : Hash(String, JSON::Any)) : Nil send, send_json(obj : Hash(String, JSON::Any)) : Nil send_json

Constructor Detail

def self.new(reader : IO, writer : IO, buffer_size : Int32 = 256) #

Creates a new stdio transport.

  • reader — the IO to read incoming JSON-RPC messages from (typically the agent process's stdout).
  • writer — the IO to write outgoing JSON-RPC messages to (typically the agent process's stdin).
  • buffer_size — how many messages to buffer in the channel before back-pressuring the reader fiber. Default 256.

[View source]

Instance Method Detail

def close : Nil #

Closes the transport, signaling the reader to stop and closing the underlying IO objects.


[View source]
def closed? : Bool #

Returns true if the transport has been closed.


[View source]
def incoming : Channel(JSON::Any | Nil) #

The channel that the reader fiber pushes parsed messages into. A nil value signals that the reader has stopped (EOF or error).


[View source]
def receive(timeout : Time::Span) : JSON::Any | Nil #

Receives with a timeout. Returns nil if no message arrives within the given duration.


[View source]
def receive : JSON::Any | Nil #

Receives the next incoming JSON-RPC message. Blocks until a message is available. Returns nil if the transport is closed or the reader encountered EOF.


[View source]
def send(message : Hash(String, JSON::Any)) : Nil #

Sends a JSON-RPC message over the transport. The message is serialized as a single line of JSON followed by a newline.


[View source]