Local sockets: Add poll support for Unix stream sockets. From Jussi Kivilinna.

This commit is contained in:
Gregory Nutt 2015-05-12 07:41:12 -06:00
parent b15cf5cdd8
commit c07a5b2775
4 changed files with 130 additions and 1 deletions

View File

@ -16,4 +16,8 @@ config EXAMPLES_USTREAM_ADDR
string "Unix domain address"
default "/dev/fifo"
config EXAMPLES_USTREAM_USE_POLL
bool "Use poll for checking socket readiness"
default n
endif # EXAMPLES_USTREAM

View File

@ -47,6 +47,7 @@
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <poll.h>
#include "ustream.h"
@ -60,6 +61,9 @@ int main(int argc, FAR char *argv[])
int client_main(int argc, char *argv[])
#endif
{
#ifdef CONFIG_EXAMPLES_USTREAM_USE_POLL
struct pollfd pfd;
#endif
struct sockaddr_un myaddr;
socklen_t addrlen;
FAR char *outbuf;
@ -128,6 +132,29 @@ int client_main(int argc, char *argv[])
/* Then send and receive one message */
#ifdef CONFIG_EXAMPLES_USTREAM_USE_POLL
memset(&pfd, 0, sizeof(pfd));
pfd.fd = sockfd;
pfd.events = POLLOUT;
ret = poll(&pfd, 1, -1);
if (ret < 0)
{
printf("client: send-poll failed: %d\n", errno);
goto errout_with_socket;
}
else if (ret == 0)
{
printf("client: send-poll failed: returned zero\n");
goto errout_with_socket;
}
else if (!(pfd.revents & POLLOUT))
{
printf("client: send-poll failed: no POLLOUT\n");
goto errout_with_socket;
}
#endif
printf("client: Sending %d bytes\n", SENDSIZE);
nbytessent = send(sockfd, outbuf, SENDSIZE, 0);
printf("client: Sent %d bytes\n", nbytessent);
@ -146,6 +173,29 @@ int client_main(int argc, char *argv[])
totalbytesrecvd = 0;
do
{
#ifdef CONFIG_EXAMPLES_USTREAM_USE_POLL
memset(&pfd, 0, sizeof(pfd));
pfd.fd = sockfd;
pfd.events = POLLIN;
ret = poll(&pfd, 1, -1);
if (ret < 0)
{
printf("client: recv-poll failed: %d\n", errno);
goto errout_with_socket;
}
else if (ret == 0)
{
printf("client: recv-poll failed: returned zero\n");
goto errout_with_socket;
}
else if (!(pfd.revents & POLLIN))
{
printf("client: recv-poll failed: no POLLOUT\n");
goto errout_with_socket;
}
#endif
printf("client: Receiving...\n");
nbytesrecvd = recv(sockfd, &inbuf[totalbytesrecvd], SENDSIZE - totalbytesrecvd, 0);

View File

@ -48,6 +48,7 @@
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <poll.h>
#include "ustream.h"
@ -61,6 +62,9 @@ int main(int argc, FAR char *argv[])
int server_main(int argc, char *argv[])
#endif
{
#ifdef CONFIG_EXAMPLES_USTREAM_USE_POLL
struct pollfd pfd;
#endif
struct sockaddr_un myaddr;
socklen_t addrlen;
FAR char *buffer;
@ -123,6 +127,29 @@ int server_main(int argc, char *argv[])
/* Accept only one connection */
#ifdef CONFIG_EXAMPLES_USTREAM_USE_POLL
memset(&pfd, 0, sizeof(pfd));
pfd.fd = listensd;
pfd.events = POLLIN;
ret = poll(&pfd, 1, -1);
if (ret < 0)
{
printf("server: poll failure: %d\n", errno);
goto errout_with_listensd;
}
else if (ret == 0)
{
printf("server: poll failure: returned zero\n");
goto errout_with_listensd;
}
else if (!(pfd.revents & POLLIN))
{
printf("server: poll failure: no POLLIN\n");
goto errout_with_listensd;
}
#endif
acceptsd = accept(listensd, (struct sockaddr*)&myaddr, &addrlen);
if (acceptsd < 0)
{
@ -137,6 +164,29 @@ int server_main(int argc, char *argv[])
totalbytesread = 0;
while (totalbytesread < SENDSIZE)
{
#ifdef CONFIG_EXAMPLES_USTREAM_USE_POLL
memset(&pfd, 0, sizeof(pfd));
pfd.fd = acceptsd;
pfd.events = POLLIN;
ret = poll(&pfd, 1, -1);
if (ret < 0)
{
printf("server: recv-poll failed: %d\n", errno);
goto errout_with_acceptsd;
}
else if (ret == 0)
{
printf("server: recv-poll failed: returned zero\n");
goto errout_with_acceptsd;
}
else if (!(pfd.revents & POLLIN))
{
printf("server: recv-poll failed: no POLLIN\n");
goto errout_with_acceptsd;
}
#endif
printf("server: Reading...\n");
nbytesread = recv(acceptsd, &buffer[totalbytesread], 2*SENDSIZE - totalbytesread, 0);
if (nbytesread < 0)
@ -179,6 +229,29 @@ int server_main(int argc, char *argv[])
/* Then send the same data back to the client */
#ifdef CONFIG_EXAMPLES_USTREAM_USE_POLL
memset(&pfd, 0, sizeof(pfd));
pfd.fd = acceptsd;
pfd.events = POLLOUT;
ret = poll(&pfd, 1, -1);
if (ret < 0)
{
printf("server: send-poll failed: %d\n", errno);
goto errout_with_acceptsd;
}
else if (ret == 0)
{
printf("server: send-poll failed: returned zero\n");
goto errout_with_acceptsd;
}
else if (!(pfd.revents & POLLOUT))
{
printf("server: send-poll failed: no POLLOUT\n");
goto errout_with_acceptsd;
}
#endif
printf("server: Sending %d bytes\n", totalbytesread);
nbytessent = send(acceptsd, buffer, totalbytesread, 0);
if (nbytessent <= 0)

View File

@ -107,7 +107,9 @@
# ifndef CONFIG_NSH_NOMAC
# error "CONFIG_NSH_NOMAC must be defined for SLIP"
# endif
#elif !defined(CONFIG_NET_LOCAL)
#elif defined(CONFIG_NET_LOCAL)
# define NET_DEVNAME "lo"
#else
# error ERROR: No link layer protocol defined
#endif