Synchronize the remote counter, and correct an issue with rest() concerning generation IDs
This commit is contained in:
+33
-14
@@ -1146,30 +1146,39 @@ public abstract class AbstractTeamspeakClientSocket
|
||||
|
||||
@Override
|
||||
public int getGeneration(int packetId) {
|
||||
Pair<Integer, Integer> bufferStartCopy;
|
||||
Pair<Integer, Integer> bufferEndCopy;
|
||||
|
||||
// Shorter synchronization
|
||||
synchronized (this) {
|
||||
bufferStartCopy = this.bufferStart;
|
||||
bufferEndCopy = this.bufferEnd;
|
||||
}
|
||||
|
||||
packetId %= generationSize;
|
||||
|
||||
// find if right of the start and within the start's bounds
|
||||
if (packetId >= bufferStart.getValue()
|
||||
&& packetId < bufferStart.getValue() + bufferSize
|
||||
if (packetId >= bufferStartCopy.getValue()
|
||||
&& packetId < bufferStartCopy.getValue() + bufferSize
|
||||
&& packetId < generationSize) {
|
||||
return bufferStart.getKey();
|
||||
return bufferStartCopy.getKey();
|
||||
}
|
||||
|
||||
// find if left of end and within the end's bounds
|
||||
if (packetId <= bufferEnd.getValue()
|
||||
if (packetId <= bufferEndCopy.getValue()
|
||||
&& packetId > 0
|
||||
&& packetId > bufferEnd.getValue() - bufferSize
|
||||
&& packetId > bufferEndCopy.getValue() - bufferSize
|
||||
&& packetId < generationSize) {
|
||||
return bufferEnd.getKey();
|
||||
return bufferEndCopy.getKey();
|
||||
}
|
||||
|
||||
// find if outside of buffer to the right
|
||||
if (packetId > bufferEnd.getValue())
|
||||
return bufferEnd.getKey(); // ahead in current generation
|
||||
if (packetId > bufferEndCopy.getValue())
|
||||
return bufferEndCopy.getKey(); // ahead in current generation
|
||||
|
||||
// find if outside of buffer to the left
|
||||
if (packetId < bufferStart.getValue())
|
||||
return bufferEnd.getKey() + 1; // modulated wrap, ahead of current generation
|
||||
if (packetId < bufferStartCopy.getValue())
|
||||
return bufferEndCopy.getKey() + 1; // modulated wrap, ahead of current generation
|
||||
|
||||
// should never arrive here.
|
||||
throw new IllegalStateException();
|
||||
@@ -1187,6 +1196,7 @@ public abstract class AbstractTeamspeakClientSocket
|
||||
|
||||
@Override
|
||||
public boolean put(int packetId) {
|
||||
synchronized (this) {
|
||||
// find if right of the start and within the start's bounds
|
||||
if (packetId >= bufferStart.getValue()
|
||||
&& packetId < bufferStart.getValue() + bufferSize
|
||||
@@ -1229,7 +1239,7 @@ public abstract class AbstractTeamspeakClientSocket
|
||||
System.arraycopy(buffer, amountMoved, buffer, 0, toMove);
|
||||
|
||||
int toNullify = Math.max(0, Math.min(bufferSize, amountMoved));
|
||||
for (int i = bufferSize - toNullify; i < bufferSize; i ++)
|
||||
for (int i = bufferSize - toNullify; i < bufferSize; i++)
|
||||
buffer[i] = null;
|
||||
|
||||
int bufferStartGeneration = bufferStart.getKey();
|
||||
@@ -1238,7 +1248,7 @@ public abstract class AbstractTeamspeakClientSocket
|
||||
bufferStartPosition += amountMoved;
|
||||
if (bufferStartPosition >= generationSize) {
|
||||
bufferStartPosition %= generationSize;
|
||||
bufferStartGeneration ++;
|
||||
bufferStartGeneration++;
|
||||
}
|
||||
|
||||
int bufferEndGeneration;
|
||||
@@ -1258,14 +1268,22 @@ public abstract class AbstractTeamspeakClientSocket
|
||||
|
||||
return put(packetId);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reset() {
|
||||
for (int i = 0; i < bufferSize; i ++)
|
||||
buffer[i] = null;
|
||||
synchronized (this) {
|
||||
for (int i = 0; i < bufferSize; i++)
|
||||
this.buffer[i] = null;
|
||||
|
||||
this.bufferStart = new Pair<>(0, 0);
|
||||
this.bufferEnd = new Pair<>(0, bufferSize - 1);
|
||||
this.latestPacketId = 0;
|
||||
}
|
||||
}
|
||||
|
||||
private boolean putRelative(int index, int generation) {
|
||||
synchronized (this) {
|
||||
Integer existing = buffer[index];
|
||||
if (existing == null || existing != generation) {
|
||||
buffer[index] = generation;
|
||||
@@ -1275,6 +1293,7 @@ public abstract class AbstractTeamspeakClientSocket
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class RemoteCounterFullModulated extends RemoteCounterFull {
|
||||
private final int modulationSize;
|
||||
|
||||
Reference in New Issue
Block a user