Skip to main content

kcl_lib/engine/engine_manager/
engine_transport.rs

1use std::collections::HashMap;
2
3use anyhow::Result;
4use kcmc::websocket::WebSocketRequest;
5use kcmc::websocket::WebSocketResponse;
6use kittycad_modeling_cmds::{self as kcmc};
7use uuid::Uuid;
8
9use crate::KclError;
10use crate::SourceRange;
11use crate::engine_connection::TransportCloseError;
12
13/// Handles sending requests to the engine, and waiting for responses.
14/// Should have at least two implementations:
15/// 1. native code on x86/arm with network access
16/// 2. wasm in the browser sandbox, using wasm-bindgen to get a handle for
17/// sending and receiving data over the websocket.
18#[async_trait::async_trait]
19pub trait EngineTransport: Send + Sync + 'static {
20    /// Send a modeling command without waiting for any response.
21    async fn inner_fire_modeling_cmd(
22        &self,
23        cmd_id: uuid::Uuid,
24        source_range: SourceRange,
25        cmd: WebSocketRequest,
26        id_to_source_range: HashMap<Uuid, SourceRange>,
27    ) -> Result<(), KclError>;
28
29    /// Send a modeling command, wait for a response.
30    async fn inner_send_modeling_cmd(
31        &self,
32        cmd_id: uuid::Uuid,
33        source_range: SourceRange,
34        cmd: WebSocketRequest,
35        id_to_source_range: HashMap<Uuid, SourceRange>,
36    ) -> Result<WebSocketResponse, KclError>;
37
38    /// If client is reused across sessions, implement this to clear sessions.
39    async fn start_new_session(&self, _source_range: SourceRange) -> Result<(), KclError> {
40        Ok(())
41    }
42
43    /// Close connection to the engine, clean up resources.
44    async fn close(&self) -> Result<(), TransportCloseError>;
45}