non bloquante problème recv à douille lors de son utilisation avec epoll
-
01-10-2019 - |
Question
J'ai un problème: parfois (pas régulièrement) retourne recv -1
et errno == EAGAIN
tout en utilisant epoll
en mode edge-triggered
. morceau de code:
server_sock = startup(&port);
if ( (epollfd = epoll_create(4096)) < 0) {
perror("epoll_create error");
exit(EXIT_FAILURE);
}
ev.events = EPOLLIN | EPOLLET;
ev.data.fd = server_sock;
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, server_sock, &ev) == -1) {
perror("epoll_ctl: server_sock");
exit(EXIT_FAILURE);
}
while (1) {
int nfds = epoll_wait(epollfd, events, 4096, -1);
if (nfds == -1) {
perror("epoll_wait");
exit(EXIT_FAILURE);
}
for (int i = 0; i < nfds; i++) {
if (events[i].data.fd == server_sock) {
client_sock = accept(server_sock,
(struct sockaddr *)&client_name,
(socklen_t *)(&client_name_len));
if (client_sock == -1) //server overloaded
continue;
if (events[i].events & EPOLLIN) {
std::cout << "EPOLLIN on " << client_sock << std::endl;
}
Arch::set_nonblocking(client_sock);
ev.events = EPOLLIN | EPOLLRDHUP | EPOLLET; //input data and connection closing
ev.data.fd = client_sock;
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, client_sock, &ev) == -1) {
perror("epoll_ctl: client_socket");
exit(EXIT_FAILURE);
}
accept_request(client_sock);
} else {
if (events[i].events & EPOLLRDHUP) {
epoll_ctl(epollfd, EPOLL_CTL_DEL, events[i].data.fd, &ev);
}
}
}
}
startup(&port)
crée socket non bloquante, la liaison avec le port et ainsi de suite. mon script envoie les données suivantes:
GET /connect?id=1&secret=1 HTTP/1.0\r\n\r\n
mais recv
retour parfois -1
dans cette fonction (appel à l'intérieur de accept_request
):
/**********************************************************************/
/* Get a line from a socket, whether the line ends in a newline,
* carriage return, or a CRLF combination. Terminates the string read
* with a null character. If no newline indicator is found before the
* end of the buffer, the string is terminated with a null. If any of
* the above three line terminators is read, the last character of the
* string will be a linefeed and the string will be terminated with a
* null character.
* Parameters: the socket descriptor
* the buffer to save the data in
* the size of the buffer
* Returns: the number of bytes stored (excluding null) */
/**********************************************************************/
int get_line(int sock, char *buf, int size) {
int i = 0;
char c = '\0';
int n;
while ((i < size - 1) && (c != '\n')) {
n = recv(sock, &c, 1, 0);
//debug
std::cout << "n = " << n << std::endl;
if (n > 0) {
if (c == '\r') {
n = recv(sock, &c, 1, MSG_PEEK);
if ((n > 0) && (c == '\n'))
recv(sock, &c, 1, 0);
else
c = '\n';
}
buf[i] = c;
i++;
} else {
//debug
if (errno == EWOULDBLOCK)
std::cout << "EWOULDBLOCK" << std::endl;
c = '\n';
}
}
buf[i] = '\0';
return(i);
}
epoll page man a écrit que je dois lecture / écriture jusqu'à ce que je EAGAIN
, mais je m'y suis déjà! et je vous le tampon ne pas être vide. ce que je fais mal?
UPD: J'ai trouvé une chose intéressante: lorsque cette situation est arrivé, je l'utilise à nouveau dans mon code sleep(1)
et recc(...)
et je reçois des données que j'attends! c'est un truc sale. est-il une approche plus gracieuse pour résoudre ce problème?
La solution
Il est tout à fait normal pour la première recv()
dans ce cas à EAGAIN
de retour. epoll()
vous a jamais dit si elle était encore lisible ou non.
Chaque recv()
doit être prêt à gérer EAGAIN
si vous utilisez des prises non-blocage. wakeups Spurious sont possibles, donc chaque fois qu'une API comme select()
, poll()
ou epoll()
vous indique qu'une prise est lisible, il est seulement dit « il peut être lisible - faire un essai »