Read from socket is the same as recv with flags==0

git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@2017 42af7a65-404d-4744-a932-0658087f49c3
This commit is contained in:
patacongo 2009-08-15 15:57:15 +00:00
parent 023de4390c
commit 69544599c3
3 changed files with 125 additions and 81 deletions

View file

@ -41,7 +41,7 @@ AOBJS = $(ASRCS:.S=$(OBJEXT))
CSRCS =
ifeq ($(CONFIG_NFILE_DESCRIPTORS),0)
ifneq ($(CONFIG_NSOCKET_DESCRIPTORS),0)
CSRCS += fs_close.c fs_write.c fs_ioctl.c fs_poll.c fs_select.c
CSRCS += fs_close.c fs_read.c fs_write.c fs_ioctl.c fs_poll.c fs_select.c
endif
else
CSRCS += fs_open.c fs_close.c fs_read.c fs_write.c fs_ioctl.c \

View file

@ -1,7 +1,7 @@
/****************************************************************************
* fs_read.c
*
* Copyright (C) 2007, 2008 Gregory Nutt. All rights reserved.
* Copyright (C) 2007-2009 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* Redistribution and use in source and binary forms, with or without
@ -39,17 +39,21 @@
#include <nuttx/config.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <fcntl.h>
#include <sched.h>
#include <errno.h>
#include "fs_internal.h"
/****************************************************************************
* Global Functions
* Private Functions
****************************************************************************/
ssize_t read(int fd, FAR void *buf, size_t nbytes)
#if CONFIG_NFILE_DESCRIPTORS > 0
static inline ssize_t file_read(int fd, FAR void *buf, size_t nbytes)
{
FAR struct filelist *list;
int ret = -EBADF;
@ -103,4 +107,37 @@ ssize_t read(int fd, FAR void *buf, size_t nbytes)
return ret;
}
#endif
/****************************************************************************
* Public Functions
****************************************************************************/
ssize_t read(int fd, FAR void *buf, size_t nbytes)
{
/* Did we get a valid file descriptor? */
#if CONFIG_NFILE_DESCRIPTORS > 0
if ((unsigned int)fd >= CONFIG_NFILE_DESCRIPTORS)
#endif
{
/* No.. If networking is enabled, read() is the same as recv() with
* the flags parameter set to zero.
*/
#if defined(CONFIG_NET) && CONFIG_NSOCKET_DESCRIPTORS > 0
return recv(fd, buf, nbytes, 0);
#else
/* No networking... it is a bad descriptor in any event */
errno = EBADF;
return ERROR;
#endif
}
/* The descriptor is in a valid range to file descriptor... do the read */
#if CONFIG_NFILE_DESCRIPTORS > 0
return file_read(fd, buf, nbytes);
#endif
}

View file

