use poll instead of select

This commit is contained in:
DanyLE 2022-08-30 17:27:01 +02:00
parent c7f6cf42cb
commit e35c8b7a6f

View File

@ -5,6 +5,7 @@
#include <string.h> #include <string.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <poll.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <stdio.h> #include <stdio.h>
#include <errno.h> #include <errno.h>
@ -418,8 +419,7 @@ int antd_send(void *src, const void *data_in, int len_in)
ptr = (char *)data; ptr = (char *)data;
writelen = len > BUFFLEN ? BUFFLEN : len; writelen = len > BUFFLEN ? BUFFLEN : len;
written = 0; written = 0;
fd_set fds; struct pollfd pfd;
struct timeval timeout;
while (writelen > 0) //source->attempt < MAX_ATTEMPT while (writelen > 0) //source->attempt < MAX_ATTEMPT
{ {
// clear the error queue // clear the error queue
@ -464,13 +464,10 @@ int antd_send(void *src, const void *data_in, int len_in)
//printf("SSL_ERROR_WANT_READ\n"); //printf("SSL_ERROR_WANT_READ\n");
int sock = SSL_get_rfd(source->ssl); int sock = SSL_get_rfd(source->ssl);
FD_ZERO(&fds); pfd.fd = sock;
FD_SET(sock, &fds); pfd.events = POLLIN;
err = poll(&pfd, 1, 500);
timeout.tv_sec = 0; if (err == 0 || (err > 0 && (pfd.revents & POLLIN)))
timeout.tv_usec = 500;
err = select(sock + 1, &fds, NULL, NULL, &timeout);
if (err == 0 || (err > 0 && FD_ISSET(sock, &fds)))
{ {
//source->attempt++; //source->attempt++;
continue; // more data to read... continue; // more data to read...
@ -484,14 +481,10 @@ int antd_send(void *src, const void *data_in, int len_in)
// socket not writable right now, wait a few seconds and try again... // socket not writable right now, wait a few seconds and try again...
//printf("SSL_ERROR_WANT_WRITE \n"); //printf("SSL_ERROR_WANT_WRITE \n");
int sock = SSL_get_wfd(source->ssl); int sock = SSL_get_wfd(source->ssl);
FD_ZERO(&fds); pfd.fd = sock;
FD_SET(sock, &fds); pfd.events = POLLOUT;
err = poll(&pfd, 1, 500);
timeout.tv_sec = 0; if (err == 0 || (err > 0 && (pfd.revents & POLLOUT)))
timeout.tv_usec = 500;
err = select(sock + 1, NULL, &fds, NULL, &timeout);
if (err == 0 || (err > 0 && FD_ISSET(sock, &fds)))
{ {
//source->attempt++; //source->attempt++;
continue; // can write more data now... continue; // can write more data now...
@ -637,8 +630,7 @@ int antd_recv(void *src, void *data, int len)
ptr = (char *)data; ptr = (char *)data;
readlen = len > BUFFLEN ? BUFFLEN : len; readlen = len > BUFFLEN ? BUFFLEN : len;
read = 0; read = 0;
fd_set fds; struct pollfd pfd;
struct timeval timeout;
while (readlen > 0) //&& source->attempt < MAX_ATTEMPT while (readlen > 0) //&& source->attempt < MAX_ATTEMPT
{ {
ERR_clear_error(); ERR_clear_error();
@ -683,13 +675,10 @@ int antd_recv(void *src, void *data, int len)
//printf("SSL_ERROR_WANT_READ\n"); //printf("SSL_ERROR_WANT_READ\n");
int sock = SSL_get_rfd(source->ssl); int sock = SSL_get_rfd(source->ssl);
FD_ZERO(&fds); pfd.fd = sock;
FD_SET(sock, &fds); pfd.events = POLLIN;
err = poll(&pfd, 1, 500);
timeout.tv_sec = 0; if (err == 0 || (err > 0 && (pfd.revents & POLLIN)))
timeout.tv_usec = 500;
err = select(sock + 1, &fds, NULL, NULL, &timeout);
if (err == 0 || (err > 0 && FD_ISSET(sock, &fds)))
{ {
//source->attempt++; //source->attempt++;
continue; // more data to read... continue; // more data to read...
@ -703,14 +692,10 @@ int antd_recv(void *src, void *data, int len)
// socket not writable right now, wait a few seconds and try again... // socket not writable right now, wait a few seconds and try again...
//printf("SSL_ERROR_WANT_WRITE \n"); //printf("SSL_ERROR_WANT_WRITE \n");
int sock = SSL_get_wfd(source->ssl); int sock = SSL_get_wfd(source->ssl);
FD_ZERO(&fds); pfd.fd = sock;
FD_SET(sock, &fds); pfd.events = POLLOUT;
err = poll(&pfd, 1, 500);
timeout.tv_sec = 0; if (err == 0 || (err > 0 && (pfd.revents & POLLOUT)))
timeout.tv_usec = 500;
err = select(sock + 1, NULL, &fds, NULL, &timeout);
if (err == 0 || (err > 0 && FD_ISSET(sock, &fds)))
{ {
//source->attempt++; //source->attempt++;
continue; // can write more data now... continue; // can write more data now...