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