-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontrol_server.cpp
More file actions
231 lines (207 loc) · 5.35 KB
/
Copy pathcontrol_server.cpp
File metadata and controls
231 lines (207 loc) · 5.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#include "rtp/control_server.h"
#include <arpa/inet.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <poll.h>
#include <sys/socket.h>
#include <unistd.h>
#include <sstream>
#include <vector>
namespace rtp {
namespace {
std::vector<std::string> Split(const std::string& s) {
std::vector<std::string> out;
std::istringstream iss(s);
std::string tok;
while (iss >> tok) {
out.push_back(tok);
}
return out;
}
uint32_t ParseU32(const std::string& s, bool* ok) {
try {
size_t idx = 0;
const unsigned long v = std::stoul(s, &idx, 10);
if (idx != s.size() || v > 0xffffffffUL) {
*ok = false;
return 0;
}
*ok = true;
return static_cast<uint32_t>(v);
} catch (...) {
*ok = false;
return 0;
}
}
} // namespace
bool ControlServer::Start(const std::string& ip, uint16_t port) {
Stop();
listen_fd_ = ::socket(AF_INET, SOCK_STREAM, 0);
if (listen_fd_ < 0) {
return false;
}
int yes = 1;
::setsockopt(listen_fd_, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes));
int flags = ::fcntl(listen_fd_, F_GETFL, 0);
if (flags >= 0) {
::fcntl(listen_fd_, F_SETFL, flags | O_NONBLOCK);
}
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) {
Stop();
return false;
}
if (::bind(listen_fd_, reinterpret_cast<sockaddr*>(&addr), sizeof(addr)) < 0) {
Stop();
return false;
}
if (::listen(listen_fd_, 8) < 0) {
Stop();
return false;
}
return true;
}
void ControlServer::Stop() {
if (client_fd_ >= 0) {
::close(client_fd_);
client_fd_ = -1;
}
if (listen_fd_ >= 0) {
::close(listen_fd_);
listen_fd_ = -1;
}
client_buf_.clear();
}
void ControlServer::ServeOnce(int timeout_ms) {
if (listen_fd_ < 0) {
return;
}
pollfd pfds[2];
nfds_t nfd = 1;
pfds[0].fd = listen_fd_;
pfds[0].events = POLLIN;
pfds[0].revents = 0;
if (client_fd_ >= 0) {
pfds[1].fd = client_fd_;
pfds[1].events = POLLIN;
pfds[1].revents = 0;
nfd = 2;
}
if (::poll(pfds, nfd, timeout_ms) <= 0) {
return;
}
if (pfds[0].revents & POLLIN) {
sockaddr_in peer{};
socklen_t plen = sizeof(peer);
const int fd = ::accept(listen_fd_, reinterpret_cast<sockaddr*>(&peer), &plen);
if (fd >= 0) {
if (client_fd_ >= 0) {
::close(client_fd_);
}
client_fd_ = fd;
client_buf_.clear();
int flags = ::fcntl(client_fd_, F_GETFL, 0);
if (flags >= 0) {
::fcntl(client_fd_, F_SETFL, flags | O_NONBLOCK);
}
}
}
if (nfd == 2 && (pfds[1].revents & (POLLIN | POLLHUP | POLLERR))) {
char tmp[512];
const ssize_t n = ::recv(client_fd_, tmp, sizeof(tmp), 0);
if (n <= 0) {
::close(client_fd_);
client_fd_ = -1;
client_buf_.clear();
return;
}
client_buf_.append(tmp, static_cast<size_t>(n));
size_t pos;
while ((pos = client_buf_.find('\n')) != std::string::npos) {
std::string line = client_buf_.substr(0, pos);
client_buf_.erase(0, pos + 1);
if (!line.empty() && line.back() == '\r') {
line.pop_back();
}
if (line.empty()) {
continue;
}
const std::string reply = HandleLine(line) + "\n";
::send(client_fd_, reply.data(), reply.size(), 0);
}
}
}
std::string ControlServer::HandleLine(const std::string& line) {
const auto t = Split(line);
if (t.empty()) {
return "ERR empty";
}
const std::string& cmd = t[0];
if (cmd == "CREATE_ROOM") {
if (t.size() != 2) {
return "ERR usage: CREATE_ROOM <room_id>";
}
return fwd_->AddRoom(t[1]) ? "OK" : "ERR create_room";
}
if (cmd == "DELETE_ROOM") {
if (t.size() != 2) {
return "ERR usage: DELETE_ROOM <room_id>";
}
return fwd_->RemoveRoom(t[1]) ? "OK" : "ERR not_found";
}
if (cmd == "ADD_PARTICIPANT") {
// ADD_PARTICIPANT room pid ingest egress_ip egress_port ssrc send_pt recv_pt
if (t.size() != 9) {
return "ERR usage: ADD_PARTICIPANT <room> <pid> <ingest_port> "
"<egress_ip> <egress_port> <ssrc> <send_pt> <recv_pt>";
}
bool ok = true;
ParticipantMedia p;
p.id = t[2];
p.ingest_port = static_cast<uint16_t>(ParseU32(t[3], &ok));
if (!ok) {
return "ERR ingest_port";
}
p.egress.ip = t[4];
p.egress.port = static_cast<uint16_t>(ParseU32(t[5], &ok));
if (!ok) {
return "ERR egress_port";
}
p.ssrc = ParseU32(t[6], &ok);
if (!ok) {
return "ERR ssrc";
}
const uint32_t send_pt = ParseU32(t[7], &ok);
if (!ok || send_pt > 127) {
return "ERR send_pt";
}
const uint32_t recv_pt = ParseU32(t[8], &ok);
if (!ok || recv_pt > 127) {
return "ERR recv_pt";
}
p.send_pt = static_cast<uint8_t>(send_pt);
p.recv_pt = static_cast<uint8_t>(recv_pt);
if (!fwd_->AddParticipant(t[1], p)) {
return "ERR add_participant";
}
return "OK";
}
if (cmd == "REMOVE_PARTICIPANT") {
if (t.size() != 3) {
return "ERR usage: REMOVE_PARTICIPANT <room> <pid>";
}
return fwd_->RemoveParticipant(t[1], t[2]) ? "OK" : "ERR not_found";
}
if (cmd == "STATS" || cmd == "LIST") {
return "OK " + fwd_->Dump();
}
if (cmd == "PING") {
return "OK PONG";
}
return "ERR unknown_cmd " + cmd;
}
} // namespace rtp