12a18e699SShuo Chen// excerpts from http://code.google.com/p/muduo/
22a18e699SShuo Chen//
32a18e699SShuo Chen// Use of this source code is governed by a BSD-style license
42a18e699SShuo Chen// that can be found in the License file.
52a18e699SShuo Chen//
62a18e699SShuo Chen// Author: Shuo Chen (chenshuo at chenshuo dot com)
72a18e699SShuo Chen
82a18e699SShuo Chen#include "Buffer.h"
92a18e699SShuo Chen#include "SocketsOps.h"
102a18e699SShuo Chen#include "logging/Logging.h"
112a18e699SShuo Chen
122a18e699SShuo Chen#include <errno.h>
132a18e699SShuo Chen#include <memory.h>
142a18e699SShuo Chen#include <sys/uio.h>
152a18e699SShuo Chen
162a18e699SShuo Chenusing namespace muduo;
172a18e699SShuo Chen
182a18e699SShuo Chenssize_t Buffer::readFd(int fd, int* savedErrno)
192a18e699SShuo Chen{
202a18e699SShuo Chen  char extrabuf[65536];
212a18e699SShuo Chen  struct iovec vec[2];
220dd528a5SShuo Chen  const size_t writable = writableBytes();
232a18e699SShuo Chen  vec[0].iov_base = begin()+writerIndex_;
242a18e699SShuo Chen  vec[0].iov_len = writable;
252a18e699SShuo Chen  vec[1].iov_base = extrabuf;
262a18e699SShuo Chen  vec[1].iov_len = sizeof extrabuf;
270dd528a5SShuo Chen  const ssize_t n = readv(fd, vec, 2);
281af3e5baSShuo Chen  if (n < 0) {
292a18e699SShuo Chen    *savedErrno = errno;
301af3e5baSShuo Chen  } else if (implicit_cast<size_t>(n) <= writable) {
312a18e699SShuo Chen    writerIndex_ += n;
321af3e5baSShuo Chen  } else {
332a18e699SShuo Chen    writerIndex_ = buffer_.size();
342a18e699SShuo Chen    append(extrabuf, n - writable);
352a18e699SShuo Chen  }
362a18e699SShuo Chen  return n;
372a18e699SShuo Chen}
382a18e699SShuo Chen
39