Merge pull request #252 from mildsunrise/avoid-extra-rtt

avoid unnecessary RTTs for communication
This commit is contained in:
Frank Morgner
2023-03-17 11:19:43 +01:00
committed by GitHub
2 changed files with 17 additions and 10 deletions

View File

@@ -179,13 +179,12 @@ class VPCDWorker extends AsyncTask<VPCDWorker.VPCDWorkerParams, Void, Void> {
private void sendToVPCD(byte[] data) throws IOException {
/* convert length to network byte order.
Note that Java always uses network byte order internally. */
byte[] length = new byte[2];
length[0] = (byte) (data.length >> 8);
length[1] = (byte) (data.length & 0xff);
outputStream.write(length);
outputStream.write(data, 0, data.length);
byte[] packet = new byte[2 + data.length];
packet[0] = (byte) (data.length >> 8);
packet[1] = (byte) (data.length & 0xff);
System.arraycopy(data, 0, packet, 2, data.length);
outputStream.write(packet);
outputStream.flush();
}

View File

@@ -219,22 +219,30 @@ static ssize_t sendToVICC(struct vicc_ctx *ctx, size_t length, const unsigned ch
{
ssize_t r;
uint16_t size;
char *sendBuffer;
if (!ctx || length > 0xFFFF) {
errno = EINVAL;
return -1;
}
/* allocate buffer for outgoing message */
sendBuffer = (char *) malloc(length + 2);
if (sendBuffer == NULL) {
errno = ENOMEM;
return -1;
}
/* send size of message on 2 bytes */
size = htons((uint16_t) length);
r = sendall(ctx->client_sock, (void *) &size, sizeof size);
if (r == sizeof size)
/* send message */
r = sendall(ctx->client_sock, buffer, length);
memcpy(sendBuffer, &size, 2);
memcpy(sendBuffer + 2, buffer, length);
r = sendall(ctx->client_sock, sendBuffer, length + 2);
if (r < 0)
vicc_eject(ctx);
free(sendBuffer);
return r;
}