1e254a845SShuo Chen// excerpts from http://code.google.com/p/muduo/
2e254a845SShuo Chen//
3e254a845SShuo Chen// Use of this source code is governed by a BSD-style license
4e254a845SShuo Chen// that can be found in the License file.
5e254a845SShuo Chen//
6e254a845SShuo Chen// Author: Shuo Chen (chenshuo at chenshuo dot com)
7e254a845SShuo Chen
8e254a845SShuo Chen#include "Buffer.h"
9e254a845SShuo Chen#include "SocketsOps.h"
10e254a845SShuo Chen#include "logging/Logging.h"
11e254a845SShuo Chen
12e254a845SShuo Chen#include <errno.h>
13e254a845SShuo Chen#include <memory.h>
14e254a845SShuo Chen#include <sys/uio.h>
15e254a845SShuo Chen
16e254a845SShuo Chenusing namespace muduo;
17e254a845SShuo Chen
18e254a845SShuo Chenssize_t Buffer::readFd(int fd, int* savedErrno)
19e254a845SShuo Chen{
20e254a845SShuo Chen  char extrabuf[65536];
21e254a845SShuo Chen  struct iovec vec[2];
220dd528a5SShuo Chen  const size_t writable = writableBytes();
23e254a845SShuo Chen  vec[0].iov_base = begin()+writerIndex_;
24e254a845SShuo Chen  vec[0].iov_len = writable;
25e254a845SShuo Chen  vec[1].iov_base = extrabuf;
26e254a845SShuo Chen  vec[1].iov_len = sizeof extrabuf;
270dd528a5SShuo Chen  const ssize_t n = readv(fd, vec, 2);
28e254a845SShuo Chen  if (n < 0) {
29e254a845SShuo Chen    *savedErrno = errno;
30e254a845SShuo Chen  } else if (implicit_cast<size_t>(n) <= writable) {
31e254a845SShuo Chen    writerIndex_ += n;
32e254a845SShuo Chen  } else {
33e254a845SShuo Chen    writerIndex_ = buffer_.size();
34e254a845SShuo Chen    append(extrabuf, n - writable);
35e254a845SShuo Chen  }
36e254a845SShuo Chen  return n;
37e254a845SShuo Chen}
38e254a845SShuo Chen
39