Buffer.cc revision 2a18e699
12a18e699SShuo Chen// excerpts from http://code.google.com/p/muduo/ 22a18e699SShuo Chen// 32a18e699SShuo Chen// Use of this source code is governed by a BSD-style license 42a18e699SShuo Chen// that can be found in the License file. 52a18e699SShuo Chen// 62a18e699SShuo Chen// Author: Shuo Chen (chenshuo at chenshuo dot com) 72a18e699SShuo Chen 82a18e699SShuo Chen#include "Buffer.h" 92a18e699SShuo Chen#include "SocketsOps.h" 102a18e699SShuo Chen#include "logging/Logging.h" 112a18e699SShuo Chen 122a18e699SShuo Chen#include <errno.h> 132a18e699SShuo Chen#include <memory.h> 142a18e699SShuo Chen#include <sys/uio.h> 152a18e699SShuo Chen 162a18e699SShuo Chenusing namespace muduo; 172a18e699SShuo Chen 182a18e699SShuo Chenssize_t Buffer::readFd(int fd, int* savedErrno) 192a18e699SShuo Chen{ 202a18e699SShuo Chen char extrabuf[65536]; 212a18e699SShuo Chen struct iovec vec[2]; 222a18e699SShuo Chen size_t writable = writableBytes(); 232a18e699SShuo Chen vec[0].iov_base = begin()+writerIndex_; 242a18e699SShuo Chen vec[0].iov_len = writable; 252a18e699SShuo Chen vec[1].iov_base = extrabuf; 262a18e699SShuo Chen vec[1].iov_len = sizeof extrabuf; 272a18e699SShuo Chen ssize_t n = readv(fd, vec, 2); 282a18e699SShuo Chen if (n < 0) 292a18e699SShuo Chen { 302a18e699SShuo Chen *savedErrno = errno; 312a18e699SShuo Chen } 322a18e699SShuo Chen else if (implicit_cast<size_t>(n) <= writable) 332a18e699SShuo Chen { 342a18e699SShuo Chen writerIndex_ += n; 352a18e699SShuo Chen } 362a18e699SShuo Chen else 372a18e699SShuo Chen { 382a18e699SShuo Chen writerIndex_ = buffer_.size(); 392a18e699SShuo Chen append(extrabuf, n - writable); 402a18e699SShuo Chen } 412a18e699SShuo Chen return n; 422a18e699SShuo Chen} 432a18e699SShuo Chen 44