diff options
author | Igor Sysoev <igor@sysoev.ru> | 2017-02-22 15:09:59 +0300 |
---|---|---|
committer | Igor Sysoev <igor@sysoev.ru> | 2017-02-22 15:09:59 +0300 |
commit | 029942f4eb7196c2cff0d0e26bc6ff274138f7d8 (patch) | |
tree | f4686c4d7b9cd574fe94c6f4918479a580fecf75 /src/nxt_socket.c | |
parent | 059a8642898a6bd4b47d13a1c1d599cd44af7e1c (diff) | |
download | unit-029942f4eb7196c2cff0d0e26bc6ff274138f7d8.tar.gz unit-029942f4eb7196c2cff0d0e26bc6ff274138f7d8.tar.bz2 |
I/O operations refactoring.
Diffstat (limited to 'src/nxt_socket.c')
-rw-r--r-- | src/nxt_socket.c | 108 |
1 files changed, 45 insertions, 63 deletions
diff --git a/src/nxt_socket.c b/src/nxt_socket.c index 19ba21c0..2663b855 100644 --- a/src/nxt_socket.c +++ b/src/nxt_socket.c @@ -12,8 +12,8 @@ static const char *nxt_socket_sockopt_name(nxt_uint_t level, nxt_socket_t -nxt_socket_create(nxt_uint_t domain, nxt_uint_t type, nxt_uint_t protocol, - nxt_uint_t flags) +nxt_socket_create(nxt_task_t *task, nxt_uint_t domain, nxt_uint_t type, + nxt_uint_t protocol, nxt_uint_t flags) { nxt_socket_t s; @@ -28,18 +28,18 @@ nxt_socket_create(nxt_uint_t domain, nxt_uint_t type, nxt_uint_t protocol, s = socket(domain, type, protocol); if (nxt_slow_path(s == -1)) { - nxt_thread_log_alert("socket(%ui, 0x%uXi, %ui) failed %E", - domain, type, protocol, nxt_socket_errno); + nxt_log(task, NXT_LOG_CRIT, "socket(%ui, 0x%uXi, %ui) failed %E", + domain, type, protocol, nxt_socket_errno); return s; } - nxt_thread_log_debug("socket(): %d", s); + nxt_debug(task, "socket(): %d", s); #if !(NXT_HAVE_SOCK_NONBLOCK) if (flags & NXT_NONBLOCK) { - if (nxt_slow_path(nxt_socket_nonblocking(s) != NXT_OK)) { - nxt_socket_close(s); + if (nxt_slow_path(nxt_socket_nonblocking(task, s) != NXT_OK)) { + nxt_socket_close(task, s); return -1; } } @@ -51,19 +51,21 @@ nxt_socket_create(nxt_uint_t domain, nxt_uint_t type, nxt_uint_t protocol, void -nxt_socket_close(nxt_socket_t s) +nxt_socket_close(nxt_task_t *task, nxt_socket_t s) { if (nxt_fast_path(close(s) == 0)) { - nxt_thread_log_debug("socket close(%d)", s); + nxt_debug(task, "socket close(%d)", s); } else { - nxt_thread_log_alert("socket close(%d) failed %E", s, nxt_socket_errno); + nxt_log(task, NXT_LOG_CRIT, "socket close(%d) failed %E", + s, nxt_socket_errno); } } nxt_int_t -nxt_socket_getsockopt(nxt_socket_t s, nxt_uint_t level, nxt_uint_t sockopt) +nxt_socket_getsockopt(nxt_task_t *task, nxt_socket_t s, nxt_uint_t level, + nxt_uint_t sockopt) { int val; socklen_t len; @@ -71,38 +73,36 @@ nxt_socket_getsockopt(nxt_socket_t s, nxt_uint_t level, nxt_uint_t sockopt) len = sizeof(val); if (nxt_fast_path(getsockopt(s, level, sockopt, &val, &len) == 0)) { - nxt_thread_log_debug("getsockopt(%d, %ui, %s): %d", - s, level, - nxt_socket_sockopt_name(level, sockopt), val); + nxt_debug(task, "getsockopt(%d, %ui, %s): %d", + s, level, nxt_socket_sockopt_name(level, sockopt), val); return val; } - nxt_thread_log_error(NXT_LOG_CRIT, "getsockopt(%d, %ui, %s) failed %E", - s, level, nxt_socket_sockopt_name(level, sockopt), - val, nxt_socket_errno); + nxt_log(task, NXT_LOG_CRIT, "getsockopt(%d, %ui, %s) failed %E", + s, level, nxt_socket_sockopt_name(level, sockopt), + val, nxt_socket_errno); return -1; } nxt_int_t -nxt_socket_setsockopt(nxt_socket_t s, nxt_uint_t level, nxt_uint_t sockopt, - int val) +nxt_socket_setsockopt(nxt_task_t *task, nxt_socket_t s, nxt_uint_t level, + nxt_uint_t sockopt, int val) { socklen_t len; len = sizeof(val); if (nxt_fast_path(setsockopt(s, level, sockopt, &val, len) == 0)) { - nxt_thread_log_debug("setsockopt(%d, %ui, %s): %d", - s, level, - nxt_socket_sockopt_name(level, sockopt), val); + nxt_debug(task, "setsockopt(%d, %ui, %s): %d", + s, level, nxt_socket_sockopt_name(level, sockopt), val); return NXT_OK; } - nxt_thread_log_error(NXT_LOG_CRIT, "setsockopt(%d, %ui, %s, %d) failed %E", - s, level, nxt_socket_sockopt_name(level, sockopt), - val, nxt_socket_errno); + nxt_log(task, NXT_LOG_CRIT, "setsockopt(%d, %ui, %s, %d) failed %E", + s, level, nxt_socket_sockopt_name(level, sockopt), + val, nxt_socket_errno); return NXT_ERROR; } @@ -123,7 +123,7 @@ nxt_socket_sockopt_name(nxt_uint_t level, nxt_uint_t sockopt) return "SO_RCVBUF"; case SO_REUSEADDR: - return "SO_TYPE"; + return "SO_REUSEADDR"; case SO_TYPE: return "SO_TYPE"; @@ -164,13 +164,14 @@ nxt_socket_sockopt_name(nxt_uint_t level, nxt_uint_t sockopt) nxt_int_t -nxt_socket_bind(nxt_socket_t s, nxt_sockaddr_t *sa, nxt_bool_t test) +nxt_socket_bind(nxt_task_t *task, nxt_socket_t s, nxt_sockaddr_t *sa, + nxt_bool_t test) { nxt_err_t err; - nxt_thread_log_debug("bind(%d, %*s)", s, sa->text_len, sa->text); + nxt_debug(task, "bind(%d, %*s)", s, sa->length, nxt_sockaddr_start(sa)); - if (nxt_fast_path(bind(s, &sa->u.sockaddr, nxt_socklen(sa)) == 0)) { + if (nxt_fast_path(bind(s, &sa->u.sockaddr, sa->socklen) == 0)) { return NXT_OK; } @@ -180,23 +181,23 @@ nxt_socket_bind(nxt_socket_t s, nxt_sockaddr_t *sa, nxt_bool_t test) return NXT_DECLINED; } - nxt_thread_log_error(NXT_LOG_CRIT, "bind(%d, %*s) failed %E", - s, sa->text_len, sa->text, err); + nxt_log(task, NXT_LOG_CRIT, "bind(%d, %*s) failed %E", + s, sa->length, nxt_sockaddr_start(sa), err); return NXT_ERROR; } nxt_int_t -nxt_socket_connect(nxt_socket_t s, nxt_sockaddr_t *sa) +nxt_socket_connect(nxt_task_t *task, nxt_socket_t s, nxt_sockaddr_t *sa) { nxt_err_t err; nxt_int_t ret; nxt_uint_t level; - nxt_thread_log_debug("connect(%d, %*s)", s, sa->text_len, sa->text); + nxt_debug(task, "connect(%d, %*s)", s, sa->length, nxt_sockaddr_start(sa)); - if (connect(s, &sa->u.sockaddr, nxt_socklen(sa)) == 0) { + if (connect(s, &sa->u.sockaddr, sa->socklen) == 0) { return NXT_OK; } @@ -205,7 +206,8 @@ nxt_socket_connect(nxt_socket_t s, nxt_sockaddr_t *sa) switch (err) { case NXT_EINPROGRESS: - nxt_thread_log_debug("connect(%d) in progress", s); + nxt_debug(task, "connect(%d, %*s) in progress", + s, sa->length, nxt_sockaddr_start(sa)); return NXT_AGAIN; case NXT_ECONNREFUSED: @@ -234,21 +236,21 @@ nxt_socket_connect(nxt_socket_t s, nxt_sockaddr_t *sa) ret = NXT_ERROR; } - nxt_thread_log_error(level, "connect(%d, %*s) failed %E", - s, sa->text_len, sa->text, err); + nxt_log(task, level, "connect(%d, %*s) failed %E", + s, sa->length, nxt_sockaddr_start(sa), err); return ret; } void -nxt_socket_shutdown(nxt_socket_t s, nxt_uint_t how) +nxt_socket_shutdown(nxt_task_t *task, nxt_socket_t s, nxt_uint_t how) { nxt_err_t err; nxt_uint_t level; if (nxt_fast_path(shutdown(s, how) == 0)) { - nxt_thread_log_debug("shutdown(%d, %ui)", s, how); + nxt_debug(task, "shutdown(%d, %ui)", s, how); return; } @@ -272,46 +274,26 @@ nxt_socket_shutdown(nxt_socket_t s, nxt_uint_t how) level = NXT_LOG_CRIT; } - nxt_thread_log_error(level, "shutdown(%d, %ui) failed %E", s, how, err); + nxt_log(task, level, "shutdown(%d, %ui) failed %E", s, how, err); } nxt_uint_t -nxt_socket_error_level(nxt_err_t err, nxt_socket_error_level_t level) +nxt_socket_error_level(nxt_err_t err) { switch (err) { - case NXT_ECONNRESET: - - if ((level & NXT_SOCKET_ECONNRESET_IGNORE) != 0) { - return NXT_LOG_DEBUG; - } - - return NXT_LOG_ERR; - - case NXT_EINVAL: - - if ((level & NXT_SOCKET_EINVAL_IGNORE) != 0) { - return NXT_LOG_DEBUG; - } - - return NXT_LOG_ALERT; - case NXT_EPIPE: + case NXT_ECONNRESET: case NXT_ENOTCONN: case NXT_ETIMEDOUT: case NXT_ENETDOWN: case NXT_ENETUNREACH: case NXT_EHOSTDOWN: case NXT_EHOSTUNREACH: - - if ((level & NXT_SOCKET_ERROR_IGNORE) != 0) { - return NXT_LOG_INFO; - } - return NXT_LOG_ERR; default: - return NXT_LOG_ALERT; + return NXT_LOG_CRIT; } } |