-
Notifications
You must be signed in to change notification settings - Fork 0
/
socket.cc
87 lines (74 loc) · 2.2 KB
/
socket.cc
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
// This is an open source non-commercial project. Dear PVS-Studio, please check it.
// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
#include "socket.hpp"
#include <sys/socket.h>
#include <system_error>
#include <netinet/ip.h>
Socket::Socket()
: File()
, domain(PF_UNSPEC)
, type(0)
, protocol(IPPROTO_IP)
{
}
Socket::Socket(int _domain, int _type, int _protocol)
: File(socket(_domain, _type, _protocol))
, domain(_domain)
, type(_type)
, protocol(_protocol)
{
}
Socket::Socket(int _domain, int _type, int _protocol, int _fd)
: File(_fd)
, domain(_domain)
, type(_type)
, protocol(_protocol)
{
}
Socket::Socket(Socket &&other)
: File(std::move(other))
, domain(PF_UNSPEC)
, type(0)
, protocol(IPPROTO_IP)
{
using std::swap;
swap(domain, other.domain);
swap(type, other.type);
swap(protocol, other.protocol);
}
Socket &Socket::operator =(Socket &&other)
{
using std::swap;
File::operator =(std::move(other));
swap(domain, other.domain);
swap(type, other.type);
swap(protocol, other.protocol);
return *this;
}
void Socket::setsockopt(int level, int optname, const void *optval, size_t optlen)
{
if(!isValid()) {
throw std::system_error(std::make_error_code(std::errc::bad_file_descriptor), __PRETTY_FUNCTION__);
}
if(::setsockopt(get_fd(), level, optname, optval, optlen) != 0) {
throw std::system_error(errno, std::system_category(), __PRETTY_FUNCTION__);
}
}
void Socket::getsockopt(int level, int optname, void *optval, socklen_t *optlen)
{
if(!isValid()) {
throw std::system_error(std::make_error_code(std::errc::bad_file_descriptor), __PRETTY_FUNCTION__);
}
if(::getsockopt(get_fd(), level, optname, optval, optlen) != 0) {
throw std::system_error(errno, std::system_category(), __PRETTY_FUNCTION__);
}
}
void Socket::bind(const sockaddr *addr, socklen_t addrLen)
{
if(!isValid()) {
throw std::system_error(std::make_error_code(std::errc::bad_file_descriptor), __PRETTY_FUNCTION__);
}
if(::bind(get_fd(), addr, addrLen) != 0) {
throw std::system_error(errno, std::system_category(), __PRETTY_FUNCTION__);
}
}