threadshare: Update from the deprecated net2 to the socket2 crate

This commit is contained in:
Sebastian Dröge 2020-05-29 13:07:14 +03:00
parent 2e283b855a
commit 08da51744b
3 changed files with 35 additions and 18 deletions

View file

@ -26,11 +26,14 @@ tokio = { git = "https://github.com/fengalin/tokio", branch = "fengalin/throttli
futures = { version = "0.3", features = ["thread-pool"] }
lazy_static = "1.0"
rand = "0.7"
net2 = "0.2"
socket2 = "0.3"
[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3", features = ["winsock2", "processthreadsapi"] }
[target.'cfg(unix)'.dependencies]
socket2 = { version = "0.3", features = ["reuseport"] }
[lib]
name = "gstthreadshare"
crate-type = ["cdylib", "rlib"]

View file

@ -1003,18 +1003,26 @@ impl UdpSink {
let saddr = SocketAddr::new(bind_addr, bind_port as u16);
gst_debug!(CAT, obj: element, "Binding to {:?}", saddr);
let builder = match family {
SocketFamily::Ipv4 => net2::UdpBuilder::new_v4(),
SocketFamily::Ipv6 => net2::UdpBuilder::new_v6(),
let socket = match family {
SocketFamily::Ipv4 => socket2::Socket::new(
socket2::Domain::ipv4(),
socket2::Type::dgram(),
Some(socket2::Protocol::udp()),
),
SocketFamily::Ipv6 => socket2::Socket::new(
socket2::Domain::ipv6(),
socket2::Type::dgram(),
Some(socket2::Protocol::udp()),
),
};
let builder = match builder {
Ok(builder) => builder,
let socket = match socket {
Ok(socket) => socket,
Err(err) => {
gst_warning!(
CAT,
obj: element,
"Failed to create {} socket builder: {}",
"Failed to create {} socket: {}",
match family {
SocketFamily::Ipv4 => "IPv4",
SocketFamily::Ipv6 => "IPv6",
@ -1025,7 +1033,7 @@ impl UdpSink {
}
};
let socket = builder.bind(&saddr).map_err(|err| {
socket.bind(&saddr.into()).map_err(|err| {
gst_error_msg!(
gst::ResourceError::OpenWrite,
["Failed to bind socket: {}", err]
@ -1033,7 +1041,7 @@ impl UdpSink {
})?;
let socket = context.enter(|| {
tokio::net::UdpSocket::from_std(socket).map_err(|err| {
tokio::net::UdpSocket::from_std(socket.into()).map_err(|err| {
gst_error_msg!(
gst::ResourceError::OpenWrite,
["Failed to setup socket for tokio: {}", err]

View file

@ -618,10 +618,18 @@ impl UdpSrc {
saddr
};
let builder = if addr.is_ipv4() {
net2::UdpBuilder::new_v4()
let socket = if addr.is_ipv4() {
socket2::Socket::new(
socket2::Domain::ipv4(),
socket2::Type::dgram(),
Some(socket2::Protocol::udp()),
)
} else {
net2::UdpBuilder::new_v6()
socket2::Socket::new(
socket2::Domain::ipv6(),
socket2::Type::dgram(),
Some(socket2::Protocol::udp()),
)
}
.map_err(|err| {
gst_error_msg!(
@ -630,7 +638,7 @@ impl UdpSrc {
)
})?;
builder.reuse_address(settings.reuse).map_err(|err| {
socket.set_reuse_address(settings.reuse).map_err(|err| {
gst_error_msg!(
gst::ResourceError::OpenRead,
["Failed to set reuse_address: {}", err]
@ -639,9 +647,7 @@ impl UdpSrc {
#[cfg(unix)]
{
use net2::unix::UnixUdpBuilderExt;
builder.reuse_port(settings.reuse).map_err(|err| {
socket.set_reuse_port(settings.reuse).map_err(|err| {
gst_error_msg!(
gst::ResourceError::OpenRead,
["Failed to set reuse_port: {}", err]
@ -649,7 +655,7 @@ impl UdpSrc {
})?;
}
let socket = builder.bind(&saddr).map_err(|err| {
socket.bind(&saddr.into()).map_err(|err| {
gst_error_msg!(
gst::ResourceError::OpenRead,
["Failed to bind socket: {}", err]
@ -657,7 +663,7 @@ impl UdpSrc {
})?;
let socket = context.enter(|| {
tokio::net::UdpSocket::from_std(socket).map_err(|err| {
tokio::net::UdpSocket::from_std(socket.into()).map_err(|err| {
gst_error_msg!(
gst::ResourceError::OpenRead,
["Failed to setup socket for tokio: {}", err]