Skip to content

Commit 0842f8f

Browse files
committed
poll version
1 parent 2f6d926 commit 0842f8f

File tree

1 file changed

+30
-39
lines changed

1 file changed

+30
-39
lines changed

tls/server-tls-nonblocking.c

Lines changed: 30 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,15 @@
3030
#include <arpa/inet.h>
3131
#include <netinet/in.h>
3232
#include <unistd.h>
33+
#include <poll.h>
3334

3435
/* wolfSSL */
3536
#include <wolfssl/options.h>
3637
#include <wolfssl/ssl.h>
3738
#include <wolfssl/wolfio.h>
3839

3940
#define DEFAULT_PORT 11111
40-
#define SELECT_WAIT_SEC 1
41+
#define SELECT_WAIT_mSEC 1000
4142

4243
#define CERT_FILE "../certs/server-cert.pem"
4344
#define KEY_FILE "../certs/server-key.pem"
@@ -50,39 +51,29 @@ enum {
5051
TEST_ERROR_READY
5152
};
5253

53-
static int tcp_select(SOCKET_T socketfd, int to_sec, int rx)
54+
static int tcp_poll(SOCKET_T socketfd, int msec, int rx)
5455
{
55-
fd_set errfds;
56-
fd_set recvfds;
57-
fd_set sendfds;
58-
SOCKET_T nfds = socketfd + 1;
59-
struct timeval timeout;
60-
int result;
61-
62-
timeout.tv_sec = to_sec;
63-
64-
FD_ZERO(&recvfds);
65-
FD_SET(socketfd, &recvfds);
66-
FD_ZERO(&sendfds);
67-
FD_SET(socketfd, &sendfds);
68-
FD_ZERO(&errfds);
69-
FD_SET(socketfd, &errfds);
70-
71-
result = select(nfds, &recvfds, &sendfds, &errfds, &timeout);
72-
printf("fd = %d, select = %d\n", socketfd, result);
73-
sleep(1);
74-
if (result == 0)
75-
return TEST_TIMEOUT;
76-
else if (result > 0) {
77-
if (FD_ISSET(socketfd, &recvfds))
78-
printf("Socket is ready for recv\n");
79-
if (FD_ISSET(socketfd, &sendfds))
80-
printf("Socket is ready for recv\n");
81-
if (FD_ISSET(socketfd, &errfds))
82-
printf("Socket is ready for error\n");
83-
}
84-
85-
return TEST_SELECT_FAIL;
56+
struct pollfd fds[1];
57+
int ret;
58+
59+
fds[0].fd = socketfd;
60+
if(rx)
61+
fds[0].events = POLLIN;
62+
else
63+
fds[0].events = POLLOUT;
64+
65+
if ((ret = poll(fds, 1, msec)) == 0)
66+
ret = TEST_TIMEOUT;
67+
else if (ret > 0) {
68+
if (fds[0].revents & POLLIN) {
69+
ret = TEST_RECV_READY;
70+
} else if (fds[0].revents & POLLOUT)
71+
ret = TEST_SEND_READY;
72+
else
73+
ret = TEST_SELECT_FAIL;
74+
} else ret = TEST_SELECT_FAIL;
75+
printf("Poll result = %d\n", ret);
76+
return ret;
8677
}
8778

8879

@@ -183,7 +174,7 @@ int main()
183174
== -1) {
184175
if (errno == EAGAIN || errno == EWOULDBLOCK) {
185176
/* non-blocking, wait for read activity on socket */
186-
tcp_select(sockfd, SELECT_WAIT_SEC, 1);
177+
tcp_poll(sockfd, SELECT_WAIT_mSEC, 1);
187178
continue;
188179
}
189180
else if (errno == EINPROGRESS || errno == EALREADY) {
@@ -193,7 +184,7 @@ int main()
193184
ret = -1;
194185
goto exit;
195186
}
196-
187+
197188
/* Set the socket options to use nonblocking I/O */
198189
if (fcntl(connd, F_SETFL, O_NONBLOCK) == -1) {
199190
fprintf(stderr, "ERROR: failed to set socket options\n");
@@ -221,7 +212,7 @@ int main()
221212
ret = wolfSSL_accept(ssl);
222213
err = wolfSSL_get_error(ssl, ret);
223214
if (err == WOLFSSL_ERROR_WANT_READ)
224-
tcp_select(connd, SELECT_WAIT_SEC, 1);
215+
tcp_poll(connd, SELECT_WAIT_mSEC, 1);
225216
} while (err == WOLFSSL_ERROR_WANT_READ || err == WOLFSSL_ERROR_WANT_WRITE);
226217
if (ret != WOLFSSL_SUCCESS) {
227218
fprintf(stderr, "wolfSSL_accept error %d (%d)\n", err, ret);
@@ -236,7 +227,7 @@ int main()
236227
ret = wolfSSL_read(ssl, buff, sizeof(buff)-1);
237228
err = wolfSSL_get_error(ssl, ret);
238229
if (err == WOLFSSL_ERROR_WANT_READ)
239-
tcp_select(connd, SELECT_WAIT_SEC, 1);
230+
tcp_poll(sockfd, SELECT_WAIT_mSEC, 1);
240231
}
241232
while (err == WOLFSSL_ERROR_WANT_READ);
242233
if (ret < 0) {
@@ -263,7 +254,7 @@ int main()
263254
ret = wolfSSL_write(ssl, reply, len);
264255
err = wolfSSL_get_error(ssl, ret);
265256
if (err == WOLFSSL_ERROR_WANT_WRITE)
266-
tcp_select(connd, SELECT_WAIT_SEC, 0);
257+
tcp_poll(sockfd, SELECT_WAIT_mSEC, 0);
267258
}
268259
while (err == WOLFSSL_ERROR_WANT_WRITE);
269260
if (ret < 0) {
@@ -276,7 +267,7 @@ int main()
276267
ret = wolfSSL_shutdown(ssl);
277268
err = wolfSSL_get_error(ssl, 0);
278269
if (err == WOLFSSL_ERROR_WANT_READ)
279-
tcp_select(sockfd, SELECT_WAIT_SEC, 1);
270+
tcp_poll(sockfd, SELECT_WAIT_mSEC, 1);
280271
} while (err == WOLFSSL_ERROR_WANT_READ || err == WOLFSSL_ERROR_WANT_WRITE);
281272

282273
/* Cleanup after this connection */

0 commit comments

Comments
 (0)