Explorar o código

rpc/jsonrpc: Add function for sending TCP JSON-RPC requests.

parazyd %!s(int64=4) %!d(string=hai) anos
pai
achega
dfc7def113
Modificáronse 1 ficheiros con 18 adicións e 1 borrados
  1. 18 1
      src/rpc/jsonrpc.rs

+ 18 - 1
src/rpc/jsonrpc.rs

@@ -3,9 +3,13 @@ use std::str;
 use rand::Rng;
 use serde::{Deserialize, Serialize};
 use serde_json::{json, Value};
+use tokio::io::{AsyncReadExt, AsyncWriteExt};
+use tokio::net::TcpStream;
+
+use crate::Error;
 
-#[derive(Serialize, Deserialize, Debug)]
 #[serde(untagged)]
+#[derive(Serialize, Deserialize, Debug)]
 pub enum JsonResult {
     Resp(JsonResponse),
     Err(JsonError),
@@ -86,3 +90,16 @@ pub fn notification(m: Value, p: Value) -> JsonNotification {
         params: p,
     }
 }
+
+pub async fn send_request(url: String, data: Value) -> Result<JsonResult, Error> {
+    // TODO: TLS
+    let mut buf = [0; 2048];
+    let mut stream = TcpStream::connect(url).await?;
+    let data_str = serde_json::to_string(&data)?;
+
+    stream.write_all(&data_str.as_bytes()).await?;
+    let bytes_read = stream.read(&mut buf[..]).await?;
+
+    let reply: JsonResult = serde_json::from_slice(&buf[0..bytes_read])?;
+    Ok(reply)
+}