|
|
@ -1,15 +1,9 @@
|
|
|
|
use std::collections::HashMap;
|
|
|
|
use crate::Result;
|
|
|
|
use std::io;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use log::{error, info};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use crate::Error;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type Result<T> = core::result::Result<T, Error>;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use jsonrpc_core as jsonrpc;
|
|
|
|
use jsonrpc_core as jsonrpc;
|
|
|
|
|
|
|
|
use log::{error, info};
|
|
|
|
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use serde_json::Value;
|
|
|
|
use serde_json::Value;
|
|
|
|
|
|
|
|
use std::collections::HashMap;
|
|
|
|
use tokio::{
|
|
|
|
use tokio::{
|
|
|
|
io::{AsyncBufRead, AsyncBufReadExt, AsyncReadExt, AsyncWriteExt, BufReader, BufWriter},
|
|
|
|
io::{AsyncBufRead, AsyncBufReadExt, AsyncReadExt, AsyncWriteExt, BufReader, BufWriter},
|
|
|
|
process::{ChildStderr, ChildStdin, ChildStdout},
|
|
|
|
process::{ChildStderr, ChildStdin, ChildStdout},
|
|
|
@ -26,12 +20,11 @@ pub enum Payload {
|
|
|
|
Response(jsonrpc::Output),
|
|
|
|
Response(jsonrpc::Output),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
|
|
|
/// A type representing all possible values sent from the server to the client.
|
|
|
|
/// A type representing all possible values sent from the server to the client.
|
|
|
|
#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
|
|
|
|
#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
|
|
|
|
#[serde(deny_unknown_fields)]
|
|
|
|
#[serde(deny_unknown_fields)]
|
|
|
|
#[serde(untagged)]
|
|
|
|
#[serde(untagged)]
|
|
|
|
enum Message {
|
|
|
|
enum ServerMessage {
|
|
|
|
/// A regular JSON-RPC request output (single response).
|
|
|
|
/// A regular JSON-RPC request output (single response).
|
|
|
|
Output(jsonrpc::Output),
|
|
|
|
Output(jsonrpc::Output),
|
|
|
|
/// A JSON-RPC request or notification.
|
|
|
|
/// A JSON-RPC request or notification.
|
|
|
@ -39,34 +32,32 @@ enum Message {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub struct Transport {
|
|
|
|
pub struct Transport {
|
|
|
|
incoming: UnboundedSender<jsonrpc::Call>,
|
|
|
|
client_tx: UnboundedSender<jsonrpc::Call>,
|
|
|
|
outgoing: UnboundedReceiver<Payload>,
|
|
|
|
client_rx: UnboundedReceiver<Payload>,
|
|
|
|
|
|
|
|
|
|
|
|
pending_requests: HashMap<jsonrpc::Id, Sender<Result<Value>>>,
|
|
|
|
pending_requests: HashMap<jsonrpc::Id, Sender<Result<Value>>>,
|
|
|
|
headers: HashMap<String, String>,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
writer: BufWriter<ChildStdin>,
|
|
|
|
server_stdin: BufWriter<ChildStdin>,
|
|
|
|
reader: BufReader<ChildStdout>,
|
|
|
|
server_stdout: BufReader<ChildStdout>,
|
|
|
|
stderr: BufReader<ChildStderr>,
|
|
|
|
server_stderr: BufReader<ChildStderr>,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl Transport {
|
|
|
|
impl Transport {
|
|
|
|
pub fn start(
|
|
|
|
pub fn start(
|
|
|
|
reader: BufReader<ChildStdout>,
|
|
|
|
server_stdout: BufReader<ChildStdout>,
|
|
|
|
writer: BufWriter<ChildStdin>,
|
|
|
|
server_stdin: BufWriter<ChildStdin>,
|
|
|
|
stderr: BufReader<ChildStderr>,
|
|
|
|
server_stderr: BufReader<ChildStderr>,
|
|
|
|
) -> (UnboundedReceiver<jsonrpc::Call>, UnboundedSender<Payload>) {
|
|
|
|
) -> (UnboundedReceiver<jsonrpc::Call>, UnboundedSender<Payload>) {
|
|
|
|
let (incoming, rx) = unbounded_channel();
|
|
|
|
let (client_tx, rx) = unbounded_channel();
|
|
|
|
let (tx, outgoing) = unbounded_channel();
|
|
|
|
let (tx, client_rx) = unbounded_channel();
|
|
|
|
|
|
|
|
|
|
|
|
let transport = Self {
|
|
|
|
let transport = Self {
|
|
|
|
reader,
|
|
|
|
server_stdout,
|
|
|
|
writer,
|
|
|
|
server_stdin,
|
|
|
|
stderr,
|
|
|
|
server_stderr,
|
|
|
|
incoming,
|
|
|
|
client_tx,
|
|
|
|
outgoing,
|
|
|
|
client_rx,
|
|
|
|
pending_requests: HashMap::default(),
|
|
|
|
pending_requests: HashMap::default(),
|
|
|
|
headers: HashMap::default(),
|
|
|
|
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
tokio::spawn(transport.duplex());
|
|
|
|
tokio::spawn(transport.duplex());
|
|
|
@ -74,105 +65,104 @@ impl Transport {
|
|
|
|
(rx, tx)
|
|
|
|
(rx, tx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async fn recv(
|
|
|
|
async fn recv_server_message(
|
|
|
|
reader: &mut (impl AsyncBufRead + Unpin + Send),
|
|
|
|
reader: &mut (impl AsyncBufRead + Unpin + Send),
|
|
|
|
headers: &mut HashMap<String, String>,
|
|
|
|
buffer: &mut String,
|
|
|
|
) -> core::result::Result<Message, std::io::Error> {
|
|
|
|
) -> Result<ServerMessage> {
|
|
|
|
// read headers
|
|
|
|
let mut content_length = None;
|
|
|
|
loop {
|
|
|
|
loop {
|
|
|
|
let mut header = String::new();
|
|
|
|
buffer.truncate(0);
|
|
|
|
// detect pipe closed if 0
|
|
|
|
reader.read_line(buffer).await?;
|
|
|
|
reader.read_line(&mut header).await?;
|
|
|
|
let header = buffer.trim();
|
|
|
|
let header = header.trim();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if header.is_empty() {
|
|
|
|
if header.is_empty() {
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
let parts: Vec<&str> = header.split(": ").collect();
|
|
|
|
let mut parts = header.split(": ");
|
|
|
|
if parts.len() != 2 {
|
|
|
|
|
|
|
|
|
|
|
|
match (parts.next(), parts.next(), parts.next()) {
|
|
|
|
|
|
|
|
(Some("Content-Length"), Some(value), None) => {
|
|
|
|
|
|
|
|
content_length = Some(value.parse().unwrap());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
(Some(_), Some(_), None) => {}
|
|
|
|
|
|
|
|
_ => {
|
|
|
|
return Err(std::io::Error::new(
|
|
|
|
return Err(std::io::Error::new(
|
|
|
|
std::io::ErrorKind::Other,
|
|
|
|
std::io::ErrorKind::Other,
|
|
|
|
"Failed to parse header",
|
|
|
|
"Failed to parse header",
|
|
|
|
));
|
|
|
|
)
|
|
|
|
|
|
|
|
.into());
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
headers.insert(parts[0].to_string(), parts[1].to_string());
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// find content-length
|
|
|
|
let content_length = content_length.unwrap();
|
|
|
|
let content_length = headers.get("Content-Length").unwrap().parse().unwrap();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//TODO: reuse vector
|
|
|
|
let mut content = vec![0; content_length];
|
|
|
|
let mut content = vec![0; content_length];
|
|
|
|
reader.read_exact(&mut content).await?;
|
|
|
|
reader.read_exact(&mut content).await?;
|
|
|
|
let msg = String::from_utf8(content).unwrap();
|
|
|
|
let msg = String::from_utf8(content).unwrap();
|
|
|
|
|
|
|
|
|
|
|
|
// read data
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
info!("<- {}", msg);
|
|
|
|
info!("<- {}", msg);
|
|
|
|
|
|
|
|
|
|
|
|
// try parsing as output (server response) or call (server request)
|
|
|
|
// try parsing as output (server response) or call (server request)
|
|
|
|
let output: serde_json::Result<Message> = serde_json::from_str(&msg);
|
|
|
|
let output: serde_json::Result<ServerMessage> = serde_json::from_str(&msg);
|
|
|
|
|
|
|
|
|
|
|
|
Ok(output?)
|
|
|
|
Ok(output?)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async fn err(
|
|
|
|
async fn recv_server_error(
|
|
|
|
err: &mut (impl AsyncBufRead + Unpin + Send),
|
|
|
|
err: &mut (impl AsyncBufRead + Unpin + Send),
|
|
|
|
) -> core::result::Result<(), std::io::Error> {
|
|
|
|
buffer: &mut String,
|
|
|
|
let mut line = String::new();
|
|
|
|
) -> Result<()> {
|
|
|
|
err.read_line(&mut line).await?;
|
|
|
|
buffer.truncate(0);
|
|
|
|
error!("err <- {}", line);
|
|
|
|
err.read_line(buffer).await?;
|
|
|
|
|
|
|
|
error!("err <- {}", buffer);
|
|
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub async fn send_payload(&mut self, payload: Payload) -> io::Result<()> {
|
|
|
|
async fn send_payload_to_server(&mut self, payload: Payload) -> Result<()> {
|
|
|
|
match payload {
|
|
|
|
//TODO: reuse string
|
|
|
|
|
|
|
|
let json = match payload {
|
|
|
|
Payload::Request { chan, value } => {
|
|
|
|
Payload::Request { chan, value } => {
|
|
|
|
self.pending_requests.insert(value.id.clone(), chan);
|
|
|
|
self.pending_requests.insert(value.id.clone(), chan);
|
|
|
|
|
|
|
|
serde_json::to_string(&value)?
|
|
|
|
let json = serde_json::to_string(&value)?;
|
|
|
|
|
|
|
|
self.send(json).await
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Payload::Notification(value) => {
|
|
|
|
|
|
|
|
let json = serde_json::to_string(&value)?;
|
|
|
|
|
|
|
|
self.send(json).await
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Payload::Response(error) => {
|
|
|
|
|
|
|
|
let json = serde_json::to_string(&error)?;
|
|
|
|
|
|
|
|
self.send(json).await
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Payload::Notification(value) => serde_json::to_string(&value)?,
|
|
|
|
|
|
|
|
Payload::Response(error) => serde_json::to_string(&error)?,
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
self.send_string_to_server(json).await
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub async fn send(&mut self, request: String) -> io::Result<()> {
|
|
|
|
async fn send_string_to_server(&mut self, request: String) -> Result<()> {
|
|
|
|
info!("-> {}", request);
|
|
|
|
info!("-> {}", request);
|
|
|
|
|
|
|
|
|
|
|
|
// send the headers
|
|
|
|
// send the headers
|
|
|
|
self.writer
|
|
|
|
self.server_stdin
|
|
|
|
.write_all(format!("Content-Length: {}\r\n\r\n", request.len()).as_bytes())
|
|
|
|
.write_all(format!("Content-Length: {}\r\n\r\n", request.len()).as_bytes())
|
|
|
|
.await?;
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
|
|
|
|
// send the body
|
|
|
|
// send the body
|
|
|
|
self.writer.write_all(request.as_bytes()).await?;
|
|
|
|
self.server_stdin.write_all(request.as_bytes()).await?;
|
|
|
|
|
|
|
|
|
|
|
|
self.writer.flush().await?;
|
|
|
|
self.server_stdin.flush().await?;
|
|
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async fn recv_msg(&mut self, msg: Message) -> anyhow::Result<()> {
|
|
|
|
async fn process_server_message(&mut self, msg: ServerMessage) -> Result<()> {
|
|
|
|
match msg {
|
|
|
|
match msg {
|
|
|
|
Message::Output(output) => self.recv_response(output).await?,
|
|
|
|
ServerMessage::Output(output) => self.process_request_response(output).await?,
|
|
|
|
Message::Call(call) => {
|
|
|
|
ServerMessage::Call(call) => {
|
|
|
|
self.incoming.send(call).unwrap();
|
|
|
|
self.client_tx.send(call).unwrap();
|
|
|
|
// let notification = Notification::parse(&method, params);
|
|
|
|
// let notification = Notification::parse(&method, params);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
Ok(())
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async fn recv_response(&mut self, output: jsonrpc::Output) -> io::Result<()> {
|
|
|
|
async fn process_request_response(&mut self, output: jsonrpc::Output) -> Result<()> {
|
|
|
|
let (id, result) = match output {
|
|
|
|
let (id, result) = match output {
|
|
|
|
jsonrpc::Output::Success(jsonrpc::Success { id, result, .. }) => {
|
|
|
|
jsonrpc::Output::Success(jsonrpc::Success { id, result, .. }) => {
|
|
|
|
info!("<- {}", result);
|
|
|
|
info!("<- {}", result);
|
|
|
@ -200,29 +190,33 @@ impl Transport {
|
|
|
|
Ok(())
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub async fn duplex(mut self) {
|
|
|
|
async fn duplex(mut self) {
|
|
|
|
|
|
|
|
let mut recv_buffer = String::new();
|
|
|
|
|
|
|
|
let mut err_buffer = String::new();
|
|
|
|
loop {
|
|
|
|
loop {
|
|
|
|
tokio::select! {
|
|
|
|
tokio::select! {
|
|
|
|
// client -> server
|
|
|
|
// client -> server
|
|
|
|
msg = self.outgoing.recv() => {
|
|
|
|
msg = self.client_rx.recv() => {
|
|
|
|
if msg.is_none() {
|
|
|
|
match msg {
|
|
|
|
break;
|
|
|
|
Some(msg) => {
|
|
|
|
|
|
|
|
self.send_payload_to_server(msg).await.unwrap()
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
None => break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let msg = msg.unwrap();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.send_payload(msg).await.unwrap();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// server <- client
|
|
|
|
// server -> client
|
|
|
|
msg = Self::recv(&mut self.reader, &mut self.headers) => {
|
|
|
|
msg = Self::recv_server_message(&mut self.server_stdout, &mut recv_buffer) => {
|
|
|
|
if msg.is_err() {
|
|
|
|
match msg {
|
|
|
|
|
|
|
|
Ok(msg) => {
|
|
|
|
|
|
|
|
self.process_server_message(msg).await.unwrap();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Err(_) => {
|
|
|
|
error!("err: <- {:?}", msg);
|
|
|
|
error!("err: <- {:?}", msg);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let msg = msg.unwrap();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.recv_msg(msg).await.unwrap();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_msg = Self::err(&mut self.stderr) => {}
|
|
|
|
_msg = Self::recv_server_error(&mut self.server_stderr, &mut err_buffer) => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|