Skip to content

Commit 762dd71

Browse files
committed
Fixed disconnect reasons not passed (fixes #717)
1 parent f385f02 commit 762dd71

File tree

5 files changed

+24
-18
lines changed

5 files changed

+24
-18
lines changed

TS3AudioBot/Ts3Client.cs

+1
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,7 @@ private void TsFullClient_OnDisconnected(object sender, DisconnectEventArgs e)
579579
e.ExitReason == Reason.KickedFromServer ? ReconnectType.Kick :
580580
e.ExitReason == Reason.ServerShutdown || e.ExitReason == Reason.ServerStopped ? ReconnectType.ServerShutdown :
581581
e.ExitReason == Reason.Banned ? ReconnectType.Ban :
582+
e.ExitReason == Reason.SocketError ? ReconnectType.Error :
582583
ReconnectType.None))
583584
return;
584585
}

TSLib/Full/PacketHandler.cs

+6-5
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ internal sealed class PacketHandler<TIn, TOut>
6565
private int closed; // bool
6666

6767
public PacketEvent<TIn> PacketEvent;
68-
public Action<Reason> StopEvent;
68+
public Action<Reason?> StopEvent;
6969

7070
public PacketHandler(TsCrypt ts3Crypt, Id id)
7171
{
@@ -165,7 +165,8 @@ private void Initialize(IPEndPoint address, bool connect)
165165
}
166166
}
167167

168-
public void Stop(Reason closeReason = Reason.Timeout)
168+
public void Stop() => Stop(null);
169+
private void Stop(Reason? closeReason)
169170
{
170171
var wasClosed = Interlocked.Exchange(ref closed, 1);
171172
if (wasClosed != 0)
@@ -345,7 +346,7 @@ private static void FetchPacketEvent(object selfObj, SocketAsyncEventArgs args)
345346
Log.Debug("Socket error: {@args}", args);
346347
if (args.SocketError == SocketError.ConnectionReset)
347348
{
348-
self.Stop();
349+
self.Stop(Reason.SocketError);
349350
}
350351
}
351352

@@ -672,7 +673,7 @@ private void ResendLoop()
672673
}
673674
if (close)
674675
{
675-
Stop();
676+
Stop(Reason.Timeout);
676677
return;
677678
}
678679

@@ -695,7 +696,7 @@ private void ResendLoop()
695696
if (elapsed > PacketTimeout)
696697
{
697698
LogTimeout.Debug("TIMEOUT: Got no ping packet response for {0}", elapsed);
698-
Stop();
699+
Stop(Reason.Timeout);
699700
return;
700701
}
701702
}

TSLib/Full/QuickerLz.cs

+13-11
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public static Span<byte> Compress(ReadOnlySpan<byte> data, int level)
105105
}
106106
else
107107
{
108-
Write24(dest, destPos, hash << 4 | (matchlen << 16));
108+
Write24(destSpan, destPos, hash << 4 | (matchlen << 16));
109109
destPos += 3;
110110
}
111111
sourcePos += matchlen;
@@ -262,28 +262,30 @@ private static void WriteHeader(Span<byte> dest, int srcLen, int level, int head
262262
}
263263

264264
[MethodImpl(MethodImplOptions.AggressiveInlining)]
265-
private static void Write24(byte[] outArr, int outOff, int value)
265+
private static void Write24(Span<byte> outArr, int outOff, int value)
266266
{
267-
outArr[outOff + 0] = unchecked((byte)(value >> 00));
268-
outArr[outOff + 1] = unchecked((byte)(value >> 08));
269-
outArr[outOff + 2] = unchecked((byte)(value >> 16));
267+
var sli3 = outArr.Slice(outOff, 3);
268+
BinaryPrimitives.WriteUInt16LittleEndian(sli3, unchecked((ushort)value));
269+
sli3[2] = unchecked((byte)(value >> 16));
270270
}
271271

272272
[MethodImpl(MethodImplOptions.AggressiveInlining)]
273273
private static int Read24(ReadOnlySpan<byte> intArr, int inOff)
274-
=> unchecked(intArr[inOff] | (intArr[inOff + 1] << 8) | (intArr[inOff + 2] << 16));
274+
{
275+
var sli3 = intArr.Slice(inOff, 3);
276+
return unchecked(BinaryPrimitives.ReadUInt16LittleEndian(sli3) | (sli3[2] << 16));
277+
}
275278

276279
[MethodImpl(MethodImplOptions.AggressiveInlining)]
277280
private static int Hash(int value) => ((value >> 12) ^ value) & 0xfff;
278281

279282
[MethodImpl(MethodImplOptions.AggressiveInlining)]
280283
private static bool Is6Same(ReadOnlySpan<byte> arr)
281284
{
282-
return arr[0] == arr[1]
283-
&& arr[1] == arr[2]
284-
&& arr[2] == arr[3]
285-
&& arr[3] == arr[4]
286-
&& arr[4] == arr[5];
285+
var sli6 = arr.Slice(0, 6);
286+
var i0 = BinaryPrimitives.ReadUInt32LittleEndian(sli6);
287+
var u1 = BinaryPrimitives.ReadUInt16LittleEndian(sli6.Slice(4));
288+
return i0 == i0 >> 8 && unchecked((ushort)i0) == u1;
287289
}
288290

289291
/// <summary>Copy <code>[start; start + length)</code> bytes from `data` to the end of `data`</summary>

TSLib/Full/TsFullClient.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public override void Connect(ConnectionData conData)
9797

9898
packetHandler = new PacketHandler<S2C, C2S>(tsCrypt, conData.LogId);
9999
packetHandler.PacketEvent = (ref Packet<S2C> packet) => { PacketEvent(ctx, ref packet); };
100-
packetHandler.StopEvent = (closeReason) => { ctx.ExitReason = closeReason; DisconnectInternal(ctx, setStatus: TsClientStatus.Disconnected); };
100+
packetHandler.StopEvent = (closeReason) => { ctx.ExitReason = ctx.ExitReason ?? closeReason; DisconnectInternal(ctx, setStatus: TsClientStatus.Disconnected); };
101101
packetHandler.Connect(remoteAddress);
102102
dispatcher = new ExtraThreadEventDispatcher();
103103
dispatcher.Init(InvokeEvent, conData.LogId);
@@ -284,7 +284,7 @@ partial void ProcessEachClientLeftView(ClientLeftView clientLeftView)
284284
{
285285
if (clientLeftView.ClientId == packetHandler.ClientId)
286286
{
287-
context.ExitReason = Reason.LeftServer;
287+
context.ExitReason = clientLeftView.Reason;
288288
DisconnectInternal(context, setStatus: TsClientStatus.Disconnected);
289289
}
290290
}

TSLib/TsEnums.cs

+2
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ public enum Reason
8282
ChannelUpdated,
8383
ServerOrChannelEdited,
8484
ServerShutdown,
85+
86+
SocketError = 1000,
8587
}
8688

8789
public enum GroupWhisperType : byte

0 commit comments

Comments
 (0)