From: Michael Tremer Date: Thu, 18 Feb 2021 16:01:12 +0000 (+0000) Subject: Move functions to send/receive data to main X-Git-Tag: 0.2.0~16 X-Git-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=c7f6973abe3794b1ad718d3b74a2f3886b5acfff;p=fireperf.git Move functions to send/receive data to main Signed-off-by: Michael Tremer --- diff --git a/src/client.c b/src/client.c index a68a46c..d92c6e1 100644 --- a/src/client.c +++ b/src/client.c @@ -135,28 +135,6 @@ ERROR: return -1; } -static int send_data_to_server(struct fireperf_config* conf, - struct fireperf_stats* stats, int fd) { - const char* buffer = ZERO; - ssize_t bytes_sent; - - if (conf->pool) { - buffer = fireperf_random_pool_get_slice(conf->pool, SOCKET_SEND_BUFFER_SIZE); - } - - do { - bytes_sent = send(fd, buffer, SOCKET_SEND_BUFFER_SIZE, 0); - } while (bytes_sent < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)); - - DEBUG(conf, "bytes_sent = %zu\n", bytes_sent); - - // Update statistics - stats->bytes_sent += bytes_sent; - stats->total_bytes_sent += bytes_sent; - - return 0; -} - static int handle_connection_ready(struct fireperf_config* conf, struct fireperf_stats* stats, int fd) { // Are we supposed to close this connection straight away? @@ -169,7 +147,7 @@ static int handle_connection_ready(struct fireperf_config* conf, return 0; } - return send_data_to_server(conf, stats, fd); + return handle_connection_send(conf, stats, fd); } int fireperf_client(struct fireperf_config* conf, struct fireperf_stats* stats, diff --git a/src/main.c b/src/main.c index 983d5c8..db4a23b 100644 --- a/src/main.c +++ b/src/main.c @@ -414,3 +414,48 @@ int fireperf_dump_stats(struct fireperf_config* conf, struct fireperf_stats* sta return 0; } + +int handle_connection_send(struct fireperf_config* conf, + struct fireperf_stats* stats, int fd) { + const char* buffer = ZERO; + ssize_t bytes_sent; + + if (conf->pool) { + buffer = fireperf_random_pool_get_slice(conf->pool, SOCKET_SEND_BUFFER_SIZE); + } + + do { + bytes_sent = send(fd, buffer, SOCKET_SEND_BUFFER_SIZE, 0); + } while (bytes_sent < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)); + + // Update statistics + stats->bytes_sent += bytes_sent; + stats->total_bytes_sent += bytes_sent; + + return 0; +} + +int handle_connection_recv(struct fireperf_config* conf, + struct fireperf_stats* stats, int fd) { + char buffer[SOCKET_RECV_BUFFER_SIZE]; + ssize_t bytes_read; + + // Try reading into buffer + do { + bytes_read = recv(fd, buffer, sizeof(buffer), 0); + } while (bytes_read < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)); + + // Error? + if (bytes_read < 0) { + ERROR(conf, "Could not read from socket %d: %s\n", fd, strerror(errno)); + return -1; + } + + DEBUG(conf, "Read %zu bytes from socket %d\n", bytes_read, fd); + + // Update statistics + stats->bytes_received += bytes_read; + stats->total_bytes_received += bytes_read; + + return 0; +} diff --git a/src/main.h b/src/main.h index 49b05f9..7f74104 100644 --- a/src/main.h +++ b/src/main.h @@ -85,4 +85,9 @@ struct fireperf_stats { int fireperf_dump_stats(struct fireperf_config* conf, struct fireperf_stats* stats, int mode); +int handle_connection_send(struct fireperf_config* conf, + struct fireperf_stats* stats, int fd); +int handle_connection_recv(struct fireperf_config* conf, + struct fireperf_stats* stats, int fd); + #endif /* FIREPERF_MAIN_H */ diff --git a/src/server.c b/src/server.c index d7adda4..9253c1d 100644 --- a/src/server.c +++ b/src/server.c @@ -110,31 +110,6 @@ static int accept_connection(struct fireperf_config* conf, int sockfd) { return fd; } -static int handle_io_on_connection(struct fireperf_config* conf, - struct fireperf_stats* stats, int fd) { - char buffer[SOCKET_RECV_BUFFER_SIZE]; - ssize_t bytes_read; - - // Try reading into buffer - do { - bytes_read = recv(fd, buffer, sizeof(buffer), 0); - } while (bytes_read < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)); - - // Error? - if (bytes_read < 0) { - ERROR(conf, "Could not read from socket %d: %s\n", fd, strerror(errno)); - return -1; - } - - DEBUG(conf, "Read %zu bytes from socket %d\n", bytes_read, fd); - - // Update statistics - stats->bytes_received += bytes_read; - stats->total_bytes_received += bytes_read; - - return 0; -} - static int is_listening_socket(struct fireperf_config* conf, int* sockets, int fd) { for (unsigned int i = 0; i < conf->listening_sockets; i++) { if (sockets[i] == fd) @@ -247,7 +222,7 @@ int fireperf_server(struct fireperf_config* conf, struct fireperf_stats* stats, // One of the connections had IO if (events[i].events & EPOLLIN) { - r = handle_io_on_connection(conf, stats, fd); + r = handle_connection_recv(conf, stats, fd); if (r < 0) goto ERROR; }