Compare commits

...
6 Commits
Author SHA1 Message Date
erikbrockoandClaude Opus 5.5 004aa41732 Pad a short RSA puzzle answer instead of failing the connect
An answer with a leading zero byte comes out of toByteArray() as 63 bytes,
and copying it started one byte too far, failing about one connect in 256.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-14 20:17:49 +00:00
erikbrockoandClaude Opus 5.5 0549b6bf02 Sign myTeamSpeak identity into clientinit
Adds an authenticator hook that appends the myTeamSpeak parameters to
clientinit/updatemytsid, and the per-connection challenge they sign.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-10 20:04:55 +00:00
erikbrockoandClaude Opus 5.5 1430782e2a Send the microphone's voice as whispers to a target
setWhisperTarget routes voice into VOICE_WHISPER packets to channels and
clients until cleared; a voice session under way ends on its old route
first. Whisper packets carry their own packet id and get the redundant
copies voice does.

WhisperTargetMultiple sized its two counts as two bytes each, leaving
zero bytes after the voice data, and read the client ids by the channel
count.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-09 22:18:07 +00:00
erikbrockoandClaude Opus 5.5 2d6f5cdf78 Send every ready live-capture frame per tick and cap the timer's catch-up burst
A mic running faster than the 20 ms timer filled the queue until frames were
dropped; live sources now drain each tick. The catch-up clamp was inverted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-08 20:14:42 +00:00
erikbrockoandClaude Opus 5.5 a4ce0feb61 Report a minute's bandwidth as bytes per second
setconnectioninfo sent the bytes of the whole last minute where the
server expects the minute's average rate, so others saw our bandwidth
60 times too high. PacketStatistics gains the per-second averages, and
the packets-last-minute count looked at the last second only.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-06 20:26:59 +00:00
erikbrockoandClaude Opus 5.5 90bf6e030c Resend voice packets for loss resilience
A microphone can ask for every voice packet to be sent again, a number
of times at a fixed spacing. The copies are the same encrypted packet,
so listeners keep whichever arrives first. A TS3 server forwards them,
and a jitter buffer that sees them arrive late grows to wait for them:
the sender buys its listeners a deeper buffer, trading latency for
riding out loss, with native clients as much as with others.

