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 { private void sendToVPCD(byte[] data) throws IOException {
/* convert length to network byte order. /* convert length to network byte order.
Note that Java always uses network byte order internally. */ Note that Java always uses network byte order internally. */
byte[] length = new byte[2]; byte[] packet = new byte[2 + data.length];
length[0] = (byte) (data.length >> 8); packet[0] = (byte) (data.length >> 8);
length[1] = (byte) (data.length & 0xff); packet[1] = (byte) (data.length & 0xff);
outputStream.write(length); System.arraycopy(data, 0, packet, 2, data.length);
outputStream.write(data, 0, data.length);
outputStream.write(packet);
outputStream.flush(); 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; ssize_t r;
uint16_t size; uint16_t size;
char *sendBuffer;
if (!ctx || length > 0xFFFF) { if (!ctx || length > 0xFFFF) {
errno = EINVAL; errno = EINVAL;
return -1; 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 */ /* send size of message on 2 bytes */
size = htons((uint16_t) length); size = htons((uint16_t) length);
r = sendall(ctx->client_sock, (void *) &size, sizeof size); memcpy(sendBuffer, &size, 2);
if (r == sizeof size) memcpy(sendBuffer + 2, buffer, length);
/* send message */ r = sendall(ctx->client_sock, sendBuffer, length + 2);
r = sendall(ctx->client_sock, buffer, length);
if (r < 0) if (r < 0)
vicc_eject(ctx); vicc_eject(ctx);
free(sendBuffer);
return r; return r;
} }