Replace byteQueue with bufferQueue

This commit is contained in:
PeytonPlayz595 2023-12-22 02:06:39 -05:00
parent b22376e5af
commit 3d5072a414
3 changed files with 2944 additions and 2939 deletions

5836
js/app.js

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -16,11 +16,11 @@ import java.util.Queue;
public class WebSocketChannel { public class WebSocketChannel {
private final WebSocket socket; private final WebSocket socket;
private boolean isConnected = false; private boolean isConnected = false;
private Queue<byte[]> byteQueue; private Queue<ByteBuffer> bufferQueue;
public WebSocketChannel(String url) { public WebSocketChannel(String url) {
socket = WebSocket.create(url); socket = WebSocket.create(url);
byteQueue = new LinkedList<byte[]>(); bufferQueue = new LinkedList<ByteBuffer>();
socket.onMessage(event -> { socket.onMessage(event -> {
ArrayBuffer arrayBuffer = event.getData().cast(); ArrayBuffer arrayBuffer = event.getData().cast();
@ -29,20 +29,17 @@ public class WebSocketChannel {
for (int i = 0; i < int8Array.getLength(); i++) { for (int i = 0; i < int8Array.getLength(); i++) {
byteBuffer.put(i, (byte) int8Array.get(i)); byteBuffer.put(i, (byte) int8Array.get(i));
} }
bufferQueue.offer(byteBuffer);
byte[] data = new byte[byteBuffer.remaining()];
byteBuffer.get(data);
byteQueue.offer(data);
}); });
socket.onOpen(event -> { socket.onOpen(event -> {
isConnected = true; isConnected = true;
byteQueue.clear(); bufferQueue.clear();
}); });
socket.onClose((event) -> { socket.onClose((event) -> {
isConnected = false; isConnected = false;
byteQueue.clear(); bufferQueue.clear();
}); });
socket.onError((event) -> { socket.onError((event) -> {
@ -51,25 +48,23 @@ public class WebSocketChannel {
}); });
} }
public int read(ByteBuffer dst) { public int read(ByteBuffer destination) {
int bytesRead = 0; int totalBytesWritten = 0;
int remaining = dst.remaining();
int position = 0;
while (remaining > 0 && !byteQueue.isEmpty()) { while (!bufferQueue.isEmpty()) {
byte[] currentByteArray = byteQueue.peek(); ByteBuffer sourceBuffer = bufferQueue.poll();
int currentPosition = position % currentByteArray.length; int remaining = sourceBuffer.remaining();
dst.put(currentByteArray[currentPosition]); int spaceRemaining = destination.remaining();
bytesRead++;
position++;
remaining--;
if (currentPosition == currentByteArray.length - 1) { if (spaceRemaining < remaining) {
byteQueue.poll(); break;
}
} }
return bytesRead; destination.put(sourceBuffer);
totalBytesWritten += remaining;
}
return totalBytesWritten;
} }
@JSBody(params = { "sock", "buffer" }, script = "sock.send(buffer);") @JSBody(params = { "sock", "buffer" }, script = "sock.send(buffer);")