Fix Salts management

This commit is contained in:
Wizou 2025-06-29 16:01:05 +02:00
parent 3ff1200068
commit 25990a8477

View file

@ -140,6 +140,7 @@ namespace WTelegram
TcpHandler = cloneOf.TcpHandler; TcpHandler = cloneOf.TcpHandler;
MTProxyUrl = cloneOf.MTProxyUrl; MTProxyUrl = cloneOf.MTProxyUrl;
PingInterval = cloneOf.PingInterval; PingInterval = cloneOf.PingInterval;
MaxAutoReconnects = cloneOf.MaxAutoReconnects;
TLConfig = cloneOf.TLConfig; TLConfig = cloneOf.TLConfig;
_dcSession = dcSession; _dcSession = dcSession;
} }
@ -561,7 +562,7 @@ namespace WTelegram
{ {
var keys = _dcSession.Salts.Keys; var keys = _dcSession.Salts.Keys;
if (keys[^1] == DateTime.MaxValue) return; // GetFutureSalts ongoing if (keys[^1] == DateTime.MaxValue) return; // GetFutureSalts ongoing
var now = DateTime.UtcNow.AddTicks(_dcSession.serverTicksOffset); var now = DateTime.UtcNow.AddTicks(_dcSession.serverTicksOffset - TimeSpan.TicksPerMinute);
bool removed = false; bool removed = false;
for (; keys.Count > 1 && keys[1] < now; _dcSession.OldSalt = _dcSession.Salt, _dcSession.Salt = _dcSession.Salts.Values[0], removed = true) for (; keys.Count > 1 && keys[1] < now; _dcSession.OldSalt = _dcSession.Salt, _dcSession.Salt = _dcSession.Salts.Values[0], removed = true)
_dcSession.Salts.RemoveAt(0); _dcSession.Salts.RemoveAt(0);
@ -742,10 +743,7 @@ namespace WTelegram
case MsgsAck msgsAck: case MsgsAck msgsAck:
break; // we don't do anything with these, for now break; // we don't do anything with these, for now
case BadMsgNotification badMsgNotification: case BadMsgNotification badMsgNotification:
await _sendSemaphore.WaitAsync(); bool retryRpcs = true;
bool retryLast = badMsgNotification.bad_msg_id == _dcSession.lastSentMsgId;
var lastSentMsg = _lastSentMsg;
_sendSemaphore.Release();
var logLevel = badMsgNotification.error_code == 48 ? 2 : 4; var logLevel = badMsgNotification.error_code == 48 ? 2 : 4;
Helpers.Log(logLevel, $"BadMsgNotification {badMsgNotification.error_code} for msg #{(short)badMsgNotification.bad_msg_id.GetHashCode():X4}"); Helpers.Log(logLevel, $"BadMsgNotification {badMsgNotification.error_code} for msg #{(short)badMsgNotification.bad_msg_id.GetHashCode():X4}");
switch (badMsgNotification.error_code) switch (badMsgNotification.error_code)
@ -760,7 +758,7 @@ namespace WTelegram
case 32: // msg_seqno too low (the server has already received a message with a lower msg_id but with either a higher or an equal and odd seqno) case 32: // msg_seqno too low (the server has already received a message with a lower msg_id but with either a higher or an equal and odd seqno)
case 33: // msg_seqno too high (similarly, there is a message with a higher msg_id but with either a lower or an equal and odd seqno) case 33: // msg_seqno too high (similarly, there is a message with a higher msg_id but with either a lower or an equal and odd seqno)
if (_dcSession.seqno <= 1) if (_dcSession.seqno <= 1)
retryLast = false; retryRpcs = false;
else else
{ {
await ResetAsync(false, false); await ResetAsync(false, false);
@ -775,25 +773,19 @@ namespace WTelegram
CheckSalt(); CheckSalt();
break; break;
default: default:
retryLast = false; retryRpcs = false;
break; break;
} }
if (retryLast) if (retryRpcs)
{ {
Rpc prevRequest;
lock (_pendingRpcs) lock (_pendingRpcs)
_pendingRpcs.TryGetValue(badMsgNotification.bad_msg_id, out prevRequest);
await SendAsync(lastSentMsg, lastSentMsg is not MsgContainer, prevRequest);
lock (_pendingRpcs)
_pendingRpcs.Remove(badMsgNotification.bad_msg_id);
}
else if (PullPendingRequest(badMsgNotification.bad_msg_id) is Rpc rpc)
{ {
if (_bareRpc?.msgId == badMsgNotification.bad_msg_id) _bareRpc = null; foreach (var rpc in _pendingRpcs.Values)
rpc.tcs.SetException(new WTException($"BadMsgNotification {badMsgNotification.error_code}")); rpc.tcs.TrySetResult(new RpcError { error_code = -503, error_message = $"BadMsgNotification {badMsgNotification.error_code}" });
_pendingRpcs.Clear();
} }
else
RaiseUpdates(badMsgNotification); RaiseUpdates(badMsgNotification);
}
break; break;
default: default:
RaiseUpdates(obj); RaiseUpdates(obj);