Handle protocol message with a length while reading.

master
Jose Marcial Vieira Bisneto 2018-11-16 11:33:52 -03:00
parent b0b18632f9
commit b272440954
No known key found for this signature in database
GPG Key ID: 103E935E7E6E831E
1 changed files with 18 additions and 1 deletions

View File

@ -73,7 +73,24 @@ int libp2p_net_connection_read(void* stream_context, struct StreamMessage** msg,
uint8_t* result_buffer = NULL;
int current_size = 0;
while (1) {
int retVal = socket_read(ctx->socket_descriptor, (char*)&buffer[0], 4096, 0, timeout_secs);
int retVal;
if (current_size) {
retVal = socket_read(ctx->socket_descriptor, (char*)&buffer[0], sizeof(buffer), 0, timeout_secs);
} else {
// initial reading, see if it is a protocol request with length.
retVal = socket_read(ctx->socket_descriptor, (char*)&buffer[0], 2, 0, timeout_secs);
if (retVal == 2) {
if (buffer[0] > 0 && buffer[1] == '/') {
// if the second byte is a slash, it may be a protocol message with a length in the first byte.
retVal = socket_read(ctx->socket_descriptor, (char*)&buffer[2], buffer[0]-1, 0, timeout_secs);
} else {
// if it isn't, read as much as possible.
retVal = socket_read(ctx->socket_descriptor, (char*)&buffer[2], sizeof(buffer)-2, 0, timeout_secs);
}
if (retVal > 0)
retVal += 2; // compensate the first read.
}
}
ctx->last_comm_epoch = time(NULL);
libp2p_logger_debug("connectionstream", "Retrieved %d bytes from socket %d.\n", retVal, ctx->socket_descriptor);
if (retVal < 1) { // get out of the loop