Skip to content

Commit bed7856

Browse files
committed
Use epoll
1 parent bd824c1 commit bed7856

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

contrib/multimaster/sockhub/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
CC = gcc
2-
CFLAGS = -c -I. -Wall -O3 -g -fPIC -DUSE_EPOLL
2+
CFLAGS = -c -I. -Wall -O3 -g -fPIC
33
LD = $(CC)
44
LDFLAGS = -g
55
AR = ar

contrib/multimaster/sockhub/sockhub.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,9 @@ void ShubInitialize(Shub* shub, ShubParams* params)
263263
shub->output = -1;
264264
#ifdef USE_EPOLL
265265
shub->epollfd = epoll_create(MAX_EVENTS);
266+
if (shub->epollfd < 0) {
267+
shub->params->error_handler("Failed to create epoll", SHUB_FATAL_ERROR);
268+
}
266269
#else
267270
FD_ZERO(&shub->inset);
268271
shub->max_fd = 0;
@@ -299,14 +302,15 @@ void ShubLoop(Shub* shub)
299302
int i, rc;
300303
#ifdef USE_EPOLL
301304
struct epoll_event events[MAX_EVENTS];
302-
rc = epoll_wait(shub->epollfd, events, MAX_EVENTS, shub->params->delay);
305+
rc = epoll_wait(shub->epollfd, events, MAX_EVENTS, shub->in_buffer_used == 0 ? -1 : shub->params->delay);
303306
#else
304307
fd_set events;
305308
struct timeval tm;
306309
int max_fd = shub->max_fd;
307310

308311
tm.tv_sec = shub->params->delay/1000;
309312
tm.tv_usec = shub->params->delay % 1000 * 1000;
313+
events = shub->inset;
310314

311315
rc = select(max_fd+1, &events, NULL, NULL, shub->in_buffer_used == 0 ? NULL : &tm);
312316
#endif
@@ -320,8 +324,14 @@ void ShubLoop(Shub* shub)
320324
#ifdef USE_EPOLL
321325
int j;
322326
for (j = 0; j < rc; j++) {
323-
{
324-
i = events[j].data.fd;
327+
i = events[j].data.fd;
328+
fprintf(stderr, "events[j].events=%d, events[j].data.fd=%d\n", events[j].events, events[j].data.fd);
329+
if (events[j].events & EPOLLERR) {
330+
if (i != shub->input && i != shub->output) {
331+
notify_disconnect(shub, i);
332+
}
333+
close_socket(shub, i);
334+
} else if (events[j].events & EPOLLIN) {
325335
#else
326336
for (i = 0; i <= max_fd; i++) {
327337
if (FD_ISSET(i, &events)) {
@@ -331,7 +341,7 @@ void ShubLoop(Shub* shub)
331341
if (s < 0) {
332342
shub->params->error_handler("Failed to accept socket", SHUB_RECOVERABLE_ERROR);
333343
} else {
334-
ShubAddSocket(shub, i);
344+
ShubAddSocket(shub, s);
335345
}
336346
} else if (i == shub->output) { /* receive response from server */
337347
/* try to read as much as possible */

contrib/multimaster/sockhub/sockhub.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#define __SOCKHUB_H__
33

44

5+
#define USE_EPOLL 1
6+
57
#ifdef USE_EPOLL
68
#include <sys/epoll.h>
79
#define MAX_EVENTS 1024

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy