Skip to content

Commit

Permalink
Fix infinite recursion on Dispose (#274) 🎬Take 3
Browse files Browse the repository at this point in the history
  • Loading branch information
wiz0u committed Sep 7, 2024
1 parent 68a1c86 commit b6cb627
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions src/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,7 @@ private Session.DCSession GetOrCreateDCSession(int dcId, DcOption.Flags flags)
flags &= ~DcOption.Flags.media_only;
dcId = Math.Abs(dcId);
}
var dcOptions = _session.DcOptions.Where(dc => dc.id == Math.Abs(dcId))
.OrderBy(dc => dc.flags.HasFlag(DcOption.Flags.media_only) ^ (dcId < 0)).ThenBy(dc => dc.flags ^ flags);
var dcOptions = GetDcOptions(Math.Abs(dcId), flags);
var dcOption = dcOptions.FirstOrDefault() ?? throw new WTException($"Could not find adequate dc_option for DC {dcId}");
dcSession ??= new Session.DCSession { Id = Helpers.RandomLong() }; // create new session only if not already existing
dcSession.DataCenter = dcOption;
Expand Down Expand Up @@ -831,6 +830,14 @@ public async Task ConnectAsync(bool quickResume = false)
await _connecting;
}

private IEnumerable<DcOption> GetDcOptions(int dcId, DcOption.Flags flags) => !flags.HasFlag(DcOption.Flags.media_only)
? _session.DcOptions.Where(dc => dc.id == dcId && (dc.flags & (DcOption.Flags.cdn | DcOption.Flags.tcpo_only | DcOption.Flags.media_only)) == 0)
.OrderBy(dc => dc.flags ^ flags)
: _session.DcOptions.Where(dc => dc.id == dcId && (dc.flags & (DcOption.Flags.cdn | DcOption.Flags.tcpo_only)) == 0)
.OrderBy(dc => ~dc.flags & DcOption.Flags.media_only).ThenBy(dc => dc.flags ^ flags)
.Select(dc => dc.flags.HasFlag(DcOption.Flags.media_only) ? dc : new DcOption { id = dc.id, port = dc.port,
ip_address = dc.ip_address, secret = dc.secret, flags = dc.flags | DcOption.Flags.media_only });

private async Task DoConnectAsync(bool quickResume)
{
_cts = new();
Expand Down Expand Up @@ -883,11 +890,7 @@ private async Task DoConnectAsync(bool quickResume)
var triedEndpoints = new HashSet<IPEndPoint> { endpoint };
if (_session.DcOptions != null)
{
var flags = _dcSession.DataCenter.flags;
var altOptions = _session.DcOptions.Where(dc => dc.id == _dcSession.DataCenter.id && dc.flags != flags
&& (dc.flags & DcOption.Flags.media_only) <= (flags & DcOption.Flags.media_only)
&& (dc.flags & (DcOption.Flags.cdn | DcOption.Flags.tcpo_only)) == 0)
.OrderBy(dc => (dc.flags ^ flags) & DcOption.Flags.media_only).ThenBy(dc => dc.flags ^ flags);
var altOptions = GetDcOptions(_dcSession.DataCenter.id, _dcSession.DataCenter.flags);
// try alternate addresses for this DC
foreach (var dcOption in altOptions)
{
Expand All @@ -897,11 +900,7 @@ private async Task DoConnectAsync(bool quickResume)
try
{
tcpClient = await TcpHandler(endpoint.Address.ToString(), endpoint.Port);
if (((dcOption.flags ^ flags) & DcOption.Flags.media_only) == 0) // test to prevent AltDC becoming MainDC
_dcSession.DataCenter = dcOption;
else
_dcSession.DataCenter = new DcOption { flags = dcOption.flags ^ DcOption.Flags.media_only,
id = dcOption.id, ip_address = dcOption.ip_address, port = dcOption.port, secret = dcOption.secret };
_dcSession.DataCenter = dcOption;
break;
}
catch (SocketException) { }
Expand Down

0 comments on commit b6cb627

Please sign in to comment.