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