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