diff --git a/HISTORY b/HISTORY index 4a6d29c..148e8fa 100644 --- a/HISTORY +++ b/HISTORY @@ -1,5 +1,5 @@ -Version 1.44 2020-03-23 +Version 1.44 2020-03-26 * add test file src/tests/test_pthread_lock.c * add uniq_skiplist.[hc] * add function split_string_ex @@ -22,6 +22,7 @@ Version 1.44 2020-03-23 * pthread_func.[hc] add functions: create_work_threads_ex and fc_create_thread * sched_add_entries use temp ScheduleArray for rare case * add function common_blocked_queue_return_nodes + * add functions getIpAndPort and getPeerIpAndPort Version 1.43 2019-12-25 * replace function call system to getExecResult, diff --git a/src/fast_task_queue.c b/src/fast_task_queue.c index 63940ee..3a68cda 100644 --- a/src/fast_task_queue.c +++ b/src/fast_task_queue.c @@ -538,6 +538,17 @@ int free_queue_realloc_buffer(struct fast_task_info *pTask, return task_queue_realloc_buffer(&g_free_queue, pTask, expect_size); } +int free_queue_set_max_buffer_size(struct fast_task_info *pTask) +{ + return task_queue_set_buffer_size(&g_free_queue, pTask, + g_free_queue.max_buff_size); +} + +int free_queue_realloc_max_buffer(struct fast_task_info *pTask) +{ + return task_queue_realloc_buffer(&g_free_queue, pTask, + g_free_queue.max_buff_size); +} int task_queue_push(struct fast_task_queue *pQueue, \ struct fast_task_info *pTask) { @@ -716,4 +727,3 @@ int task_queue_realloc_buffer(struct fast_task_queue *pQueue, return _realloc_buffer(pTask, new_size, true); } - diff --git a/src/fast_task_queue.h b/src/fast_task_queue.h index 01508fb..550fd77 100644 --- a/src/fast_task_queue.h +++ b/src/fast_task_queue.h @@ -71,6 +71,7 @@ struct fast_task_info int size; //alloc size int length; //data length int offset; //current offset + uint16_t port; //client port char nio_stage; //stage for network IO bool canceled; //if task canceled int64_t req_count; //request count @@ -116,6 +117,10 @@ int free_queue_set_buffer_size(struct fast_task_info *pTask, int free_queue_realloc_buffer(struct fast_task_info *pTask, const int expect_size); +int free_queue_set_max_buffer_size(struct fast_task_info *pTask); + +int free_queue_realloc_max_buffer(struct fast_task_info *pTask); + int task_queue_init(struct fast_task_queue *pQueue); int task_queue_push(struct fast_task_queue *pQueue, \ struct fast_task_info *pTask); diff --git a/src/ioevent_loop.c b/src/ioevent_loop.c index 7293031..f8e9591 100644 --- a/src/ioevent_loop.c +++ b/src/ioevent_loop.c @@ -152,15 +152,23 @@ int ioevent_loop(struct nio_thread_data *pThreadData, } } - if (pThreadData->notify.enabled) { + if (pThreadData->notify.enabled) + { int64_t n; - if ((n=__sync_fetch_and_add(&pThreadData->notify.counter, 0)) != 0) { - logInfo("file: "__FILE__", line: %d, " - "n ==== %"PRId64, __LINE__, n); + if ((n=__sync_fetch_and_add(&pThreadData->notify.counter, 0)) != 0) + { __sync_fetch_and_sub(&pThreadData->notify.counter, n); + /* + logInfo("file: "__FILE__", line: %d, " + "n ==== %"PRId64", now: %"PRId64, + __LINE__, n, __sync_fetch_and_add( + &pThreadData->notify.counter, 0)); + */ } } - if (pThreadData->thread_loop_callback != NULL) { + + if (pThreadData->thread_loop_callback != NULL) + { pThreadData->thread_loop_callback(pThreadData); } } diff --git a/src/sockopt.c b/src/sockopt.c index 00fe17a..4a1b762 100644 --- a/src/sockopt.c +++ b/src/sockopt.c @@ -768,6 +768,38 @@ in_addr_t getIpaddr(getnamefunc getname, int sock, \ return ((struct sockaddr_in *)&addr)->sin_addr.s_addr; //DO NOT support IPv6 } +int getIpAndPort(getnamefunc getname, int sock, + char *buff, const int bufferSize, int *port) +{ + struct sockaddr addr; + socklen_t addrlen; + + memset(&addr, 0, sizeof(addr)); + addrlen = sizeof(addr); + + if (getname(sock, &addr, &addrlen) != 0) + { + *buff = '\0'; + return errno != 0 ? errno : EINVAL; + } + + if (addrlen > 0) + { + fc_inet_ntop(&addr, buff, bufferSize); + } + else + { + *buff = '\0'; + } + + if (addr.sa_family == AF_INET) { + *port = ntohs(((struct sockaddr_in *)&addr)->sin_port); + } else { + *port = ntohs(((struct sockaddr_in6 *)&addr)->sin6_port); + } + return 0; +} + char *getHostnameByIp(const char *szIpAddr, char *buff, const int bufferSize) { struct hostent *ent; diff --git a/src/sockopt.h b/src/sockopt.h index 81e9bce..c293450 100644 --- a/src/sockopt.h +++ b/src/sockopt.h @@ -92,6 +92,12 @@ typedef int (*tcprecvdata_exfunc)(int sock, void *data, const int size, \ #define getPeerIpaddr(sock, buff, bufferSize) \ getIpaddr(getpeername, sock, buff, bufferSize) +#define getSockIpAddPort(sock, buff, bufferSize, port) \ + getIpAndPort(getsockname, sock, buff, bufferSize, port) + +#define getPeerIpAddPort(sock, buff, bufferSize, port) \ + getIpAndPort(getpeername, sock, buff, bufferSize, port) + /** get a line from socket * parameters: * sock: the socket @@ -260,6 +266,18 @@ int tcpprintkeepalive(int fd); in_addr_t getIpaddr(getnamefunc getname, int sock, \ char *buff, const int bufferSize); +/** get ip address + * parameters: + * getname: the function name, should be getpeername or getsockname + * sock: the socket + * buff: buffer to store the ip address + * bufferSize: the buffer size (max bytes) + * port: return the port + * return: error no, 0 success, != 0 fail +*/ +int getIpAndPort(getnamefunc getname, int sock, + char *buff, const int bufferSize, int *port); + /** get hostname by it's ip address * parameters: * szIpAddr: the ip address diff --git a/src/tests/test_pipe.c b/src/tests/test_pipe.c index 7149d51..dae2b9a 100644 --- a/src/tests/test_pipe.c +++ b/src/tests/test_pipe.c @@ -52,7 +52,8 @@ int main(int argc, char *argv[]) return result; } } - printf("child done, time used: %"PRId64" ms\n", get_current_time_ms() - start_time); + printf("child done, LOOP: %d, time used: %"PRId64" ms\n", + LOOP, get_current_time_ms() - start_time); } else { printf("the child proccess: %d\n", pid); start_time = get_current_time_ms(); @@ -68,8 +69,9 @@ int main(int argc, char *argv[]) } } - printf("parent done, time used: %"PRId64" ms\n", get_current_time_ms() - start_time); - sleep(5); + printf("parent done, LOOP: %d, time used: %"PRId64" ms\n", + LOOP, get_current_time_ms() - start_time); + sleep(1); } return 0; diff --git a/src/tests/test_pthread_lock.c b/src/tests/test_pthread_lock.c index 412f09d..eb41f4b 100644 --- a/src/tests/test_pthread_lock.c +++ b/src/tests/test_pthread_lock.c @@ -21,6 +21,7 @@ int main(int argc, char *argv[]) int k; int64_t sum; int64_t start_time; + char time_buff[32]; pthread_mutex_t lock; log_init(); @@ -41,7 +42,8 @@ int main(int argc, char *argv[]) __sync_add_and_fetch(&sum, k); } - printf("atom add, sum: %"PRId64", time used: %"PRId64" ms\n", + printf("atom add, LOOP_COUNT: %s, sum: %"PRId64", time used: " + "%"PRId64" ms\n", int_to_comma_str(LOOP_COUNT, time_buff), sum, get_current_time_ms() - start_time); start_time = get_current_time_ms(); @@ -52,7 +54,8 @@ int main(int argc, char *argv[]) pthread_mutex_unlock(&lock); } - printf("locked add, sum: %"PRId64", time used: %"PRId64" ms\n", + printf("locked add, LOOP_COUNT: %s, sum: %"PRId64", time used: " + "%"PRId64" ms\n", int_to_comma_str(LOOP_COUNT, time_buff), sum, get_current_time_ms() - start_time); return 0; }