Multi-client server, protocol versioning, streaming decompression fixes
This commit is contained in:
@@ -53,7 +53,7 @@ int main(int argc, char *argv[]) {
|
|||||||
save_to_disk = true;
|
save_to_disk = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
Config *config = config_create(str_dup("1.0.0"), NULL, NULL,
|
Config *config = config_create(str_dup(PROTOCOL_VERSION), NULL, NULL,
|
||||||
save_to_disk, false, false, false, false, 5, false, 0);
|
save_to_disk, false, false, false, false, 5, false, 0);
|
||||||
|
|
||||||
int positional_args[2];
|
int positional_args[2];
|
||||||
|
|||||||
+19
-3
@@ -26,6 +26,11 @@ int receive_files(Config *config, int file_descriptor) {
|
|||||||
if (config->use_compression) {
|
if (config->use_compression) {
|
||||||
data_to_process = data_decompress(chunk_data);
|
data_to_process = data_decompress(chunk_data);
|
||||||
data_destroy(chunk_data);
|
data_destroy(chunk_data);
|
||||||
|
if (data_to_process == NULL) {
|
||||||
|
log_message(LOG_LEVEL_ERROR, "Failed to decompress chunk");
|
||||||
|
send_status(file_descriptor, STATUS_ERROR);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Chunk *chunk = chunk_deserialize(data_to_process, config->use_metadata);
|
Chunk *chunk = chunk_deserialize(data_to_process, config->use_metadata);
|
||||||
data_destroy(data_to_process);
|
data_destroy(data_to_process);
|
||||||
@@ -95,6 +100,16 @@ void handler(int file_descriptor) {
|
|||||||
close(file_descriptor);
|
close(file_descriptor);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static Server *g_server = NULL;
|
||||||
|
|
||||||
|
static void cleanup(int sig) {
|
||||||
|
(void)sig;
|
||||||
|
if (g_server) {
|
||||||
|
server_delete(&g_server);
|
||||||
|
}
|
||||||
|
_exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
signal(SIGPIPE, SIG_IGN);
|
signal(SIGPIPE, SIG_IGN);
|
||||||
for (int i = 1; i < argc; i++) {
|
for (int i = 1; i < argc; i++) {
|
||||||
@@ -106,8 +121,9 @@ int main(int argc, char *argv[]) {
|
|||||||
set_log_level(LOG_LEVEL_DEBUG);
|
set_log_level(LOG_LEVEL_DEBUG);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Server *server = server_create(8080);
|
signal(SIGINT, cleanup);
|
||||||
server_listen(server, handler);
|
signal(SIGTERM, cleanup);
|
||||||
server_delete(&server);
|
g_server = server_create(8080);
|
||||||
|
server_listen(g_server, handler);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
+39
-12
@@ -4,15 +4,22 @@
|
|||||||
#include "stdlib.h"
|
#include "stdlib.h"
|
||||||
#include "zstd.h"
|
#include "zstd.h"
|
||||||
|
|
||||||
|
#define INITIAL_DECOMPRESS_BUF_SIZE (1024 * 1024)
|
||||||
|
|
||||||
Data *data_compress(Data *data_to_compress, int compression_level) {
|
Data *data_compress(Data *data_to_compress, int compression_level) {
|
||||||
log_message(LOG_LEVEL_DEBUG, "Starting to compress data");
|
log_message(LOG_LEVEL_DEBUG, "Starting to compress data");
|
||||||
size_t dst_size = ZSTD_compressBound(data_to_compress->size);
|
size_t dst_size = ZSTD_compressBound(data_to_compress->size);
|
||||||
Data *compressed_data = data_create_empty(dst_size);
|
Data *compressed_data = data_create_empty(dst_size);
|
||||||
|
if (!compressed_data) {
|
||||||
|
log_message(LOG_LEVEL_ERROR, "Failed to allocate compression buffer");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
ZSTD_CCtx *cctx = ZSTD_createCCtx();
|
ZSTD_CCtx *cctx = ZSTD_createCCtx();
|
||||||
if (!cctx) {
|
if (!cctx) {
|
||||||
log_message(LOG_LEVEL_ERROR, "Failed to create ZSTD compression context");
|
log_message(LOG_LEVEL_ERROR, "Failed to create ZSTD compression context");
|
||||||
exit(EXIT_FAILURE);
|
data_destroy(compressed_data);
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
ZSTD_inBuffer input = {data_to_compress->data, data_to_compress->size, 0};
|
ZSTD_inBuffer input = {data_to_compress->data, data_to_compress->size, 0};
|
||||||
@@ -24,7 +31,9 @@ Data *data_compress(Data *data_to_compress, int compression_level) {
|
|||||||
if (ZSTD_isError(ret)) {
|
if (ZSTD_isError(ret)) {
|
||||||
log_message(LOG_LEVEL_ERROR, "Compression failed: %s",
|
log_message(LOG_LEVEL_ERROR, "Compression failed: %s",
|
||||||
ZSTD_getErrorName(ret));
|
ZSTD_getErrorName(ret));
|
||||||
exit(EXIT_FAILURE);
|
ZSTD_freeCCtx(cctx);
|
||||||
|
data_destroy(compressed_data);
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
} while (ret > 0);
|
} while (ret > 0);
|
||||||
|
|
||||||
@@ -40,23 +49,26 @@ Data *data_decompress(Data *compressed_data) {
|
|||||||
log_message(LOG_LEVEL_DEBUG, "Start to decompress data");
|
log_message(LOG_LEVEL_DEBUG, "Start to decompress data");
|
||||||
unsigned long long dst_size = ZSTD_getFrameContentSize(
|
unsigned long long dst_size = ZSTD_getFrameContentSize(
|
||||||
compressed_data->data, compressed_data->size);
|
compressed_data->data, compressed_data->size);
|
||||||
if (ZSTD_isError(dst_size)) {
|
|
||||||
log_message(LOG_LEVEL_ERROR, "Failed to get decompressed size: %s",
|
|
||||||
ZSTD_getErrorName(dst_size));
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
Data *uncompressed_data = data_create_empty((size_t)dst_size);
|
|
||||||
|
|
||||||
ZSTD_DCtx *dctx = ZSTD_createDCtx();
|
ZSTD_DCtx *dctx = ZSTD_createDCtx();
|
||||||
if (!dctx) {
|
if (!dctx) {
|
||||||
log_message(LOG_LEVEL_ERROR,
|
log_message(LOG_LEVEL_ERROR,
|
||||||
"Failed to create ZSTD decompression context");
|
"Failed to create ZSTD decompression context");
|
||||||
exit(EXIT_FAILURE);
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t buf_size = (!ZSTD_isError(dst_size) && dst_size > 0)
|
||||||
|
? (size_t)dst_size
|
||||||
|
: INITIAL_DECOMPRESS_BUF_SIZE;
|
||||||
|
Data *uncompressed_data = data_create_empty(buf_size);
|
||||||
|
if (!uncompressed_data) {
|
||||||
|
log_message(LOG_LEVEL_ERROR, "Failed to allocate decompression buffer");
|
||||||
|
ZSTD_freeDCtx(dctx);
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
ZSTD_inBuffer input = {compressed_data->data, compressed_data->size, 0};
|
ZSTD_inBuffer input = {compressed_data->data, compressed_data->size, 0};
|
||||||
ZSTD_outBuffer output = {uncompressed_data->data, (size_t)dst_size, 0};
|
ZSTD_outBuffer output = {uncompressed_data->data, buf_size, 0};
|
||||||
|
|
||||||
size_t ret;
|
size_t ret;
|
||||||
do {
|
do {
|
||||||
@@ -64,7 +76,22 @@ Data *data_decompress(Data *compressed_data) {
|
|||||||
if (ZSTD_isError(ret)) {
|
if (ZSTD_isError(ret)) {
|
||||||
log_message(LOG_LEVEL_ERROR, "Decompression failed: %s",
|
log_message(LOG_LEVEL_ERROR, "Decompression failed: %s",
|
||||||
ZSTD_getErrorName(ret));
|
ZSTD_getErrorName(ret));
|
||||||
exit(EXIT_FAILURE);
|
ZSTD_freeDCtx(dctx);
|
||||||
|
data_destroy(uncompressed_data);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
if (ret > 0 && output.pos == output.size) {
|
||||||
|
buf_size *= 2;
|
||||||
|
void *new_data = realloc(uncompressed_data->data, buf_size);
|
||||||
|
if (!new_data) {
|
||||||
|
log_message(LOG_LEVEL_ERROR, "Failed to grow decompression buffer");
|
||||||
|
ZSTD_freeDCtx(dctx);
|
||||||
|
data_destroy(uncompressed_data);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
uncompressed_data->data = new_data;
|
||||||
|
output.dst = new_data;
|
||||||
|
output.size = buf_size;
|
||||||
}
|
}
|
||||||
} while (ret > 0);
|
} while (ret > 0);
|
||||||
|
|
||||||
|
|||||||
@@ -90,6 +90,14 @@ void config_send(int file_descriptor, Config *config) {
|
|||||||
Config *config_receive(int file_descriptor) {
|
Config *config_receive(int file_descriptor) {
|
||||||
Config *config = (Config *)malloc(sizeof(Config));
|
Config *config = (Config *)malloc(sizeof(Config));
|
||||||
config->version = receive_str(file_descriptor);
|
config->version = receive_str(file_descriptor);
|
||||||
|
if (strcmp(config->version, PROTOCOL_VERSION) != 0) {
|
||||||
|
fprintf(stderr, "Protocol version mismatch: client=%s, server=%s\n",
|
||||||
|
config->version, PROTOCOL_VERSION);
|
||||||
|
free(config->version);
|
||||||
|
free(config);
|
||||||
|
send_status(file_descriptor, STATUS_ERROR);
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
config->send_directory = receive_str(file_descriptor);
|
config->send_directory = receive_str(file_descriptor);
|
||||||
config->receive_root_directory = receive_str(file_descriptor);
|
config->receive_root_directory = receive_str(file_descriptor);
|
||||||
config->save_to_disk = receive_int(file_descriptor);
|
config->save_to_disk = receive_int(file_descriptor);
|
||||||
|
|||||||
@@ -30,6 +30,7 @@ typedef struct Config {
|
|||||||
int exclude_count;
|
int exclude_count;
|
||||||
} Config;
|
} Config;
|
||||||
|
|
||||||
|
#define PROTOCOL_VERSION "1.0.0"
|
||||||
#define DEFAULT_CHUNK_SIZE (10 * 1024 * 1024)
|
#define DEFAULT_CHUNK_SIZE (10 * 1024 * 1024)
|
||||||
|
|
||||||
Config *config_create(char *version, char *send_directory,
|
Config *config_create(char *version, char *send_directory,
|
||||||
|
|||||||
@@ -10,6 +10,7 @@
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#include "compression.h"
|
#include "compression.h"
|
||||||
|
#include "log.h"
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "data.h"
|
#include "data.h"
|
||||||
#include "file.h"
|
#include "file.h"
|
||||||
@@ -95,6 +96,10 @@ void file_send_single_calls(File *file, int file_descriptor, bool use_metadata,
|
|||||||
if (compression_level > 0) {
|
if (compression_level > 0) {
|
||||||
Data *compressed_data = data_compress(file->data, compression_level);
|
Data *compressed_data = data_compress(file->data, compression_level);
|
||||||
data_destroy(file->data);
|
data_destroy(file->data);
|
||||||
|
if (compressed_data == NULL) {
|
||||||
|
log_message(LOG_LEVEL_ERROR, "Compression failed in file_send_single_calls");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
file->data = compressed_data;
|
file->data = compressed_data;
|
||||||
}
|
}
|
||||||
send_str(file_descriptor, file->path);
|
send_str(file_descriptor, file->path);
|
||||||
|
|||||||
@@ -85,6 +85,10 @@ static void receive_chunk_enqueue(int file_descriptor,
|
|||||||
if (context->config->use_compression) {
|
if (context->config->use_compression) {
|
||||||
data_to_process = data_decompress(chunk_data);
|
data_to_process = data_decompress(chunk_data);
|
||||||
data_destroy(chunk_data);
|
data_destroy(chunk_data);
|
||||||
|
if (data_to_process == NULL) {
|
||||||
|
log_message(LOG_LEVEL_ERROR, "Failed to decompress chunk, skipping");
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Chunk *chunk = chunk_deserialize(data_to_process, context->config->use_metadata);
|
Chunk *chunk = chunk_deserialize(data_to_process, context->config->use_metadata);
|
||||||
data_destroy(data_to_process);
|
data_destroy(data_to_process);
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#include "transport_tcp.h"
|
#include "transport_tcp.h"
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
|
#include <signal.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@@ -48,6 +49,7 @@ Server *server_create(int port) {
|
|||||||
|
|
||||||
void server_delete(Server **server) {
|
void server_delete(Server **server) {
|
||||||
if (server == NULL || *server == NULL) return;
|
if (server == NULL || *server == NULL) return;
|
||||||
|
close((*server)->file_descriptor);
|
||||||
free(*server);
|
free(*server);
|
||||||
*server = NULL;
|
*server = NULL;
|
||||||
}
|
}
|
||||||
@@ -55,22 +57,33 @@ void server_delete(Server **server) {
|
|||||||
void server_listen(Server *server, void (*handler)(int file_descriptor)) {
|
void server_listen(Server *server, void (*handler)(int file_descriptor)) {
|
||||||
log_message(LOG_LEVEL_INFO, "Start Listening on Port: %d",
|
log_message(LOG_LEVEL_INFO, "Start Listening on Port: %d",
|
||||||
server->address.sin_port);
|
server->address.sin_port);
|
||||||
if (listen(server->file_descriptor, 3) < 0) {
|
if (listen(server->file_descriptor, SOMAXCONN) < 0) {
|
||||||
perror("Could not listen on port!");
|
perror("Could not listen on port!");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
signal(SIGCHLD, SIG_IGN);
|
||||||
|
|
||||||
|
while (1) {
|
||||||
|
struct sockaddr_in client_addr;
|
||||||
|
socklen_t client_len = sizeof(client_addr);
|
||||||
int file_descriptor =
|
int file_descriptor =
|
||||||
accept(server->file_descriptor, (struct sockaddr *)&server->address,
|
accept(server->file_descriptor, (struct sockaddr *)&client_addr,
|
||||||
&server->address_length);
|
&client_len);
|
||||||
if (file_descriptor < 0) {
|
if (file_descriptor < 0) {
|
||||||
perror("Could not accept the connection");
|
perror("Could not accept the connection");
|
||||||
exit(EXIT_FAILURE);
|
continue;
|
||||||
}
|
}
|
||||||
log_message(LOG_LEVEL_INFO, "Received Connection");
|
log_message(LOG_LEVEL_INFO, "Received Connection");
|
||||||
handler(file_descriptor);
|
pid_t pid = fork();
|
||||||
|
if (pid == 0) {
|
||||||
close(server->file_descriptor);
|
close(server->file_descriptor);
|
||||||
|
handler(file_descriptor);
|
||||||
close(file_descriptor);
|
close(file_descriptor);
|
||||||
|
_exit(0);
|
||||||
|
}
|
||||||
|
close(file_descriptor);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Client *client_create() {
|
Client *client_create() {
|
||||||
|
|||||||
Reference in New Issue
Block a user