From d442b1fafaf6f8eddd339b3429c74b5325cef5f0 Mon Sep 17 00:00:00 2001 From: "Dmitriy A. Gerasimov" <dmitriy.gerasimov@demlabs.net> Date: Fri, 2 Aug 2019 17:50:23 +0700 Subject: [PATCH] [-] Removed ^M --- include/dap_client_remote.h | 262 +++++++++++++++--------------- include/dap_events.h | 188 ++++++++++----------- include/dap_events_socket.h | 316 ++++++++++++++++++------------------ include/dap_memcached.h | 98 +++++------ include/dap_server.h | 292 ++++++++++++++++----------------- include/dap_traffic_track.h | 100 ++++++------ 6 files changed, 628 insertions(+), 628 deletions(-) diff --git a/include/dap_client_remote.h b/include/dap_client_remote.h index b3b663b..22484b2 100755 --- a/include/dap_client_remote.h +++ b/include/dap_client_remote.h @@ -1,131 +1,131 @@ -/* - * Authors: - * Dmitriy A. Gearasimov <gerasimov.dmitriy@demlabs.net> - * DeM Labs Inc. https://demlabs.net - * Kelvin Project https://github.com/kelvinblockchain - * Copyright (c) 2017-2018 - * All rights reserved. - - This file is part of DAP (Deus Applications Prototypes) the open source project - - DAP (Deus Applicaions Prototypes) is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - DAP is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with any DAP based project. If not, see <http://www.gnu.org/licenses/>. -*/ -#pragma once - -#include <stdint.h> -#include <stddef.h> -#include <stdbool.h> -#include "uthash.h" - -#ifndef WIN32 -#include <sys/epoll.h> -#include <sys/timerfd.h> -#endif - -#ifndef EPOLL_HANDLE - #ifndef WIN32 - #define EPOLL_HANDLE int - #else - #define EPOLL_HANDLE HANDLE - #endif -#endif - -typedef char str_ip[16]; - -typedef struct dap_server dap_server_t; -typedef struct dap_server_thread_s dap_server_thread_t; - -struct dap_client_remote; - -typedef void (*dap_server_client_callback_t) (struct dap_client_remote *,void * arg); // Callback for specific client operations - -#define DAP_CLIENT_REMOTE_BUF 500000 -#define CLIENT_ID_SIZE 12 - -typedef char dap_server_client_id[CLIENT_ID_SIZE]; - -typedef struct traffic_stats { - - size_t buf_size_total; - size_t buf_size_total_old; // for calculate speed - double speed_mbs; // MegaBits per second -} traffic_stats_t; - -typedef struct dap_client_remote { - - int socket; - dap_server_client_id id; - - uint32_t flags; - bool no_close; - bool kill_signal; - - uint16_t port; - str_ip s_ip; - - uint32_t buf_out_zero_count; - char buf_in[DAP_CLIENT_REMOTE_BUF]; // Internal buffer for input data - - size_t buf_in_size; // size of data that is in the input buffer - - traffic_stats_t upload_stat; - traffic_stats_t download_stat; - - char buf_out[DAP_CLIENT_REMOTE_BUF]; // Internal buffer for output data - size_t buf_out_offset; - - char hostaddr[1024]; // Address - char service[128]; - - size_t buf_out_size; // size of data that is in the output buffer - struct epoll_event pevent; - - EPOLL_HANDLE efd; // Epoll fd - int tn; // working thread index - - time_t time_connection; - time_t last_time_active; - - struct dap_server *server; - dap_server_thread_t *thread; - - UT_hash_handle hh; - struct dap_client_remote *next, *prev; - struct dap_client_remote *knext, *kprev; - - void *_internal; - void *_inheritor; // Internal data to specific client type, usualy states for state machine - -} dap_client_remote_t; // Node of bidirectional list of clients - -int dap_client_remote_init( void ); // Init clients module -void dap_client_remote_deinit( void ); // Deinit clients module - -dap_client_remote_t *dap_client_remote_create( dap_server_t *sh, int s, dap_server_thread_t *dsth ); -dap_client_remote_t *dap_client_remote_find( int sock, dap_server_thread_t *t ); - -bool dap_client_remote_is_ready_to_read( dap_client_remote_t * sc ); -bool dap_client_remote_is_ready_to_write( dap_client_remote_t * sc ); -void dap_client_remote_ready_to_read( dap_client_remote_t * sc,bool is_ready ); -void dap_client_remote_ready_to_write( dap_client_remote_t * sc,bool is_ready ); - -size_t dap_client_remote_write( dap_client_remote_t *sc, const void * data, size_t data_size ); -size_t dap_client_remote_write_f( dap_client_remote_t *a_client, const char * a_format,... ); -size_t dap_client_remote_read( dap_client_remote_t *sc, void * data, size_t data_size ); - -//void dap_client_remote_remove( dap_client_remote_t *sc, struct dap_server * sh ); // Removes the client from the list -void dap_client_remote_remove( dap_client_remote_t *sc ); - -void dap_client_remote_shrink_buf_in( dap_client_remote_t * cl, size_t shrink_size ); - +/* + * Authors: + * Dmitriy A. Gearasimov <gerasimov.dmitriy@demlabs.net> + * DeM Labs Inc. https://demlabs.net + * Kelvin Project https://github.com/kelvinblockchain + * Copyright (c) 2017-2018 + * All rights reserved. + + This file is part of DAP (Deus Applications Prototypes) the open source project + + DAP (Deus Applicaions Prototypes) is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + DAP is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with any DAP based project. If not, see <http://www.gnu.org/licenses/>. +*/ +#pragma once + +#include <stdint.h> +#include <stddef.h> +#include <stdbool.h> +#include "uthash.h" + +#ifndef WIN32 +#include <sys/epoll.h> +#include <sys/timerfd.h> +#endif + +#ifndef EPOLL_HANDLE + #ifndef WIN32 + #define EPOLL_HANDLE int + #else + #define EPOLL_HANDLE HANDLE + #endif +#endif + +typedef char str_ip[16]; + +typedef struct dap_server dap_server_t; +typedef struct dap_server_thread_s dap_server_thread_t; + +struct dap_client_remote; + +typedef void (*dap_server_client_callback_t) (struct dap_client_remote *,void * arg); // Callback for specific client operations + +#define DAP_CLIENT_REMOTE_BUF 500000 +#define CLIENT_ID_SIZE 12 + +typedef char dap_server_client_id[CLIENT_ID_SIZE]; + +typedef struct traffic_stats { + + size_t buf_size_total; + size_t buf_size_total_old; // for calculate speed + double speed_mbs; // MegaBits per second +} traffic_stats_t; + +typedef struct dap_client_remote { + + int socket; + dap_server_client_id id; + + uint32_t flags; + bool no_close; + bool kill_signal; + + uint16_t port; + str_ip s_ip; + + uint32_t buf_out_zero_count; + char buf_in[DAP_CLIENT_REMOTE_BUF]; // Internal buffer for input data + + size_t buf_in_size; // size of data that is in the input buffer + + traffic_stats_t upload_stat; + traffic_stats_t download_stat; + + char buf_out[DAP_CLIENT_REMOTE_BUF]; // Internal buffer for output data + size_t buf_out_offset; + + char hostaddr[1024]; // Address + char service[128]; + + size_t buf_out_size; // size of data that is in the output buffer + struct epoll_event pevent; + + EPOLL_HANDLE efd; // Epoll fd + int tn; // working thread index + + time_t time_connection; + time_t last_time_active; + + struct dap_server *server; + dap_server_thread_t *thread; + + UT_hash_handle hh; + struct dap_client_remote *next, *prev; + struct dap_client_remote *knext, *kprev; + + void *_internal; + void *_inheritor; // Internal data to specific client type, usualy states for state machine + +} dap_client_remote_t; // Node of bidirectional list of clients + +int dap_client_remote_init( void ); // Init clients module +void dap_client_remote_deinit( void ); // Deinit clients module + +dap_client_remote_t *dap_client_remote_create( dap_server_t *sh, int s, dap_server_thread_t *dsth ); +dap_client_remote_t *dap_client_remote_find( int sock, dap_server_thread_t *t ); + +bool dap_client_remote_is_ready_to_read( dap_client_remote_t * sc ); +bool dap_client_remote_is_ready_to_write( dap_client_remote_t * sc ); +void dap_client_remote_ready_to_read( dap_client_remote_t * sc,bool is_ready ); +void dap_client_remote_ready_to_write( dap_client_remote_t * sc,bool is_ready ); + +size_t dap_client_remote_write( dap_client_remote_t *sc, const void * data, size_t data_size ); +size_t dap_client_remote_write_f( dap_client_remote_t *a_client, const char * a_format,... ); +size_t dap_client_remote_read( dap_client_remote_t *sc, void * data, size_t data_size ); + +//void dap_client_remote_remove( dap_client_remote_t *sc, struct dap_server * sh ); // Removes the client from the list +void dap_client_remote_remove( dap_client_remote_t *sc ); + +void dap_client_remote_shrink_buf_in( dap_client_remote_t * cl, size_t shrink_size ); + diff --git a/include/dap_events.h b/include/dap_events.h index 2b27453..1b2d52a 100755 --- a/include/dap_events.h +++ b/include/dap_events.h @@ -1,94 +1,94 @@ -/* - * Authors: - * Dmitriy A. Gearasimov <gerasimov.dmitriy@demlabs.net> - * DeM Labs Inc. https://demlabs.net - * Kelvin Project https://github.com/kelvinblockchain - * Copyright (c) 2017-2019 - * All rights reserved. - - This file is part of DAP (Deus Applications Prototypes) the open source project - - DAP (Deus Applicaions Prototypes) is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - DAP is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with any DAP based project. If not, see <http://www.gnu.org/licenses/>. -*/ -#pragma once - -#ifndef WIN32 -#include <netinet/in.h> - -#include <stdint.h> -#include <pthread.h> -#define EPOLL_HANDLE int -#else -#define EPOLL_HANDLE HANDLE -#endif - -#include "uthash.h" -#include "dap_events_socket.h" -#include "dap_server.h" - -struct dap_events; - -typedef void (*dap_events_callback_t) (struct dap_events *, void *arg); // Callback for specific server's operations - -typedef struct dap_thread { - pthread_t tid; -} dap_thread_t; - -struct dap_worker; - -typedef struct dap_events { - - dap_events_socket_t *sockets; // Hashmap of event sockets - dap_events_socket_t *dlsockets; // Dlist of event sockets - dap_events_socket_t *to_kill_sockets; // Dlist of event sockets - - pthread_rwlock_t sockets_rwlock; - void *_inheritor; // Pointer to the internal data, HTTP for example - dap_thread_t proc_thread; - pthread_rwlock_t servers_rwlock; - -} dap_events_t; - -typedef struct dap_worker -{ - uint32_t event_sockets_count; - uint32_t event_to_kill_count; - - EPOLL_HANDLE epoll_fd; - uint32_t number_thread; - pthread_mutex_t locker_on_count; - dap_events_t *events; - -} dap_worker_t; - -int32_t dap_events_init( uint32_t a_threads_count, size_t conn_t ); // Init server module -void dap_events_deinit( ); // Deinit server module - -void dap_events_thread_wake_up( dap_thread_t *th ); -dap_events_t* dap_events_new( ); -void dap_events_delete( dap_events_t * sh ); -//void dap_events_socket_remove_and_delete( dap_events_socket_t* a_es ); -void dap_events_socket_remove_and_delete(dap_events_socket_t* a_es, bool preserve_inheritor ); - -void dap_events_kill_socket( dap_events_socket_t *a_es ); - -int32_t dap_events_start( dap_events_t *sh ); -int32_t dap_events_wait( dap_events_t *sh ); - -uint32_t dap_worker_get_index_min( ); -dap_worker_t *dap_worker_get_min( ); - -void dap_worker_add_events_socket( dap_events_socket_t * a_events_socket ); -void dap_worker_print_all( ); - +/* + * Authors: + * Dmitriy A. Gearasimov <gerasimov.dmitriy@demlabs.net> + * DeM Labs Inc. https://demlabs.net + * Kelvin Project https://github.com/kelvinblockchain + * Copyright (c) 2017-2019 + * All rights reserved. + + This file is part of DAP (Deus Applications Prototypes) the open source project + + DAP (Deus Applicaions Prototypes) is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + DAP is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with any DAP based project. If not, see <http://www.gnu.org/licenses/>. +*/ +#pragma once + +#ifndef WIN32 +#include <netinet/in.h> + +#include <stdint.h> +#include <pthread.h> +#define EPOLL_HANDLE int +#else +#define EPOLL_HANDLE HANDLE +#endif + +#include "uthash.h" +#include "dap_events_socket.h" +#include "dap_server.h" + +struct dap_events; + +typedef void (*dap_events_callback_t) (struct dap_events *, void *arg); // Callback for specific server's operations + +typedef struct dap_thread { + pthread_t tid; +} dap_thread_t; + +struct dap_worker; + +typedef struct dap_events { + + dap_events_socket_t *sockets; // Hashmap of event sockets + dap_events_socket_t *dlsockets; // Dlist of event sockets + dap_events_socket_t *to_kill_sockets; // Dlist of event sockets + + pthread_rwlock_t sockets_rwlock; + void *_inheritor; // Pointer to the internal data, HTTP for example + dap_thread_t proc_thread; + pthread_rwlock_t servers_rwlock; + +} dap_events_t; + +typedef struct dap_worker +{ + uint32_t event_sockets_count; + uint32_t event_to_kill_count; + + EPOLL_HANDLE epoll_fd; + uint32_t number_thread; + pthread_mutex_t locker_on_count; + dap_events_t *events; + +} dap_worker_t; + +int32_t dap_events_init( uint32_t a_threads_count, size_t conn_t ); // Init server module +void dap_events_deinit( ); // Deinit server module + +void dap_events_thread_wake_up( dap_thread_t *th ); +dap_events_t* dap_events_new( ); +void dap_events_delete( dap_events_t * sh ); +//void dap_events_socket_remove_and_delete( dap_events_socket_t* a_es ); +void dap_events_socket_remove_and_delete(dap_events_socket_t* a_es, bool preserve_inheritor ); + +void dap_events_kill_socket( dap_events_socket_t *a_es ); + +int32_t dap_events_start( dap_events_t *sh ); +int32_t dap_events_wait( dap_events_t *sh ); + +uint32_t dap_worker_get_index_min( ); +dap_worker_t *dap_worker_get_min( ); + +void dap_worker_add_events_socket( dap_events_socket_t * a_events_socket ); +void dap_worker_print_all( ); + diff --git a/include/dap_events_socket.h b/include/dap_events_socket.h index ea18224..cdd7d7b 100755 --- a/include/dap_events_socket.h +++ b/include/dap_events_socket.h @@ -1,158 +1,158 @@ -/* - * Authors: - * Dmitriy A. Gearasimov <gerasimov.dmitriy@demlabs.net> - * DeM Labs Inc. https://demlabs.net - * Kelvin Project https://github.com/kelvinblockchain - * Copyright (c) 2017-2019 - * All rights reserved. - - This file is part of DAP (Deus Applications Prototypes) the open source project - - DAP (Deus Applicaions Prototypes) is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - DAP is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with any DAP based project. If not, see <http://www.gnu.org/licenses/>. -*/ -#pragma once - -#include <stdint.h> -#include <stddef.h> -#include <stdbool.h> -#include "uthash.h" -#ifndef _WIN32 -#include <sys/epoll.h> -#endif - -struct dap_events; -struct dap_events_socket; -struct dap_worker; - -typedef struct dap_server dap_server_t; -typedef void (*dap_events_socket_callback_t) (struct dap_events_socket *,void * arg); // Callback for specific client operations - -typedef struct dap_events_socket_callbacks { - - dap_events_socket_callback_t new_callback; // Create new client callback - dap_events_socket_callback_t delete_callback; // Delete client callback - dap_events_socket_callback_t read_callback; // Read function - dap_events_socket_callback_t write_callback; // Write function - dap_events_socket_callback_t error_callback; // Error processing function - -} dap_events_socket_callbacks_t; - -#define DAP_EVENTS_SOCKET_BUF 100000 - -#if 0 -typedef struct dap_events_socket { - - int socket; - bool signal_close; - - bool _ready_to_write; - bool _ready_to_read; - - uint32_t buf_out_zero_count; - union{ - uint8_t buf_in[DAP_EVENTS_SOCKET_BUF+1]; // Internal buffer for input data - char buf_in_str[DAP_EVENTS_SOCKET_BUF+1]; - }; - size_t buf_in_size; // size of data that is in the input buffer - - uint8_t buf_out[DAP_EVENTS_SOCKET_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 - - struct dap_events *events; - - struct dap_worker *dap_worker; - dap_events_socket_callbacks_t *callbacks; - - time_t time_connection; - time_t last_ping_request; - bool is_pingable; - - UT_hash_handle hh; - - void * _inheritor; // Inheritor data to specific client type, usualy states for state machine -} dap_events_socket_t; // Node of bidirectional list of clients -#endif - -typedef struct dap_events_socket { - - int32_t socket; - - uint32_t flags; -// bool signal_close; - bool no_close; - bool kill_signal; -// bool _ready_to_write; -// bool _ready_to_read; - - uint32_t buf_out_zero_count; - union{ - uint8_t buf_in[DAP_EVENTS_SOCKET_BUF+1]; // Internal buffer for input data - char buf_in_str[DAP_EVENTS_SOCKET_BUF+1]; - }; - size_t buf_in_size; // size of data that is in the input buffer - - uint8_t buf_out[DAP_EVENTS_SOCKET_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 - - struct dap_events *events; - struct dap_worker *dap_worker; - struct epoll_event ev; - - dap_events_socket_callbacks_t *callbacks; - - time_t time_connection; - time_t last_time_active; - time_t last_ping_request; - bool is_pingable; - - UT_hash_handle hh; - struct dap_events_socket *next, *prev; - struct dap_events_socket *knext, *kprev; - - void *_inheritor; // Inheritor data to specific client type, usualy states for state machine - -} dap_events_socket_t; // Node of bidirectional list of clients - -int dap_events_socket_init(); // Init clients module -void dap_events_socket_deinit(); // Deinit clients module - -void dap_events_socket_create_after(dap_events_socket_t * a_es); - -dap_events_socket_t * dap_events_socket_wrap_no_add(struct dap_events * a_events, - int s, dap_events_socket_callbacks_t * a_callbacks); // Create new client and add it to the list - - -dap_events_socket_t * dap_events_socket_find(int sock, struct dap_events * sh); // Find client by socket - -bool dap_events_socket_is_ready_to_read(dap_events_socket_t * sc); -bool dap_events_socket_is_ready_to_write(dap_events_socket_t * sc); -void dap_events_socket_set_readable(dap_events_socket_t * sc,bool is_ready); -void dap_events_socket_set_writable(dap_events_socket_t * sc,bool is_ready); - -size_t dap_events_socket_write(dap_events_socket_t *sc, const void * data, size_t data_size); -size_t dap_events_socket_write_f(dap_events_socket_t *sc, const char * format,...); -size_t dap_events_socket_read(dap_events_socket_t *sc, void * data, size_t data_size); - -void dap_events_socket_delete(dap_events_socket_t *sc,bool preserve_inheritor); // Removes the client from the list - -void dap_events_socket_shrink_buf_in(dap_events_socket_t * cl, size_t shrink_size); - +/* + * Authors: + * Dmitriy A. Gearasimov <gerasimov.dmitriy@demlabs.net> + * DeM Labs Inc. https://demlabs.net + * Kelvin Project https://github.com/kelvinblockchain + * Copyright (c) 2017-2019 + * All rights reserved. + + This file is part of DAP (Deus Applications Prototypes) the open source project + + DAP (Deus Applicaions Prototypes) is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + DAP is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with any DAP based project. If not, see <http://www.gnu.org/licenses/>. +*/ +#pragma once + +#include <stdint.h> +#include <stddef.h> +#include <stdbool.h> +#include "uthash.h" +#ifndef _WIN32 +#include <sys/epoll.h> +#endif + +struct dap_events; +struct dap_events_socket; +struct dap_worker; + +typedef struct dap_server dap_server_t; +typedef void (*dap_events_socket_callback_t) (struct dap_events_socket *,void * arg); // Callback for specific client operations + +typedef struct dap_events_socket_callbacks { + + dap_events_socket_callback_t new_callback; // Create new client callback + dap_events_socket_callback_t delete_callback; // Delete client callback + dap_events_socket_callback_t read_callback; // Read function + dap_events_socket_callback_t write_callback; // Write function + dap_events_socket_callback_t error_callback; // Error processing function + +} dap_events_socket_callbacks_t; + +#define DAP_EVENTS_SOCKET_BUF 100000 + +#if 0 +typedef struct dap_events_socket { + + int socket; + bool signal_close; + + bool _ready_to_write; + bool _ready_to_read; + + uint32_t buf_out_zero_count; + union{ + uint8_t buf_in[DAP_EVENTS_SOCKET_BUF+1]; // Internal buffer for input data + char buf_in_str[DAP_EVENTS_SOCKET_BUF+1]; + }; + size_t buf_in_size; // size of data that is in the input buffer + + uint8_t buf_out[DAP_EVENTS_SOCKET_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 + + struct dap_events *events; + + struct dap_worker *dap_worker; + dap_events_socket_callbacks_t *callbacks; + + time_t time_connection; + time_t last_ping_request; + bool is_pingable; + + UT_hash_handle hh; + + void * _inheritor; // Inheritor data to specific client type, usualy states for state machine +} dap_events_socket_t; // Node of bidirectional list of clients +#endif + +typedef struct dap_events_socket { + + int32_t socket; + + uint32_t flags; +// bool signal_close; + bool no_close; + bool kill_signal; +// bool _ready_to_write; +// bool _ready_to_read; + + uint32_t buf_out_zero_count; + union{ + uint8_t buf_in[DAP_EVENTS_SOCKET_BUF+1]; // Internal buffer for input data + char buf_in_str[DAP_EVENTS_SOCKET_BUF+1]; + }; + size_t buf_in_size; // size of data that is in the input buffer + + uint8_t buf_out[DAP_EVENTS_SOCKET_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 + + struct dap_events *events; + struct dap_worker *dap_worker; + struct epoll_event ev; + + dap_events_socket_callbacks_t *callbacks; + + time_t time_connection; + time_t last_time_active; + time_t last_ping_request; + bool is_pingable; + + UT_hash_handle hh; + struct dap_events_socket *next, *prev; + struct dap_events_socket *knext, *kprev; + + void *_inheritor; // Inheritor data to specific client type, usualy states for state machine + +} dap_events_socket_t; // Node of bidirectional list of clients + +int dap_events_socket_init(); // Init clients module +void dap_events_socket_deinit(); // Deinit clients module + +void dap_events_socket_create_after(dap_events_socket_t * a_es); + +dap_events_socket_t * dap_events_socket_wrap_no_add(struct dap_events * a_events, + int s, dap_events_socket_callbacks_t * a_callbacks); // Create new client and add it to the list + + +dap_events_socket_t * dap_events_socket_find(int sock, struct dap_events * sh); // Find client by socket + +bool dap_events_socket_is_ready_to_read(dap_events_socket_t * sc); +bool dap_events_socket_is_ready_to_write(dap_events_socket_t * sc); +void dap_events_socket_set_readable(dap_events_socket_t * sc,bool is_ready); +void dap_events_socket_set_writable(dap_events_socket_t * sc,bool is_ready); + +size_t dap_events_socket_write(dap_events_socket_t *sc, const void * data, size_t data_size); +size_t dap_events_socket_write_f(dap_events_socket_t *sc, const char * format,...); +size_t dap_events_socket_read(dap_events_socket_t *sc, void * data, size_t data_size); + +void dap_events_socket_delete(dap_events_socket_t *sc,bool preserve_inheritor); // Removes the client from the list + +void dap_events_socket_shrink_buf_in(dap_events_socket_t * cl, size_t shrink_size); + diff --git a/include/dap_memcached.h b/include/dap_memcached.h index ba7c4fa..98041c9 100755 --- a/include/dap_memcached.h +++ b/include/dap_memcached.h @@ -1,49 +1,49 @@ -#pragma once - -#include <stdint.h> -#include <stdbool.h> - -#include "dap_common.h" - -/** - * @brief dap_memcached_init - * @param server_host - * @param port - * @return - */ -int dap_memcached_init(const char *server_host, uint16_t port); - -/** - * @brief is_dap_memcache_enable - * @return - */ -bool dap_memcache_is_enable(void); - -/** - * @brief dap_memcached_deinit - */ -void dap_memcached_deinit(void); - -/** - * @brief dap_memcache_put - * @param key - * @param value - * @param value_size - * @param expiration if 0 value is never expire - * @return - */ -bool dap_memcache_put(const char* key, void *value, size_t value_size, time_t expiration); - -/** - * @brief dap_memcache_get - * @param key - * @return true if key found - */ -bool dap_memcache_get(const char* key, size_t * value_size, void ** result); - -/** - * @brief dap_memcache_delete - * @param key - * @return - */ -bool dap_memcache_delete(const char* key); +#pragma once + +#include <stdint.h> +#include <stdbool.h> + +#include "dap_common.h" + +/** + * @brief dap_memcached_init + * @param server_host + * @param port + * @return + */ +int dap_memcached_init(const char *server_host, uint16_t port); + +/** + * @brief is_dap_memcache_enable + * @return + */ +bool dap_memcache_is_enable(void); + +/** + * @brief dap_memcached_deinit + */ +void dap_memcached_deinit(void); + +/** + * @brief dap_memcache_put + * @param key + * @param value + * @param value_size + * @param expiration if 0 value is never expire + * @return + */ +bool dap_memcache_put(const char* key, void *value, size_t value_size, time_t expiration); + +/** + * @brief dap_memcache_get + * @param key + * @return true if key found + */ +bool dap_memcache_get(const char* key, size_t * value_size, void ** result); + +/** + * @brief dap_memcache_delete + * @param key + * @return + */ +bool dap_memcache_delete(const char* key); diff --git a/include/dap_server.h b/include/dap_server.h index eb668b3..1b3351a 100755 --- a/include/dap_server.h +++ b/include/dap_server.h @@ -1,146 +1,146 @@ -/* - Copyright (c) 2017-2018 (c) Project "DeM Labs Inc" https://github.com/demlabsinc - All rights reserved. - - This file is part of DAP (Deus Applications Prototypes) the open source project - - DAP (Deus Applicaions Prototypes) is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - DAP is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public License - along with any DAP based project. If not, see <http://www.gnu.org/licenses/>. -*/ -#pragma once - -#ifndef _WIN32 -#include <netinet/in.h> - -#include <stdint.h> -#include <pthread.h> - -#include <sys/epoll.h> -#include <sys/timerfd.h> -#define EPOLL_HANDLE int -#else -#define EPOLL_HANDLE HANDLE -#endif - -#include "uthash.h" -#include "utlist.h" - -#include "dap_cpu_monitor.h" -#include "dap_client_remote.h" - -typedef enum dap_server_type {DAP_SERVER_TCP} dap_server_type_t; - -#define BIT( x ) ( 1 << x ) - -#define DAP_SOCK_READY_TO_READ BIT( 0 ) -#define DAP_SOCK_READY_TO_WRITE BIT( 1 ) -#define DAP_SOCK_SIGNAL_CLOSE BIT( 2 ) -#define DAP_SOCK_ACTIVE BIT( 3 ) - -typedef struct dap_server_thread_s { - - EPOLL_HANDLE epoll_fd; - - uint32_t thread_num; - uint32_t connections_count; - uint32_t to_kill_count; - - struct epoll_event *epoll_events; - dap_client_remote_t *dap_remote_clients; - dap_client_remote_t *hclients; // Hashmap of clients - dap_client_remote_t *dap_clients_to_kill; - - pthread_mutex_t mutex_dlist_add_remove; - pthread_mutex_t mutex_on_hash; - -} dap_server_thread_t; - -struct dap_server; - -typedef void (*dap_server_callback_t)( struct dap_server *,void * arg ); // Callback for specific server's operations - -typedef struct dap_server { - - dap_server_type_t type; // Server's type - uint16_t port; // Listen port - char *address; // Listen address - - int32_t socket_listener; // Socket for listener - EPOLL_HANDLE epoll_fd; // Epoll fd - - struct sockaddr_in listener_addr; // Kernel structure for listener's binded address - - void *_inheritor; // Pointer to the internal data, HTTP for example - - dap_cpu_stats_t cpu_stats; - - dap_server_callback_t server_delete_callback; - - dap_server_client_callback_t client_new_callback; // Create new client callback - dap_server_client_callback_t client_delete_callback; // Delete client callback - dap_server_client_callback_t client_read_callback; // Read function - dap_server_client_callback_t client_write_callback; // Write function - dap_server_client_callback_t client_error_callback; // Error processing function - -} dap_server_t; - -int32_t dap_server_init( uint32_t count_threads ); // Init server module -void dap_server_deinit( void ); // Deinit server module - -dap_server_t *dap_server_listen( const char *addr, uint16_t port, dap_server_type_t type ); - -int32_t dap_server_loop( dap_server_t *d_server ); - -#define DL_LIST_REMOVE_NODE( head, obj, _prev_, _next_, total ) \ - \ - if ( obj->_next_ ) { \ - \ - if ( obj->_prev_ ) \ - obj->_next_->_prev_ = obj->_prev_; \ - else { \ - \ - obj->_next_->_prev_ = NULL; \ - head = obj->_next_; \ - } \ - } \ - \ - if ( obj->_prev_ ) { \ - \ - if ( obj->_next_ ) \ - obj->_prev_->_next_ = obj->_next_; \ - else { \ - \ - obj->_prev_->_next_ = NULL; \ - } \ - } \ - -- total; - -#define DL_LIST_ADD_NODE_HEAD( head, obj, _prev_, _next_, total )\ - \ - if ( !total ) { \ - \ - obj->_prev_ = NULL; \ - obj->_next_ = NULL; \ - \ - head = obj; \ - } \ - else { \ - \ - head->_prev_ = obj; \ - \ - obj->_prev_ = NULL; \ - obj->_next_ = head; \ - \ - head = obj; \ - } \ - ++ total; +/* + Copyright (c) 2017-2018 (c) Project "DeM Labs Inc" https://github.com/demlabsinc + All rights reserved. + + This file is part of DAP (Deus Applications Prototypes) the open source project + + DAP (Deus Applicaions Prototypes) is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + DAP is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with any DAP based project. If not, see <http://www.gnu.org/licenses/>. +*/ +#pragma once + +#ifndef _WIN32 +#include <netinet/in.h> + +#include <stdint.h> +#include <pthread.h> + +#include <sys/epoll.h> +#include <sys/timerfd.h> +#define EPOLL_HANDLE int +#else +#define EPOLL_HANDLE HANDLE +#endif + +#include "uthash.h" +#include "utlist.h" + +#include "dap_cpu_monitor.h" +#include "dap_client_remote.h" + +typedef enum dap_server_type {DAP_SERVER_TCP} dap_server_type_t; + +#define BIT( x ) ( 1 << x ) + +#define DAP_SOCK_READY_TO_READ BIT( 0 ) +#define DAP_SOCK_READY_TO_WRITE BIT( 1 ) +#define DAP_SOCK_SIGNAL_CLOSE BIT( 2 ) +#define DAP_SOCK_ACTIVE BIT( 3 ) + +typedef struct dap_server_thread_s { + + EPOLL_HANDLE epoll_fd; + + uint32_t thread_num; + uint32_t connections_count; + uint32_t to_kill_count; + + struct epoll_event *epoll_events; + dap_client_remote_t *dap_remote_clients; + dap_client_remote_t *hclients; // Hashmap of clients + dap_client_remote_t *dap_clients_to_kill; + + pthread_mutex_t mutex_dlist_add_remove; + pthread_mutex_t mutex_on_hash; + +} dap_server_thread_t; + +struct dap_server; + +typedef void (*dap_server_callback_t)( struct dap_server *,void * arg ); // Callback for specific server's operations + +typedef struct dap_server { + + dap_server_type_t type; // Server's type + uint16_t port; // Listen port + char *address; // Listen address + + int32_t socket_listener; // Socket for listener + EPOLL_HANDLE epoll_fd; // Epoll fd + + struct sockaddr_in listener_addr; // Kernel structure for listener's binded address + + void *_inheritor; // Pointer to the internal data, HTTP for example + + dap_cpu_stats_t cpu_stats; + + dap_server_callback_t server_delete_callback; + + dap_server_client_callback_t client_new_callback; // Create new client callback + dap_server_client_callback_t client_delete_callback; // Delete client callback + dap_server_client_callback_t client_read_callback; // Read function + dap_server_client_callback_t client_write_callback; // Write function + dap_server_client_callback_t client_error_callback; // Error processing function + +} dap_server_t; + +int32_t dap_server_init( uint32_t count_threads ); // Init server module +void dap_server_deinit( void ); // Deinit server module + +dap_server_t *dap_server_listen( const char *addr, uint16_t port, dap_server_type_t type ); + +int32_t dap_server_loop( dap_server_t *d_server ); + +#define DL_LIST_REMOVE_NODE( head, obj, _prev_, _next_, total ) \ + \ + if ( obj->_next_ ) { \ + \ + if ( obj->_prev_ ) \ + obj->_next_->_prev_ = obj->_prev_; \ + else { \ + \ + obj->_next_->_prev_ = NULL; \ + head = obj->_next_; \ + } \ + } \ + \ + if ( obj->_prev_ ) { \ + \ + if ( obj->_next_ ) \ + obj->_prev_->_next_ = obj->_next_; \ + else { \ + \ + obj->_prev_->_next_ = NULL; \ + } \ + } \ + -- total; + +#define DL_LIST_ADD_NODE_HEAD( head, obj, _prev_, _next_, total )\ + \ + if ( !total ) { \ + \ + obj->_prev_ = NULL; \ + obj->_next_ = NULL; \ + \ + head = obj; \ + } \ + else { \ + \ + head->_prev_ = obj; \ + \ + obj->_prev_ = NULL; \ + obj->_next_ = head; \ + \ + head = obj; \ + } \ + ++ total; diff --git a/include/dap_traffic_track.h b/include/dap_traffic_track.h index 899a77a..1cfae9d 100755 --- a/include/dap_traffic_track.h +++ b/include/dap_traffic_track.h @@ -1,50 +1,50 @@ -/* - * Authors: - * Anatoliy Jurotich <anatoliy.kurotich@demlabs.net> - * DeM Labs Inc. https://demlabs.net - * Kelvin Project https://github.com/kelvinblockchain - * Copyright (c) 2017-2018 - * All rights reserved. - - This file is part of DAP (Deus Applications Prototypes) the open source project - - DAP (Deus Applicaions Prototypes) is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - DAP is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with any DAP based project. If not, see <http://www.gnu.org/licenses/>. -*/ - -#pragma once - -#include "dap_client_remote.h" -#include "dap_server.h" - -typedef void (*dap_traffic_callback_t) (dap_server_t *); - -/** - * @brief dap_traffic_track_init - * @param clients - * @param timeout callback - */ -void dap_traffic_track_init( dap_server_t *server, time_t timeout ); - -/** - * @brief dap_traffic_track_deinit - */ -void dap_traffic_track_deinit( void ); - -/** - * @brief dap_traffic_add_callback - */ -void dap_traffic_callback_set( dap_traffic_callback_t ); - -void dap_traffic_callback_stop( void ); - +/* + * Authors: + * Anatoliy Jurotich <anatoliy.kurotich@demlabs.net> + * DeM Labs Inc. https://demlabs.net + * Kelvin Project https://github.com/kelvinblockchain + * Copyright (c) 2017-2018 + * All rights reserved. + + This file is part of DAP (Deus Applications Prototypes) the open source project + + DAP (Deus Applicaions Prototypes) is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + DAP is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with any DAP based project. If not, see <http://www.gnu.org/licenses/>. +*/ + +#pragma once + +#include "dap_client_remote.h" +#include "dap_server.h" + +typedef void (*dap_traffic_callback_t) (dap_server_t *); + +/** + * @brief dap_traffic_track_init + * @param clients + * @param timeout callback + */ +void dap_traffic_track_init( dap_server_t *server, time_t timeout ); + +/** + * @brief dap_traffic_track_deinit + */ +void dap_traffic_track_deinit( void ); + +/** + * @brief dap_traffic_add_callback + */ +void dap_traffic_callback_set( dap_traffic_callback_t ); + +void dap_traffic_callback_stop( void ); + -- GitLab