Skip to content

Commit

Permalink
perf(utils): remove loop for getting configuration
Browse files Browse the repository at this point in the history
perf(server): remove getting resp data length/count as string
chore(utils): remove client_error method
  • Loading branch information
aloima committed Oct 12, 2024
1 parent 3916bff commit 2f326ce
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 102 deletions.
2 changes: 0 additions & 2 deletions headers/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,3 @@ void close_logs();

bool is_integer(const char *value);
uint32_t get_digit_count(int32_t number);

void client_error();
7 changes: 4 additions & 3 deletions src/server/commands.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "../../headers/telly.h"
#include "../../headers/server.h"
#include "../../headers/commands.h"
#include "../../headers/utils.h"
Expand Down Expand Up @@ -64,15 +65,15 @@ uint32_t get_command_count() {

void execute_command(struct Client *client, respdata_t *data) {
if (data->type == RDT_ARRAY) {
string_t name = data->value.array[0]->value.string;
const string_t name = data->value.array[0]->value.string;

char input[name.len + 1];
to_uppercase(name.value, input);

bool executed = false;

for (uint32_t i = 0; i < command_count; ++i) {
struct Command command = commands[i];
const struct Command command = commands[i];

if (streq(input, command.name)) {
command.run(client, data);
Expand All @@ -89,6 +90,6 @@ void execute_command(struct Client *client, respdata_t *data) {
write(client->connfd, res, len);
}
} else {
client_error();
write_log(LOG_ERR, "Received data from Client #%d is not RDT_ARRAY, so it is not readable as a command.", client->id);
}
}
111 changes: 52 additions & 59 deletions src/server/resp.c
Original file line number Diff line number Diff line change
@@ -1,139 +1,132 @@
#include "../../headers/telly.h"
#include "../../headers/server.h"
#include "../../headers/utils.h"

#include <openssl/ssl.h>

#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <ctype.h>

#define DATA_ERR(client) write_log(LOG_ERR, "Received data from Client #%d cannot be validated as a RESP data, so it cannot be created.", (client)->id);

respdata_t *parse_resp_array(struct Client *client, uint8_t type) {
respdata_t *data = malloc(sizeof(respdata_t));
data->type = type;

char *len = malloc(33);
uint32_t read_count = 0;
data->count = 0;

while (true) {
char c;
_read(client, &c, 1);

if (isdigit(c)) {
len[read_count] = c;
read_count += 1;
data->count = (data->count * 10) + (c - 48);
} else if (c == '\r') {
_read(client, &c, 1);

if (c == '\n') {
len[read_count] = '\0';
data->count = atoi(len);
free(len);

data->value.array = malloc(data->count * sizeof(respdata_t));

for (uint32_t i = 0; i < data->count; ++i) {
data->value.array[i] = get_resp_data(client);
}

break;
return data;
} else {
client_error();
DATA_ERR(client);
free(data);
return NULL;
}
} else {
client_error();
}

if (read_count % 32 == 0) {
len = realloc(len, read_count + 33);
DATA_ERR(client);
free(data);
return NULL;
}
}

return data;
}

respdata_t *parse_resp_sstring(struct Client *client, uint8_t type) {
respdata_t *data = malloc(sizeof(respdata_t));
data->type = type;
data->count = 0;

string_t string = {
data->value.string = (string_t) {
.value = malloc(33),
.len = 0
};

string_t *string = &data->value.string;

while (true) {
string.len += _read(client, string.value + string.len, 1);
string->len += _read(client, string->value + string->len, 1);

if (string.len % 32 == 0) {
string.value = realloc(string.value, string.len + 33);
if (string->len % 32 == 0) {
string->value = realloc(string->value, string->len + 33);
}

if (string.value[string.len - 1] == '\r') {
if (string->value[string->len - 1] == '\r') {
char c;
_read(client, &c, 1);

if (c == '\n') {
string.value[string.len - 1] = '\0';
string.len -= 1;
data->value.string = string;
break;
string->len -= 1;
string->value[string->len] = '\0';
return data;
} else {
client_error();
DATA_ERR(client);
free_resp_data(data);
return NULL;
}
} else {
DATA_ERR(client);
free_resp_data(data);
return NULL;
}
}

return data;
}

respdata_t *parse_resp_bstring(struct Client *client, uint8_t type) {
respdata_t *data = malloc(sizeof(respdata_t));
data->type = type;
data->count = 0;
data->value.string.len = 0;

char *len = malloc(33);
uint32_t read_count = 0;
string_t *string = &data->value.string;

while (true) {
read_count += _read(client, len + read_count, 1);

if (read_count % 32 == 0) {
len = realloc(len, read_count + 33);
}
char c;
_read(client, &c, 1);

if (len[read_count - 1] == '\r') {
char c;
if (isdigit(c)) {
string->len = (string->len * 10) + (c - 48);
} else if (c == '\r') {
_read(client, &c, 1);

if (c == '\n') {
len[read_count - 1] = '\0';
uint32_t lend = atoi(len);
free(len);

data->value.string = (string_t) {
.value = malloc(lend + 1),
.len = lend
};

uint64_t total = lend;
string->value = malloc(string->len + 1);
uint64_t total = string->len;

while (total != 0) {
total -= _read(client, data->value.string.value + lend - total, total);
total -= _read(client, string->value + string->len - total, total);
}

data->value.string.value[lend] = '\0';
string->value[string->len] = '\0';

char buf[2];
_read(client, buf, 2);

if (buf[0] != '\r' || buf[1] != '\n') {
client_error();
DATA_ERR(client);
free_resp_data(data);
return NULL;
}

break;
return data;
} else {
client_error();
DATA_ERR(client);
free(data);
return NULL;
}
} else {
DATA_ERR(client);
free(data);
return NULL;
}
}

Expand Down
50 changes: 21 additions & 29 deletions src/utils/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>

static struct Configuration default_conf = {
Expand All @@ -26,7 +27,7 @@ static void pass_line(FILE *file, char c) {
}

static void parse_value(FILE *file, char *buf) {
char c = 1;
char c;

while ((c = fgetc(file)) != EOF && c != '\n') {
strncat(buf, &c, 1);
Expand Down Expand Up @@ -96,7 +97,7 @@ struct Configuration parse_configuration(FILE *file) {
buf[0] = '\0';
parse_value(file, buf);

bool enabled = streq(buf, "true");
const bool enabled = streq(buf, "true");

if (enabled || streq(buf, "false")) {
conf.tls = enabled;
Expand Down Expand Up @@ -124,33 +125,24 @@ struct Configuration parse_configuration(FILE *file) {
}

static void get_allowed_log_levels(char *allowed_log_levels, struct Configuration conf) {
const enum LogLevel log_levels[3] = {LOG_ERR, LOG_INFO, LOG_WARN};
uint32_t len = 0;

for (uint32_t i = 0; i < 3; ++i) {
const enum LogLevel level = log_levels[i];

if (conf.allowed_log_levels & level) {
switch (level) {
case LOG_ERR:
allowed_log_levels[len] = 'e';
len += 1;
break;

case LOG_WARN:
allowed_log_levels[len] = 'w';
len += 1;
break;

case LOG_INFO:
allowed_log_levels[len] = 'i';
len += 1;
break;
}
}
if (conf.allowed_log_levels & LOG_ERR) {
allowed_log_levels[len] = 'e';
len += 1;
}

if (conf.allowed_log_levels & LOG_WARN) {
allowed_log_levels[len] = 'w';
len += 1;
}

if (conf.allowed_log_levels & LOG_INFO) {
allowed_log_levels[len] = 'i';
len += 1;
}

allowed_log_levels[len] = 0;
allowed_log_levels[len] = '\0';
}

uint32_t get_configuration_string(char *buf, struct Configuration conf) {
Expand Down Expand Up @@ -197,8 +189,8 @@ struct Configuration *get_configuration(const char *filename) {
if (filename == NULL) {
FILE *file = fopen(".tellyconf", "r");

if (file != NULL) {
struct Configuration data = parse_configuration(file);
if (file) {
const struct Configuration data = parse_configuration(file);
memcpy(conf, &data, sizeof(struct Configuration));
fclose(file);
} else {
Expand All @@ -209,8 +201,8 @@ struct Configuration *get_configuration(const char *filename) {
} else {
FILE *file = fopen(filename, "r");

if (file != NULL) {
struct Configuration data = parse_configuration(file);
if (file) {
const struct Configuration data = parse_configuration(file);
memcpy(conf, &data, sizeof(struct Configuration));
fclose(file);

Expand Down
9 changes: 0 additions & 9 deletions src/utils/errors.c

This file was deleted.

0 comments on commit 2f326ce

Please sign in to comment.