@ -1,7 +1,7 @@
/****************************************************************************
* fs/fs_write.c
*
* Copyright (C) 2007, 2008 Gregory Nutt. All rights reserved.
* Copyright (C) 2007-2009 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* Redistribution and use in source and binary forms, with or without
@ -51,90 +51,18 @@
#include "fs_internal.h"
/****************************************************************************
* Global Functions
* Private Functions
****************************************************************************/
/***************************************************************************
* Function: write
*
* Description:
* write() writes up to nytes bytes to the file referenced by the file
* descriptor fd from the buffer starting at buf.
*
* Parameters:
* fd file descriptor (or socket descriptor) to write to
* buf Data to write
* nbytes Length of data to write
*
* Returned Value:
* On success, the number of bytes written are returned (zero indicates
* nothing was written). On error, -1 is returned, and errno is set appro
* priately:
*
* EAGAIN
* Non-blocking I/O has been selected using O_NONBLOCK and the write
* would block.
* EBADF
* fd is not a valid file descriptor or is not open for writing.
* EFAULT
* buf is outside your accessible address space.
* EFBIG
* An attempt was made to write a file that exceeds the implementation
* defined maximum file size or the process' file size limit, or
* to write at a position past the maximum allowed offset.
* EINTR
* The call was interrupted by a signal before any data was written.
* EINVAL
* fd is attached to an object which is unsuitable for writing; or
* the file was opened with the O_DIRECT flag, and either the address
* specified in buf, the value specified in count, or the current
* file offset is not suitably aligned.
* EIO
* A low-level I/O error occurred while modifying the inode.
* ENOSPC
* The device containing the file referred to by fd has no room for
* the data.
* EPIPE
* fd is connected to a pipe or socket whose reading end is closed.
* When this happens the writing process will also receive a SIGPIPE
* signal. (Thus, the write return value is seen only if the program
* catches, blocks or ignores this signal.)
*
* Assumptions:
*
********************************************************************************************/
ssize_t write(int fd, FAR const void *buf, size_t nbytes)
{
#if CONFIG_NFILE_DESCRIPTORS > 0
static inline ssize_t file_write(int fd, FAR const void *buf, size_t nbytes)
{
FAR struct filelist *list;
FAR struct file *this_file;
FAR struct inode *inode;
int ret;
#endif
int err;
/* Did we get a valid file descriptor? */
if ((unsigned int)fd >= CONFIG_NFILE_DESCRIPTORS)
{
/* Write to a socket descriptor is equivalent to send with flags == 0 */
#if defined(CONFIG_NET_TCP) && CONFIG_NSOCKET_DESCRIPTORS > 0
if ((unsigned int)fd < (CONFIG_NFILE_DESCRIPTORS+CONFIG_NSOCKET_DESCRIPTORS))
{
return send(fd, buf, nbytes, 0);
}
else
#endif
{
err = EBADF;
goto errout;
}
}
#if CONFIG_NFILE_DESCRIPTORS > 0
/* Get the thread-specific file list */
list = sched_getfiles();
@ -172,10 +100,89 @@ ssize_t write(int fd, FAR const void *buf, size_t nbytes)
}
return ret;
#endif
errout:
*get_errno_ptr() = err;
return ERROR;
}
#endif
/****************************************************************************
* Public Functions
****************************************************************************/
/***************************************************************************
* Function: write
*
* Description:
* write() writes up to nytes bytes to the file referenced by the file
* descriptor fd from the buffer starting at buf.
*
* Parameters:
* fd file descriptor (or socket descriptor) to write to
* buf Data to write
* nbytes Length of data to write
*
* Returned Value:
* On success, the number of bytes written are returned (zero indicates
* nothing was written). On error, -1 is returned, and errno is set appro-
* priately:
*
* EAGAIN
* Non-blocking I/O has been selected using O_NONBLOCK and the write
* would block.
* EBADF
* fd is not a valid file descriptor or is not open for writing.
* EFAULT
* buf is outside your accessible address space.
* EFBIG
* An attempt was made to write a file that exceeds the implementation
* defined maximum file size or the process' file size limit, or
* to write at a position past the maximum allowed offset.
* EINTR
* The call was interrupted by a signal before any data was written.
* EINVAL
* fd is attached to an object which is unsuitable for writing; or
* the file was opened with the O_DIRECT flag, and either the address
* specified in buf, the value specified in count, or the current
* file offset is not suitably aligned.
* EIO
* A low-level I/O error occurred while modifying the inode.
* ENOSPC
* The device containing the file referred to by fd has no room for
* the data.
* EPIPE
* fd is connected to a pipe or socket whose reading end is closed.
* When this happens the writing process will also receive a SIGPIPE
* signal. (Thus, the write return value is seen only if the program
* catches, blocks or ignores this signal.)
*
* Assumptions:
*
********************************************************************************************/
ssize_t write(int fd, FAR const void *buf, size_t nbytes)
{
/* Did we get a valid file descriptor? */
#if CONFIG_NFILE_DESCRIPTORS > 0
if ((unsigned int)fd >= CONFIG_NFILE_DESCRIPTORS)
#endif
{
/* Write to a socket descriptor is equivalent to send with flags == 0 */
#if defined(CONFIG_NET_TCP) && CONFIG_NSOCKET_DESCRIPTORS > 0
return send(fd, buf, nbytes, 0);
#else
errno = EBADF;
return ERROR;
#endif
}
/* The descriptor is in the right range to be a file descriptor... write to the file */
#if CONFIG_NFILE_DESCRIPTORS > 0
return file_write(fd, buf, nbytes);
#endif
}