Buffer.cc revision 714cd85f
1714cd85fSShuo Chen// excerpts from http://code.google.com/p/muduo/
2714cd85fSShuo Chen//
3714cd85fSShuo Chen// Use of this source code is governed by a BSD-style license
4714cd85fSShuo Chen// that can be found in the License file.
5714cd85fSShuo Chen//
6714cd85fSShuo Chen// Author: Shuo Chen (chenshuo at chenshuo dot com)
7714cd85fSShuo Chen
8714cd85fSShuo Chen#include "Buffer.h"
9714cd85fSShuo Chen#include "SocketsOps.h"
10714cd85fSShuo Chen#include "logging/Logging.h"
11714cd85fSShuo Chen
12714cd85fSShuo Chen#include <errno.h>
13714cd85fSShuo Chen#include <memory.h>
14714cd85fSShuo Chen#include <sys/uio.h>
15714cd85fSShuo Chen
16714cd85fSShuo Chenusing namespace muduo;
17714cd85fSShuo Chen
18714cd85fSShuo Chenssize_t Buffer::readFd(int fd, int* savedErrno)
19714cd85fSShuo Chen{
20714cd85fSShuo Chen  char extrabuf[65536];
21714cd85fSShuo Chen  struct iovec vec[2];
22714cd85fSShuo Chen  size_t writable = writableBytes();
23714cd85fSShuo Chen  vec[0].iov_base = begin()+writerIndex_;
24714cd85fSShuo Chen  vec[0].iov_len = writable;
25714cd85fSShuo Chen  vec[1].iov_base = extrabuf;
26714cd85fSShuo Chen  vec[1].iov_len = sizeof extrabuf;
27714cd85fSShuo Chen  ssize_t n = readv(fd, vec, 2);
28714cd85fSShuo Chen  if (n < 0)
29714cd85fSShuo Chen  {
30714cd85fSShuo Chen    *savedErrno = errno;
31714cd85fSShuo Chen  }
32714cd85fSShuo Chen  else if (implicit_cast<size_t>(n) <= writable)
33714cd85fSShuo Chen  {
34714cd85fSShuo Chen    writerIndex_ += n;
35714cd85fSShuo Chen  }
36714cd85fSShuo Chen  else
37714cd85fSShuo Chen  {
38714cd85fSShuo Chen    writerIndex_ = buffer_.size();
39714cd85fSShuo Chen    append(extrabuf, n - writable);
40714cd85fSShuo Chen  }
41714cd85fSShuo Chen  return n;
42714cd85fSShuo Chen}
43714cd85fSShuo Chen
44