9
0
Fork 0

Basic setup of network select

git-svn-id: https://nuttx.svn.sourceforge.net/svnroot/nuttx/trunk@1278 7fd9a85b-ad96-42d3-883c-3090e2eb8679
This commit is contained in:
patacongo 2008-11-18 22:14:25 +00:00
parent a5161eb56f
commit 49bb914e8f
7 changed files with 440 additions and 32 deletions

View File

@ -38,7 +38,7 @@
ASRCS =
AOBJS = $(ASRCS:.S=$(OBJEXT))
CSRCS = poll_main.c poll_listener.c select_listener.c
CSRCS = poll_main.c poll_listener.c select_listener.c net_listener.c
COBJS = $(CSRCS:.c=$(OBJEXT))
SRCS = $(ASRCS) $(CSRCS)

View File

@ -0,0 +1,372 @@
/****************************************************************************
* examples/poll/net_listener.c
*
* Copyright (C) 2008 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <pthread.h>
#include <errno.h>
#include <debug.h>
#include "poll_internal.h"
/****************************************************************************
* Definitions
****************************************************************************/
#define IOBUFFER_SIZE 80
/****************************************************************************
* Private Types
****************************************************************************/
struct net_listener_s
{
struct sockaddr_in addr;
fd_set master;
fd_set working;
char buffer[IOBUFFER_SIZE];
int listensd;
int mxsd;
};
/****************************************************************************
* Private Data
****************************************************************************/
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: net_closeclient
****************************************************************************/
static boolean net_closeclient(struct net_listener_s *nls, int sd)
{
message("net_listener: Closing host side connection sd=%d\n", sd);
close(sd);
FD_CLR(sd, &nls->master);
/* If we just closed the max SD, then search downward for the next biggest SD. */
while (FD_ISSET(nls->mxsd, &nls->master) == FALSE)
{
nls->mxsd -= 1;
}
return TRUE;
}
/****************************************************************************
* Name: net_incomingdata
****************************************************************************/
static inline boolean net_incomingdata(struct net_listener_s *nls, int sd)
{
int nbytes;
int ret;
/* Read data from the socket */
#ifdef FIONBIO
for (;;)
#endif
{
message("net_listener: Read data from sd=%d\n", sd);
ret = recv(sd, nls->buffer, IOBUFFER_SIZE, 0);
if (ret < 0)
{
if (errno != EINTR)
{
message("net_listener: recv failed sd=%d: %d\n", sd, errno);
if (errno != EAGAIN)
{
net_closeclient(nls, sd);
return FALSE;
}
}
}
else if (ret == 0)
{
message("net_listener: Client connection lost sd=%d\n", sd);
net_closeclient(nls, sd);
return FALSE;
}
else
{
nls->buffer[ret]='\0';
message("poll_listener: Read '%s' (%d bytes)\n", sd, nls->buffer, ret);
/* Echo the data back to the client */
for (nbytes = ret; nbytes > 0; )
{
ret = send(sd, nls->buffer, nbytes, 0);
if (ret < 0)
{
if (errno != EINTR)
{
message("net_listener: Send faile sd=%d: \n", sd, errno);
net_closeclient(nls, sd);
return FALSE;
}
}
else
{
nbytes += ret;
}
}
}
}
}
/****************************************************************************
* Name: net_connection
****************************************************************************/
static inline boolean net_connection(struct net_listener_s *nls)
{
int sd;
/* Loop until all connections have been processed */
#ifdef FIONBIO
for (;;)
#endif
{
message("net_listener: Accepting accepting connection on sd=%d\n", nls->listensd);
sd = accept(nls->listensd, NULL, NULL);
if (sd < 0)
{
message("net_listener: accept failed: %d\n", sd);
if (errno != EINTR)
{
return FALSE;
}
}
else
{
/* Add the new connection to the master set */
FD_SET(sd, &nls->master);
if (sd > nls->mxsd)
{
nls->mxsd = sd;
}
return TRUE;
}
}
}
/****************************************************************************
* Name: net_mksocket
****************************************************************************/
static inline boolean net_mksocket(struct net_listener_s *nls)
{
int value;
int ret;
/* Create a listening socket */
message("net_listener: Initializing listener socket\n");
nls->listensd = socket(AF_INET, SOCK_STREAM, 0);
if (nls->listensd < 0)
{
message("net_listener: socket failed: %d\n", errno);
return FALSE;
}
/* Configure the socket */
value = 1;
ret = setsockopt(nls->listensd, SOL_SOCKET, SO_REUSEADDR, (char*)&value, sizeof(int));
if (ret < 0)
{
message("net_listener: setsockopt failed: %d\n", errno);
close(nls->listensd);
return FALSE;
}
/* Set the socket to non-blocking */
#ifdef FIONBIO
ret = ioctl(nls->listensd, FIONBIO, (char *)&value);
if (ret < 0)
{
message("net_listener: ioctl failed: %d\n", errno);
close(nls->listensd);
return FALSE;
}
#endif
/* Bind the socket */
memset(&nls->addr, 0, sizeof(struct sockaddr_in));
nls->addr.sin_family = AF_INET;
nls->addr.sin_addr.s_addr = htonl(INADDR_ANY);
nls->addr.sin_port = htons(LISTENER_PORT);
ret = bind(nls->listensd, (struct sockaddr *)&nls->addr, sizeof(struct sockaddr_in));
if (ret < 0)
{
message("net_listener: bind failed: %d\n", errno);
close(nls->listensd);
return FALSE;
}
/* Mark the socket as a listener */
ret = listen(nls->listensd, 32);
if (ret < 0)
{
message("net_listener: bind failed: %d\n", errno);
close(nls->listensd);
return FALSE;
}
return TRUE;
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: net_listener
****************************************************************************/
void *net_listener(pthread_addr_t pvarg)
{
struct net_listener_s nls;
struct timeval timeout;
int nsds;
int ret;
int i;
/* Set up a listening socket */
memset(&nls, 0, sizeof(struct net_listener_s));
if (!net_mksocket(&nls))
{
return (void*)1;
}
/* Initialize the 'master' file descriptor set */
FD_ZERO(&nls.master);
nls.mxsd = nls.listensd;
FD_SET(nls.listensd, &nls.master);
/* Set up a 3 second timeout */
timeout.tv_sec = NET_LISTENER_DELAY;
timeout.tv_usec = 0;
/* Loop waiting for incoming connections or for incoming data
* on any of the connect sockets.
*/
for (;;)
{
/* Wait on select */
message("net_listener: Calling select(), listener sd=%d\n", nls.listensd);
memcpy(&nls.working, &nls.master, sizeof(fd_set));
ret = select(nls.mxsd + 1, &nls.working, NULL, NULL, &timeout);
if (ret < 0)
{
message("net_listener: select failed: %d\n", errno);
break;
}
/* Check for timeout */
if (ret == 0)
{
message("net_listener: Timeout\n");
continue;
}
/* Find which descriptors caused the wakeup */
nsds = ret;
for (i = 0; i <= nls.mxsd && nsds > 0; i++)
{
/* Is this descriptor ready? */
if (FD_ISSET(i, &nls.working))
{
/* Yes, is it our listener? */
message("net_listener: Activity on sd=%d\n", i);
nsds--;
if (i == nls.listensd)
{
(void)net_connection(&nls);
}
else
{
net_incomingdata(&nls, i);
}
}
}
}
/* Cleanup */
#if 0 /* Don't get here */
for (i = 0; i <= nls.mxsd; +i++)
{
if (FD_ISSET(i, &nls.master))
{
close(i);
}
}
#endif
return NULL; /* Keeps some compilers from complaining */
}

View File

@ -1,5 +1,5 @@
/****************************************************************************
* examples/poll/poll.h
* examples/poll/poll_internal.h
*
* Copyright (C) 2008 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
@ -50,17 +50,27 @@
* Definitions
****************************************************************************/
#define FIFO_PATH1 "/dev/fifo0"
#define FIFO_PATH2 "/dev/fifo1"
#define POLL_LISTENER_DELAY 2000 /* 2 seconds */
#define SELECT_LISTENER_DELAY 4 /* 4 seconds */
#define WRITER_DELAY 6 /* 6 seconds */
#ifdef CONFIG_DISABLE_POLL
# error "The polling API is disabled"
#endif
/* Here are all of the configuration settings that must be met to have TCP/IP
* poll/select support. This kind of looks like overkill.
*
* CONFIG_NET - Network support must be enabled
* CONFIG_NSOCKET_DESCRIPTORS - Socket descriptors must be allocated
* CONFIG_NET_TCP - Only support on TCP (because read-ahead
* ibuffering s not yet support for UDP)
* CONFIG_NET_NTCP_READAHEAD_BUFFERS - TCP/IP read-ahead buffering must be enabled
*/
#if defined(CONFIG_NET) && CONFIG_NSOCKET_DESCRIPTORS > 0 && \
defined(CONFIG_NET_TCP) && CONFIG_NET_NTCP_READAHEAD_BUFFERS > 0
# define HAVE_NETPOLL 1
#else
# undef HAVE_NETPOLL
#endif
/* If debug is enabled, then use lib_rawprintf so that OS debug output and
* the test output are synchronized.
*/
@ -73,6 +83,16 @@
# define msgflush() fflush(stdout)
#endif
#define FIFO_PATH1 "/dev/fifo0"
#define FIFO_PATH2 "/dev/fifo1"
#define POLL_LISTENER_DELAY 2000 /* 2 seconds */
#define SELECT_LISTENER_DELAY 4 /* 4 seconds */
#define NET_LISTENER_DELAY 3 /* 3 seconds */
#define WRITER_DELAY 6 /* 6 seconds */
#define LISTENER_PORT 5471
/****************************************************************************
* Public Types
****************************************************************************/
@ -88,4 +108,7 @@
extern void *poll_listener(pthread_addr_t pvarg);
extern void *select_listener(pthread_addr_t pvarg);
#ifdef HAVE_NETPOLL
extern void *net_listener(pthread_addr_t pvarg);
#endif
#endif /* __EXAMPLES_PIPE_PIPE_H */

View File

@ -91,6 +91,9 @@ int user_start(int argc, char *argv[])
ssize_t nbytes;
pthread_t tid1;
pthread_t tid2;
#ifdef HAVE_NETPOLL
pthread_t tid3;
#endif
int count;
int fd1;
int fd2;
@ -132,9 +135,9 @@ int user_start(int argc, char *argv[])
return 2;
}
/* Start the listener */
/* Start the listeners */
message("user_start: Starting poll_listener thread\n");
message("user_start: Starting poll_listener thread\n");
ret = pthread_create(&tid1, NULL, poll_listener, NULL);
if (ret != 0)
@ -143,7 +146,7 @@ int user_start(int argc, char *argv[])
return 3;
}
message("user_start: Starting select_listener thread\n");
message("user_start: Starting select_listener thread\n");
ret = pthread_create(&tid2, NULL, select_listener, NULL);
if (ret != 0)
@ -152,6 +155,16 @@ int user_start(int argc, char *argv[])
return 3;
}
#ifdef HAVE_NETPOLL
message("user_start: Starting net_listener thread\n");
ret = pthread_create(&tid3, NULL, net_listener, NULL);
if (ret != 0)
{
message("user_start: Failed to create net_listener thread: %d\n", ret);
}
#endif
/* Loop forever */
for (count = 0; ; count++)

