Skip to content
项目
群组
代码片段
帮助
正在加载...
登录
切换导航
M
mediasoup
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
聂卓夫
mediasoup
Commits
5ebf8759
提交
5ebf8759
authored
4月 04, 2017
作者:
Iñaki Baz Castillo
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
More C++ syntax work
上级
5ba4023b
显示空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
134 行增加
和
109 行删除
+134
-109
TcpConnection.cpp
worker/src/RTC/TcpConnection.cpp
+17
-17
TcpServer.cpp
worker/src/RTC/TcpServer.cpp
+62
-48
UdpSocket.cpp
worker/src/RTC/UdpSocket.cpp
+55
-44
没有找到文件。
worker/src/RTC/TcpConnection.cpp
浏览文件 @
5ebf8759
...
@@ -49,26 +49,24 @@ namespace RTC
...
@@ -49,26 +49,24 @@ namespace RTC
if
(
IsClosing
())
if
(
IsClosing
())
return
;
return
;
size_t
data
_l
en
=
this
->
bufferDataLen
-
this
->
frameStart
;
size_t
data
L
en
=
this
->
bufferDataLen
-
this
->
frameStart
;
size_t
packet
_l
en
;
size_t
packet
L
en
;
if
(
data
_l
en
>=
2
)
if
(
data
L
en
>=
2
)
packet
_l
en
=
(
size_t
)
Utils
::
Byte
::
Get2Bytes
(
this
->
buffer
+
this
->
frameStart
,
0
);
packet
L
en
=
(
size_t
)
Utils
::
Byte
::
Get2Bytes
(
this
->
buffer
+
this
->
frameStart
,
0
);
// We have packet
_l
en bytes.
// We have packet
L
en bytes.
if
(
data
_len
>=
2
&&
data_len
>=
2
+
packet_l
en
)
if
(
data
Len
>=
2
&&
dataLen
>=
2
+
packetL
en
)
{
{
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
_l
en
)
==
this
->
bufferSize
)
if
((
this
->
frameStart
+
2
+
packet
L
en
)
==
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
_l
en
;
this
->
frameStart
+=
2
+
packet
L
en
;
}
}
// 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
_l
en
[
2
];
uint8_t
frame
L
en
[
2
];
Utils
::
Byte
::
Set2Bytes
(
frame
_l
en
,
0
,
len
);
Utils
::
Byte
::
Set2Bytes
(
frame
L
en
,
0
,
len
);
Write
(
frame
_l
en
,
2
,
data
,
len
);
Write
(
frame
L
en
,
2
,
data
,
len
);
}
}
}
}
worker/src/RTC/TcpServer.cpp
浏览文件 @
5ebf8759
...
@@ -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
_f
amily
)
uv_tcp_t
*
TcpServer
::
GetRandomPort
(
int
address
F
amily
)
{
{
MS_TRACE
();
MS_TRACE
();
if
(
address
_f
amily
==
AF_INET
&&
!
Settings
::
configuration
.
hasIPv4
)
if
(
address
F
amily
==
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
_f
amily
==
AF_INET6
&&
!
Settings
::
configuration
.
hasIPv6
)
else
if
(
address
F
amily
==
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
_a
ddr
;
struct
sockaddr_storage
bind
A
ddr
;
const
char
*
listen
_i
p
;
const
char
*
listen
I
p
;
uint16_t
initial
_p
ort
;
uint16_t
initial
P
ort
;
uint16_t
iterating
_p
ort
;
uint16_t
iterating
P
ort
;
uint16_t
attempt
=
0
;
uint16_t
attempt
=
0
;
uint16_t
bind
_a
ttempt
=
0
;
uint16_t
bind
A
ttempt
=
0
;
int
flags
=
0
;
int
flags
=
0
;
std
::
unordered_map
<
uint16_t
,
bool
>*
available
_p
orts
;
std
::
unordered_map
<
uint16_t
,
bool
>*
available
P
orts
;
switch
(
address
_f
amily
)
switch
(
address
F
amily
)
{
{
case
AF_INET
:
case
AF_INET
:
available
_p
orts
=
&
RTC
::
TcpServer
::
availableIPv4Ports
;
available
P
orts
=
&
RTC
::
TcpServer
::
availableIPv4Ports
;
bind
_a
ddr
=
RTC
::
TcpServer
::
sockaddrStorageIPv4
;
bind
A
ddr
=
RTC
::
TcpServer
::
sockaddrStorageIPv4
;
listen
_i
p
=
Settings
::
configuration
.
rtcIPv4
.
c_str
();
listen
I
p
=
Settings
::
configuration
.
rtcIPv4
.
c_str
();
break
;
break
;
case
AF_INET6
:
case
AF_INET6
:
available
_p
orts
=
&
RTC
::
TcpServer
::
availableIPv6Ports
;
available
P
orts
=
&
RTC
::
TcpServer
::
availableIPv6Ports
;
bind
_a
ddr
=
RTC
::
TcpServer
::
sockaddrStorageIPv6
;
bind
A
ddr
=
RTC
::
TcpServer
::
sockaddrStorageIPv6
;
listen
_i
p
=
Settings
::
configuration
.
rtcIPv6
.
c_str
();
listen
I
p
=
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_p
ort
;
iterating
Port
=
initialP
ort
;
// 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
_p
ort
<
RTC
::
TcpServer
::
maxPort
)
if
(
iterating
P
ort
<
RTC
::
TcpServer
::
maxPort
)
iterating
_p
ort
+=
1
;
iterating
P
ort
+=
1
;
else
else
iterating
_p
ort
=
RTC
::
TcpServer
::
minPort
;
iterating
P
ort
=
RTC
::
TcpServer
::
minPort
;
// Check whether the chosen port is available.
// Check whether the chosen port is available.
if
(
!
(
*
available
_ports
)[
iterating_p
ort
])
if
(
!
(
*
available
Ports
)[
iteratingP
ort
])
{
{
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_p
ort
)
if
(
iterating
Port
==
initialP
ort
)
MS_THROW_ERROR
(
"no more available ports for IP '%s'"
,
listen
_i
p
);
MS_THROW_ERROR
(
"no more available ports for IP '%s'"
,
listen
I
p
);
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
_f
amily
)
switch
(
address
F
amily
)
{
{
case
AF_INET
:
case
AF_INET
:
((
struct
sockaddr_in
*
)
&
bind
_addr
)
->
sin_port
=
htons
(
iterating_p
ort
);
((
struct
sockaddr_in
*
)
&
bind
Addr
)
->
sin_port
=
htons
(
iteratingP
ort
);
break
;
break
;
case
AF_INET6
:
case
AF_INET6
:
((
struct
sockaddr_in6
*
)
&
bind
_addr
)
->
sin6_port
=
htons
(
iterating_p
ort
);
((
struct
sockaddr_in6
*
)
&
bind
Addr
)
->
sin6_port
=
htons
(
iteratingP
ort
);
break
;
break
;
}
}
// Try to bind on it.
// Try to bind on it.
++
bind
_a
ttempt
;
++
bind
A
ttempt
;
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
_a
ddr
,
flags
);
err
=
uv_tcp_bind
(
uvHandle
,
(
const
struct
sockaddr
*
)
&
bind
A
ddr
,
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_p
ort
)
if
(
iterating
Port
==
initialP
ort
)
MS_THROW_ERROR
(
"no more available ports for IP '%s'"
,
listen
_i
p
);
MS_THROW_ERROR
(
"no more available ports for IP '%s'"
,
listen
I
p
);
continue
;
continue
;
}
}
// Set the port as unavailable.
// Set the port as unavailable.
(
*
available
_ports
)[
iterating_p
ort
]
=
false
;
(
*
available
Ports
)[
iteratingP
ort
]
=
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_p
ort
,
attempt
);
listen
Ip
,
iteratingP
ort
,
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
_f
amily
)
:
TcpServer
::
TcpServer
(
Listener
*
listener
,
RTC
::
TcpConnection
::
Listener
*
connListener
,
int
address
F
amily
)
:
// 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
_f
amily
),
256
),
::
TcpServer
::
TcpServer
(
GetRandomPort
(
address
F
amily
),
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 M
AX_TCP_CONNECTIONS_PER_SERVER
.
// Allow just M
axTcpConnectionsPerServer
.
if
(
GetNumConnections
()
>
M
AX_TCP_CONNECTIONS_PER_SERVER
)
if
(
GetNumConnections
()
>
M
axTcpConnectionsPerServer
)
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
()
...
...
worker/src/RTC/UdpSocket.cpp
浏览文件 @
5ebf8759
...
@@ -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
_f
amily
)
uv_udp_t
*
UdpSocket
::
GetRandomPort
(
int
address
F
amily
)
{
{
MS_TRACE
();
MS_TRACE
();
if
(
address
_f
amily
==
AF_INET
&&
!
Settings
::
configuration
.
hasIPv4
)
if
(
address
F
amily
==
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
_f
amily
==
AF_INET6
&&
!
Settings
::
configuration
.
hasIPv6
)
else
if
(
address
F
amily
==
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
_a
ddr
;
struct
sockaddr_storage
bind
A
ddr
;
const
char
*
listen
_i
p
;
const
char
*
listen
I
p
;
uint16_t
initial
_p
ort
;
uint16_t
initial
P
ort
;
uint16_t
iterating
_p
ort
;
uint16_t
iterating
P
ort
;
uint16_t
attempt
=
0
;
uint16_t
attempt
=
0
;
uint16_t
bind
_a
ttempt
=
0
;
uint16_t
bind
A
ttempt
=
0
;
int
flags
=
0
;
int
flags
=
0
;
std
::
unordered_map
<
uint16_t
,
bool
>*
available
_p
orts
;
std
::
unordered_map
<
uint16_t
,
bool
>*
available
P
orts
;
switch
(
address
_f
amily
)
switch
(
address
F
amily
)
{
{
case
AF_INET
:
case
AF_INET
:
available
_p
orts
=
&
RTC
::
UdpSocket
::
availableIPv4Ports
;
available
P
orts
=
&
RTC
::
UdpSocket
::
availableIPv4Ports
;
bind
_a
ddr
=
RTC
::
UdpSocket
::
sockaddrStorageIPv4
;
bind
A
ddr
=
RTC
::
UdpSocket
::
sockaddrStorageIPv4
;
listen
_i
p
=
Settings
::
configuration
.
rtcIPv4
.
c_str
();
listen
I
p
=
Settings
::
configuration
.
rtcIPv4
.
c_str
();
break
;
break
;
case
AF_INET6
:
case
AF_INET6
:
available
_p
orts
=
&
RTC
::
UdpSocket
::
availableIPv6Ports
;
available
P
orts
=
&
RTC
::
UdpSocket
::
availableIPv6Ports
;
bind
_a
ddr
=
RTC
::
UdpSocket
::
sockaddrStorageIPv6
;
bind
A
ddr
=
RTC
::
UdpSocket
::
sockaddrStorageIPv6
;
listen
_i
p
=
Settings
::
configuration
.
rtcIPv6
.
c_str
();
listen
I
p
=
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_p
ort
;
iterating
Port
=
initialP
ort
;
// 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
_p
ort
<
RTC
::
UdpSocket
::
maxPort
)
if
(
iterating
P
ort
<
RTC
::
UdpSocket
::
maxPort
)
++
iterating
_p
ort
;
++
iterating
P
ort
;
else
else
iterating
_p
ort
=
RTC
::
UdpSocket
::
minPort
;
iterating
P
ort
=
RTC
::
UdpSocket
::
minPort
;
// Check whether the chosen port is available.
// Check whether the chosen port is available.
if
(
!
(
*
available
_ports
)[
iterating_p
ort
])
if
(
!
(
*
available
Ports
)[
iteratingP
ort
])
{
{
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_p
ort
)
if
(
iterating
Port
==
initialP
ort
)
MS_THROW_ERROR
(
"no more available ports for IP '%s'"
,
listen
_i
p
);
MS_THROW_ERROR
(
"no more available ports for IP '%s'"
,
listen
I
p
);
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
_f
amily
)
switch
(
address
F
amily
)
{
{
case
AF_INET
:
case
AF_INET
:
((
struct
sockaddr_in
*
)
&
bind
_addr
)
->
sin_port
=
htons
(
iterating_p
ort
);
((
struct
sockaddr_in
*
)
&
bind
Addr
)
->
sin_port
=
htons
(
iteratingP
ort
);
break
;
break
;
case
AF_INET6
:
case
AF_INET6
:
((
struct
sockaddr_in6
*
)
&
bind
_addr
)
->
sin6_port
=
htons
(
iterating_p
ort
);
((
struct
sockaddr_in6
*
)
&
bind
Addr
)
->
sin6_port
=
htons
(
iteratingP
ort
);
break
;
break
;
}
}
// Try to bind on it.
// Try to bind on it.
++
bind
_a
ttempt
;
++
bind
A
ttempt
;
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
_a
ddr
,
flags
);
err
=
uv_udp_bind
(
uvHandle
,
(
const
struct
sockaddr
*
)
&
bind
A
ddr
,
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_p
ort
)
if
(
iterating
Port
==
initialP
ort
)
MS_THROW_ERROR
(
"no more available ports for IP '%s'"
,
listen
_i
p
);
MS_THROW_ERROR
(
"no more available ports for IP '%s'"
,
listen
I
p
);
continue
;
continue
;
}
}
// Set the port as unavailable.
// Set the port as unavailable.
(
*
available
_ports
)[
iterating_p
ort
]
=
false
;
(
*
available
Ports
)[
iteratingP
ort
]
=
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_p
ort
,
attempt
);
listen
Ip
,
iteratingP
ort
,
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
_f
amily
)
:
UdpSocket
::
UdpSocket
(
Listener
*
listener
,
int
address
F
amily
)
:
// 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
_f
amily
)),
::
UdpSocket
::
UdpSocket
(
GetRandomPort
(
address
F
amily
)),
listener
(
listener
)
listener
(
listener
)
{
{
MS_TRACE
();
MS_TRACE
();
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论