37 #line 38 "Listener.cc"
39 #define EPOLLEVENTS 1024
42 : myIdentity(*myIdentityArg)
51 struct epoll_event ev;
52 ev.events = EPOLLIN | EPOLLHUP | EPOLLET;
53 ev.data.fd = listenersockfd;
57 fprintf(
logfile,
"%s %i epoll_ctl errno %i\n", __FILE__, __LINE__, errno);
60 ev.data.fd = pglistenersockfd;
64 fprintf(
logfile,
"%s %i epoll_ctl errno %i\n", __FILE__, __LINE__, errno);
67 struct sockaddr_in their_addr;
69 socklen_t sin_size =
sizeof(their_addr);
73 struct epoll_event events[EPOLLEVENTS];
85 for (
int n=0; n < eventcount; n++)
87 int fd = events[n].data.fd;
88 int event = events[n].events;
90 if (fd==listenersockfd || fd==pglistenersockfd)
92 if (fd==listenersockfd)
96 int newfd = accept(fd, (
struct sockaddr *)&their_addr,
101 printf(
"%s %i accept errno %i\n", __FILE__, __LINE__,
107 fprintf(
logfile,
"%s %i fd %i > %i\n", __FILE__,
114 fcntl(newfd, F_SETFL, O_NONBLOCK);
116 setsockopt(newfd, SOL_SOCKET, SO_KEEPALIVE, &optval,
128 else if (fd==pglistenersockfd)
133 int newfd = accept(fd, (
struct sockaddr *)&their_addr,
138 if (errno != EAGAIN && errno != EWOULDBLOCK)
140 printf(
"%s %i accept errno %i\n", __FILE__,
150 fprintf(
logfile,
"%s %i fd %i > %i\n", __FILE__,
156 fcntl(newfd, F_SETFL, O_NONBLOCK);
158 setsockopt(newfd, SOL_SOCKET, SO_KEEPALIVE, &optval,
183 fprintf(
logfile,
"%s %i event %i on spurious sockfd %i\n", __FILE__,
184 __LINE__, event, fd);
213 struct addrinfo hints = {};
214 hints.ai_family = AF_INET;
215 hints.ai_socktype = SOCK_STREAM;
216 hints.ai_protocol = IPPROTO_TCP;
217 hints.ai_flags = AI_PASSIVE;
218 struct addrinfo *servinfo;
220 char *nodeptr = NULL;
224 nodeptr = (
char *)node.c_str();
227 int rv = getaddrinfo((
const char *)nodeptr, service.c_str(), &hints,
232 fprintf(
logfile,
"%s %i getaddrinfo %s %i\n", __FILE__, __LINE__,
233 gai_strerror(rv), rv);
239 struct addrinfo *p=NULL;
241 for (p = servinfo; p != NULL; p = p->ai_next)
243 sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
247 fprintf(
logfile,
"%s %i socket errno %i\n", __FILE__, __LINE__,
252 fcntl(sockfd, F_SETFL, O_NONBLOCK);
254 if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes,
sizeof(yes))==-1)
256 fprintf(
logfile,
"%s %i setsockopt errno %i\n", __FILE__, __LINE__,
261 if (bind(sockfd, p->ai_addr, p->ai_addrlen)==-1)
264 fprintf(
logfile,
"%s %i bind errno %i\n", __FILE__, __LINE__, errno);
271 freeaddrinfo(servinfo);
275 fprintf(
logfile,
"%s %i listener: failed to bind\n", __FILE__, __LINE__);
279 if (listen(sockfd, 1000) == -1)
281 fprintf(
logfile,
"%s %i listen errno %i\n", __FILE__, __LINE__, errno);