提交 5ebf8759 authored 作者: Iñaki Baz Castillo's avatar Iñaki Baz Castillo

More C++ syntax work

上级 5ba4023b
...@@ -49,26 +49,24 @@ namespace RTC ...@@ -49,26 +49,24 @@ namespace RTC
if (IsClosing()) if (IsClosing())
return; return;
size_t data_len = this->bufferDataLen - this->frameStart; size_t dataLen = this->bufferDataLen - this->frameStart;
size_t packet_len; size_t packetLen;
if (data_len >= 2) if (dataLen >= 2)
packet_len = (size_t)Utils::Byte::Get2Bytes(this->buffer + this->frameStart, 0); packetLen = (size_t)Utils::Byte::Get2Bytes(this->buffer + this->frameStart, 0);
// We have packet_len bytes. // We have packetLen bytes.
if (data_len >= 2 && data_len >= 2 + packet_len) if (dataLen >= 2 && dataLen >= 2 + packetLen)
{ {
const uint8_t* packet = this->buffer + this->frameStart + 2; const uint8_t* packet = this->buffer + this->frameStart + 2;
// Notify the listener. // Notify the listener.
if (packet_len != 0) if (packetLen != 0)
{ this->listener->onPacketRecv(this, packet, packetLen);
this->listener->onPacketRecv(this, packet, packet_len);
}
// If there is no more space available in the buffer and that is because // If there is no more space available in the buffer and that is because
// the latest parsed frame filled it, then empty the full buffer. // the latest parsed frame filled it, then empty the full buffer.
if ((this->frameStart + 2 + packet_len) == this->bufferSize) if ((this->frameStart + 2 + packetLen) == this->bufferSize)
{ {
MS_DEBUG_DEV("no more space in the buffer, emptying the buffer data"); MS_DEBUG_DEV("no more space in the buffer, emptying the buffer data");
...@@ -79,7 +77,7 @@ namespace RTC ...@@ -79,7 +77,7 @@ namespace RTC
// frame to the next position after the parsed frame. // frame to the next position after the parsed frame.
else else
{ {
this->frameStart += 2 + packet_len; this->frameStart += 2 + packetLen;
} }
// If there is more data in the buffer after the parsed frame then // If there is more data in the buffer after the parsed frame then
...@@ -106,9 +104,11 @@ namespace RTC ...@@ -106,9 +104,11 @@ namespace RTC
// the buffer, so move the frame to the position 0. // the buffer, so move the frame to the position 0.
if (this->frameStart != 0) if (this->frameStart != 0)
{ {
MS_DEBUG_DEV("no more space in the buffer, moving parsed bytes to the beginning of the buffer and wait for more data"); MS_DEBUG_DEV("no more space in the buffer, moving parsed bytes to the beginning of "
"the buffer and wait for more data");
std::memmove(this->buffer, this->buffer + this->frameStart, this->bufferSize - this->frameStart); std::memmove(this->buffer, this->buffer + this->frameStart,
this->bufferSize - this->frameStart);
this->bufferDataLen = this->bufferSize - this->frameStart; this->bufferDataLen = this->bufferSize - this->frameStart;
this->frameStart = 0; this->frameStart = 0;
} }
...@@ -140,10 +140,10 @@ namespace RTC ...@@ -140,10 +140,10 @@ namespace RTC
// Write according to Framing RFC 4571. // Write according to Framing RFC 4571.
uint8_t frame_len[2]; uint8_t frameLen[2];
Utils::Byte::Set2Bytes(frame_len, 0, len); Utils::Byte::Set2Bytes(frameLen, 0, len);
Write(frame_len, 2, data, len); Write(frameLen, 2, data, len);
} }
} }
...@@ -9,9 +9,6 @@ ...@@ -9,9 +9,6 @@
#include "Logger.hpp" #include "Logger.hpp"
#include <string> #include <string>
#define MAX_BIND_ATTEMPTS 20
#define MAX_TCP_CONNECTIONS_PER_SERVER 10
/* Static methods for UV callbacks. */ /* Static methods for UV callbacks. */
static inline static inline
...@@ -22,6 +19,11 @@ void on_error_close(uv_handle_t* handle) ...@@ -22,6 +19,11 @@ void on_error_close(uv_handle_t* handle)
namespace RTC namespace RTC
{ {
/* Static. */
static constexpr uint16_t MaxBindAttempts = 20;
static constexpr size_t MaxTcpConnectionsPerServer = 10;
/* Class variables. */ /* Class variables. */
struct sockaddr_storage TcpServer::sockaddrStorageIPv4; struct sockaddr_storage TcpServer::sockaddrStorageIPv4;
...@@ -41,14 +43,18 @@ namespace RTC ...@@ -41,14 +43,18 @@ namespace RTC
if (!Settings::configuration.rtcIPv4.empty()) if (!Settings::configuration.rtcIPv4.empty())
{ {
err = uv_ip4_addr(Settings::configuration.rtcIPv4.c_str(), 0, (struct sockaddr_in*)&RTC::TcpServer::sockaddrStorageIPv4); err = uv_ip4_addr(Settings::configuration.rtcIPv4.c_str(), 0,
(struct sockaddr_in*)&RTC::TcpServer::sockaddrStorageIPv4);
if (err) if (err)
MS_THROW_ERROR("uv_ipv4_addr() failed: %s", uv_strerror(err)); MS_THROW_ERROR("uv_ipv4_addr() failed: %s", uv_strerror(err));
} }
if (!Settings::configuration.rtcIPv6.empty()) if (!Settings::configuration.rtcIPv6.empty())
{ {
err = uv_ip6_addr(Settings::configuration.rtcIPv6.c_str(), 0, (struct sockaddr_in6*)&RTC::TcpServer::sockaddrStorageIPv6); err = uv_ip6_addr(Settings::configuration.rtcIPv6.c_str(), 0,
(struct sockaddr_in6*)&RTC::TcpServer::sockaddrStorageIPv6);
if (err) if (err)
MS_THROW_ERROR("uv_ipv6_addr() failed: %s", uv_strerror(err)); MS_THROW_ERROR("uv_ipv6_addr() failed: %s", uv_strerror(err));
} }
...@@ -57,6 +63,7 @@ namespace RTC ...@@ -57,6 +63,7 @@ namespace RTC
TcpServer::maxPort = Settings::configuration.rtcMaxPort; TcpServer::maxPort = Settings::configuration.rtcMaxPort;
uint16_t i = RTC::TcpServer::minPort; uint16_t i = RTC::TcpServer::minPort;
do do
{ {
RTC::TcpServer::availableIPv4Ports[i] = true; RTC::TcpServer::availableIPv4Ports[i] = true;
...@@ -65,38 +72,38 @@ namespace RTC ...@@ -65,38 +72,38 @@ namespace RTC
while (i++ != RTC::TcpServer::maxPort); while (i++ != RTC::TcpServer::maxPort);
} }
uv_tcp_t* TcpServer::GetRandomPort(int address_family) uv_tcp_t* TcpServer::GetRandomPort(int addressFamily)
{ {
MS_TRACE(); MS_TRACE();
if (address_family == AF_INET && !Settings::configuration.hasIPv4) if (addressFamily == AF_INET && !Settings::configuration.hasIPv4)
MS_THROW_ERROR("IPv4 family not available for RTC"); MS_THROW_ERROR("IPv4 family not available for RTC");
else if (address_family == AF_INET6 && !Settings::configuration.hasIPv6) else if (addressFamily == AF_INET6 && !Settings::configuration.hasIPv6)
MS_THROW_ERROR("IPv6 family not available for RTC"); MS_THROW_ERROR("IPv6 family not available for RTC");
int err; int err;
uv_tcp_t* uvHandle = nullptr; uv_tcp_t* uvHandle = nullptr;
struct sockaddr_storage bind_addr; struct sockaddr_storage bindAddr;
const char* listen_ip; const char* listenIp;
uint16_t initial_port; uint16_t initialPort;
uint16_t iterating_port; uint16_t iteratingPort;
uint16_t attempt = 0; uint16_t attempt = 0;
uint16_t bind_attempt = 0; uint16_t bindAttempt = 0;
int flags = 0; int flags = 0;
std::unordered_map<uint16_t, bool>* available_ports; std::unordered_map<uint16_t, bool>* availablePorts;
switch (address_family) switch (addressFamily)
{ {
case AF_INET: case AF_INET:
available_ports = &RTC::TcpServer::availableIPv4Ports; availablePorts = &RTC::TcpServer::availableIPv4Ports;
bind_addr = RTC::TcpServer::sockaddrStorageIPv4; bindAddr = RTC::TcpServer::sockaddrStorageIPv4;
listen_ip = Settings::configuration.rtcIPv4.c_str(); listenIp = Settings::configuration.rtcIPv4.c_str();
break; break;
case AF_INET6: case AF_INET6:
available_ports = &RTC::TcpServer::availableIPv6Ports; availablePorts = &RTC::TcpServer::availableIPv6Ports;
bind_addr = RTC::TcpServer::sockaddrStorageIPv6; bindAddr = RTC::TcpServer::sockaddrStorageIPv6;
listen_ip = Settings::configuration.rtcIPv6.c_str(); listenIp = Settings::configuration.rtcIPv6.c_str();
// Don't also bind into IPv4 when listening in IPv6. // Don't also bind into IPv4 when listening in IPv6.
flags |= UV_TCP_IPV6ONLY; flags |= UV_TCP_IPV6ONLY;
break; break;
...@@ -107,9 +114,10 @@ namespace RTC ...@@ -107,9 +114,10 @@ namespace RTC
} }
// Choose a random first port to start from. // Choose a random first port to start from.
initial_port = (uint16_t)Utils::Crypto::GetRandomUInt((uint32_t)RTC::TcpServer::minPort, (uint32_t)RTC::TcpServer::maxPort); initialPort = (uint16_t)Utils::Crypto::GetRandomUInt((uint32_t)RTC::TcpServer::minPort,
(uint32_t)RTC::TcpServer::maxPort);
iterating_port = initial_port; iteratingPort = initialPort;
// Iterate the RTC TCP ports until getting one available. // Iterate the RTC TCP ports until getting one available.
// Fail also after bind() fails N times in theorically available ports. // Fail also after bind() fails N times in theorically available ports.
...@@ -118,19 +126,20 @@ namespace RTC ...@@ -118,19 +126,20 @@ namespace RTC
++attempt; ++attempt;
// Increase the iterate port) within the range of RTC TCP ports. // Increase the iterate port) within the range of RTC TCP ports.
if (iterating_port < RTC::TcpServer::maxPort) if (iteratingPort < RTC::TcpServer::maxPort)
iterating_port += 1; iteratingPort += 1;
else else
iterating_port = RTC::TcpServer::minPort; iteratingPort = RTC::TcpServer::minPort;
// Check whether the chosen port is available. // Check whether the chosen port is available.
if (!(*available_ports)[iterating_port]) if (!(*availablePorts)[iteratingPort])
{ {
MS_DEBUG_DEV("port in use, trying again [port:%" PRIu16 ", attempt:%" PRIu16 "]", iterating_port, attempt); MS_DEBUG_DEV("port in use, trying again [port:%" PRIu16 ", attempt:%" PRIu16 "]",
iteratingPort, attempt);
// If we have tried all the ports in the range raise an error. // If we have tried all the ports in the range raise an error.
if (iterating_port == initial_port) if (iteratingPort == initialPort)
MS_THROW_ERROR("no more available ports for IP '%s'", listen_ip); MS_THROW_ERROR("no more available ports for IP '%s'", listenIp);
continue; continue;
} }
...@@ -139,18 +148,18 @@ namespace RTC ...@@ -139,18 +148,18 @@ namespace RTC
// Now let's check whether no other process is listening into it. // Now let's check whether no other process is listening into it.
// Set the chosen port into the sockaddr struct(s). // Set the chosen port into the sockaddr struct(s).
switch (address_family) switch (addressFamily)
{ {
case AF_INET: case AF_INET:
((struct sockaddr_in*)&bind_addr)->sin_port = htons(iterating_port); ((struct sockaddr_in*)&bindAddr)->sin_port = htons(iteratingPort);
break; break;
case AF_INET6: case AF_INET6:
((struct sockaddr_in6*)&bind_addr)->sin6_port = htons(iterating_port); ((struct sockaddr_in6*)&bindAddr)->sin6_port = htons(iteratingPort);
break; break;
} }
// Try to bind on it. // Try to bind on it.
++bind_attempt; ++bindAttempt;
uvHandle = new uv_tcp_t(); uvHandle = new uv_tcp_t();
...@@ -161,10 +170,11 @@ namespace RTC ...@@ -161,10 +170,11 @@ namespace RTC
MS_THROW_ERROR("uv_tcp_init() failed: %s", uv_strerror(err)); MS_THROW_ERROR("uv_tcp_init() failed: %s", uv_strerror(err));
} }
err = uv_tcp_bind(uvHandle, (const struct sockaddr*)&bind_addr, flags); err = uv_tcp_bind(uvHandle, (const struct sockaddr*)&bindAddr, flags);
if (err) if (err)
{ {
MS_WARN_DEV("uv_tcp_bind() failed [port:%" PRIu16 ", attempt:%" PRIu16 "]: %s", attempt, iterating_port, uv_strerror(err)); MS_WARN_DEV("uv_tcp_bind() failed [port:%" PRIu16 ", attempt:%" PRIu16 "]: %s",
attempt, iteratingPort, uv_strerror(err));
uv_close((uv_handle_t*)uvHandle, (uv_close_cb)on_error_close); uv_close((uv_handle_t*)uvHandle, (uv_close_cb)on_error_close);
...@@ -172,22 +182,23 @@ namespace RTC ...@@ -172,22 +182,23 @@ namespace RTC
if (err == UV_EMFILE) if (err == UV_EMFILE)
MS_THROW_ERROR("uv_tcp_bind() fails due to many open files"); MS_THROW_ERROR("uv_tcp_bind() fails due to many open files");
// If bind() fails for more that MAX_BIND_ATTEMPTS then raise an error. // If bind() fails for more that MaxBindAttempts then raise an error.
if (bind_attempt > MAX_BIND_ATTEMPTS) if (bindAttempt > MaxBindAttempts)
MS_THROW_ERROR("uv_tcp_bind() fails more than %" PRIu16 " times for IP '%s'", (uint16_t)MAX_BIND_ATTEMPTS, listen_ip); MS_THROW_ERROR("uv_tcp_bind() fails more than %" PRIu16 " times for IP '%s'",
MaxBindAttempts, listenIp);
// If we have tried all the ports in the range raise an error. // If we have tried all the ports in the range raise an error.
if (iterating_port == initial_port) if (iteratingPort == initialPort)
MS_THROW_ERROR("no more available ports for IP '%s'", listen_ip); MS_THROW_ERROR("no more available ports for IP '%s'", listenIp);
continue; continue;
} }
// Set the port as unavailable. // Set the port as unavailable.
(*available_ports)[iterating_port] = false; (*availablePorts)[iteratingPort] = false;
MS_DEBUG_DEV("bind success [ip:%s, port:%" PRIu16 ", attempt:%" PRIu16 "]", MS_DEBUG_DEV("bind success [ip:%s, port:%" PRIu16 ", attempt:%" PRIu16 "]",
listen_ip, iterating_port, attempt); listenIp, iteratingPort, attempt);
return uvHandle; return uvHandle;
}; };
...@@ -195,11 +206,11 @@ namespace RTC ...@@ -195,11 +206,11 @@ namespace RTC
/* Instance methods. */ /* Instance methods. */
TcpServer::TcpServer(Listener* listener, RTC::TcpConnection::Listener* connListener, int address_family) : TcpServer::TcpServer(Listener* listener, RTC::TcpConnection::Listener* connListener, int addressFamily) :
// Provide the parent class constructor with a UDP uv handle. // Provide the parent class constructor with a UDP uv handle.
// NOTE: This may throw a MediaSoupError exception if the address family is not available // NOTE: This may throw a MediaSoupError exception if the address family is not available
// or there are no available ports. // or there are no available ports.
::TcpServer::TcpServer(GetRandomPort(address_family), 256), ::TcpServer::TcpServer(GetRandomPort(addressFamily), 256),
listener(listener), listener(listener),
connListener(connListener) connListener(connListener)
{ {
...@@ -218,8 +229,8 @@ namespace RTC ...@@ -218,8 +229,8 @@ namespace RTC
{ {
MS_TRACE(); MS_TRACE();
// Allow just MAX_TCP_CONNECTIONS_PER_SERVER. // Allow just MaxTcpConnectionsPerServer.
if (GetNumConnections() > MAX_TCP_CONNECTIONS_PER_SERVER) if (GetNumConnections() > MaxTcpConnectionsPerServer)
connection->Destroy(); connection->Destroy();
} }
...@@ -231,7 +242,10 @@ namespace RTC ...@@ -231,7 +242,10 @@ namespace RTC
// NOTE: Don't do it if closing (since at this point the listener is already freed). // NOTE: Don't do it if closing (since at this point the listener is already freed).
// At the end, this is just called if the connection was remotely closed. // At the end, this is just called if the connection was remotely closed.
if (!IsClosing()) if (!IsClosing())
this->listener->onRtcTcpConnectionClosed(this, static_cast<RTC::TcpConnection*>(connection), is_closed_by_peer); {
this->listener->onRtcTcpConnectionClosed(this, static_cast<RTC::TcpConnection*>(connection),
is_closed_by_peer);
}
} }
void TcpServer::userOnTcpServerClosed() void TcpServer::userOnTcpServerClosed()
......
...@@ -9,8 +9,6 @@ ...@@ -9,8 +9,6 @@
#include "Logger.hpp" #include "Logger.hpp"
#include <string> #include <string>
#define MAX_BIND_ATTEMPTS 20
/* Static methods for UV callbacks. */ /* Static methods for UV callbacks. */
static inline static inline
...@@ -21,6 +19,10 @@ void on_error_close(uv_handle_t* handle) ...@@ -21,6 +19,10 @@ void on_error_close(uv_handle_t* handle)
namespace RTC namespace RTC
{ {
/* Static. */
static constexpr uint16_t MaxBindAttempts = 20;
/* Class variables. */ /* Class variables. */
struct sockaddr_storage UdpSocket::sockaddrStorageIPv4; struct sockaddr_storage UdpSocket::sockaddrStorageIPv4;
...@@ -40,14 +42,18 @@ namespace RTC ...@@ -40,14 +42,18 @@ namespace RTC
if (Settings::configuration.hasIPv4) if (Settings::configuration.hasIPv4)
{ {
err = uv_ip4_addr(Settings::configuration.rtcIPv4.c_str(), 0, (struct sockaddr_in*)&RTC::UdpSocket::sockaddrStorageIPv4); err = uv_ip4_addr(Settings::configuration.rtcIPv4.c_str(), 0,
(struct sockaddr_in*)&RTC::UdpSocket::sockaddrStorageIPv4);
if (err) if (err)
MS_THROW_ERROR("uv_ipv4_addr() failed: %s", uv_strerror(err)); MS_THROW_ERROR("uv_ipv4_addr() failed: %s", uv_strerror(err));
} }
if (Settings::configuration.hasIPv6) if (Settings::configuration.hasIPv6)
{ {
err = uv_ip6_addr(Settings::configuration.rtcIPv6.c_str(), 0, (struct sockaddr_in6*)&RTC::UdpSocket::sockaddrStorageIPv6); err = uv_ip6_addr(Settings::configuration.rtcIPv6.c_str(), 0,
(struct sockaddr_in6*)&RTC::UdpSocket::sockaddrStorageIPv6);
if (err) if (err)
MS_THROW_ERROR("uv_ipv6_addr() failed: %s", uv_strerror(err)); MS_THROW_ERROR("uv_ipv6_addr() failed: %s", uv_strerror(err));
} }
...@@ -56,6 +62,7 @@ namespace RTC ...@@ -56,6 +62,7 @@ namespace RTC
UdpSocket::maxPort = Settings::configuration.rtcMaxPort; UdpSocket::maxPort = Settings::configuration.rtcMaxPort;
uint16_t i = RTC::UdpSocket::minPort; uint16_t i = RTC::UdpSocket::minPort;
do do
{ {
RTC::UdpSocket::availableIPv4Ports[i] = true; RTC::UdpSocket::availableIPv4Ports[i] = true;
...@@ -64,38 +71,38 @@ namespace RTC ...@@ -64,38 +71,38 @@ namespace RTC
while (i++ != RTC::UdpSocket::maxPort); while (i++ != RTC::UdpSocket::maxPort);
} }
uv_udp_t* UdpSocket::GetRandomPort(int address_family) uv_udp_t* UdpSocket::GetRandomPort(int addressFamily)
{ {
MS_TRACE(); MS_TRACE();
if (address_family == AF_INET && !Settings::configuration.hasIPv4) if (addressFamily == AF_INET && !Settings::configuration.hasIPv4)
MS_THROW_ERROR("IPv4 family not available for RTC"); MS_THROW_ERROR("IPv4 family not available for RTC");
else if (address_family == AF_INET6 && !Settings::configuration.hasIPv6) else if (addressFamily == AF_INET6 && !Settings::configuration.hasIPv6)
MS_THROW_ERROR("IPv6 family not available for RTC"); MS_THROW_ERROR("IPv6 family not available for RTC");
int err; int err;
uv_udp_t* uvHandle = nullptr; uv_udp_t* uvHandle = nullptr;
struct sockaddr_storage bind_addr; struct sockaddr_storage bindAddr;
const char* listen_ip; const char* listenIp;
uint16_t initial_port; uint16_t initialPort;
uint16_t iterating_port; uint16_t iteratingPort;
uint16_t attempt = 0; uint16_t attempt = 0;
uint16_t bind_attempt = 0; uint16_t bindAttempt = 0;
int flags = 0; int flags = 0;
std::unordered_map<uint16_t, bool>* available_ports; std::unordered_map<uint16_t, bool>* availablePorts;
switch (address_family) switch (addressFamily)
{ {
case AF_INET: case AF_INET:
available_ports = &RTC::UdpSocket::availableIPv4Ports; availablePorts = &RTC::UdpSocket::availableIPv4Ports;
bind_addr = RTC::UdpSocket::sockaddrStorageIPv4; bindAddr = RTC::UdpSocket::sockaddrStorageIPv4;
listen_ip = Settings::configuration.rtcIPv4.c_str(); listenIp = Settings::configuration.rtcIPv4.c_str();
break; break;
case AF_INET6: case AF_INET6:
available_ports = &RTC::UdpSocket::availableIPv6Ports; availablePorts = &RTC::UdpSocket::availableIPv6Ports;
bind_addr = RTC::UdpSocket::sockaddrStorageIPv6; bindAddr = RTC::UdpSocket::sockaddrStorageIPv6;
listen_ip = Settings::configuration.rtcIPv6.c_str(); listenIp = Settings::configuration.rtcIPv6.c_str();
// Don't also bind into IPv4 when listening in IPv6. // Don't also bind into IPv4 when listening in IPv6.
flags |= UV_UDP_IPV6ONLY; flags |= UV_UDP_IPV6ONLY;
break; break;
...@@ -106,9 +113,10 @@ namespace RTC ...@@ -106,9 +113,10 @@ namespace RTC
} }
// Choose a random port to start from. // Choose a random port to start from.
initial_port = (uint16_t)Utils::Crypto::GetRandomUInt((uint32_t)RTC::UdpSocket::minPort, (uint32_t)RTC::UdpSocket::maxPort); initialPort = (uint16_t)Utils::Crypto::GetRandomUInt((uint32_t)RTC::UdpSocket::minPort,
(uint32_t)RTC::UdpSocket::maxPort);
iterating_port = initial_port; iteratingPort = initialPort;
// Iterate the RTC UDP ports until getting one available. // Iterate the RTC UDP ports until getting one available.
// Fail also after bind() fails N times in theorically available ports. // Fail also after bind() fails N times in theorically available ports.
...@@ -117,19 +125,20 @@ namespace RTC ...@@ -117,19 +125,20 @@ namespace RTC
++attempt; ++attempt;
// Increase the iterate port within the range of RTC UDP ports. // Increase the iterate port within the range of RTC UDP ports.
if (iterating_port < RTC::UdpSocket::maxPort) if (iteratingPort < RTC::UdpSocket::maxPort)
++iterating_port; ++iteratingPort;
else else
iterating_port = RTC::UdpSocket::minPort; iteratingPort = RTC::UdpSocket::minPort;
// Check whether the chosen port is available. // Check whether the chosen port is available.
if (!(*available_ports)[iterating_port]) if (!(*availablePorts)[iteratingPort])
{ {
MS_DEBUG_DEV("port in use, trying again [port:%" PRIu16 ", attempt:%" PRIu16 "]", iterating_port, attempt); MS_DEBUG_DEV("port in use, trying again [port:%" PRIu16 ", attempt:%" PRIu16 "]",
iteratingPort, attempt);
// If we have tried all the ports in the range raise an error. // If we have tried all the ports in the range raise an error.
if (iterating_port == initial_port) if (iteratingPort == initialPort)
MS_THROW_ERROR("no more available ports for IP '%s'", listen_ip); MS_THROW_ERROR("no more available ports for IP '%s'", listenIp);
continue; continue;
} }
...@@ -138,18 +147,18 @@ namespace RTC ...@@ -138,18 +147,18 @@ namespace RTC
// Now let's check whether no other process is listening into it. // Now let's check whether no other process is listening into it.
// Set the chosen port into the sockaddr struct(s). // Set the chosen port into the sockaddr struct(s).
switch (address_family) switch (addressFamily)
{ {
case AF_INET: case AF_INET:
((struct sockaddr_in*)&bind_addr)->sin_port = htons(iterating_port); ((struct sockaddr_in*)&bindAddr)->sin_port = htons(iteratingPort);
break; break;
case AF_INET6: case AF_INET6:
((struct sockaddr_in6*)&bind_addr)->sin6_port = htons(iterating_port); ((struct sockaddr_in6*)&bindAddr)->sin6_port = htons(iteratingPort);
break; break;
} }
// Try to bind on it. // Try to bind on it.
++bind_attempt; ++bindAttempt;
uvHandle = new uv_udp_t(); uvHandle = new uv_udp_t();
...@@ -160,10 +169,11 @@ namespace RTC ...@@ -160,10 +169,11 @@ namespace RTC
MS_THROW_ERROR("uv_udp_init() failed: %s", uv_strerror(err)); MS_THROW_ERROR("uv_udp_init() failed: %s", uv_strerror(err));
} }
err = uv_udp_bind(uvHandle, (const struct sockaddr*)&bind_addr, flags); err = uv_udp_bind(uvHandle, (const struct sockaddr*)&bindAddr, flags);
if (err) if (err)
{ {
MS_WARN_DEV("uv_udp_bind() failed [port:%" PRIu16 ", attempt:%" PRIu16 "]: %s", attempt, iterating_port, uv_strerror(err)); MS_WARN_DEV("uv_udp_bind() failed [port:%" PRIu16 ", attempt:%" PRIu16 "]: %s",
attempt, iteratingPort, uv_strerror(err));
uv_close((uv_handle_t*)uvHandle, (uv_close_cb)on_error_close); uv_close((uv_handle_t*)uvHandle, (uv_close_cb)on_error_close);
...@@ -171,22 +181,23 @@ namespace RTC ...@@ -171,22 +181,23 @@ namespace RTC
if (err == UV_EMFILE) if (err == UV_EMFILE)
MS_THROW_ERROR("uv_udp_bind() fails due to many open files"); MS_THROW_ERROR("uv_udp_bind() fails due to many open files");
// If bind() fails for more that MAX_BIND_ATTEMPTS then raise an error. // If bind() fails for more that MaxBindAttempts then raise an error.
if (bind_attempt > MAX_BIND_ATTEMPTS) if (bindAttempt > MaxBindAttempts)
MS_THROW_ERROR("uv_udp_bind() fails more than %" PRIu16 " times for IP '%s'", (uint16_t)MAX_BIND_ATTEMPTS, listen_ip); MS_THROW_ERROR("uv_udp_bind() fails more than %" PRIu16 " times for IP '%s'",
MaxBindAttempts, listenIp);
// If we have tried all the ports in the range raise an error. // If we have tried all the ports in the range raise an error.
if (iterating_port == initial_port) if (iteratingPort == initialPort)
MS_THROW_ERROR("no more available ports for IP '%s'", listen_ip); MS_THROW_ERROR("no more available ports for IP '%s'", listenIp);
continue; continue;
} }
// Set the port as unavailable. // Set the port as unavailable.
(*available_ports)[iterating_port] = false; (*availablePorts)[iteratingPort] = false;
MS_DEBUG_DEV("bind success [ip:%s, port:%" PRIu16 ", attempt:%" PRIu16 "]", MS_DEBUG_DEV("bind success [ip:%s, port:%" PRIu16 ", attempt:%" PRIu16 "]",
listen_ip, iterating_port, attempt); listenIp, iteratingPort, attempt);
return uvHandle; return uvHandle;
}; };
...@@ -194,11 +205,11 @@ namespace RTC ...@@ -194,11 +205,11 @@ namespace RTC
/* Instance methods. */ /* Instance methods. */
UdpSocket::UdpSocket(Listener* listener, int address_family) : UdpSocket::UdpSocket(Listener* listener, int addressFamily) :
// Provide the parent class constructor with a UDP uv handle. // Provide the parent class constructor with a UDP uv handle.
// NOTE: This may throw a MediaSoupError exception if the address family is not available // NOTE: This may throw a MediaSoupError exception if the address family is not available
// or there are no available ports. // or there are no available ports.
::UdpSocket::UdpSocket(GetRandomPort(address_family)), ::UdpSocket::UdpSocket(GetRandomPort(addressFamily)),
listener(listener) listener(listener)
{ {
MS_TRACE(); MS_TRACE();
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论