From 60319999a63908c38e85668344c138651f0caf28 Mon Sep 17 00:00:00 2001 From: Greg Heartsfield Date: Thu, 25 Nov 2021 20:31:08 -0600 Subject: [PATCH] NOTICE sent for parsing errors --- Cargo.lock | 37 ++++++++++--------------------------- Cargo.toml | 2 +- src/main.rs | 18 +++++++++++++++++- src/proto.rs | 2 +- 4 files changed, 29 insertions(+), 30 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a2eb8c6..ef38322 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -158,18 +158,16 @@ checksum = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba" [[package]] name = "futures-core" -version = "0.3.17" +version = "0.3.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "88d1c26957f23603395cd326b0ffe64124b818f4449552f960d815cfba83a53d" +checksum = "629316e42fe7c2a0b9a65b47d159ceaa5453ab14e8f0a3c5eedbb8cd55b4a445" [[package]] name = "futures-macro" -version = "0.3.17" +version = "0.3.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "18e4a4b95cea4b4ccbcf1c5675ca7c4ee4e9e75eb79944d07defde18068f79bb" +checksum = "a89f17b21645bc4ed773c69af9c9a0effd4a3f1a3876eadd453469f8854e7fdd" dependencies = [ - "autocfg 1.0.1", - "proc-macro-hack", "proc-macro2", "quote", "syn", @@ -177,31 +175,28 @@ dependencies = [ [[package]] name = "futures-sink" -version = "0.3.17" +version = "0.3.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "36ea153c13024fe480590b3e3d4cad89a0cfacecc24577b68f86c6ced9c2bc11" +checksum = "996c6442437b62d21a32cd9906f9c41e7dc1e19a9579843fad948696769305af" [[package]] name = "futures-task" -version = "0.3.17" +version = "0.3.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1d3d00f4eddb73e498a54394f228cd55853bdf059259e8e7bc6e69d408892e99" +checksum = "dabf1872aaab32c886832f2276d2f5399887e2bd613698a02359e4ea83f8de12" [[package]] name = "futures-util" -version = "0.3.17" +version = "0.3.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "36568465210a3a6ee45e1f165136d68671471a501e632e9a98d96872222b5481" +checksum = "41d22213122356472061ac0f1ab2cee28d2bac8491410fd68c2af53d1cedb83e" dependencies = [ - "autocfg 1.0.1", "futures-core", "futures-macro", "futures-sink", "futures-task", "pin-project-lite", "pin-utils", - "proc-macro-hack", - "proc-macro-nested", "slab", ] @@ -442,18 +437,6 @@ version = "0.2.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ed0cfbc8191465bed66e1718596ee0b0b35d5ee1f41c5df2189d0fe8bde535ba" -[[package]] -name = "proc-macro-hack" -version = "0.5.19" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dbf0c48bc1d91375ae5c3cd81e3722dff1abcf81a30960240640d223f59fe0e5" - -[[package]] -name = "proc-macro-nested" -version = "0.1.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bc881b2c22681370c6a780e47af9840ef841837bc98118431d4e1868bd0c1086" - [[package]] name = "proc-macro2" version = "1.0.32" diff --git a/Cargo.toml b/Cargo.toml index 5aa20cd..3a954c5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,7 +9,7 @@ edition = "2018" log = "0.4.14" env_logger = "0.9.0" tokio = { version = "1.14.0", features = ["full"] } -futures-util = "0.3.17" +futures-util = "0.3.18" tokio-tungstenite = "0.16.0" tungstenite = "0.16.0" thiserror = "1.0.30" diff --git a/src/main.rs b/src/main.rs index 909d426..66b70ae 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,10 @@ use std::{env, io::Error}; +//use futures::stream::SplitSink; use futures_util::{SinkExt, StreamExt}; use log::{debug, info, warn}; use nostr_rs_relay::proto::Proto; +//use tokio::io::{ReadHalf, WriteHalf}; use tokio::net::{TcpListener, TcpStream}; use tokio::runtime::Builder; use tokio_tungstenite::WebSocketStream; @@ -73,6 +75,7 @@ async fn nostr_server(stream: TcpStream) { async fn process_client(stream: WebSocketStream) { // get a protocol helper; let mut proto = Proto::new(); + // futures::stream::Stream? let (mut write, mut read) = stream.split(); // TODO: select on a timeout to kill non-responsive clients @@ -91,7 +94,20 @@ async fn process_client(stream: WebSocketStream) { .await .expect("send failed"); // Handle this request. Everything else below is basically websocket error handling. - proto.process_message(cmd).ok(); + let proto_error = proto.process_message(cmd); + match proto_error { + Err(_) => { + write + .send(Message::Text( + "[\"NOTICE\", \"Failed to process message.\"]".to_owned(), + )) + .await + .expect("send failed"); + } + Ok(_) => { + info!("Message processed successfully"); + } + } } Ok(Message::Binary(_)) => { info!("Ignoring Binary message"); diff --git a/src/proto.rs b/src/proto.rs index 0f529b6..00892c0 100644 --- a/src/proto.rs +++ b/src/proto.rs @@ -27,7 +27,7 @@ impl Proto { p } - // TODO: figure out NOTICE handling for errors here + // Error results will be transformed into client NOTICEs pub fn process_message(&mut self, cmd: String) -> Result<()> { info!( "Processing message in proto for client: {:?}",