]> git.ipfire.org Git - fireperf.git/commitdiff
Move functions to send/receive data to main
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 18 Feb 2021 16:01:12 +0000 (16:01 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 18 Feb 2021 16:01:12 +0000 (16:01 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/client.c
src/main.c
src/main.h
src/server.c

index a68a46cf4d4e6118318c337ccd48da895c5e71ba..d92c6e1c8abe8910d5dc7f5cbe7f3253348365be 100644 (file)
@@ -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,
index 983d5c8bf7b8eaadc9b9e55cd26703bb6b62ca33..db4a23bae99370462cae8283a091e5eae3f55782 100644 (file)
@@ -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;
+}
index 49b05f9ab2986c86718698642cca2a3c358775bf..7f741042f1669cef78760b66c4ea7b1c4b98b0f6 100644 (file)
@@ -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 */
index d7adda49c845074ab3821fbe49d607ab64680582..9253c1d084f658abc99ce09ae33dbb216fa564a7 100644 (file)
@@ -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;
                                }