From 8b0c655039930e7302013aa56b35f950f727a7e3 Mon Sep 17 00:00:00 2001 From: yuqing Date: Mon, 21 May 2018 21:36:16 +0800 Subject: [PATCH] ioevent refined for FreeBSD/MacOS --- src/ioevent.c | 29 +++++++++++++++++++------ src/multi_socket_client.c | 45 ++++++++------------------------------- 2 files changed, 32 insertions(+), 42 deletions(-) diff --git a/src/ioevent.c b/src/ioevent.c index f3443d5..43551df 100644 --- a/src/ioevent.c +++ b/src/ioevent.c @@ -41,20 +41,31 @@ int ioevent_init(IOEventPoller *ioevent, const int size, #if IOEVENT_USE_EPOLL ioevent->poll_fd = epoll_create(ioevent->size); + if (ioevent->poll_fd < 0) { + return errno != 0 ? errno : ENOMEM; + } bytes = sizeof(struct epoll_event) * size; ioevent->events = (struct epoll_event *)malloc(bytes); #elif IOEVENT_USE_KQUEUE ioevent->poll_fd = kqueue(); + if (ioevent->poll_fd < 0) { + return errno != 0 ? errno : ENOMEM; + } bytes = sizeof(struct kevent) * size; ioevent->events = (struct kevent *)malloc(bytes); #elif IOEVENT_USE_PORT ioevent->poll_fd = port_create(); + if (ioevent->poll_fd < 0) { + return errno != 0 ? errno : ENOMEM; + } bytes = sizeof(port_event_t) * size; ioevent->events = (port_event_t *)malloc(bytes); #endif if (ioevent->events == NULL) { - return errno != 0 ? errno : ENOMEM; + close(ioevent->poll_fd); + ioevent->poll_fd = -1; + return ENOMEM; } ioevent_set_timeout(ioevent, timeout_ms); @@ -92,6 +103,9 @@ int ioevent_attach(IOEventPoller *ioevent, const int fd, const int e, if (e & IOEVENT_WRITE) { EV_SET(&ev[n++], fd, EVFILT_WRITE, EV_ADD | ioevent->extra_events, 0, 0, data); } + if (n == 0) { + return ENOENT; + } return kevent(ioevent->poll_fd, ev, n, NULL, 0, NULL); #elif IOEVENT_USE_PORT return port_associate(ioevent->poll_fd, PORT_SOURCE_FD, fd, e, data); @@ -109,6 +123,7 @@ int ioevent_modify(IOEventPoller *ioevent, const int fd, const int e, return epoll_ctl(ioevent->poll_fd, EPOLL_CTL_MOD, fd, &ev); #elif IOEVENT_USE_KQUEUE struct kevent ev[2]; + int result; int n = 0; if (e & IOEVENT_READ) { @@ -125,12 +140,14 @@ int ioevent_modify(IOEventPoller *ioevent, const int fd, const int e, EV_SET(&ev[n++], fd, EVFILT_WRITE, EV_DELETE, 0, 0, NULL); } - if (n > 0) { - return kevent(ioevent->poll_fd, ev, n, NULL, 0, NULL); - } - else { - return 0; + result = kevent(ioevent->poll_fd, ev, n, NULL, 0, NULL); + if (result == -1) { + result = ioevent_detach(ioevent, fd); + if (e & (IOEVENT_READ | IOEVENT_WRITE)) { + result = ioevent_attach(ioevent, fd, e, data); + } } + return result; #elif IOEVENT_USE_PORT return port_associate(ioevent->poll_fd, PORT_SOURCE_FD, fd, e, data); #endif diff --git a/src/multi_socket_client.c b/src/multi_socket_client.c index 48f1ac9..d234ba5 100644 --- a/src/multi_socket_client.c +++ b/src/multi_socket_client.c @@ -142,9 +142,10 @@ static int fast_multi_sock_client_do_send(FastMultiSockClient *client, if (entry->remain == 0) { entry->remain = client->header_length; //to recv pkg header entry->io_callback = fast_multi_sock_client_do_recv; - if ((result=ioevent_modify(&client->ioevent, - entry->conn->sock, IOEVENT_READ, entry)) != 0) + if (ioevent_modify(&client->ioevent, entry->conn->sock, + IOEVENT_READ, entry) != 0) { + result = errno != 0 ? errno : EACCES; logError("file: "__FILE__", line: %d, " "ioevent_modify fail, errno: %d, error info: %s", __LINE__, result, STRERROR(result)); @@ -161,9 +162,7 @@ static int fast_multi_sock_client_send_data(FastMultiSockClient *client, { int i; int result; - int remain_timeout; - remain_timeout = 0; for (i=0; ientry_count; i++) { client->entries[i].remain = send_buffer->length; client->entries[i].done = false; @@ -181,37 +180,11 @@ static int fast_multi_sock_client_send_data(FastMultiSockClient *client, continue; } - /* - remain_timeout = client->deadline_time - get_current_time(); - if (remain_timeout <= 0) { - client->entries[i].error_no = ETIMEDOUT; - client->entries[i].done = true; - logError("file: "__FILE__", line: %d, " - "tcpsenddata to %s:%d timedout", - __LINE__, client->entries[i].conn->ip_addr, - client->entries[i].conn->port); - continue; - } - - client->entries[i].error_no = tcpsenddata(client->entries[i].conn->sock, - buffer->data, buffer->length, client->timeout); - if (client->entries[i].error_no != 0) { - client->entries[i].done = true; - result = client->entries[i].error_no; - logError("file: "__FILE__", line: %d, " - "tcpsenddata to %s:%d fail, " - "errno: %d, error info: %s", - __LINE__, client->entries[i].conn->ip_addr, - client->entries[i].conn->port, - result, STRERROR(result)); - continue; - } - */ - - client->entries[i].error_no = ioevent_attach(&client->ioevent, - client->entries[i].conn->sock, IOEVENT_WRITE, - client->entries + i); - if (client->entries[i].error_no != 0) { + if (ioevent_attach(&client->ioevent, + client->entries[i].conn->sock, IOEVENT_WRITE, + client->entries + i) != 0) + { + client->entries[i].error_no = errno != 0 ? errno : EACCES; client->entries[i].done = true; result = client->entries[i].error_no; logError("file: "__FILE__", line: %d, " @@ -223,7 +196,7 @@ static int fast_multi_sock_client_send_data(FastMultiSockClient *client, client->pulling_count++; } - return client->pulling_count > 0 ? 0 : (remain_timeout > 0 ? ENOENT : ETIMEDOUT); + return client->pulling_count > 0 ? 0 : ENOENT; } static inline void fast_multi_sock_client_finish(FastMultiSockClient *client,