diff --git a/CMakeLists.txt b/CMakeLists.txt index b3e49769d59907c9eff926f766b73ad3b7debbb4..fc1e096d09551ce67efbd51563ae14882d9b86d2 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,6 +6,18 @@ file(GLOB DAP_CHAIN_MEMPOOL_HDR *.h) add_definitions ("-DNODE_NETNAME=\"kelvin\"") +if(WIN32) + include_directories(../libdap/src/win32/) + include_directories(../3rdparty/libmemcached/) + include_directories(../3rdparty/libmemcached/win32/) + include_directories(../3rdparty/wepoll/include/) + include_directories(../3rdparty/uthash/src/) + include_directories(../3rdparty/libjson-c/) + include_directories(../3rdparty/libmagic/src/) + include_directories(../3rdparty/curl/include/) + include_directories(../3rdparty/libsqlite3/) +endif() + add_library(${PROJECT_NAME} STATIC ${DAP_CHAIN_MEMPOOL_SRC} ${DAP_CHAIN_MEMPOOL_HDR}) target_link_libraries(dap_chain_mempool dap_http_server dap_client dap_chain_net dap_chain_global_db dap_core) diff --git a/client_mempool.c b/client_mempool.c index 8f6e53d614f51020b2a92bf6e8b4d204bb35f9dc..5c1440efb5ac07d63ee6c5a0b3e8fda21e7e714e 100755 --- a/client_mempool.c +++ b/client_mempool.c @@ -28,6 +28,20 @@ #include <time.h> #include <assert.h> #include <errno.h> + +#ifdef _WIN32 +#undef _WIN32_WINNT +#define _WIN32_WINNT 0x0600 +#include <winsock2.h> +#include <windows.h> +#include <mswsock.h> +#include <ws2tcpip.h> +#include <io.h> +#include <time.h> +#include <wepoll.h> +#include <pthread.h> +#endif + #include "dap_common.h" #include "dap_config.h" #include "dap_events.h" @@ -40,6 +54,8 @@ #define SYSTEM_PREFIX "/opt/"DAP_APP_NAME #define SYSTEM_CONFIGS_DIR SYSTEM_PREFIX"/etc" +#define LOG_TAG "dap_client_mempool" + static int listen_port_tcp = 8079; // send request to server @@ -74,7 +90,13 @@ static void a_response_proc(dap_client_t *a_client, void *str, size_t str_len) } pthread_mutex_lock(&mempool->wait_mutex); mempool->state = CLIENT_MEMPOOL_SENDED; + +#ifndef _WIN32 pthread_cond_signal(&mempool->wait_cond); +#else + SetEvent( mempool->wait_cond ); +#endif + pthread_mutex_unlock(&mempool->wait_mutex); } } @@ -88,7 +110,11 @@ static void a_response_error(dap_client_t *a_client, int val) if(mempool) { pthread_mutex_lock(&mempool->wait_mutex); mempool->state = CLIENT_MEMPOOL_ERROR; +#ifndef _WIN32 pthread_cond_signal(&mempool->wait_cond); +#else + SetEvent( mempool->wait_cond ); +#endif pthread_mutex_unlock(&mempool->wait_mutex); } } @@ -101,7 +127,11 @@ static void a_stage_end_callback(dap_client_t *a_client, void *a_arg) if(mempool) { pthread_mutex_lock(&mempool->wait_mutex); mempool->state = CLIENT_MEMPOOL_CONNECTED; +#ifndef _WIN32 pthread_cond_signal(&mempool->wait_cond); +#else + SetEvent( mempool->wait_cond ); +#endif pthread_mutex_unlock(&mempool->wait_mutex); } } @@ -129,16 +159,24 @@ void client_mempool_deinit() dap_client_deinit(); } -client_mempool_t* client_mempool_connect(const char *addr) +client_mempool_t *client_mempool_connect(const char *addr) { if(!addr || strlen(addr) < 1) return NULL; client_mempool_t *mempool = DAP_NEW_Z(client_mempool_t); mempool->state = CLIENT_MEMPOOL_INIT; + + log_it(L_NOTICE, "======= client_mempool_connect( )" ); + +#ifndef _WIN32 pthread_condattr_t attr; pthread_condattr_init(&attr); pthread_condattr_setclock(&attr, CLOCK_MONOTONIC); pthread_cond_init(&mempool->wait_cond, &attr); +#else + mempool->wait_cond = CreateEventA( NULL, FALSE, FALSE, NULL ); +#endif + pthread_mutex_init(&mempool->wait_mutex, NULL); mempool->a_events = dap_events_new(); mempool->a_client = dap_client_new(mempool->a_events, stage_status_callback, stage_status_error_callback); @@ -166,8 +204,11 @@ client_mempool_t* client_mempool_connect(const char *addr) int client_mempool_wait(client_mempool_t *mempool, int waited_state, int timeout_ms) { int ret = -1; - if(!mempool) + if( !mempool ) return -1; + + log_it(L_NOTICE, "======= client_mempool_wait( ) tm %u ms", timeout_ms ); + pthread_mutex_lock(&mempool->wait_mutex); // have waited if(mempool->state == waited_state) { @@ -175,6 +216,7 @@ int client_mempool_wait(client_mempool_t *mempool, int waited_state, int timeout return 1; } // prepare for signal waiting +#ifndef _WIN32 struct timespec to; clock_gettime(CLOCK_MONOTONIC, &to); int64_t nsec_new = to.tv_nsec + timeout_ms * 1000000ll; @@ -191,6 +233,16 @@ int client_mempool_wait(client_mempool_t *mempool, int waited_state, int timeout ret = 1; else if(wait == ETIMEDOUT) // 110 260 ret = 0; +#else + int wait = WaitForSingleObject( mempool->wait_cond, (uint32_t)timeout_ms ); + pthread_mutex_lock(&mempool->wait_mutex); + + if ( wait == WAIT_OBJECT_0 ) return 1; + else if ( wait == WAIT_TIMEOUT || wait == WAIT_FAILED ) { + ret = 0; + } +#endif + pthread_mutex_unlock(&mempool->wait_mutex); return ret; } diff --git a/dap_chain_mempool.c b/dap_chain_mempool.c index 76c5530189c2dc36f5677a9e789cddc28e8ac604..2939b34b0e323e7aab3ea5cafb2b07a6d31bfc74 100755 --- a/dap_chain_mempool.c +++ b/dap_chain_mempool.c @@ -29,8 +29,19 @@ #include <assert.h> #include <memory.h> -//#include <dap_http_simple.h> -//#include <http_status_code.h> +#ifdef _WIN32 +#undef _WIN32_WINNT +#define _WIN32_WINNT 0x0600 +#include <winsock2.h> +#include <windows.h> +#include <mswsock.h> +#include <ws2tcpip.h> +#include <io.h> +#include <time.h> +#include <wepoll.h> +#include <pthread.h> +#endif + #include "dap_common.h" #include "dap_hash.h" #include "dap_http_client.h" @@ -309,7 +320,9 @@ int dap_chain_mempool_tx_create_massive( dap_chain_t * a_chain, dap_enc_key_t *a while(l_list_tmp) { list_used_item_t *item = l_list_tmp->data; char l_in_hash_str[70]; + dap_chain_hash_fast_to_str(&item->tx_hash_fast,l_in_hash_str,sizeof (l_in_hash_str) ); + if(dap_chain_datum_tx_add_in_item(&l_tx_new, &item->tx_hash_fast, (uint32_t) item->num_idx_out) == 1) { l_value_to_items += item->value; log_it(L_DEBUG,"Added input %s with %llu datoshi",l_in_hash_str, item->value); @@ -768,11 +781,15 @@ static char* calc_datum_hash(const char *datum_str, size_t datum_size) dap_hash( datum_str, datum_size, a_hash.raw, sizeof(a_hash.raw), DAP_HASH_TYPE_SLOW_0); size_t a_str_max = (sizeof(a_hash.raw) + 1) * 2 + 2; /* heading 0x */ char *a_str = DAP_NEW_Z_SIZE(char, a_str_max); - size_t hash_len = dap_chain_hash_fast_to_str(&a_hash, a_str, a_str_max); - if(!hash_len) { - DAP_DELETE(a_str); - return NULL; - } + +// size_t hash_len = dap_chain_hash_fast_to_str(&a_hash, a_str, a_str_max); + dap_chain_hash_fast_to_str(&a_hash, a_str, a_str_max); + +// if(!hash_len) { +// DAP_DELETE(a_str); +// return NULL; +// } + return a_str; }