-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathudp_socket.cpp
More file actions
147 lines (129 loc) · 3.35 KB
/
Copy pathudp_socket.cpp
File metadata and controls
147 lines (129 loc) · 3.35 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
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
#include "rtp/udp_socket.h"
#include <arpa/inet.h>
#include <errno.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <unistd.h>
#include <cstring>
#include <sstream>
namespace rtp {
std::string SockAddr::ToString() const {
std::ostringstream oss;
oss << ip << ":" << port;
return oss.str();
}
UdpSocket::~UdpSocket() { Close(); }
UdpSocket::UdpSocket(UdpSocket&& other) noexcept {
fd_ = other.fd_;
bound_port_ = other.bound_port_;
bound_ip_ = std::move(other.bound_ip_);
other.fd_ = -1;
other.bound_port_ = 0;
}
UdpSocket& UdpSocket::operator=(UdpSocket&& other) noexcept {
if (this != &other) {
Close();
fd_ = other.fd_;
bound_port_ = other.bound_port_;
bound_ip_ = std::move(other.bound_ip_);
other.fd_ = -1;
other.bound_port_ = 0;
}
return *this;
}
void UdpSocket::Close() {
if (fd_ >= 0) {
::close(fd_);
fd_ = -1;
}
}
bool UdpSocket::Bind(const std::string& ip, uint16_t port) {
Close();
fd_ = ::socket(AF_INET, SOCK_DGRAM, 0);
if (fd_ < 0) {
return false;
}
int yes = 1;
::setsockopt(fd_, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes));
sockaddr_in addr{};
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
if (ip.empty() || ip == "0.0.0.0") {
addr.sin_addr.s_addr = INADDR_ANY;
} else if (::inet_pton(AF_INET, ip.c_str(), &addr.sin_addr) != 1) {
Close();
return false;
}
if (::bind(fd_, reinterpret_cast<sockaddr*>(&addr), sizeof(addr)) < 0) {
Close();
return false;
}
sockaddr_in got{};
socklen_t got_len = sizeof(got);
if (::getsockname(fd_, reinterpret_cast<sockaddr*>(&got), &got_len) == 0) {
bound_port_ = ntohs(got.sin_port);
char buf[INET_ADDRSTRLEN] = {0};
::inet_ntop(AF_INET, &got.sin_addr, buf, sizeof(buf));
bound_ip_ = buf;
} else {
bound_port_ = port;
bound_ip_ = ip.empty() ? "0.0.0.0" : ip;
}
return true;
}
bool UdpSocket::SetNonBlocking(bool nb) {
if (fd_ < 0) {
return false;
}
int flags = ::fcntl(fd_, F_GETFL, 0);
if (flags < 0) {
return false;
}
if (nb) {
flags |= O_NONBLOCK;
} else {
flags &= ~O_NONBLOCK;
}
return ::fcntl(fd_, F_SETFL, flags) == 0;
}
int UdpSocket::RecvFrom(uint8_t* buf, size_t cap, SockAddr* from) {
if (fd_ < 0) {
return -1;
}
sockaddr_in addr{};
socklen_t addr_len = sizeof(addr);
const ssize_t n = ::recvfrom(fd_, buf, cap, 0,
reinterpret_cast<sockaddr*>(&addr), &addr_len);
if (n < 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
return 0;
}
return -1;
}
if (from != nullptr) {
char ip[INET_ADDRSTRLEN] = {0};
::inet_ntop(AF_INET, &addr.sin_addr, ip, sizeof(ip));
from->ip = ip;
from->port = ntohs(addr.sin_port);
}
return static_cast<int>(n);
}
int UdpSocket::SendTo(const uint8_t* data, size_t len, const SockAddr& to) {
if (fd_ < 0) {
return -1;
}
sockaddr_in addr{};
addr.sin_family = AF_INET;
addr.sin_port = htons(to.port);
if (::inet_pton(AF_INET, to.ip.c_str(), &addr.sin_addr) != 1) {
return -1;
}
const ssize_t n = ::sendto(fd_, data, len, 0,
reinterpret_cast<sockaddr*>(&addr), sizeof(addr));
return n < 0 ? -1 : static_cast<int>(n);
}
int UdpSocket::SendTo(const std::vector<uint8_t>& data, const SockAddr& to) {
return SendTo(data.data(), data.size(), to);
}
} // namespace rtp