-
Notifications
You must be signed in to change notification settings - Fork 2
/
file_descriptor.cpp
150 lines (118 loc) · 2.99 KB
/
file_descriptor.cpp
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include "file_descriptor.h"
#include <sstream>
#include <stdexcept>
#include <errno.h>
#include <sys/socket.h>
#include "throw_error.h"
weak_file_descriptor::weak_file_descriptor() noexcept
: fd(-1)
{}
weak_file_descriptor::weak_file_descriptor(int fd) noexcept
: fd(fd)
{}
weak_file_descriptor::weak_file_descriptor(file_descriptor const& handle) noexcept
: fd(handle.getfd())
{}
weak_file_descriptor::weak_file_descriptor(weak_file_descriptor const& other) noexcept
: fd(other.fd)
{}
weak_file_descriptor& weak_file_descriptor::operator=(weak_file_descriptor const& other) noexcept
{
fd = other.getfd();
return *this;
}
void weak_file_descriptor::close() noexcept
{
if (fd == -1)
return;
int r = ::close(fd);
assert(r == 0);
fd = -1;
}
int weak_file_descriptor::getfd() const noexcept
{
return fd;
}
void swap(weak_file_descriptor& a, weak_file_descriptor& b) noexcept
{
std::swap(a.fd, b.fd);
}
file_descriptor::file_descriptor() noexcept
{}
file_descriptor::file_descriptor(int fd) noexcept
: weak(fd)
{}
file_descriptor::file_descriptor(file_descriptor&& other) noexcept
: weak(other.release())
{}
file_descriptor::~file_descriptor() noexcept
{
this->close();
}
file_descriptor& file_descriptor::operator=(file_descriptor&& rhs) noexcept
{
weak& that = *this;
that = rhs.release();
return *this;
}
file_descriptor::weak file_descriptor::release() noexcept
{
weak& that = *this;
weak tmp = that;
that = weak{};
return tmp;
}
void file_descriptor::reset(int new_fd) noexcept
{
weak& that = *this;
this->close();
that = weak{new_fd};
}
void swap(file_descriptor& a, file_descriptor& b) noexcept
{
swap(static_cast<weak_file_descriptor&>(a), static_cast<weak_file_descriptor&>(b));
}
size_t read_some(weak_file_descriptor fdc, void* data, size_t size)
{
int fd = fdc.getfd();
assert(fd != -1);
ssize_t res = ::read(fd, data, size);
if (res == -1)
{
int err = errno;
if (err == EAGAIN)
return 0;
throw_error(err, "read()");
}
assert(res >= 0);
size_t bytes_read = static_cast<size_t>(res);
assert(bytes_read <= size);
return bytes_read;
}
size_t write_some(weak_file_descriptor fdc, void const* data, std::size_t size)
{
int fd = fdc.getfd();
assert(fd != -1);
ssize_t res = ::send(fd, data, size, MSG_NOSIGNAL);
if (res == -1)
{
int err = errno;
if (err == EAGAIN || err == ECONNRESET || err == EPIPE)
return 0;
throw_error(err, "write()");
}
assert(res >= 0);
size_t written = static_cast<size_t>(res);
assert(written <= size);
return written;
}
void write(weak_file_descriptor fdc, const void *data, std::size_t size)
{
size_t written = write_some(fdc, data, size);
if (written != size)
{
std::stringstream ss;
ss << "incomplete write, requested: " << size << ", written: " << written;
throw std::runtime_error(ss.str());
}
}