diff --git a/src/ioevent_loop.c b/src/ioevent_loop.c index 83fe22d..a6de380 100644 --- a/src/ioevent_loop.c +++ b/src/ioevent_loop.c @@ -201,14 +201,13 @@ int ioevent_set(struct fast_task_info *task, struct nio_thread_data *pThread, task->thread_data = pThread; task->event.fd = sock; task->event.callback = callback; - if (ioevent_attach(&pThread->ev_puller, - sock, event, task) < 0) + if (ioevent_attach(&pThread->ev_puller, sock, event, task) < 0) { result = errno != 0 ? errno : ENOENT; - logError("file: "__FILE__", line: %d, " \ - "ioevent_attach fail, " \ - "errno: %d, error info: %s", \ - __LINE__, result, STRERROR(result)); + logError("file: "__FILE__", line: %d, " + "ioevent_attach fail, fd: %d, " + "errno: %d, error info: %s", + __LINE__, sock, result, STRERROR(result)); return result; } @@ -216,3 +215,19 @@ int ioevent_set(struct fast_task_info *task, struct nio_thread_data *pThread, fast_timer_add(&pThread->timer, &task->event.timer); return 0; } + +int ioevent_reset(struct fast_task_info *task, int new_fd, short event) +{ + if (task->event.fd == new_fd) + { + return 0; + } + + if (task->event.fd >= 0) + { + ioevent_detach(&task->thread_data->ev_puller, task->event.fd); + } + + task->event.fd = new_fd; + return ioevent_attach(&task->thread_data->ev_puller, new_fd, event, task); +} diff --git a/src/ioevent_loop.h b/src/ioevent_loop.h index 66f8ab9..c176126 100644 --- a/src/ioevent_loop.h +++ b/src/ioevent_loop.h @@ -32,6 +32,8 @@ int ioevent_remove(IOEventPoller *ioevent, void *data); int ioevent_set(struct fast_task_info *pTask, struct nio_thread_data *pThread, int sock, short event, IOEventCallback callback, const int timeout); +int ioevent_reset(struct fast_task_info *task, int new_fd, short event); + static inline bool ioevent_is_canceled(struct fast_task_info *task) { return __sync_fetch_and_add(&task->canceled, 0) != 0;