MCPcopy Create free account
hub / github.com/aeroxy/chrome-devtools-cli / handle_connection

Function handle_connection

src/daemon.rs:106–187  ·  view source on GitHub ↗
(
    mut stream: S,
    client: &mut Option<CdpClient>,
    ws_url: &str,
)

Source from the content-addressed store, hash-verified

104}
105
106async fn handle_connection<S>(
107 mut stream: S,
108 client: &mut Option<CdpClient>,
109 ws_url: &str,
110) -> ConnectionOutcome
111where
112 S: AsyncReadExt + AsyncWriteExt + Unpin,
113{
114 let req_bytes = match read_msg(&mut stream).await {
115 Ok(b) => b,
116 Err(e) => {
117 eprintln!("daemon: read error: {e}");
118 return ConnectionOutcome::Continue;
119 }
120 };
121
122 let request: DaemonRequest = match serde_json::from_slice(&req_bytes) {
123 Ok(r) => r,
124 Err(e) => {
125 let resp = DaemonResponse {
126 success: false,
127 output: String::new(),
128 error: format!("Invalid request: {e}"),
129 navigated_to: None,
130 error_code: Some(ErrorCode::InvalidInput as u32),
131 };
132 if let Ok(resp_bytes) = serde_json::to_vec(&resp) {
133 let _ = write_msg(&mut stream, &resp_bytes).await;
134 }
135 return ConnectionOutcome::Continue;
136 }
137 };
138
139 // Connect lazily
140 if client.is_none() {
141 match CdpClient::connect(ws_url).await {
142 Ok(c) => *client = Some(c),
143 Err(e) => {
144 let resp = DaemonResponse {
145 success: false,
146 output: String::new(),
147 error: format!("Failed to connect to Chrome: {e:#}"),
148 navigated_to: None,
149 error_code: Some(ErrorCode::ChromeConnection as u32),
150 };
151 if let Ok(resp_bytes) = serde_json::to_vec(&resp) {
152 let _ = write_msg(&mut stream, &resp_bytes).await;
153 }
154 // Exit daemon if we can't connect, so the next CLI call will spawn a fresh daemon
155 return ConnectionOutcome::Fatal;
156 }
157 }
158 }
159
160 let response = match client.as_mut() {
161 Some(client) => handle_request(client, &request).await,
162 None => DaemonResponse {
163 success: false,

Callers

nothing calls this directly

Calls 3

read_msgFunction · 0.85
write_msgFunction · 0.85
handle_requestFunction · 0.85

Tested by

no test coverage detected