ioevent refined for FreeBSD/MacOS
parent
03037c5d5f
commit
8b0c655039
|
|
@ -41,20 +41,31 @@ int ioevent_init(IOEventPoller *ioevent, const int size,
|
||||||
|
|
||||||
#if IOEVENT_USE_EPOLL
|
#if IOEVENT_USE_EPOLL
|
||||||
ioevent->poll_fd = epoll_create(ioevent->size);
|
ioevent->poll_fd = epoll_create(ioevent->size);
|
||||||
|
if (ioevent->poll_fd < 0) {
|
||||||
|
return errno != 0 ? errno : ENOMEM;
|
||||||
|
}
|
||||||
bytes = sizeof(struct epoll_event) * size;
|
bytes = sizeof(struct epoll_event) * size;
|
||||||
ioevent->events = (struct epoll_event *)malloc(bytes);
|
ioevent->events = (struct epoll_event *)malloc(bytes);
|
||||||
#elif IOEVENT_USE_KQUEUE
|
#elif IOEVENT_USE_KQUEUE
|
||||||
ioevent->poll_fd = kqueue();
|
ioevent->poll_fd = kqueue();
|
||||||
|
if (ioevent->poll_fd < 0) {
|
||||||
|
return errno != 0 ? errno : ENOMEM;
|
||||||
|
}
|
||||||
bytes = sizeof(struct kevent) * size;
|
bytes = sizeof(struct kevent) * size;
|
||||||
ioevent->events = (struct kevent *)malloc(bytes);
|
ioevent->events = (struct kevent *)malloc(bytes);
|
||||||
#elif IOEVENT_USE_PORT
|
#elif IOEVENT_USE_PORT
|
||||||
ioevent->poll_fd = port_create();
|
ioevent->poll_fd = port_create();
|
||||||
|
if (ioevent->poll_fd < 0) {
|
||||||
|
return errno != 0 ? errno : ENOMEM;
|
||||||
|
}
|
||||||
bytes = sizeof(port_event_t) * size;
|
bytes = sizeof(port_event_t) * size;
|
||||||
ioevent->events = (port_event_t *)malloc(bytes);
|
ioevent->events = (port_event_t *)malloc(bytes);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (ioevent->events == NULL) {
|
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);
|
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) {
|
if (e & IOEVENT_WRITE) {
|
||||||
EV_SET(&ev[n++], fd, EVFILT_WRITE, EV_ADD | ioevent->extra_events, 0, 0, data);
|
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);
|
return kevent(ioevent->poll_fd, ev, n, NULL, 0, NULL);
|
||||||
#elif IOEVENT_USE_PORT
|
#elif IOEVENT_USE_PORT
|
||||||
return port_associate(ioevent->poll_fd, PORT_SOURCE_FD, fd, e, data);
|
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);
|
return epoll_ctl(ioevent->poll_fd, EPOLL_CTL_MOD, fd, &ev);
|
||||||
#elif IOEVENT_USE_KQUEUE
|
#elif IOEVENT_USE_KQUEUE
|
||||||
struct kevent ev[2];
|
struct kevent ev[2];
|
||||||
|
int result;
|
||||||
int n = 0;
|
int n = 0;
|
||||||
|
|
||||||
if (e & IOEVENT_READ) {
|
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);
|
EV_SET(&ev[n++], fd, EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (n > 0) {
|
result = kevent(ioevent->poll_fd, ev, n, NULL, 0, NULL);
|
||||||
return 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);
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
return result;
|
||||||
#elif IOEVENT_USE_PORT
|
#elif IOEVENT_USE_PORT
|
||||||
return port_associate(ioevent->poll_fd, PORT_SOURCE_FD, fd, e, data);
|
return port_associate(ioevent->poll_fd, PORT_SOURCE_FD, fd, e, data);
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -142,9 +142,10 @@ static int fast_multi_sock_client_do_send(FastMultiSockClient *client,
|
||||||
if (entry->remain == 0) {
|
if (entry->remain == 0) {
|
||||||
entry->remain = client->header_length; //to recv pkg header
|
entry->remain = client->header_length; //to recv pkg header
|
||||||
entry->io_callback = fast_multi_sock_client_do_recv;
|
entry->io_callback = fast_multi_sock_client_do_recv;
|
||||||
if ((result=ioevent_modify(&client->ioevent,
|
if (ioevent_modify(&client->ioevent, entry->conn->sock,
|
||||||
entry->conn->sock, IOEVENT_READ, entry)) != 0)
|
IOEVENT_READ, entry) != 0)
|
||||||
{
|
{
|
||||||
|
result = errno != 0 ? errno : EACCES;
|
||||||
logError("file: "__FILE__", line: %d, "
|
logError("file: "__FILE__", line: %d, "
|
||||||
"ioevent_modify fail, errno: %d, error info: %s",
|
"ioevent_modify fail, errno: %d, error info: %s",
|
||||||
__LINE__, result, STRERROR(result));
|
__LINE__, result, STRERROR(result));
|
||||||
|
|
@ -161,9 +162,7 @@ static int fast_multi_sock_client_send_data(FastMultiSockClient *client,
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
int result;
|
int result;
|
||||||
int remain_timeout;
|
|
||||||
|
|
||||||
remain_timeout = 0;
|
|
||||||
for (i=0; i<client->entry_count; i++) {
|
for (i=0; i<client->entry_count; i++) {
|
||||||
client->entries[i].remain = send_buffer->length;
|
client->entries[i].remain = send_buffer->length;
|
||||||
client->entries[i].done = false;
|
client->entries[i].done = false;
|
||||||
|
|
@ -181,37 +180,11 @@ static int fast_multi_sock_client_send_data(FastMultiSockClient *client,
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
if (ioevent_attach(&client->ioevent,
|
||||||
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].conn->sock, IOEVENT_WRITE,
|
||||||
client->entries + i);
|
client->entries + i) != 0)
|
||||||
if (client->entries[i].error_no != 0) {
|
{
|
||||||
|
client->entries[i].error_no = errno != 0 ? errno : EACCES;
|
||||||
client->entries[i].done = true;
|
client->entries[i].done = true;
|
||||||
result = client->entries[i].error_no;
|
result = client->entries[i].error_no;
|
||||||
logError("file: "__FILE__", line: %d, "
|
logError("file: "__FILE__", line: %d, "
|
||||||
|
|
@ -223,7 +196,7 @@ static int fast_multi_sock_client_send_data(FastMultiSockClient *client,
|
||||||
client->pulling_count++;
|
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,
|
static inline void fast_multi_sock_client_finish(FastMultiSockClient *client,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue