From d1a598081c1a1520cac15bfcf5b5f1ed0f138999 Mon Sep 17 00:00:00 2001 From: armatusmiles <akurotych@gmail.com> Date: Wed, 29 Aug 2018 22:57:15 +0300 Subject: [PATCH] [+] buf_in/out_totals and calculate speed --- dap_server.c | 8 +++--- dap_server_client.h | 10 ++++++++ dap_traffic_track.c | 59 ++++++++++++++++++++++++++++++++++++--------- dap_traffic_track.h | 8 ++---- 4 files changed, 64 insertions(+), 21 deletions(-) diff --git a/dap_server.c b/dap_server.c index 244a96f..99e1a33 100644 --- a/dap_server.c +++ b/dap_server.c @@ -52,7 +52,7 @@ #define LOG_TAG "dap_server" -static void read_write_cb (struct ev_loop* loop, struct ev_io* watcher, int revents); +static void read_write_cb (struct ev_loop* _loop, struct ev_io* watcher, int revents); static struct ev_loop* listener_clients_loop; static ev_async async_watcher; @@ -153,8 +153,9 @@ static void read_write_cb (struct ev_loop* loop, struct ev_io* watcher, int reve 0); if(bytes_read > 0) { + dap_cur->buf_in_size_total += bytes_read; dap_cur->buf_in_size += bytes_read; - sh->client_read_callback(dap_cur,NULL); + sh->client_read_callback(dap_cur, NULL); } else if(bytes_read < 0) { @@ -186,11 +187,12 @@ static void read_write_cb (struct ev_loop* loop, struct ev_io* watcher, int reve dap_cur->buf_out_size - total_sent, MSG_DONTWAIT | MSG_NOSIGNAL ); if(bytes_sent < 0) { - log_it(L_ERROR,"Some error occured in send() function"); + log_it(L_ERROR, "Some error occured in send() function"); break; } total_sent += bytes_sent; } + dap_cur->buf_out_size_total += dap_cur->buf_out_size; dap_cur->buf_out_size = 0; } } diff --git a/dap_server_client.h b/dap_server_client.h index e783e0e..9a0e7a9 100644 --- a/dap_server_client.h +++ b/dap_server_client.h @@ -44,15 +44,25 @@ typedef struct dap_server_client{ uint32_t buf_out_zero_count; char buf_in[DAP_CLIENT_REMOTE_BUF+1]; // Internal buffer for input data + size_t buf_in_size; // size of data that is in the input buffer + size_t buf_in_size_total_old; + size_t buf_in_size_total; + double upload_speed_bytes; // fills if module dap_traffic_track initialized + char buf_out[DAP_CLIENT_REMOTE_BUF+1]; // Internal buffer for output data char hostaddr[1024]; // Address char service[128]; + size_t buf_out_size; // size of data that is in the output buffer + size_t buf_out_size_total_old; + size_t buf_out_size_total; + double download_speed_bytes; // fills if module dap_traffic_track initialized + ev_io* watcher_client; struct dap_server * server; diff --git a/dap_traffic_track.c b/dap_traffic_track.c index 7ca2cae..9c6b097 100644 --- a/dap_traffic_track.c +++ b/dap_traffic_track.c @@ -2,35 +2,70 @@ #include "dap_common.h" #define LOG_TAG "dap_traffic_track" +#define BYTES_IN_MB 1048576.0 static dap_traffic_callback_t callback = NULL; -static dap_server_client_t * server_clients; -static ev_timer timeout_watcher; +static dap_server_t * _dap_server; +static ev_timer _timeout_watcher; static struct ev_loop *loop; +/** + * @brief calculate_mbs_speed + * @param count_bytes + * @details timeout we gots from _timeout_watcher.repeat + * @return mbs speed + */ +static double calculate_mbs_speed(size_t count_bytes) { + size_t bytes_per_sec = count_bytes / (size_t)_timeout_watcher.repeat; + log_it(L_DEBUG, "TIMEOUT: %d, bytes_per_sec: %d", + (size_t)_timeout_watcher.repeat, bytes_per_sec); + return bytes_per_sec / BYTES_IN_MB; +} + static void timeout_cb() { - if(callback != NULL) { - callback(NULL, 0); - return; + pthread_mutex_lock(&_dap_server->mutex_on_hash); + + dap_server_client_t *dap_cur, *tmp; + HASH_ITER(hh,_dap_server->clients,dap_cur,tmp) { + log_it(L_DEBUG, "hash iter socket: %d buf_in_total_new: %d, buf_in_total_old: %d", + dap_cur->socket, dap_cur->buf_in_size_total, dap_cur->buf_in_size_total_old); + + dap_cur->upload_speed_bytes = + calculate_mbs_speed(dap_cur->buf_in_size_total - dap_cur->buf_in_size_total_old); + dap_cur->buf_in_size_total_old = dap_cur->buf_in_size_total; + + dap_cur->download_speed_bytes = + calculate_mbs_speed(dap_cur->buf_out_size_total - dap_cur->buf_out_size_total_old); + dap_cur->buf_out_size_total_old = dap_cur->buf_out_size_total; + + log_it(L_DEBUG, "upload_mbs: %f, download_mbs: %f", dap_cur->upload_speed_bytes, dap_cur->download_speed_bytes); + } - log_it(L_WARNING, "Callback is NULL!"); + + pthread_mutex_unlock(&_dap_server->mutex_on_hash); + +// if(callback != NULL) { +// callback(NULL, 0); +// return; +// } +// log_it(L_WARNING, "Callback is NULL!"); } -void dap_traffic_track_init(dap_server_client_t * clients, +void dap_traffic_track_init(dap_server_t * server, time_t timeout) { - server_clients = clients; - timeout_watcher.repeat = timeout; + _dap_server = server; + _timeout_watcher.repeat = timeout; loop = EV_DEFAULT; - ev_init(&timeout_watcher, timeout_cb); - ev_timer_again (loop, &timeout_watcher); + ev_init(&_timeout_watcher, timeout_cb); + ev_timer_again (loop, &_timeout_watcher); log_it(L_NOTICE, "Initialized traffic track module"); } void dap_traffic_track_deinit() { - ev_timer_stop(loop, &timeout_watcher); + ev_timer_stop(loop, &_timeout_watcher); log_it(L_NOTICE, "Deinitialized traffic track module"); } diff --git a/dap_traffic_track.h b/dap_traffic_track.h index a8a8886..c693d1d 100644 --- a/dap_traffic_track.h +++ b/dap_traffic_track.h @@ -1,11 +1,7 @@ #ifndef _TRAFFIC_TRACK_H_ #define _TRAFFIC_TRACK_H_ #include "dap_server_client.h" - -typedef struct dap_traffic_info { - dap_server_client_t * client; - size_t traffic_speed_bytes; -} dap_traffic_info_t; +#include "dap_server.h" typedef void (*dap_traffic_callback_t) (struct dap_traffic_info *, size_t count_info); // Callback for specific server's operations @@ -14,7 +10,7 @@ typedef void (*dap_traffic_callback_t) (struct dap_traffic_info *, size_t count_ * @param clients * @param timeout callback */ -void dap_traffic_track_init(dap_server_client_t * clients, +void dap_traffic_track_init(dap_server_t * server, time_t timeout); /** -- GitLab