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); recv(socket, wait_buffer, 1, MSG_PEEK);
why2_deallocate(wait_buffer); why2_deallocate(wait_buffer);
why2_bool empty_buffer = 0; //WHETHER MSG_PEEK SHOULD BE USER OR NOT
do do
{ {
//FIND THE SENT SIZE //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; if (ioctl(socket, FIONREAD, &content_size) < 0 || content_size <= 0) continue;
//ALLOCATE //ALLOCATE
content_buffer = why2_realloc(content_buffer, content_size + 1); content_buffer = why2_malloc(content_size + 1);
read_section:
//READ JSON MESSAGE //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"); 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); } while (content_buffer == NULL || strncmp(content_buffer + (content_size - 2), "\"}", 2) != 0);
//REMOVE JUNK FROM BUFFER (CUZ THE MSG_PEEK FLAG) //ACTUALLY READ
empty_buffer = 1; content_buffer = why2_calloc(content_size + 1, sizeof(char)); //ALLOCATE
goto read_section; //TODO: remove the stupid goto
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 //VALIDATE JSON FORMAT
struct json_object *json = json_tokener_parse(content_buffer); struct json_object *json = json_tokener_parse(content_buffer);