From 8e668633c72bd4beb5284dac7107919fdc7d6639 Mon Sep 17 00:00:00 2001 From: Mirko Bonadei Date: Thu, 22 Nov 2018 14:35:05 +0100 Subject: [PATCH] Remove cricket::UdpTransport. This code is never built by GN, and the header is never included. Bug: webrtc:9855 Change-Id: I7f79c2b16e4a833fa7788be87dbdf9b41247c9e4 Reviewed-on: https://webrtc-review.googlesource.com/c/111755 Reviewed-by: Karl Wiberg Reviewed-by: Niels Moller Commit-Queue: Mirko Bonadei Cr-Commit-Position: refs/heads/master@{#25754} --- p2p/base/udptransport.cc | 133 --------------------- p2p/base/udptransport.h | 88 -------------- p2p/base/udptransport_unittest.cc | 189 ------------------------------ 3 files changed, 410 deletions(-) delete mode 100644 p2p/base/udptransport.cc delete mode 100644 p2p/base/udptransport.h delete mode 100644 p2p/base/udptransport_unittest.cc diff --git a/p2p/base/udptransport.cc b/p2p/base/udptransport.cc deleted file mode 100644 index d60d89c454..0000000000 --- a/p2p/base/udptransport.cc +++ /dev/null @@ -1,133 +0,0 @@ -/* - * Copyright 2016 The WebRTC project authors. All Rights Reserved. - * - * Use of this source code is governed by a BSD-style license - * that can be found in the LICENSE file in the root of the source - * tree. An additional intellectual property rights grant can be found - * in the file PATENTS. All contributing project authors may - * be found in the AUTHORS file in the root of the source tree. - */ - -#include "p2p/base/udptransport.h" - -#include -#include // For std::move. - -#include "rtc_base/asyncpacketsocket.h" -#include "rtc_base/asyncudpsocket.h" -#include "rtc_base/logging.h" -#include "rtc_base/nethelper.h" -#include "rtc_base/socketaddress.h" -#include "rtc_base/thread.h" -#include "rtc_base/thread_checker.h" - -namespace cricket { - -UdpTransport::UdpTransport(const std::string& transport_name, - std::unique_ptr socket) - : transport_name_(transport_name), socket_(std::move(socket)) { - RTC_DCHECK(socket_); - socket_->SignalReadPacket.connect(this, &UdpTransport::OnSocketReadPacket); - socket_->SignalSentPacket.connect(this, &UdpTransport::OnSocketSentPacket); -} - -UdpTransport::~UdpTransport() { - RTC_DCHECK_RUN_ON(&network_thread_checker_); -} - -rtc::SocketAddress UdpTransport::GetLocalAddress() const { - RTC_DCHECK_RUN_ON(&network_thread_checker_); - return socket_->GetLocalAddress(); -} - -bool UdpTransport::SetRemoteAddress(const rtc::SocketAddress& addr) { - RTC_DCHECK_RUN_ON(&network_thread_checker_); - if (!addr.IsComplete()) { - RTC_LOG(LS_WARNING) << "Remote address not complete."; - return false; - } - // TODO(johan): check for ipv4, other settings. - bool prev_destination_nil = remote_address_.IsNil(); - remote_address_ = addr; - // Going from "didn't have destination" to "have destination" or vice versa. - if (prev_destination_nil != remote_address_.IsNil()) { - SignalWritableState(this); - if (prev_destination_nil) { - SignalReadyToSend(this); - } - } - return true; -} - -rtc::SocketAddress UdpTransport::GetRemoteAddress() const { - RTC_DCHECK_RUN_ON(&network_thread_checker_); - return remote_address_; -} - -const std::string& UdpTransport::transport_name() const { - return transport_name_; -} - -bool UdpTransport::receiving() const { - // TODO(johan): Implement method and signal. - return true; -} - -bool UdpTransport::writable() const { - RTC_DCHECK_RUN_ON(&network_thread_checker_); - return !remote_address_.IsNil(); -} - -int UdpTransport::SendPacket(const char* data, - size_t len, - const rtc::PacketOptions& options, - int flags) { - // No thread_checker in high frequency network function. - if (remote_address_.IsNil()) { - RTC_LOG(LS_WARNING) << "Remote address not set."; - send_error_ = ENOTCONN; - return -1; - } - int result = - socket_->SendTo((const void*)data, len, remote_address_, options); - if (result <= 0) { - RTC_LOG(LS_VERBOSE) << "SendPacket() " << result; - } - return result; -} - -absl::optional UdpTransport::network_route() const { - rtc::NetworkRoute network_route; - network_route.packet_overhead = - /*kUdpOverhead=*/8 + GetIpOverhead(GetLocalAddress().family()); - return absl::optional(network_route); -} - -int UdpTransport::SetOption(rtc::Socket::Option opt, int value) { - return 0; -} - -int UdpTransport::GetError() { - return send_error_; -} - -rtc::PacketTransportInternal* UdpTransport::GetInternal() { - return this; -} - -void UdpTransport::OnSocketReadPacket(rtc::AsyncPacketSocket* socket, - const char* data, - size_t len, - const rtc::SocketAddress& remote_addr, - const int64_t& packet_time_us) { - // No thread_checker in high frequency network function. - SignalReadPacket(this, data, len, packet_time_us, 0); -} - -void UdpTransport::OnSocketSentPacket(rtc::AsyncPacketSocket* socket, - const rtc::SentPacket& packet) { - RTC_DCHECK_EQ(socket_.get(), socket); - SignalSentPacket(this, packet); -} - -} // namespace cricket diff --git a/p2p/base/udptransport.h b/p2p/base/udptransport.h deleted file mode 100644 index f215ca36c5..0000000000 --- a/p2p/base/udptransport.h +++ /dev/null @@ -1,88 +0,0 @@ -/* - * Copyright 2016 The WebRTC project authors. All Rights Reserved. - * - * Use of this source code is governed by a BSD-style license - * that can be found in the LICENSE file in the root of the source - * tree. An additional intellectual property rights grant can be found - * in the file PATENTS. All contributing project authors may - * be found in the AUTHORS file in the root of the source tree. - */ - -#ifndef P2P_BASE_UDPTRANSPORT_H_ -#define P2P_BASE_UDPTRANSPORT_H_ - -#include -#include - -#include "absl/types/optional.h" -#include "api/ortc/udptransportinterface.h" -#include "p2p/base/packettransportinternal.h" -#include "rtc_base/asyncpacketsocket.h" // For PacketOptions. -#include "rtc_base/thread_checker.h" - -namespace rtc { -class AsyncPacketSocket; -struct SentPacket; -class SocketAddress; -} // namespace rtc - -namespace cricket { - -// Implementation of UdpTransportInterface. -// Used by OrtcFactory. -class UdpTransport : public rtc::PacketTransportInternal, - public webrtc::UdpTransportInterface { - public: - // |transport_name| is only used for identification/logging. - // |socket| must be non-null. - UdpTransport(const std::string& transport_name, - std::unique_ptr socket); - ~UdpTransport() override; - - // Overrides of UdpTransportInterface, used by the API consumer. - rtc::SocketAddress GetLocalAddress() const override; - bool SetRemoteAddress(const rtc::SocketAddress& addr) override; - rtc::SocketAddress GetRemoteAddress() const override; - - // Overrides of PacketTransportInternal, used by webrtc internally. - const std::string& transport_name() const override; - - bool receiving() const override; - - bool writable() const override; - - int SendPacket(const char* data, - size_t len, - const rtc::PacketOptions& options, - int flags) override; - - int SetOption(rtc::Socket::Option opt, int value) override; - - int GetError() override; - - absl::optional network_route() const override; - - protected: - PacketTransportInternal* GetInternal() override; - - private: - void OnSocketReadPacket(rtc::AsyncPacketSocket* socket, - const char* data, - size_t len, - const rtc::SocketAddress& remote_addr, - const int64_t& packet_time_us); - void OnSocketSentPacket(rtc::AsyncPacketSocket* socket, - const rtc::SentPacket& packet); - bool IsLocalConsistent(); - - std::string transport_name_; - int send_error_ = 0; - std::unique_ptr socket_; - // If not set, will be an "nil" address ("IsNil" returns true). - rtc::SocketAddress remote_address_; - rtc::ThreadChecker network_thread_checker_; -}; - -} // namespace cricket - -#endif // P2P_BASE_UDPTRANSPORT_H_ diff --git a/p2p/base/udptransport_unittest.cc b/p2p/base/udptransport_unittest.cc deleted file mode 100644 index 92f27328f5..0000000000 --- a/p2p/base/udptransport_unittest.cc +++ /dev/null @@ -1,189 +0,0 @@ -/* - * Copyright 2016 The WebRTC Project Authors. All rights reserved. - * - * Use of this source code is governed by a BSD-style license - * that can be found in the LICENSE file in the root of the source - * tree. An additional intellectual property rights grant can be found - * in the file PATENTS. All contributing project authors may - * be found in the AUTHORS file in the root of the source tree. - */ - -#include -#include -#include -#include -#include - -#include "p2p/base/basicpacketsocketfactory.h" -#include "p2p/base/packettransportinternal.h" -#include "p2p/base/udptransport.h" -#include "rtc_base/asyncpacketsocket.h" -#include "rtc_base/gunit.h" -#include "rtc_base/ipaddress.h" -#include "rtc_base/socketaddress.h" -#include "rtc_base/socketserver.h" -#include "rtc_base/thread.h" -#include "rtc_base/virtualsocketserver.h" - -namespace cricket { - -constexpr int kTimeoutMs = 10000; -static const rtc::IPAddress kIPv4LocalHostAddress = - rtc::IPAddress(0x7F000001); // 127.0.0.1 - -class UdpTransportTest : public testing::Test, public sigslot::has_slots<> { - public: - UdpTransportTest() - : virtual_socket_server_(new rtc::VirtualSocketServer()), - network_thread_(virtual_socket_server_.get()), - ep1_("Name1", - std::unique_ptr( - socket_factory_.CreateUdpSocket( - rtc::SocketAddress(rtc::GetAnyIP(AF_INET), 0), - 0, - 0))), - ep2_("Name2", - std::unique_ptr( - socket_factory_.CreateUdpSocket( - rtc::SocketAddress(rtc::GetAnyIP(AF_INET), 0), - 0, - 0))) { - // Setup IP Address for outgoing packets from sockets bound to IPV4 - // INADDR_ANY ("0.0.0.0."), as used above when creating the virtual - // sockets. The virtual socket server sends these packets only if the - // default address is explicit set. With a physical socket, the actual - // network stack / operating system would set the IP address for outgoing - // packets. - virtual_socket_server_->SetDefaultRoute(kIPv4LocalHostAddress); - } - - struct Endpoint : public sigslot::has_slots<> { - explicit Endpoint(std::string tch_name, - std::unique_ptr socket) { - ch_.reset(new UdpTransport(std::move(tch_name), std::move(socket))); - ch_->SignalReadPacket.connect(this, &Endpoint::OnReadPacket); - ch_->SignalSentPacket.connect(this, &Endpoint::OnSentPacket); - ch_->SignalReadyToSend.connect(this, &Endpoint::OnReadyToSend); - ch_->SignalWritableState.connect(this, &Endpoint::OnWritableState); - } - - bool CheckData(const char* data, int len) { - bool ret = false; - if (!ch_packets_.empty()) { - std::string packet = ch_packets_.front(); - ret = (packet == std::string(data, len)); - ch_packets_.pop_front(); - } - return ret; - } - - void OnWritableState(rtc::PacketTransportInternal* transport) { - num_sig_writable_++; - } - - void OnReadyToSend(rtc::PacketTransportInternal* transport) { - num_sig_ready_to_send_++; - } - - void OnReadPacket(rtc::PacketTransportInternal* transport, - const char* data, - size_t len, - const int64_t& /* packet_time_us */, - int flags) { - num_received_packets_++; - RTC_LOG(LS_VERBOSE) << "OnReadPacket (unittest)"; - ch_packets_.push_front(std::string(data, len)); - } - - void OnSentPacket(rtc::PacketTransportInternal* transport, - const rtc::SentPacket&) { - num_sig_sent_packets_++; - } - - int SendData(const char* data, size_t len) { - rtc::PacketOptions options; - return ch_->SendPacket(data, len, options, 0); - } - - void GetLocalPort(uint16_t* local_port) { - *local_port = ch_->GetLocalAddress().port(); - } - - std::list ch_packets_; - std::unique_ptr ch_; - uint32_t num_received_packets_ = 0; // Increases on SignalReadPacket. - uint32_t num_sig_sent_packets_ = 0; // Increases on SignalSentPacket. - uint32_t num_sig_writable_ = 0; // Increases on SignalWritable. - uint32_t num_sig_ready_to_send_ = 0; // Increases on SignalReadyToSend. - }; - - std::unique_ptr virtual_socket_server_; - rtc::AutoSocketServerThread network_thread_; - // Uses current thread's socket server, which will be set by ss_scope_. - rtc::BasicPacketSocketFactory socket_factory_; - - Endpoint ep1_; - Endpoint ep2_; - - void TestSendRecv() { - for (uint32_t i = 0; i < 5; ++i) { - static const char* data = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; - int len = static_cast(strlen(data)); - // local_channel <==> remote_channel - EXPECT_EQ_WAIT(len, ep1_.SendData(data, len), kTimeoutMs); - EXPECT_TRUE_WAIT(ep2_.CheckData(data, len), kTimeoutMs); - EXPECT_EQ_WAIT(i + 1u, ep2_.num_received_packets_, kTimeoutMs); - EXPECT_EQ_WAIT(len, ep2_.SendData(data, len), kTimeoutMs); - EXPECT_TRUE_WAIT(ep1_.CheckData(data, len), kTimeoutMs); - EXPECT_EQ_WAIT(i + 1u, ep1_.num_received_packets_, kTimeoutMs); - } - } -}; - -TEST_F(UdpTransportTest, AddressGetters) { - // Initially, remote address should be nil but local address shouldn't be. - EXPECT_FALSE(ep1_.ch_->GetLocalAddress().IsNil()); - EXPECT_TRUE(ep1_.ch_->GetRemoteAddress().IsNil()); - rtc::SocketAddress destination("127.0.0.1", 1337); - ASSERT_TRUE(ep1_.ch_->SetRemoteAddress(destination)); - EXPECT_EQ(destination, ep1_.ch_->GetRemoteAddress()); -} - -// Setting an invalid address should fail and have no effect. -TEST_F(UdpTransportTest, SettingIncompleteRemoteAddressFails) { - EXPECT_FALSE(ep1_.ch_->SetRemoteAddress(rtc::SocketAddress("127.0.0.1", 0))); - EXPECT_TRUE(ep1_.ch_->GetRemoteAddress().IsNil()); -} - -TEST_F(UdpTransportTest, SendRecvBasic) { - uint16_t port; - ep2_.GetLocalPort(&port); - rtc::SocketAddress addr2 = rtc::SocketAddress("127.0.0.1", port); - EXPECT_TRUE(ep1_.ch_->SetRemoteAddress(addr2)); - ep1_.GetLocalPort(&port); - rtc::SocketAddress addr1 = rtc::SocketAddress("127.0.0.1", port); - EXPECT_TRUE(ep2_.ch_->SetRemoteAddress(addr1)); - TestSendRecv(); -} - -// Test the signals and state methods used internally by causing a UdpTransport -// to send a packet to itself. -TEST_F(UdpTransportTest, StatusAndSignals) { - EXPECT_EQ(0u, ep1_.num_sig_writable_); - EXPECT_EQ(0u, ep1_.num_sig_ready_to_send_); - // Loopback - EXPECT_TRUE(!ep1_.ch_->writable()); - rtc::SocketAddress addr = ep1_.ch_->GetLocalAddress(); - // Keep port, but explicitly set IP. - addr.SetIP("127.0.0.1"); - ep1_.ch_->SetRemoteAddress(addr); - EXPECT_TRUE(ep1_.ch_->writable()); - EXPECT_EQ(1u, ep1_.num_sig_writable_); - EXPECT_EQ(1u, ep1_.num_sig_ready_to_send_); - const char data[] = "abc"; - ep1_.SendData(data, sizeof(data)); - EXPECT_EQ_WAIT(1u, ep1_.ch_packets_.size(), kTimeoutMs); - EXPECT_EQ_WAIT(1u, ep1_.num_sig_sent_packets_, kTimeoutMs); -} - -} // namespace cricket