From 902f62eca99bd936afd06dc4109c3918d100d9a4 Mon Sep 17 00:00:00 2001 From: Uros Majstorovic Date: Fri, 4 Aug 2017 16:18:31 +0200 Subject: fixed threads; fixed conn_close --- code/core/core.c | 19 +++++++------------ code/core/core.h | 2 -- code/core/timer.c | 11 ++++++++--- 3 files changed, 15 insertions(+), 17 deletions(-) (limited to 'code') diff --git a/code/core/core.c b/code/core/core.c index 7a24d96..78387b3 100644 --- a/code/core/core.c +++ b/code/core/core.c @@ -65,8 +65,8 @@ static int ctable_create(ECPSockCTable *conn, ECPContext *ctx) { #ifdef ECP_WITH_PTHREAD rv = pthread_mutex_init(&conn->mutex, NULL); - if (rv && ctx->ht.init) ctx->ht.destroy(conn->htable); #endif + if (rv && ctx->ht.init) ctx->ht.destroy(conn->htable); return rv; } @@ -189,6 +189,7 @@ int ecp_sock_create(ECPSocket *sock, ECPContext *ctx, ECPDHKey *key) { rv = ecp_timer_create(&sock->timer); if (rv) { ctable_destroy(&sock->conn, sock->ctx); + return rv; } #ifdef ECP_WITH_PTHREAD @@ -196,6 +197,7 @@ int ecp_sock_create(ECPSocket *sock, ECPContext *ctx, ECPDHKey *key) { if (rv) { ecp_timer_destroy(&sock->timer); ctable_destroy(&sock->conn, sock->ctx); + return rv; } #endif @@ -404,7 +406,7 @@ static int conn_shsec_set(ECPConnection *conn, unsigned char s_idx, unsigned cha } int ecp_conn_create(ECPConnection *conn, ECPSocket *sock, unsigned char ctype) { - int i; + int i, rv = ECP_OK; if (conn == NULL) return ECP_ERR; if (sock == NULL) return ECP_ERR; @@ -427,7 +429,7 @@ int ecp_conn_create(ECPConnection *conn, ECPSocket *sock, unsigned char ctype) { conn->sock = sock; #ifdef ECP_WITH_PTHREAD - int rv = pthread_mutex_init(&conn->mutex, NULL); + rv = pthread_mutex_init(&conn->mutex, NULL); if (rv) return ECP_ERR; #endif @@ -528,11 +530,6 @@ int ecp_conn_close(ECPConnection *conn, unsigned int timeout) { ECPSocket *sock = conn->sock; int refcount = 0; - if (!conn->out) { - ecp_conn_destroy_t *handler = conn->sock->ctx->handler[conn->type] ? conn->sock->ctx->handler[conn->type]->conn_destroy : NULL; - if (handler) handler(conn); - if (sock->conn_destroy) sock->conn_destroy(conn); - } ecp_conn_unregister(conn); ecp_timer_remove(conn); @@ -552,6 +549,8 @@ int ecp_conn_close(ECPConnection *conn, unsigned int timeout) { #endif if (!conn->out) { + ecp_conn_destroy_t *handler = conn->sock->ctx->handler[conn->type] ? conn->sock->ctx->handler[conn->type]->conn_destroy : NULL; + if (handler) handler(conn); if (conn->proxy) { #ifdef ECP_WITH_PTHREAD pthread_mutex_lock(&conn->proxy->mutex); @@ -612,7 +611,6 @@ int ecp_conn_handle_new(ECPSocket *sock, ECPConnection **_conn, ECPConnection *p } conn->refcount = 1; - conn->type = ctype; conn->proxy = proxy; handle_create = conn->sock->ctx->handler[conn->type] ? conn->sock->ctx->handler[conn->type]->conn_create : NULL; handle_destroy = conn->sock->ctx->handler[conn->type] ? conn->sock->ctx->handler[conn->type]->conn_destroy : NULL; @@ -620,7 +618,6 @@ int ecp_conn_handle_new(ECPSocket *sock, ECPConnection **_conn, ECPConnection *p rv = conn_dhkey_new_pub_local(conn, s_idx); if (!rv) rv = conn_dhkey_new_pub_remote(conn, c_idx, c_public); if (!rv) rv = conn_shsec_set(conn, s_idx, c_idx, shsec); - if (!rv && sock->conn_create) rv = sock->conn_create(conn, payload+1, payload_size-1); if (rv) { ecp_conn_destroy(conn); if (sock->ctx->conn_free) sock->ctx->conn_free(conn); @@ -628,7 +625,6 @@ int ecp_conn_handle_new(ECPSocket *sock, ECPConnection **_conn, ECPConnection *p } if (handle_create) rv = handle_create(conn, payload+1, payload_size-1); if (rv) { - if (sock->conn_destroy) sock->conn_destroy(conn); ecp_conn_destroy(conn); if (sock->ctx->conn_free) sock->ctx->conn_free(conn); return rv; @@ -637,7 +633,6 @@ int ecp_conn_handle_new(ECPSocket *sock, ECPConnection **_conn, ECPConnection *p rv = ecp_conn_register(conn); if (rv) { if (handle_destroy) handle_destroy(conn); - if (sock->conn_destroy) sock->conn_destroy(conn); ecp_conn_destroy(conn); if (sock->ctx->conn_free) sock->ctx->conn_free(conn); return rv; diff --git a/code/core/core.h b/code/core/core.h index 2dd7951..cf4009f 100644 --- a/code/core/core.h +++ b/code/core/core.h @@ -230,8 +230,6 @@ typedef struct ECPSocket { ECPSockCTable conn; ECPTimer timer; ecp_conn_handler_new_t *conn_new; - ecp_conn_create_t *conn_create; - ecp_conn_destroy_t *conn_destroy; #ifdef ECP_WITH_PTHREAD pthread_t rcvr_thd; pthread_mutex_t mutex; diff --git a/code/core/timer.c b/code/core/timer.c index 7767e67..8368742 100644 --- a/code/core/timer.c +++ b/code/core/timer.c @@ -3,10 +3,11 @@ #include int ecp_timer_create(ECPTimer *timer) { + int rv = ECP_OK; timer->head = -1; #ifdef ECP_WITH_PTHREAD - int rv = pthread_mutex_init(&timer->mutex, NULL); + rv = pthread_mutex_init(&timer->mutex, NULL); if (rv) return ECP_ERR; #endif @@ -118,7 +119,6 @@ void ecp_timer_remove(ECPConnection *conn) { #ifdef ECP_WITH_PTHREAD pthread_mutex_lock(&timer->mutex); - pthread_mutex_lock(&conn->mutex); #endif for (i=timer->head; i>=0; i--) { @@ -130,13 +130,18 @@ void ecp_timer_remove(ECPConnection *conn) { } else { memset(timer->item+i, 0, sizeof(ECPTimerItem)); } +#ifdef ECP_WITH_PTHREAD + pthread_mutex_lock(&conn->mutex); +#endif conn->refcount--; +#ifdef ECP_WITH_PTHREAD + pthread_mutex_unlock(&conn->mutex); +#endif timer->head--; } } #ifdef ECP_WITH_PTHREAD - pthread_mutex_unlock(&conn->mutex); pthread_mutex_unlock(&timer->mutex); #endif -- cgit v1.2.3