-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuffer.cpp
More file actions
47 lines (43 loc) · 1.1 KB
/
Copy pathBuffer.cpp
File metadata and controls
47 lines (43 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include "Buffer.h"
#include <errno.h>
#include <sys/uio.h>
#include <unistd.h>
/**
* 从fd上读取数据,Poller工作在LT模式
* Buffer缓冲区有大小,但是从fd上读数据的时候,却不知道tcp数据最终的大小
*/
ssize_t Buffer::readFd(int fd, int* savedErrno)
{
char extrabuf[65536] = {0}; // 栈上的内存空间
iovec vec[2];
const size_t writable = writableBytes(); // Buffer底层缓冲区剩余可写空间大小
vec[0].iov_base = begin() + writerIndex_;
vec[0].iov_len = writable;
vec[1].iov_base = extrabuf;
vec[1].iov_len = sizeof(extrabuf);
const int iovcnt = (writable < sizeof(extrabuf)) ? 2 : 1;
const ssize_t n = ::readv(fd, vec, iovcnt);
if (n < 0)
{
*savedErrno = errno;
}
else if (n <= writable)
{
writerIndex_ += n;
}
else
{
writerIndex_ = buffer_.size();
append(extrabuf, n-writable);
}
return n;
}
ssize_t Buffer::writeFd(int fd, int* saveErrno)
{
ssize_t n = ::write(fd, peek(), readableBytes());
if (n < 0)
{
*saveErrno = errno;
}
return n;
}