Skip to main content

matrixcode_core/matrixrpc/transport/
mod.rs

1//! Transport Layer for MatrixRPC
2//!
3//! Provides transport abstraction for JSON-RPC communication.
4//! Supports Stdio and TCP transports.
5
6mod codec;
7mod stdio;
8mod tcp;
9
10use std::io;
11
12use async_trait::async_trait;
13
14pub use codec::FrameCodec;
15pub use stdio::StdioTransport;
16pub use tcp::{TcpTransport, TcpListener, REGISTRY_PORT, CALLBACK_PORT};
17
18use crate::matrixrpc::protocol::JsonRpcMessage;
19
20/// Transport trait for JSON-RPC communication
21///
22/// Implementations provide bidirectional message transport with
23/// proper framing and error handling.
24///
25/// Note: Only `Send` is required since async transports typically
26/// operate in single-threaded async contexts.
27#[async_trait]
28pub trait Transport: Send {
29    /// Send a message through the transport
30    async fn send(&mut self, message: &JsonRpcMessage) -> io::Result<()>;
31
32    /// Receive a message from the transport
33    async fn receive(&mut self) -> io::Result<Option<JsonRpcMessage>>;
34
35    /// Close the transport
36    async fn close(&mut self) -> io::Result<()>;
37
38    /// Check if the transport is closed
39    fn is_closed(&self) -> bool;
40}
41
42/// Builder for configuring transport options
43#[derive(Debug, Clone)]
44pub struct TransportConfig {
45    /// Maximum message size in bytes (default: 16MB)
46    pub max_message_size: usize,
47    /// Read timeout in milliseconds (default: 30000)
48    pub read_timeout_ms: u64,
49    /// Write timeout in milliseconds (default: 30000)
50    pub write_timeout_ms: u64,
51}
52
53impl Default for TransportConfig {
54    fn default() -> Self {
55        Self {
56            max_message_size: 16 * 1024 * 1024, // 16MB
57            read_timeout_ms: 30_000,            // 30 seconds
58            write_timeout_ms: 30_000,           // 30 seconds
59        }
60    }
61}
62
63impl TransportConfig {
64    /// Create a new transport config with defaults
65    pub fn new() -> Self {
66        Self::default()
67    }
68
69    /// Set maximum message size
70    pub fn max_message_size(mut self, size: usize) -> Self {
71        self.max_message_size = size;
72        self
73    }
74
75    /// Set read timeout in milliseconds
76    pub fn read_timeout(mut self, ms: u64) -> Self {
77        self.read_timeout_ms = ms;
78        self
79    }
80
81    /// Set write timeout in milliseconds
82    pub fn write_timeout(mut self, ms: u64) -> Self {
83        self.write_timeout_ms = ms;
84        self
85    }
86}
87
88#[cfg(test)]
89mod tests {
90    use super::*;
91
92    #[test]
93    fn test_transport_config_defaults() {
94        let config = TransportConfig::default();
95        assert_eq!(config.max_message_size, 16 * 1024 * 1024);
96        assert_eq!(config.read_timeout_ms, 30_000);
97        assert_eq!(config.write_timeout_ms, 30_000);
98    }
99
100    #[test]
101    fn test_transport_config_builder() {
102        let config = TransportConfig::new()
103            .max_message_size(1024)
104            .read_timeout(5000)
105            .write_timeout(10000);
106        assert_eq!(config.max_message_size, 1024);
107        assert_eq!(config.read_timeout_ms, 5000);
108        assert_eq!(config.write_timeout_ms, 10000);
109    }
110}