Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implementing auto-reconnection #196

Merged
merged 39 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
4056429
implemented reconnection policy
MrAliSalehi Sep 24, 2023
f8b4ddc
fmt
MrAliSalehi Sep 24, 2023
4e4bd70
replacing stream instead of `Sender`
MrAliSalehi Sep 24, 2023
c80a435
separating stream-creation from Sender instantiating
MrAliSalehi Sep 24, 2023
6ffe58a
removing `Clone` derives
MrAliSalehi Sep 24, 2023
39f586e
handling reconnection when using proxy
MrAliSalehi Sep 24, 2023
aa98c01
Merge branch 'Lonami:master' into master
MrAliSalehi Sep 24, 2023
590cade
moving reconnection policy to `step` function
MrAliSalehi Sep 24, 2023
a7534cc
Merge remote-tracking branch 'origin/master'
MrAliSalehi Sep 24, 2023
b8a52bc
adding logs
MrAliSalehi Sep 25, 2023
c5affa5
reset the mtp state
MrAliSalehi Sep 25, 2023
ac2f62f
creating new transport
MrAliSalehi Sep 26, 2023
caecdf2
matching connection failures
MrAliSalehi Sep 26, 2023
421cd5b
changing the pattern matching
MrAliSalehi Sep 26, 2023
ababe8e
logs
MrAliSalehi Sep 26, 2023
beecf1f
reset salts
MrAliSalehi Sep 26, 2023
c18b434
clear buffer and msg_count
MrAliSalehi Sep 26, 2023
9bce209
improve reconnect attempt
Lonami Sep 26, 2023
436f015
oops
MrAliSalehi Sep 26, 2023
fe76034
oops
MrAliSalehi Sep 26, 2023
5a46a63
clear some buffers
MrAliSalehi Sep 26, 2023
b20ab93
"which buffer caused the problem"-1
MrAliSalehi Sep 26, 2023
8beae02
reset the write index
MrAliSalehi Sep 26, 2023
7b5432a
change all the request states to NotSerialized
MrAliSalehi Sep 26, 2023
2bc0fe3
reset transport instance instead of creating a new one
MrAliSalehi Sep 27, 2023
8419ca0
moving reset instructions to a separate function
MrAliSalehi Sep 27, 2023
b600363
fmt
MrAliSalehi Sep 27, 2023
787ca24
moving reconnect logic to a separate function, remove unused code
MrAliSalehi Sep 27, 2023
ea9e6e8
remove unused imports
MrAliSalehi Sep 27, 2023
dba2022
fmt
MrAliSalehi Sep 27, 2023
836f41f
Merge branch 'autoreconnect'
MrAliSalehi Sep 27, 2023
b17bf42
another fmt
MrAliSalehi Sep 27, 2023
4efd6bb
change str to SocketAddr in `test_invoke_encrypted_method`
MrAliSalehi Sep 27, 2023
85520ef
Update lib/grammers-mtproto/src/mtp/mod.rs
MrAliSalehi Sep 27, 2023
e36c094
Update lib/grammers-mtsender/src/lib.rs
MrAliSalehi Sep 27, 2023
61556c4
Update lib/grammers-mtproto/src/transport/mod.rs
MrAliSalehi Sep 27, 2023
80caae0
Update lib/grammers-mtproto/src/mtp/plain.rs
MrAliSalehi Sep 27, 2023
0cdd369
moving connect methods outside the `Sender`
MrAliSalehi Sep 27, 2023
a8dd794
Merge remote-tracking branch 'origin/master'
MrAliSalehi Sep 27, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 30 additions & 10 deletions lib/grammers-mtproto/src/mtp/encrypted.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1159,19 +1159,27 @@ impl Mtp for Encrypted {
// Check to see if the next salt can be used already. If it can, drop the current one and,
// if the next salt is the last one, fetch more.
if let Some((start_secs, start_instant)) = self.start_salt_time {
if let Some(salt) = self.salts.get(self.salts.len() - 2) {
let now = start_secs + start_instant.elapsed().as_secs() as i32;
if now >= salt.valid_since + SALT_USE_DELAY {
self.salts.pop();
if self.salts.len() == 1 {
info!("only one future salt remaining; asking for more salts");
let body = tl::functions::GetFutureSalts {
num: NUM_FUTURE_SALTS,
let should_fetch_salts = self.salts.len() < 2
|| 'value: {
MrAliSalehi marked this conversation as resolved.
Show resolved Hide resolved
if let Some(salt) = self.salts.get(self.salts.len() - 2) {
let now = start_secs + start_instant.elapsed().as_secs() as i32;
if now >= salt.valid_since + SALT_USE_DELAY {
self.salts.pop();
if self.salts.len() == 1 {
info!("only one future salt remaining; asking for more salts");
break 'value true;
}
}
.to_bytes();
self.serialize_msg(&body, true);
}
false
};

if should_fetch_salts {
let body = tl::functions::GetFutureSalts {
num: NUM_FUTURE_SALTS,
}
.to_bytes();
self.serialize_msg(&body, true);
}
}

Expand Down Expand Up @@ -1246,6 +1254,18 @@ impl Mtp for Encrypted {
updates: mem::take(&mut self.updates),
})
}

fn reset(&mut self) {
self.client_id = {
let mut buffer = [0u8; 8];
getrandom(&mut buffer).expect("failed to generate a secure client_id");
i64::from_le_bytes(buffer)
};
self.sequence = 0;
self.last_msg_id = 0;
self.pending_ack.clear();
self.msg_count = 0;
}
}

#[cfg(test)]
Expand Down
3 changes: 3 additions & 0 deletions lib/grammers-mtproto/src/mtp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,9 @@ pub trait Mtp {

/// Deserializes a single incoming message payload into zero or more responses.
fn deserialize(&mut self, payload: &[u8]) -> Result<Deserialization, DeserializeError>;

/// Reset the state, as if a new instance was just created.
fn reset(&mut self);
}

#[cfg(test)]
Expand Down
4 changes: 4 additions & 0 deletions lib/grammers-mtproto/src/mtp/plain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ impl Mtp for Plain {
updates: Vec::new(),
})
}

fn reset(&mut self) {
self.buffer.clear();
}
}

#[cfg(test)]
Expand Down
4 changes: 4 additions & 0 deletions lib/grammers-mtproto/src/transport/abridged.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ impl Transport for Abridged {
output.put(&input[header_len..header_len + len]);
Ok(header_len + len)
}

fn reset(&mut self) {
self.init = false;
}
}

#[cfg(test)]
Expand Down
5 changes: 5 additions & 0 deletions lib/grammers-mtproto/src/transport/full.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ impl Transport for Full {
output.extend_from_slice(&input[8..len - 4]);
Ok(len)
}

fn reset(&mut self) {
self.recv_seq = 0;
self.send_seq = 0;
}
}

#[cfg(test)]
Expand Down
4 changes: 4 additions & 0 deletions lib/grammers-mtproto/src/transport/intermediate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ impl Transport for Intermediate {

Ok(len + 4)
}

fn reset(&mut self) {
self.init = false;
}
}

#[cfg(test)]
Expand Down
3 changes: 3 additions & 0 deletions lib/grammers-mtproto/src/transport/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,7 @@ pub trait Transport {
///
/// If successful, returns how many bytes of `input` were used.
fn unpack(&mut self, input: &[u8], output: &mut BytesMut) -> Result<usize, Error>;

/// Reset the state, as if a new instance was just created.
fn reset(&mut self);
}
Loading