Buffer.cc revision 354280cf
1354280cfSShuo Chen// excerpts from http://code.google.com/p/muduo/ 2354280cfSShuo Chen// 3354280cfSShuo Chen// Use of this source code is governed by a BSD-style license 4354280cfSShuo Chen// that can be found in the License file. 5354280cfSShuo Chen// 6354280cfSShuo Chen// Author: Shuo Chen (chenshuo at chenshuo dot com) 7354280cfSShuo Chen 8354280cfSShuo Chen#include "Buffer.h" 9354280cfSShuo Chen#include "SocketsOps.h" 10354280cfSShuo Chen#include "logging/Logging.h" 11354280cfSShuo Chen 12354280cfSShuo Chen#include <errno.h> 13354280cfSShuo Chen#include <memory.h> 14354280cfSShuo Chen#include <sys/uio.h> 15354280cfSShuo Chen 16354280cfSShuo Chenusing namespace muduo; 17354280cfSShuo Chen 18354280cfSShuo Chenssize_t Buffer::readFd(int fd, int* savedErrno) 19354280cfSShuo Chen{ 20354280cfSShuo Chen char extrabuf[65536]; 21354280cfSShuo Chen struct iovec vec[2]; 22354280cfSShuo Chen const size_t writable = writableBytes(); 23354280cfSShuo Chen vec[0].iov_base = begin()+writerIndex_; 24354280cfSShuo Chen vec[0].iov_len = writable; 25354280cfSShuo Chen vec[1].iov_base = extrabuf; 26354280cfSShuo Chen vec[1].iov_len = sizeof extrabuf; 27354280cfSShuo Chen const ssize_t n = readv(fd, vec, 2); 28354280cfSShuo Chen if (n < 0) { 29354280cfSShuo Chen *savedErrno = errno; 30354280cfSShuo Chen } else if (implicit_cast<size_t>(n) <= writable) { 31354280cfSShuo Chen writerIndex_ += n; 32354280cfSShuo Chen } else { 33354280cfSShuo Chen writerIndex_ = buffer_.size(); 34354280cfSShuo Chen append(extrabuf, n - writable); 35354280cfSShuo Chen } 36354280cfSShuo Chen return n; 37354280cfSShuo Chen} 38354280cfSShuo Chen 39