saltyrtc / saltyrtc-client-rs

SaltyRTC Rust implementation.
Apache License 2.0
34 stars 5 forks source link

NoSharedTask: Send close message #17

Closed dbrgn closed 6 years ago

dbrgn commented 6 years ago

When no shared task could be found, send a 'close' message to the peer.

Also check whether there are other places where we need to send the 'close' message.

dbrgn commented 6 years ago

To test this with the chat example:

diff --git a/examples/chat/chat_task.rs b/examples/chat/chat_task.rs
index f8536fc..40974ea 100644
--- a/examples/chat/chat_task.rs
+++ b/examples/chat/chat_task.rs
@@ -42,6 +42,7 @@ pub(crate) struct ChatTask {
     outgoing_tx: Option<UnboundedSender<TaskMessage>>,
     incoming_tx: UnboundedSender<ChatMessage>,
     disconnect_tx: Option<OneshotSender<Option<CloseCode>>>,
+    version: u8,
 }

 #[derive(Debug, Clone, PartialEq, Eq)]
@@ -59,7 +60,7 @@ impl ChatTask {
     /// * `our_name`: Our local chat nickname.
     /// * `remote` A remote reference to a Tokio reactor core.
     /// * `incoming_tx`: The futures channel sender through which incoming chat messages are sent.
-    pub fn new<S: Into<String>>(our_name: S, remote: Remote, incoming_tx: UnboundedSender<ChatMessage>) -> Self {
+    pub fn new<S: Into<String>>(our_name: S, remote: Remote, incoming_tx: UnboundedSender<ChatMessage>, version: u8) -> Self {
         ChatTask {
             our_name: our_name.into(),
             peer_name: Arc::new(Mutex::new(None)),
@@ -67,6 +68,7 @@ impl ChatTask {
             outgoing_tx: None,
             incoming_tx,
             disconnect_tx: None,
+            version,
         }
     }

@@ -237,7 +239,11 @@ impl Task for ChatTask {

     /// Return the task protocol name.
     fn name(&self) -> Cow<'static, str> {
-        Cow::Borrowed("v0.simplechat.tasks.saltyrtc.org")
+        match self.version {
+            0 => Cow::Borrowed("v0.simplechat.tasks.saltyrtc.org"),
+            1 => Cow::Borrowed("v1.simplechat.tasks.saltyrtc.org"),
+            _ => Cow::Borrowed("v?.simplechat.tasks.saltyrtc.org"),
+        }
     }

     /// Return the task data used for negotiation in the `auth` message.
diff --git a/examples/chat/main.rs b/examples/chat/main.rs
index 313954d..3b0b384 100644
--- a/examples/chat/main.rs
+++ b/examples/chat/main.rs
@@ -200,7 +200,7 @@ fn main() {
     let (incoming_tx, incoming_rx) = futures_mpsc::unbounded::<ChatMessage>();
     let (salty, auth_token_hex) = match role {
         Role::Initiator => {
-            let task = ChatTask::new("initiat0r", core.remote(), incoming_tx);
+            let task = ChatTask::new("initiat0r", core.remote(), incoming_tx, 1);
             let builder = SaltyClient::build(keypair)
                 .add_task(Box::new(task))
                 .with_ping_interval(Some(ping_interval));
@@ -214,7 +214,7 @@ fn main() {
             (salty, auth_token_hex)
         },
         Role::Responder => {
-            let task = ChatTask::new("r3spond3r", core.remote(), incoming_tx);
+            let task = ChatTask::new("r3spond3r", core.remote(), incoming_tx, 0);
             let initiator_pubkey = public_key_from_hex_str(&path).unwrap();
             let builder = SaltyClient::build(keypair)
                 .add_task(Box::new(task))