Question

I uses ACE_OS::read_n() to read some data from stdin (ACE_STDIN). My code example:

ACE_Message_Block *head = new ACE_Message_Block(BUFSIZ);
size_t bytes_trans = 0;
ssize_t nbytes = ACE_OS::read_n(
    ACE_STDIN,
    mblk->wr_ptr(),
    mblk->size(),
    &bytes_trans);

If I start my program with command ./a.out <<< "hello", it finds EOF after data and return 0. So, I can detect data received only if check bytes_trans variable.

Is it correct ACE_OS::read_n() behavior?

Était-ce utile?

La solution

Just looked at the ACE doxygen documentation

Receive len bytes into buf from handle (uses the call, which uses the system call on UNIX and the call on Win32). If errors occur, -1 is returned. If EOF occurs, 0 is returned. Whatever data has been read will be returned to the caller through bytes_transferred

Looking at the implementation (note the below code is from ACE 5.6.7... that's the source I have handy)

ssize_t
ACE_OS::read_n (ACE_HANDLE handle,
                void *buf,
                size_t len,
                size_t *bt)
{
  size_t temp;
  size_t &bytes_transferred = bt == 0 ? temp : *bt;
  ssize_t n = 0;

  for (bytes_transferred = 0;
       bytes_transferred < len;
       bytes_transferred += n)
    {
      n = ACE_OS::read (handle,
                        (char *) buf + bytes_transferred,
                        len - bytes_transferred);

      if (n == -1 || n == 0)
        {
          return n;
        }
    }

  return ACE_Utils::truncate_cast<ssize_t> (bytes_transferred);
}

So it seems that it calls multiple times to ACE_OS::read() accumulating the received bytes in the bytes_transferred

If all goes well, the return value will be the same as the contents of the bytes_transferred If read() returns 0 or -1, read_n() will return 0 or -1, and the bytes_transferred content will be the number of bytes read so far.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top