140161064SShuo Chen// excerpts from http://code.google.com/p/muduo/
240161064SShuo Chen//
340161064SShuo Chen// Use of this source code is governed by a BSD-style license
440161064SShuo Chen// that can be found in the License file.
540161064SShuo Chen//
640161064SShuo Chen// Author: Shuo Chen (chenshuo at chenshuo dot com)
740161064SShuo Chen
840161064SShuo Chen#include "Buffer.h"
940161064SShuo Chen#include "SocketsOps.h"
1040161064SShuo Chen#include "logging/Logging.h"
1140161064SShuo Chen
1240161064SShuo Chen#include <errno.h>
1340161064SShuo Chen#include <memory.h>
1440161064SShuo Chen#include <sys/uio.h>
1540161064SShuo Chen
1640161064SShuo Chenusing namespace muduo;
1740161064SShuo Chen
1840161064SShuo Chenssize_t Buffer::readFd(int fd, int* savedErrno)
1940161064SShuo Chen{
2040161064SShuo Chen  char extrabuf[65536];
2140161064SShuo Chen  struct iovec vec[2];
2240161064SShuo Chen  const size_t writable = writableBytes();
2340161064SShuo Chen  vec[0].iov_base = begin()+writerIndex_;
2440161064SShuo Chen  vec[0].iov_len = writable;
2540161064SShuo Chen  vec[1].iov_base = extrabuf;
2640161064SShuo Chen  vec[1].iov_len = sizeof extrabuf;
2740161064SShuo Chen  const ssize_t n = readv(fd, vec, 2);
2840161064SShuo Chen  if (n < 0) {
2940161064SShuo Chen    *savedErrno = errno;
3040161064SShuo Chen  } else if (implicit_cast<size_t>(n) <= writable) {
3140161064SShuo Chen    writerIndex_ += n;
3240161064SShuo Chen  } else {
3340161064SShuo Chen    writerIndex_ = buffer_.size();
3440161064SShuo Chen    append(extrabuf, n - writable);
3540161064SShuo Chen  }
3640161064SShuo Chen  return n;
3740161064SShuo Chen}
3840161064SShuo Chen
39