View File

@ -109,7 +109,7 @@ static int poll_fdsetup(int fd, FAR struct pollfd *fds, boolean setup)
#if defined(CONFIG_NET) && CONFIG_NSOCKET_DESCRIPTORS > 0
if ((unsigned int)fd < (CONFIG_NFILE_DESCRIPTORS+CONFIG_NSOCKET_DESCRIPTORS))
{
return net_poll(fds->fd, fds);
return net_poll(fds->fd, fds, setup);
}
else
#endif

View File

@ -160,7 +160,7 @@ EXTERN int netdev_ioctl(int sockfd, int cmd, struct ifreq *req);
#ifndef CONFIG_DISABLE_POLL
struct pollfd; /* Forward reference -- see poll.h */
EXTERN int net_poll(int sockfd, struct pollfd *fds);
EXTERN int net_poll(int sockfd, struct pollfd *fds, boolean setup);
#endif
/* netdev-register.c *********************************************************/

View File

@ -42,10 +42,18 @@
#include <sys/types.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <poll.h>
#include <errno.h>
#include <debug.h>
#include <net/uip/uip.h>
#include <nuttx/net.h>
#include <nuttx/arch.h>
#include <uip/uip-internal.h>
#include "net-internal.h"
/****************************************************************************
@ -57,7 +65,7 @@
* buffering.
*/
#if defined(CONFIG_DISABLE_POLL) && CONFIG_NSOCKET_DESCRIPTORS > 0 && \
#if !defined(CONFIG_DISABLE_POLL) && CONFIG_NSOCKET_DESCRIPTORS > 0 && \
defined(CONFIG_NET_TCP) && CONFIG_NET_NTCP_READAHEAD_BUFFERS > 0
# define HAVE_NETPOLL 1
#else
@ -68,11 +76,6 @@
* Private Types
****************************************************************************/
struct net_poll_s
{
FAR struct pollfd *fds; /* The descriptor poll info */
};
/****************************************************************************
* Private Functions
****************************************************************************/
@ -101,7 +104,7 @@ struct net_poll_s
static uint16 poll_interrupt(struct uip_driver_s *dev, FAR void *conn,
FAR void *pvprivate, uint16 flags)
{
struct pollfd *fds = (struct recvfrom_s *)pvprivate;
FAR struct pollfd *fds = (FAR struct pollfd *)pvprivate;
nvdbg("flags: %04x\n", flags);
@ -134,7 +137,7 @@ static uint16 poll_interrupt(struct uip_driver_s *dev, FAR void *conn,
if (eventset)
{
fds->revevents |= eventset;
fds->revents |= eventset;
sem_post(fds->sem);
}
}
@ -159,7 +162,7 @@ static uint16 poll_interrupt(struct uip_driver_s *dev, FAR void *conn,
****************************************************************************/
#ifdef HAVE_NETPOLL
static inline int net_pollsetup((FAR struct uip_conn *)conn, struct pollfd *fds)
static inline int net_pollsetup(FAR struct uip_conn *conn, struct pollfd *fds)
{
FAR struct uip_callback_s *cb;
irqstate_t flags;
@ -215,7 +218,6 @@ static inline int net_pollsetup((FAR struct uip_conn *)conn, struct pollfd *fds)
errout_with_irq:
irqrestore(flags);
free(nps);
errout:
return ret;
}
@ -236,19 +238,17 @@ errout:
****************************************************************************/
#ifdef HAVE_NETPOLL
static inline int net_pollteardown((FAR struct uip_conn *)conn, struct pollfd *fds)
static inline int net_pollteardown(FAR struct uip_conn *conn, struct pollfd *fds)
{
FAR struct uip_callback_s *cb;
irqstate_t flags;
int ret;
/* Sanity check */
#ifdef CONFIG_DEBUG
if (!conn || !fds || !fds->private)
{
ret = -EINVAL;
goto errout;
return -EINVAL;
}
#endif
@ -294,7 +294,7 @@ static inline int net_pollteardown((FAR struct uip_conn *)conn, struct pollfd *f
****************************************************************************/
#ifndef CONFIG_DISABLE_POLL
int net_poll(int sockfd, struct pollfd *fds)
int net_poll(int sockfd, struct pollfd *fds, boolean setup)
{
#ifndef HAVE_NETPOLL
return -ENOSYS;
@ -334,17 +334,17 @@ int net_poll(int sockfd, struct pollfd *fds)
#endif
/* Check if we are setting up or tearing down the poll */
if (fds)
if (setup)
{
/* Perform the TCP/IP poll() setup */
ret = net_pollsetup((FAR struct uip_conn *)psock->conn, fds);
ret = net_pollsetup((FAR struct uip_conn *)psock->s_conn, fds);
}
else
{
/* Perform the TCP/IP poll() teardown */
ret = net_pollteardown((FAR struct uip_conn *)psock->conn);
ret = net_pollteardown((FAR struct uip_conn *)psock->s_conn, fds);
}
errout: