1714cd85fSShuo Chen// excerpts from http://code.google.com/p/muduo/
2714cd85fSShuo Chen//
3714cd85fSShuo Chen// Use of this source code is governed by a BSD-style license
4714cd85fSShuo Chen// that can be found in the License file.
5714cd85fSShuo Chen//
6714cd85fSShuo Chen// Author: Shuo Chen (chenshuo at chenshuo dot com)
7714cd85fSShuo Chen
8714cd85fSShuo Chen#include "Buffer.h"
9714cd85fSShuo Chen#include "SocketsOps.h"
10714cd85fSShuo Chen#include "logging/Logging.h"
11714cd85fSShuo Chen
12714cd85fSShuo Chen#include <errno.h>
13714cd85fSShuo Chen#include <memory.h>
14714cd85fSShuo Chen#include <sys/uio.h>
15714cd85fSShuo Chen
16714cd85fSShuo Chenusing namespace muduo;
17714cd85fSShuo Chen
18714cd85fSShuo Chenssize_t Buffer::readFd(int fd, int* savedErrno)
19714cd85fSShuo Chen{
20714cd85fSShuo Chen  char extrabuf[65536];
21714cd85fSShuo Chen  struct iovec vec[2];
220dd528a5SShuo Chen  const size_t writable = writableBytes();
23714cd85fSShuo Chen  vec[0].iov_base = begin()+writerIndex_;
24714cd85fSShuo Chen  vec[0].iov_len = writable;
25714cd85fSShuo Chen  vec[1].iov_base = extrabuf;
26714cd85fSShuo Chen  vec[1].iov_len = sizeof extrabuf;
270dd528a5SShuo Chen  const ssize_t n = readv(fd, vec, 2);
281af3e5baSShuo Chen  if (n < 0) {
29714cd85fSShuo Chen    *savedErrno = errno;
301af3e5baSShuo Chen  } else if (implicit_cast<size_t>(n) <= writable) {
31714cd85fSShuo Chen    writerIndex_ += n;
321af3e5baSShuo Chen  } else {
33714cd85fSShuo Chen    writerIndex_ = buffer_.size();
34714cd85fSShuo Chen    append(extrabuf, n - writable);
35714cd85fSShuo Chen  }
36714cd85fSShuo Chen  return n;
37714cd85fSShuo Chen}
38714cd85fSShuo Chen
39