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