got rid of the dumbass goto in read_socket_raw

and possibly created 10 new problems :))
This commit is contained in:
Václav Šmejkal 2024-08-31 16:20:58 +02:00
parent 981dec55db
commit 37a615cb03
Signed by: ENGO150
GPG Key ID: 4A57E86482968843

View File

@ -173,8 +173,6 @@ char *read_socket_raw(int socket)
recv(socket, wait_buffer, 1, MSG_PEEK);
why2_deallocate(wait_buffer);
why2_bool empty_buffer = 0; //WHETHER MSG_PEEK SHOULD BE USER OR NOT
do
{
//FIND THE SENT SIZE
@ -182,26 +180,34 @@ char *read_socket_raw(int socket)
if (ioctl(socket, FIONREAD, &content_size) < 0 || content_size <= 0) continue;
//ALLOCATE
content_buffer = why2_realloc(content_buffer, content_size + 1);
read_section:
content_buffer = why2_malloc(content_size + 1);
//READ JSON MESSAGE
if (recv(socket, content_buffer, content_size, !empty_buffer ? MSG_PEEK : 0) != content_size) //READ THE MESSAGE BY CHARACTERS
if (recv(socket, content_buffer, content_size, MSG_PEEK) != content_size) //READ THE MESSAGE BY CHARACTERS
{
fprintf(stderr, "Socket probably read wrongly!\n");
}
if (empty_buffer) goto return_section; //STOP LOOPING
why2_deallocate(content_buffer); //CLEANUP
} while (content_buffer == NULL || strncmp(content_buffer + (content_size - 2), "\"}", 2) != 0);
//REMOVE JUNK FROM BUFFER (CUZ THE MSG_PEEK FLAG)
empty_buffer = 1;
goto read_section; //TODO: remove the stupid goto
//ACTUALLY READ
content_buffer = why2_calloc(content_size + 1, sizeof(char)); //ALLOCATE
return_section:
int i;
for (i = 0; i < content_size; i++)
{
//READ
if (recv(socket, content_buffer + i, 1, 0) != 1) //READ BY CHARS
{
fprintf(stderr, "Socket probably read wrongly!\n");
}
content_buffer[content_size] = '\0'; //NULL TERM
//REMOVE NON-ASCII
if (!is_ascii(content_buffer[i])) i--; //(REWRITE THE CURRENT CHAR)
}
content_buffer[i] = '\0'; //NULL TERM
//VALIDATE JSON FORMAT
struct json_object *json = json_tokener_parse(content_buffer);