-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathunique_socket.cpp
More file actions
51 lines (43 loc) · 760 Bytes
/
unique_socket.cpp
File metadata and controls
51 lines (43 loc) · 760 Bytes
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
#include "unique_socket.h"
#include <unistd.h>
unique_socket::unique_socket(int fd): _fd(fd) {}
unique_socket::unique_socket(unique_socket &&src)
{
_fd = src._fd;
src._fd = -1;
}
unique_socket &unique_socket::operator=(unique_socket &&src)
{
if (this != &src)
{
close();
_fd = src._fd;
src._fd = -1;
}
return *this;
}
unique_socket::~unique_socket()
{
close();
}
unique_socket &unique_socket::operator=(int fd)
{
close();
_fd = fd;
return *this;
}
int unique_socket::handle() const noexcept
{
return _fd;
}
unique_socket::operator bool() const noexcept
{
return _fd >= 0;
}
void unique_socket::close() noexcept
{
if (_fd < 0)
return;
::close(_fd);
_fd = -1;
}