From c6376e1d28342af0b53f4289b36a28b8bf30e4de Mon Sep 17 00:00:00 2001 From: Paul Fariello Date: Fri, 11 Sep 2020 17:05:49 +0200 Subject: [PATCH] Add server and port to connect_with_jid method on async_client --- tokio-xmpp/src/client/async_client.rs | 35 +++++++++++++++++++++------ 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/tokio-xmpp/src/client/async_client.rs b/tokio-xmpp/src/client/async_client.rs index 26b114da..b4de9b07 100644 --- a/tokio-xmpp/src/client/async_client.rs +++ b/tokio-xmpp/src/client/async_client.rs @@ -31,6 +31,8 @@ pub struct Client { jid: Jid, password: String, reconnect: bool, + server: String, + port: u16, // TODO: tls_required=true } @@ -50,19 +52,27 @@ impl Client { /// and yield events. pub fn new>(jid: &str, password: P) -> Result { let jid = Jid::from_str(jid)?; - let client = Self::new_with_jid(jid, password.into()); + let server = jid.clone().domain(); + let client = Self::new_with_jid(jid, password.into(), server, 5222); Ok(client) } /// Start a new client given that the JID is already parsed. - pub fn new_with_jid(jid: Jid, password: String) -> Self { + pub fn new_with_jid(jid: Jid, password: String, server: String, port: u16) -> Self { let local = LocalSet::new(); - let connect = local.spawn_local(Self::connect(jid.clone(), password.clone())); + let connect = local.spawn_local(Self::connect( + server.clone(), + port, + jid.clone(), + password.clone(), + )); let client = Client { jid, password, state: ClientState::Connecting(connect, local), reconnect: false, + server: server, + port: port, }; client } @@ -74,13 +84,18 @@ impl Client { self } - async fn connect(jid: Jid, password: String) -> Result { + async fn connect( + server: String, + port: u16, + jid: Jid, + password: String, + ) -> Result { let username = jid.clone().node().unwrap(); let password = password; - let domain = idna::domain_to_ascii(&jid.clone().domain()).map_err(|_| Error::Idna)?; + let domain = idna::domain_to_ascii(&server).map_err(|_| Error::Idna)?; // TCP connection - let tcp_stream = connect(&domain, Some("_xmpp-client._tcp"), 5222).await?; + let tcp_stream = connect(&domain, Some("_xmpp-client._tcp"), port).await?; // Unencryped XMPPStream let xmpp_stream = @@ -162,8 +177,12 @@ impl Stream for Client { ClientState::Disconnected if self.reconnect => { // TODO: add timeout let mut local = LocalSet::new(); - let connect = - local.spawn_local(Self::connect(self.jid.clone(), self.password.clone())); + let connect = local.spawn_local(Self::connect( + self.server.clone(), + self.port, + self.jid.clone(), + self.password.clone(), + )); let _ = Pin::new(&mut local).poll(cx); self.state = ClientState::Connecting(connect, local); self.poll_next(cx)