mirror of
https://github.com/apache/nuttx.git
synced 2026-06-07 01:05:54 +08:00
Move poll save area back into struct pollfd (as it was
git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@1288 42af7a65-404d-4744-a932-0658087f49c3
This commit is contained in:
@@ -38,7 +38,7 @@
|
||||
|
||||
ASRCS =
|
||||
AOBJS = $(ASRCS:.S=$(OBJEXT))
|
||||
CSRCS = poll_main.c poll_listener.c select_listener.c net_listener.c
|
||||
CSRCS = poll_main.c poll_listener.c select_listener.c net_listener.c net_reader.c
|
||||
COBJS = $(CSRCS:.c=$(OBJEXT))
|
||||
|
||||
SRCS = $(ASRCS) $(CSRCS)
|
||||
|
||||
@@ -99,7 +99,7 @@ int main(int argc, char **argv, char **envp)
|
||||
|
||||
myaddr.sin_family = AF_INET;
|
||||
myaddr.sin_port = htons(LISTENER_PORT);
|
||||
myaddr.sin_addr.s_addr = htonl(inet_addr(TARGETIP));
|
||||
myaddr.sin_addr.s_addr = inet_addr(TARGETIP);
|
||||
|
||||
message("client: Connecting to %s...\n", TARGETIP);
|
||||
if (connect( sockfd, (struct sockaddr*)&myaddr, sizeof(struct sockaddr_in)) < 0)
|
||||
@@ -111,7 +111,7 @@ int main(int argc, char **argv, char **envp)
|
||||
|
||||
/* Then send and receive messages */
|
||||
|
||||
for (i = 0; ; i++);
|
||||
for (i = 0; ; i++)
|
||||
{
|
||||
sprintf(outbuf, "Remote message %d", i);
|
||||
len = strlen(outbuf);
|
||||
@@ -139,6 +139,8 @@ int main(int argc, char **argv, char **envp)
|
||||
message("client: recv failed: %d\n", errno);
|
||||
goto errout_with_socket;
|
||||
}
|
||||
|
||||
inbuf[nbytesrecvd] = '\0';
|
||||
message("client: Received '%s' (%d bytes)\n", inbuf, nbytesrecvd);
|
||||
|
||||
if (nbytesrecvd != len)
|
||||
|
||||
@@ -52,6 +52,7 @@
|
||||
#include <errno.h>
|
||||
#include <debug.h>
|
||||
|
||||
#include <net/uip/uip-lib.h>
|
||||
#include "poll_internal.h"
|
||||
|
||||
/****************************************************************************
|
||||
@@ -107,6 +108,7 @@ static boolean net_closeclient(struct net_listener_s *nls, int sd)
|
||||
|
||||
static inline boolean net_incomingdata(struct net_listener_s *nls, int sd)
|
||||
{
|
||||
char *ptr;
|
||||
int nbytes;
|
||||
int ret;
|
||||
|
||||
@@ -139,25 +141,26 @@ static inline boolean net_incomingdata(struct net_listener_s *nls, int sd)
|
||||
else
|
||||
{
|
||||
nls->buffer[ret]='\0';
|
||||
message("poll_listener: Read '%s' (%d bytes)\n", sd, nls->buffer, ret);
|
||||
message("poll_listener: Read '%s' (%d bytes)\n", nls->buffer, ret);
|
||||
|
||||
/* Echo the data back to the client */
|
||||
|
||||
for (nbytes = ret; nbytes > 0; )
|
||||
for (nbytes = ret, ptr = nls->buffer; nbytes > 0; )
|
||||
{
|
||||
ret = send(sd, nls->buffer, nbytes, 0);
|
||||
ret = send(sd, ptr, nbytes, 0);
|
||||
if (ret < 0)
|
||||
{
|
||||
if (errno != EINTR)
|
||||
{
|
||||
message("net_listener: Send faile sd=%d: \n", sd, errno);
|
||||
message("net_listener: Send failed sd=%d: \n", sd, errno);
|
||||
net_closeclient(nls, sd);
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
nbytes += ret;
|
||||
nbytes -= ret;
|
||||
ptr += ret;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -271,6 +274,46 @@ static inline boolean net_mksocket(struct net_listener_s *nls)
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: net_configure
|
||||
****************************************************************************/
|
||||
|
||||
static void net_configure(void)
|
||||
{
|
||||
struct in_addr addr;
|
||||
#if defined(CONFIG_EXAMPLE_POLL_NOMAC)
|
||||
ubyte mac[IFHWADDRLEN];
|
||||
#endif
|
||||
|
||||
/* Configure uIP */
|
||||
/* Many embedded network interfaces must have a software assigned MAC */
|
||||
|
||||
#ifdef CONFIG_EXAMPLE_POLL_NOMAC
|
||||
mac[0] = 0x00;
|
||||
mac[1] = 0xe0;
|
||||
mac[2] = 0xb0;
|
||||
mac[3] = 0x0b;
|
||||
mac[4] = 0xba;
|
||||
mac[5] = 0xbe;
|
||||
uip_setmacaddr("eth0", mac);
|
||||
#endif
|
||||
|
||||
/* Set up our host address */
|
||||
|
||||
addr.s_addr = HTONL(CONFIG_EXAMPLE_POLL_IPADDR);
|
||||
uip_sethostaddr("eth0", &addr);
|
||||
|
||||
/* Set up the default router address */
|
||||
|
||||
addr.s_addr = HTONL(CONFIG_EXAMPLE_POLL_DRIPADDR);
|
||||
uip_setdraddr("eth0", &addr);
|
||||
|
||||
/* Setup the subnet mask */
|
||||
|
||||
addr.s_addr = HTONL(CONFIG_EXAMPLE_POLL_NETMASK);
|
||||
uip_setnetmask("eth0", &addr);
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
@@ -287,6 +330,10 @@ void *net_listener(pthread_addr_t pvarg)
|
||||
int ret;
|
||||
int i;
|
||||
|
||||
/* Configure uIP */
|
||||
|
||||
net_configure();
|
||||
|
||||
/* Set up a listening socket */
|
||||
|
||||
memset(&nls, 0, sizeof(struct net_listener_s));
|
||||
|
||||
@@ -0,0 +1,314 @@
|
||||
/****************************************************************************
|
||||
* examples/poll/net_reader.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 <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <pthread.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
#include <debug.h>
|
||||
|
||||
#include <net/uip/uip-lib.h>
|
||||
#include "poll_internal.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Definitions
|
||||
****************************************************************************/
|
||||
|
||||
#define IOBUFFER_SIZE 80
|
||||
|
||||
/****************************************************************************
|
||||
* Private Types
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: net_configure
|
||||
****************************************************************************/
|
||||
|
||||
static void net_configure(void)
|
||||
{
|
||||
struct in_addr addr;
|
||||
#if defined(CONFIG_EXAMPLE_POLL_NOMAC)
|
||||
ubyte mac[IFHWADDRLEN];
|
||||
#endif
|
||||
|
||||
/* Configure uIP */
|
||||
/* Many embedded network interfaces must have a software assigned MAC */
|
||||
|
||||
#ifdef CONFIG_EXAMPLE_POLL_NOMAC
|
||||
mac[0] = 0x00;
|
||||
mac[1] = 0xe0;
|
||||
mac[2] = 0xb0;
|
||||
mac[3] = 0x0b;
|
||||
mac[4] = 0xba;
|
||||
mac[5] = 0xbe;
|
||||
uip_setmacaddr("eth0", mac);
|
||||
#endif
|
||||
|
||||
/* Set up our host address */
|
||||
|
||||
addr.s_addr = HTONL(CONFIG_EXAMPLE_POLL_IPADDR);
|
||||
uip_sethostaddr("eth0", &addr);
|
||||
|
||||
/* Set up the default router address */
|
||||
|
||||
addr.s_addr = HTONL(CONFIG_EXAMPLE_POLL_DRIPADDR);
|
||||
uip_setdraddr("eth0", &addr);
|
||||
|
||||
/* Setup the subnet mask */
|
||||
|
||||
addr.s_addr = HTONL(CONFIG_EXAMPLE_POLL_NETMASK);
|
||||
uip_setnetmask("eth0", &addr);
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: net_receive
|
||||
****************************************************************************/
|
||||
|
||||
static void net_receive(int sd)
|
||||
{
|
||||
struct timeval timeout;
|
||||
char buffer[IOBUFFER_SIZE];
|
||||
char *ptr;
|
||||
fd_set readset;
|
||||
int nbytes;
|
||||
int ret;
|
||||
|
||||
/* Set up the timeout */
|
||||
|
||||
timeout.tv_sec = NET_LISTENER_DELAY;
|
||||
timeout.tv_usec = 0;
|
||||
|
||||
/* Loop while we have the connection */
|
||||
|
||||
for (;;)
|
||||
{
|
||||
/* Wait for incoming message */
|
||||
|
||||
do
|
||||
{
|
||||
FD_ZERO(&readset);
|
||||
FD_SET(sd, &readset);
|
||||
ret = select(sd + 1, &readset, NULL, NULL, &timeout);
|
||||
}
|
||||
while (ret < 0 && errno == EINTR);
|
||||
|
||||
/* Something has happened */
|
||||
|
||||
if (ret < 0)
|
||||
{
|
||||
message("net_reader: select failed: %d\n", errno);
|
||||
return;
|
||||
}
|
||||
else if (ret == 0)
|
||||
{
|
||||
message("net_reader: Timeout\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
message("net_reader: Read data from sd=%d\n", sd);
|
||||
memset(buffer, '?', IOBUFFER_SIZE); /* Just to make sure we really receive something */
|
||||
ret = recv(sd, buffer, IOBUFFER_SIZE, 0);
|
||||
if (ret < 0)
|
||||
{
|
||||
if (errno != EINTR)
|
||||
{
|
||||
message("net_reader: recv failed sd=%d: %d\n", sd, errno);
|
||||
if (errno != EAGAIN)
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (ret == 0)
|
||||
{
|
||||
message("net_reader: Client connection lost sd=%d\n", sd);
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
buffer[ret]='\0';
|
||||
message("net_reader: Read '%s' (%d bytes)\n", buffer, ret);
|
||||
|
||||
/* Echo the data back to the client */
|
||||
|
||||
for (nbytes = ret, ptr = buffer; nbytes > 0; )
|
||||
{
|
||||
ret = send(sd, ptr, nbytes, 0);
|
||||
if (ret < 0)
|
||||
{
|
||||
if (errno != EINTR)
|
||||
{
|
||||
message("net_reader: Send failed sd=%d: %d\n", sd, errno);
|
||||
return;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
nbytes -= ret;
|
||||
ptr += ret;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: net_reader
|
||||
****************************************************************************/
|
||||
|
||||
void *net_reader(pthread_addr_t pvarg)
|
||||
{
|
||||
struct sockaddr_in addr;
|
||||
#ifdef POLL_HAVE_SOLINGER
|
||||
struct linger ling;
|
||||
#endif
|
||||
int listensd;
|
||||
int acceptsd;
|
||||
socklen_t addrlen;
|
||||
int optval;
|
||||
|
||||
/* Configure uIP */
|
||||
|
||||
net_configure();
|
||||
|
||||
/* Create a new TCP socket */
|
||||
|
||||
listensd = socket(PF_INET, SOCK_STREAM, 0);
|
||||
if (listensd < 0)
|
||||
{
|
||||
message("net_reader: socket failure: %d\n", errno);
|
||||
goto errout;
|
||||
}
|
||||
|
||||
/* Set socket to reuse address */
|
||||
|
||||
optval = 1;
|
||||
if (setsockopt(listensd, SOL_SOCKET, SO_REUSEADDR, (void*)&optval, sizeof(int)) < 0)
|
||||
{
|
||||
message("net_reader: setsockopt SO_REUSEADDR failure: %d\n", errno);
|
||||
goto errout_with_listensd;
|
||||
}
|
||||
|
||||
/* Bind the socket to a local address */
|
||||
|
||||
addr.sin_family = AF_INET;
|
||||
addr.sin_port = HTONS(LISTENER_PORT);
|
||||
addr.sin_addr.s_addr = INADDR_ANY;
|
||||
|
||||
if (bind(listensd, (struct sockaddr*)&addr, sizeof(struct sockaddr_in)) < 0)
|
||||
{
|
||||
message("net_reader: bind failure: %d\n", errno);
|
||||
goto errout_with_listensd;
|
||||
}
|
||||
|
||||
/* Listen for connections on the bound TCP socket */
|
||||
|
||||
if (listen(listensd, 5) < 0)
|
||||
{
|
||||
message("net_reader: listen failure %d\n", errno);
|
||||
goto errout_with_listensd;
|
||||
}
|
||||
|
||||
/* Connection loop */
|
||||
|
||||
for (;;)
|
||||
{
|
||||
/* Accept only one connection */
|
||||
|
||||
message("net_reader: Accepting new connections on port %d\n", LISTENER_PORT);
|
||||
addrlen = sizeof(struct sockaddr_in);
|
||||
acceptsd = accept(listensd, (struct sockaddr*)&addr, &addrlen);
|
||||
if (acceptsd < 0)
|
||||
{
|
||||
message("net_reader: accept failure: %d\n", errno);
|
||||
continue;
|
||||
}
|
||||
message("net_reader: Connection accepted on sd=%d\n", acceptsd);
|
||||
|
||||
/* Configure to "linger" until all data is sent when the socket is closed */
|
||||
|
||||
#ifdef POLL_HAVE_SOLINGER
|
||||
ling.l_onoff = 1;
|
||||
ling.l_linger = 30; /* timeout is seconds */
|
||||
if (setsockopt(acceptsd, SOL_SOCKET, SO_LINGER, &ling, sizeof(struct linger)) < 0)
|
||||
{
|
||||
message("net_reader: setsockopt SO_LINGER failure: %d\n", errno);
|
||||
goto errout_with_acceptsd;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* Handle incoming messsages on the connection. */
|
||||
|
||||
net_receive(acceptsd);
|
||||
|
||||
message("net_reader: Closing sd=%d\n", acceptsd);
|
||||
close(acceptsd);
|
||||
}
|
||||
|
||||
#ifdef POLL_HAVE_SOLINGER
|
||||
errout_with_acceptsd:
|
||||
close(acceptsd);
|
||||
#endif
|
||||
errout_with_listensd:
|
||||
close(listensd);
|
||||
errout:
|
||||
return NULL;
|
||||
}
|
||||
@@ -110,5 +110,6 @@ extern void *select_listener(pthread_addr_t pvarg);
|
||||
|
||||
#ifdef HAVE_NETPOLL
|
||||
extern void *net_listener(pthread_addr_t pvarg);
|
||||
extern void *net_reader(pthread_addr_t pvarg);
|
||||
#endif
|
||||
#endif /* __EXAMPLES_PIPE_PIPE_H */
|
||||
|
||||
@@ -156,9 +156,15 @@ int user_start(int argc, char *argv[])
|
||||
}
|
||||
|
||||
#ifdef HAVE_NETPOLL
|
||||
#if 0 /* select doesn't work for connections yet */
|
||||
message("user_start: Starting net_listener thread\n");
|
||||
|
||||
ret = pthread_create(&tid3, NULL, net_listener, NULL);
|
||||
#else
|
||||
message("user_start: Starting net_reader thread\n");
|
||||
|
||||
ret = pthread_create(&tid3, NULL, net_reader, NULL);
|
||||
#endif
|
||||
if (ret != 0)
|
||||
{
|
||||
message("user_start: Failed to create net_listener thread: %d\n", ret);
|
||||
|
||||
Reference in New Issue
Block a user