Skip to content

Commit 5381889

Browse files
committed
WIP: Close when a new initiator has connected
Note that this deviates from the intention of the specification to allow for more than one connection towards an initiator over the same WebSocket connection.
1 parent 9600a3d commit 5381889

File tree

5 files changed

+34
-5
lines changed

5 files changed

+34
-5
lines changed

examples/chat/main.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,8 @@ fn main() {
243243

244244
// Connect to server
245245
let (connect_future, event_channel) = saltyrtc_client::connect(
246-
"localhost",
247-
8765,
246+
"server.saltyrtc.org",
247+
443,
248248
Some(tls_connector),
249249
&core.handle(),
250250
salty_arc.clone(),
@@ -537,7 +537,7 @@ fn setup_logging(role: Role, log_to_stdout: bool) -> Config {
537537
.unwrap();
538538

539539
// Instantiate filters
540-
let info_filter = ThresholdFilter::new(LevelFilter::Info);
540+
let info_filter = ThresholdFilter::new(LevelFilter::Debug);
541541

542542
// Config builder
543543
let builder = Config::builder()

src/lib.rs

+16
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,9 @@ pub fn do_handshake(
643643
late_error = Some(e);
644644
}
645645
},
646+
HandleAction::Close(code) => {
647+
messages.push(OwnedMessage::Close(Some(CloseData::new(code.as_number(), "".to_string()))))
648+
},
646649
}
647650
}
648651

@@ -769,6 +772,10 @@ pub fn task_loop(
769772
HandleAction::Reply(bbox) => out_messages.push(OwnedMessage::Binary(bbox.into_bytes())),
770773
HandleAction::TaskMessage(msg) => {
771774
if let TaskMessage::Close(_) = msg {
775+
out_messages.push(OwnedMessage::Close(Some(CloseData {
776+
status_code: 1000,
777+
reason: "".to_string(),
778+
})));
772779
close_stream = true;
773780
}
774781

@@ -790,6 +797,13 @@ pub fn task_loop(
790797
HandleAction::HandshakeError(_) => return boxed!(future::err(Err(
791798
SaltyError::Crash("Got HandleAction::HandshakeError in task loop".into())
792799
))),
800+
HandleAction::Close(code) => {
801+
out_messages.push(OwnedMessage::Close(Some(CloseData {
802+
status_code: code.as_number(),
803+
reason: "".to_string(),
804+
})));
805+
close_stream = true;
806+
},
793807
}
794808
}
795809

@@ -824,6 +838,8 @@ pub fn task_loop(
824838
out_future
825839
.join(in_future)
826840
.and_then(move |_| if close_stream {
841+
// TODO(dbrgn): Tear down all futures to stop the loop
842+
827843
// Stop processing stream
828844
Err(Ok(()))
829845
} else {

src/protocol/mod.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -1808,7 +1808,16 @@ impl Signaling for ResponderSignaling {
18081808
fn handle_new_initiator(&mut self, _msg: NewInitiator) -> SignalingResult<Vec<HandleAction>> {
18091809
debug!("--> Received new-initiator from server");
18101810

1811-
let mut actions: Vec<HandleAction> = vec![];
1811+
// Close when a new initiator has connected.
1812+
//
1813+
// Note: This deviates from the intention of the specification to allow
1814+
// for more than one connection towards an initiator over the same
1815+
// WebSocket connection.
1816+
let signaling_state = self.common().signaling_state();
1817+
if signaling_state == SignalingState::Task {
1818+
debug!("Received new-initiator message in state {:?}, closing", signaling_state);
1819+
return Ok(vec![HandleAction::Close(CloseCode::WsClosingNormal)])
1820+
}
18121821

18131822
// A responder who receives a 'new-initiator' message MUST proceed by
18141823
// deleting all currently cached information about and for the previous
@@ -1829,6 +1838,7 @@ impl Signaling for ResponderSignaling {
18291838
return Err(SignalingError::Crash("No auth provider set".into()));
18301839
},
18311840
}
1841+
let mut actions: Vec<HandleAction> = vec![];
18321842
if send_token {
18331843
let old_auth_provider = mem::replace(&mut self.common_mut().auth_provider, None);
18341844
if let Some(AuthProvider::Token(token)) = old_auth_provider {

src/protocol/tests/signaling_messages.rs

+1
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,7 @@ mod client_auth {
531531
HandleAction::HandshakeError(_) => panic!("Unexpected HandshakeError"),
532532
HandleAction::TaskMessage(_) => panic!("Unexpected TaskMessage"),
533533
HandleAction::Event(_) => panic!("Unexpected Event"),
534+
HandleAction::Close(_) => panic!("Unexpected Close"),
534535
};
535536

536537
let decrypted = OpenBox::<Message>::decrypt(

src/protocol/types.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::result::Result as StdResult;
55
use serde::ser::{Serialize, Serializer};
66
use serde::de::{Deserialize, Deserializer, Visitor, Error as SerdeError};
77

8-
use crate::Event;
8+
use crate::{Event, CloseCode};
99
use crate::boxes::ByteBox;
1010
use crate::errors::SaltyError;
1111
use crate::tasks::TaskMessage;
@@ -237,6 +237,8 @@ pub(crate) enum HandleAction {
237237
Event(Event),
238238
/// A task message was received and decoded.
239239
TaskMessage(TaskMessage),
240+
/// Close the websocket with a specific close code.
241+
Close(CloseCode),
240242
}
241243

242244

0 commit comments

Comments
 (0)