Copies go out from the 20 ms microphone timer, so the spacing is best a
whole number of ticks. Copies of a connection that is gone are dropped.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-06 20:17:13 +00:00
10 changed files with 294 additions and 59 deletions
@@ -12,4 +12,27 @@ public interface Microphone {
CodecType getCodec(); CodecType getCodec();
byte[] provide(); byte[] provide();
/**
* Milliseconds between a voice packet and each copy of it sent afterwards, or 0 to send it
* once. A copy is the same packet, so receivers keep whichever arrives first; a jitter buffer
* that sees copies arrive late grows to wait for them, trading latency for riding out loss.
*/
default int getRedundancyDelay() {
return 0;
}
/**
* Whether {@link #provide} hands out frames captured on a clock of their own, such as a sound
* card's. That clock drifts against the send timer, so each tick then sends every frame that is
* ready instead of one, keeping the drift from piling up as latency or overflowing a queue.
*/
default boolean isLiveCapture() {
return false;
}
/** How many copies follow each voice packet, {@link #getRedundancyDelay} apart. */
default int getRedundancyCopies() {
return 1;
}
} }
@@ -239,13 +239,13 @@ public class PacketBody1VoiceWhisper extends PacketBody {
channelIds[n] = (int) (buffer.getLong()); channelIds[n] = (int) (buffer.getLong());
clientIds = new int[clientIdCount]; clientIds = new int[clientIdCount];
for (int n = 0; n < channelIdCount; n ++) for (int n = 0; n < clientIdCount; n ++)
clientIds[n] = (buffer.getShort() & 0xFFFF); clientIds[n] = (buffer.getShort() & 0xFFFF);
} }
@Override @Override
public int getSize() { public int getSize() {
return 2 + 2 + return 1 + 1 +
(channelIds == null ? 0 : channelIds.length * 8) + (channelIds == null ? 0 : channelIds.length * 8) +
(clientIds == null ? 0 : clientIds.length * 2); (clientIds == null ? 0 : clientIds.length * 2);
} }
@@ -159,16 +159,7 @@ public class LocalClientHandlerConnecting extends LocalClientHandler {
BigInteger x = new BigInteger(1, serverReplyStep3.getX()); BigInteger x = new BigInteger(1, serverReplyStep3.getX());
BigInteger n = new BigInteger(1, serverReplyStep3.getN()); BigInteger n = new BigInteger(1, serverReplyStep3.getN());
byte[] y = new byte[64]; byte[] y = unsigned64(x.modPow(BigInteger.valueOf(2L).pow(serverReplyStep3.getLevel()), n));
byte[] solution =
x.modPow(BigInteger.valueOf(2L).pow(serverReplyStep3.getLevel()), n).toByteArray();
System.arraycopy(
solution, Math.abs(solution.length - 64),
y, solution.length < 64 ? 64 - solution.length : 0,
solution.length < 64 ? solution.length : 64
);
// Build response // Build response
PacketBody8Init1.Step4 step4 = new PacketBody8Init1.Step4(); PacketBody8Init1.Step4 step4 = new PacketBody8Init1.Step4();
@@ -355,8 +346,22 @@ public class LocalClientHandlerConnecting extends LocalClientHandler {
) )
); );
Ts3Debugging.debug(clientinit.build()); getClient().addMyTeamSpeakParameters(clientinit);
Ts3Debugging.debug("Sending clientinit");
getClient().writePacket(new PacketBody2Command(ProtocolRole.CLIENT, clientinit)); getClient().writePacket(new PacketBody2Command(ProtocolRole.CLIENT, clientinit));
} }
/**
* The puzzle's answer as 64 unsigned big-endian bytes. {@link BigInteger#toByteArray()}
* adds a sign byte when the top bit is set and drops leading zero bytes, so it can be
* 65 bytes or fewer than 64.
*/
static byte[] unsigned64(BigInteger value) {
byte[] bytes = value.toByteArray();
byte[] y = new byte[64];
int length = Math.min(bytes.length, 64);
System.arraycopy(bytes, bytes.length - length, y, 64 - length, length);
return y;
}
} }
@@ -61,7 +61,7 @@ public class PacketStatistics {
} }
public int getSentPacketsLastMinute() { public int getSentPacketsLastMinute() {
long now = System.nanoTime() - 1_000_000_000L; long now = System.nanoTime() - 60_000_000_000L;
synchronized (sentPacketList) { synchronized (sentPacketList) {
return (int) sentPacketList.stream() return (int) sentPacketList.stream()
@@ -124,6 +124,16 @@ public class PacketStatistics {
} }
} }
/** Average bytes per second sent over the last minute, as TeamSpeak reports a minute's bandwidth. */
public int getSentBandwidthLastMinute() {
return getSentBytesLastMinute() / 60;
}
/** Average bytes per second received over the last minute. */
public int getReceivedBandwidthLastMinute() {
return getReceivedBytesLastMinute() / 60;
}
public int getReceivedBytesLastMinute() { public int getReceivedBytesLastMinute() {
long now = System.nanoTime() - 60_000_000_000L; long now = System.nanoTime() - 60_000_000_000L;
@@ -32,6 +32,12 @@ public class PacketTransformation {
return fakeMac; return fakeMac;
} }
/** Connection binding used by myTeamSpeak, in the AES-128/SHA-256 protocol mode. */
public byte[] getMyTeamSpeakChallenge() {
return Ts3Crypt.hash256(ByteBuffer.allocate(6 + ivStruct.length)
.put(new byte[] {0x30, 8, 0, 0, 0, 0}).put(ivStruct).array());
}
public Pair<byte[], byte[]> computeParameters(PacketHeader header) { public Pair<byte[], byte[]> computeParameters(PacketHeader header) {
ByteBuffer temporaryByteBuffer = ByteBuffer ByteBuffer temporaryByteBuffer = ByteBuffer
.allocate(ivStruct.length == 20 ? 26 : 70) .allocate(ivStruct.length == 20 ? 26 : 70)
@@ -3,6 +3,7 @@ package com.github.manevolent.ts3j.protocol.socket.client;
import com.github.manevolent.ts3j.command.CommandException; import com.github.manevolent.ts3j.command.CommandException;
import com.github.manevolent.ts3j.command.*; import com.github.manevolent.ts3j.command.*;
import com.github.manevolent.ts3j.command.parameter.CommandSingleParameter;
import com.github.manevolent.ts3j.identity.Identity; import com.github.manevolent.ts3j.identity.Identity;
import com.github.manevolent.ts3j.protocol.NetworkPacket; import com.github.manevolent.ts3j.protocol.NetworkPacket;
import com.github.manevolent.ts3j.protocol.Packet; import com.github.manevolent.ts3j.protocol.Packet;
@@ -36,6 +37,7 @@ import java.nio.*;
import java.util.*; import java.util.*;
import java.util.concurrent.*; import java.util.concurrent.*;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.Function;
/** /**
* Server/client common abstract client socket, used to interact with the UDP connection. Provides most of the protocol * Server/client common abstract client socket, used to interact with the UDP connection. Provides most of the protocol
@@ -165,6 +167,21 @@ public abstract class AbstractTeamspeakClientSocket
setTransformation(new PacketTransformation(parameters.getIvStruct(), parameters.getFakeSignature())); setTransformation(new PacketTransformation(parameters.getIvStruct(), parameters.getFakeSignature()));
} }
private volatile Function<byte[], Map<String, String>> myTeamSpeakAuthenticator;
/** Set before connecting; invoked after the handshake with this connection's challenge. */
public void setMyTeamSpeakAuthenticator(Function<byte[], Map<String, String>> authenticator) {
myTeamSpeakAuthenticator = authenticator;
}
public void addMyTeamSpeakParameters(Command command) {
Function<byte[], Map<String, String>> authenticator = myTeamSpeakAuthenticator;
if (authenticator == null) return;
Map<String, String> parameters = authenticator.apply(transformation.getMyTeamSpeakChallenge());
for (Map.Entry<String, String> entry : parameters.entrySet())
command.add(new CommandSingleParameter(entry.getKey(), entry.getValue()));
}
protected abstract NetworkPacket readNetworkPacket(int timeout) throws IOException; protected abstract NetworkPacket readNetworkPacket(int timeout) throws IOException;
protected abstract void writeNetworkPacket(NetworkPacket packet) throws IOException; protected abstract void writeNetworkPacket(NetworkPacket packet) throws IOException;
@@ -478,6 +495,8 @@ public abstract class AbstractTeamspeakClientSocket
// > X is a ushort in H2N order of an own audio packet counter // > X is a ushort in H2N order of an own audio packet counter
// it seems it can be the same as the packet counter so we will let the packethandler do it. // it seems it can be the same as the packet counter so we will let the packethandler do it.
((PacketBody0Voice)packet.getBody()).setPacketId(header.getPacketId()); ((PacketBody0Voice)packet.getBody()).setPacketId(header.getPacketId());
} else if (header.getType() == PacketBodyType.VOICE_WHISPER) {
((PacketBody1VoiceWhisper)packet.getBody()).setPacketId(header.getPacketId());
} }
// Flush to a buffer // Flush to a buffer
@@ -16,8 +16,12 @@ import com.github.manevolent.ts3j.identity.Uid;
import com.github.manevolent.ts3j.protocol.*; import com.github.manevolent.ts3j.protocol.*;
import com.github.manevolent.ts3j.protocol.client.ClientConnectionState; import com.github.manevolent.ts3j.protocol.client.ClientConnectionState;
import com.github.manevolent.ts3j.protocol.header.PacketHeader; import com.github.manevolent.ts3j.protocol.header.PacketHeader;
import com.github.manevolent.ts3j.enums.CodecType;
import com.github.manevolent.ts3j.protocol.packet.PacketBody;
import com.github.manevolent.ts3j.protocol.packet.PacketBody0Voice; import com.github.manevolent.ts3j.protocol.packet.PacketBody0Voice;
import com.github.manevolent.ts3j.protocol.packet.PacketBody1VoiceWhisper;
import com.github.manevolent.ts3j.protocol.packet.PacketBody2Command; import com.github.manevolent.ts3j.protocol.packet.PacketBody2Command;
import com.github.manevolent.ts3j.protocol.packet.PacketBodyType;
import com.github.manevolent.ts3j.protocol.packet.handler.PacketHandler; import com.github.manevolent.ts3j.protocol.packet.handler.PacketHandler;
import com.github.manevolent.ts3j.protocol.packet.handler.client.LocalClientHandlerConnected; import com.github.manevolent.ts3j.protocol.packet.handler.client.LocalClientHandlerConnected;
import com.github.manevolent.ts3j.protocol.packet.handler.client.LocalClientHandlerConnecting; import com.github.manevolent.ts3j.protocol.packet.handler.client.LocalClientHandlerConnecting;
@@ -53,7 +57,13 @@ public class LocalTeamspeakClientSocket
private volatile InetSocketAddress remote; private volatile InetSocketAddress remote;
private Microphone microphone; private Microphone microphone;
private volatile PacketBody1VoiceWhisper.WhisperTarget whisperTarget;
private HighPrecisionTimer microphoneThread; private HighPrecisionTimer microphoneThread;
/** Voice packets still to be sent again, soonest first; see {@link Microphone#getRedundancyDelay}. */
private final Queue<VoiceCopy> voiceCopies =
new PriorityBlockingQueue<>(64, Comparator.comparingLong((VoiceCopy copy) -> copy.due));
private volatile long voiceCopyDelayNanos;
private volatile int voiceCopyCount;
private List<TS3Listener> listeners = new LinkedList<>(); private List<TS3Listener> listeners = new LinkedList<>();
@@ -144,11 +154,11 @@ public class LocalTeamspeakClientSocket
response.add(new CommandSingleParameter("connection_bandwidth_sent_last_second_" + name, response.add(new CommandSingleParameter("connection_bandwidth_sent_last_second_" + name,
Integer.toString(stats.getSentBytesLastSecond()))); Integer.toString(stats.getSentBytesLastSecond())));
response.add(new CommandSingleParameter("connection_bandwidth_sent_last_minute_" + name, response.add(new CommandSingleParameter("connection_bandwidth_sent_last_minute_" + name,
Integer.toString(stats.getSentBytesLastMinute()))); Integer.toString(stats.getSentBandwidthLastMinute())));
response.add(new CommandSingleParameter("connection_bandwidth_received_last_second_" + name, response.add(new CommandSingleParameter("connection_bandwidth_received_last_second_" + name,
Integer.toString(stats.getReceivedBytesLastSecond()))); Integer.toString(stats.getReceivedBytesLastSecond())));
response.add(new CommandSingleParameter("connection_bandwidth_received_last_minute_" + name, response.add(new CommandSingleParameter("connection_bandwidth_received_last_minute_" + name,
Integer.toString(stats.getReceivedBytesLastMinute()))); Integer.toString(stats.getReceivedBandwidthLastMinute())));
} }
response.add(new CommandSingleParameter("connection_server2client_packetloss_total", Integer.toString(0))); response.add(new CommandSingleParameter("connection_server2client_packetloss_total", Integer.toString(0)));
@@ -278,6 +288,40 @@ public class LocalTeamspeakClientSocket
packet.getDatagram().setSocketAddress(remote); packet.getDatagram().setSocketAddress(remote);
socket.send(packet.getDatagram()); socket.send(packet.getDatagram());
long copyDelay = voiceCopyDelayNanos;
PacketBodyType type = packet.getHeader().getType();
if (copyDelay > 0 && (type == PacketBodyType.VOICE || type == PacketBodyType.VOICE_WHISPER)) {
// Aim half a tick early, so each copy goes out on the tick nearest its time.
long sent = System.nanoTime() - TICK_MS * 1_000_000L / 2;
for (int i = 1, n = voiceCopyCount; i <= n; i++)
voiceCopies.add(new VoiceCopy(sent + i * copyDelay, packet.getDatagram()));
}
}
/** Sends the voice copies that are due; runs on the microphone timer. */
private void sendDueVoiceCopies() {
long now = System.nanoTime();
VoiceCopy copy;
while ((copy = voiceCopies.peek()) != null && now - copy.due >= 0) {
// Whatever poll() hands out is due no later than the one peeked at.
copy = voiceCopies.poll();
try {
socket.send(copy.datagram);
} catch (IOException e) {
getExceptionHandler().accept(e);
}
}
}
private static final class VoiceCopy {
final long due;
final DatagramPacket datagram;
VoiceCopy(long due, DatagramPacket datagram) {
this.due = due;
this.datagram = datagram;
}
} }
@@ -546,17 +590,31 @@ public class LocalTeamspeakClientSocket
return microphone; return microphone;
} }
/** Where the microphone's voice goes: null for our channel, else whisper packets to the target. */
public PacketBody1VoiceWhisper.WhisperTarget getWhisperTarget() {
return whisperTarget;
}
/**
* Sends the microphone's voice to {@code target} as whispers, or to our channel again with null.
* A voice session under way ends on its old route first.
*/
public void setWhisperTarget(PacketBody1VoiceWhisper.WhisperTarget target) {
this.whisperTarget = target;
}
public void setMicrophone(Microphone microphone) { public void setMicrophone(Microphone microphone) {
if (this.microphone != microphone) { if (this.microphone != microphone) {
this.microphone = microphone; this.microphone = microphone;
if (microphone != null && microphoneThread == null) { if (microphone != null && microphoneThread == null) {
microphoneThread = new HighPrecisionTimer( microphoneThread = new HighPrecisionTimer(
20, TICK_MS,
5F, // up to 100ms of drift/catchup 5F, // up to 100ms of drift/catchup
new MicrophoneTask() new MicrophoneTask()
); );
microphoneThread.setDaemon(true); microphoneThread.setDaemon(true);
microphoneThread.setPriority(Thread.MAX_PRIORITY);
if (getState() != ClientConnectionState.DISCONNECTED) if (getState() != ClientConnectionState.DISCONNECTED)
microphoneThread.start(); microphoneThread.start();
@@ -620,14 +678,24 @@ public class LocalTeamspeakClientSocket
} }
} }
private static final int TICK_MS = 20;
private class MicrophoneTask implements Runnable { private class MicrophoneTask implements Runnable {
private boolean wasSendingAudio = false; private boolean wasSendingAudio = false;
/** Where the voice session under way goes; null for the channel. */
private PacketBody1VoiceWhisper.WhisperTarget sendingTarget;
private byte sessionId = 1; private byte sessionId = 1;
private int flaggedPackets = 5; private int flaggedPackets = 5;
@Override @Override
public void run() { public void run() {
if (!isConnected()) return; if (!isConnected()) {
// Copies from a connection that is gone must not reach the next one.
voiceCopies.clear();
return;
}
sendDueVoiceCopies();
Microphone microphone; Microphone microphone;
@@ -636,54 +704,84 @@ public class LocalTeamspeakClientSocket
if ((microphone = LocalTeamspeakClientSocket.this.microphone) == null) if ((microphone = LocalTeamspeakClientSocket.this.microphone) == null)
return; return;
// Copies are sent from this tick, so the delay is best a whole number of ticks.
int copyDelay = microphone.getRedundancyDelay();
int copies = microphone.getRedundancyCopies();
voiceCopyCount = copies;
voiceCopyDelayNanos = copyDelay > 0 && copies > 0 ? copyDelay * 1_000_000L : 0;
PacketBody1VoiceWhisper.WhisperTarget target = whisperTarget;
if (wasSendingAudio && target != sendingTarget) {
// The listeners of the old route hear the end of what they were hearing.
if (!sendEnd(microphone)) return;
}
if (microphone.isMuted()) { if (microphone.isMuted()) {
// TODO // TODO
// Set a state and send a muted event // Set a state and send a muted event
} else if (microphone.isReady()) { } else if (microphone.isReady()) {
byte[] data = microphone.provide(); boolean live = microphone.isLiveCapture();
if (data.length <= 0) byte[] data;
return; do {
data = microphone.provide();
if (data.length <= 0) break;
PacketBody0Voice voice = new PacketBody0Voice(getRole().getOut()); try {
writePacket(voicePacket(target, microphone.getCodec(), data,
flaggedPackets > 0 && target == null ? sessionId : null));
voice.setCodecType(microphone.getCodec()); flaggedPackets--;
voice.setCodecData(data); wasSendingAudio = true;
if (flaggedPackets > 0) voice.setServerFlag0(sessionId); sendingTarget = target;
} catch (Exception e) {
try { // All we are really concerned about here is a disconnection event, in which case the loop
writePacket(voice); // will exit.
getExceptionHandler().accept(e);
flaggedPackets--; return;
wasSendingAudio = true; }
} catch (Exception e) { } while (live && microphone.isReady());
// All we are really concerned about here is a disconnection event, in which case the loop
// will exit.
getExceptionHandler().accept(e);
}
} else if (wasSendingAudio) { } else if (wasSendingAudio) {
PacketBody0Voice voice = new PacketBody0Voice(getRole().getOut()); sendEnd(microphone);
voice.setCodecType(microphone.getCodec());
voice.setCodecData(new byte[0]);
try {
writePacket(voice);
// reset decoder flag
flaggedPackets = 5;
sessionId++;
if (sessionId >= 8)
sessionId = 1;
wasSendingAudio = false;
} catch (Exception e) {
// All we are really concerned about here is a disconnection event, in which case the loop
// will exit.
getExceptionHandler().accept(e);
}
} }
} }
/** Ends the voice session on the route it went, which an empty packet does; false once disconnected. */
private boolean sendEnd(Microphone microphone) {
try {
writePacket(voicePacket(sendingTarget, microphone.getCodec(), new byte[0], null));
// reset decoder flag
flaggedPackets = 5;
sessionId++;
if (sessionId >= 8)
sessionId = 1;
wasSendingAudio = false;
return true;
} catch (Exception e) {
// All we are really concerned about here is a disconnection event, in which case the loop
// will exit.
getExceptionHandler().accept(e);
return false;
}
}
private PacketBody voicePacket(PacketBody1VoiceWhisper.WhisperTarget target, CodecType codec, byte[] data,
Byte flag) {
if (target == null) {
PacketBody0Voice voice = new PacketBody0Voice(getRole().getOut());
voice.setCodecType(codec);
voice.setCodecData(data);
if (flag != null) voice.setServerFlag0(flag);
return voice;
}
PacketBody1VoiceWhisper whisper = new PacketBody1VoiceWhisper(getRole().getOut());
whisper.setCodecType(codec);
whisper.setTarget(target);
whisper.setCodecData(data);
return whisper;
}
} }
private class ClientCommandResponse<T> private class ClientCommandResponse<T>
@@ -64,8 +64,8 @@ public class HighPrecisionTimer extends Thread {
now = System.nanoTime(); now = System.nanoTime();
// Clamp the next target to the maximum "drift" allowed // After a stall, catch up at most the allowed number of ticks rather than all of them
if (nextTarget - now > maximumDriftInNanos) if (now - nextTarget > maximumDriftInNanos)
nextTarget = now - maximumDriftInNanos; nextTarget = now - maximumDriftInNanos;
if (sleepFor(nextTarget - now)) { if (sleepFor(nextTarget - now)) {
@@ -0,0 +1,41 @@
package com.github.manevolent.ts3j;
import com.github.manevolent.ts3j.enums.CodecType;
import com.github.manevolent.ts3j.protocol.ProtocolRole;
import com.github.manevolent.ts3j.protocol.header.ClientPacketHeader;
import com.github.manevolent.ts3j.protocol.packet.PacketBody1VoiceWhisper;
import junit.framework.TestCase;
import java.nio.ByteBuffer;
import java.util.Arrays;
/** A whisper a client sends: codec, then the channel and client counts and ids, then the voice. */
public class WhisperPacketTest extends TestCase {
public void testClientWhisperRoundTrip() {
PacketBody1VoiceWhisper.WhisperTargetMultiple target = new PacketBody1VoiceWhisper.WhisperTargetMultiple();
target.setChannelIds(new int[]{7, 9});
target.setClientIds(new int[]{3});
PacketBody1VoiceWhisper sent = new PacketBody1VoiceWhisper(ProtocolRole.CLIENT);
sent.setPacketId(42);
sent.setCodecType(CodecType.OPUS_VOICE);
sent.setTarget(target);
sent.setCodecData(new byte[]{1, 2, 3});
ByteBuffer buffer = ByteBuffer.allocate(sent.getSize());
sent.write(buffer);
assertEquals("the size covers exactly what is written", buffer.capacity(), buffer.position());
assertEquals(2 + 1 + 1 + 1 + 2 * 8 + 2 + 3, buffer.capacity());
buffer.flip();
PacketBody1VoiceWhisper read = new PacketBody1VoiceWhisper(ProtocolRole.CLIENT);
read.read(new ClientPacketHeader(), buffer);
PacketBody1VoiceWhisper.WhisperTargetMultiple readTarget =
(PacketBody1VoiceWhisper.WhisperTargetMultiple) read.getTarget();
assertEquals(42, read.getPacketId());
assertEquals(CodecType.OPUS_VOICE, read.getCodecType());
assertTrue(Arrays.equals(new int[]{7, 9}, readTarget.getChannelIds()));
assertTrue(Arrays.equals(new int[]{3}, readTarget.getClientIds()));
assertTrue(Arrays.equals(new byte[]{1, 2, 3}, read.getCodecData()));
}
}
@@ -0,0 +1,33 @@
package com.github.manevolent.ts3j.protocol.packet.handler.client;
import junit.framework.TestCase;
import java.math.BigInteger;
import java.util.Arrays;
public class PuzzleAnswerTest extends TestCase {
private static byte[] bytes(int length, int first) {
byte[] b = new byte[length];
Arrays.fill(b, (byte) 0x11);
b[0] = (byte) first;
return b;
}
/** Top bit set: toByteArray() gives 65 bytes, the first a sign byte. */
public void testDropsTheSignByte() {
byte[] answer = bytes(64, 0x80);
assertTrue(Arrays.equals(answer, LocalClientHandlerConnecting.unsigned64(new BigInteger(1, answer))));
}
public void testKeepsSixtyFourBytes() {
byte[] answer = bytes(64, 0x7f);
assertTrue(Arrays.equals(answer, LocalClientHandlerConnecting.unsigned64(new BigInteger(1, answer))));
}
/** A leading zero byte: toByteArray() gives 63 bytes, which used to fail the connect. */
public void testPadsAShortAnswerWithZeros() {
byte[] answer = bytes(64, 0x00);
answer[1] = 0x42;
assertTrue(Arrays.equals(answer, LocalClientHandlerConnecting.unsigned64(new BigInteger(1, answer))));
}
}