Add AsyncListenSocket, as alias for AsyncPacketSocket

A preparation for splitting server sockets out into a separate
interface, see https://webrtc-review.googlesource.com/c/src/+/232607.

Transition plan:

1. Land this cl.

2. Update downstream code to use the new name.

3. Attempt landing
   https://webrtc-review.googlesource.com/c/src/+/232607. May need
   additional steps to not break downstream implementations of
   PacketSocketFactory::CreateServerTcpSocket.

Bug: webrtc:13065
Change-Id: Ife448c705222f4c9f66a096e3dc7eb07e0f9c3af
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/233700
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Commit-Queue: Niels Moller <nisse@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#35155}
This commit is contained in:
Niels Möller 2021-10-06 11:19:03 +02:00 committed by WebRTC LUCI CQ
parent a0577605b0
commit 6d19d14c26
9 changed files with 37 additions and 31 deletions

View file

@ -59,7 +59,7 @@ class RTC_EXPORT PacketSocketFactory {
virtual AsyncPacketSocket* CreateUdpSocket(const SocketAddress& address, virtual AsyncPacketSocket* CreateUdpSocket(const SocketAddress& address,
uint16_t min_port, uint16_t min_port,
uint16_t max_port) = 0; uint16_t max_port) = 0;
virtual AsyncPacketSocket* CreateServerTcpSocket( virtual AsyncListenSocket* CreateServerTcpSocket(
const SocketAddress& local_address, const SocketAddress& local_address,
uint16_t min_port, uint16_t min_port,
uint16_t max_port, uint16_t max_port,

View file

@ -105,7 +105,7 @@ class AsyncStunTCPSocketTest : public ::testing::Test,
++sent_packets_; ++sent_packets_;
} }
void OnNewConnection(rtc::AsyncPacketSocket* /*server*/, void OnNewConnection(rtc::AsyncListenSocket* /*server*/,
rtc::AsyncPacketSocket* new_socket) { rtc::AsyncPacketSocket* new_socket) {
recv_socket_ = absl::WrapUnique(new_socket); recv_socket_ = absl::WrapUnique(new_socket);
new_socket->SignalReadPacket.connect(this, new_socket->SignalReadPacket.connect(this,
@ -133,7 +133,7 @@ class AsyncStunTCPSocketTest : public ::testing::Test,
std::unique_ptr<rtc::VirtualSocketServer> vss_; std::unique_ptr<rtc::VirtualSocketServer> vss_;
rtc::AutoSocketServerThread thread_; rtc::AutoSocketServerThread thread_;
std::unique_ptr<AsyncStunTCPSocket> send_socket_; std::unique_ptr<AsyncStunTCPSocket> send_socket_;
std::unique_ptr<rtc::AsyncPacketSocket> listen_socket_; std::unique_ptr<rtc::AsyncListenSocket> listen_socket_;
std::unique_ptr<rtc::AsyncPacketSocket> recv_socket_; std::unique_ptr<rtc::AsyncPacketSocket> recv_socket_;
std::list<std::string> recv_packets_; std::list<std::string> recv_packets_;
int sent_packets_ = 0; int sent_packets_ = 0;

View file

@ -50,7 +50,7 @@ AsyncPacketSocket* BasicPacketSocketFactory::CreateUdpSocket(
return new AsyncUDPSocket(socket); return new AsyncUDPSocket(socket);
} }
AsyncPacketSocket* BasicPacketSocketFactory::CreateServerTcpSocket( AsyncListenSocket* BasicPacketSocketFactory::CreateServerTcpSocket(
const SocketAddress& local_address, const SocketAddress& local_address,
uint16_t min_port, uint16_t min_port,
uint16_t max_port, uint16_t max_port,

View file

@ -36,7 +36,7 @@ class BasicPacketSocketFactory : public PacketSocketFactory {
AsyncPacketSocket* CreateUdpSocket(const SocketAddress& local_address, AsyncPacketSocket* CreateUdpSocket(const SocketAddress& local_address,
uint16_t min_port, uint16_t min_port,
uint16_t max_port) override; uint16_t max_port) override;
AsyncPacketSocket* CreateServerTcpSocket(const SocketAddress& local_address, AsyncListenSocket* CreateServerTcpSocket(const SocketAddress& local_address,
uint16_t min_port, uint16_t min_port,
uint16_t max_port, uint16_t max_port,
int opts) override; int opts) override;

View file

@ -66,6 +66,7 @@
#include "test/field_trial.h" #include "test/field_trial.h"
#include "test/gtest.h" #include "test/gtest.h"
using rtc::AsyncListenSocket;
using rtc::AsyncPacketSocket; using rtc::AsyncPacketSocket;
using rtc::ByteBufferReader; using rtc::ByteBufferReader;
using rtc::ByteBufferWriter; using rtc::ByteBufferWriter;
@ -970,12 +971,12 @@ class FakePacketSocketFactory : public rtc::PacketSocketFactory {
return result; return result;
} }
AsyncPacketSocket* CreateServerTcpSocket(const SocketAddress& local_address, AsyncListenSocket* CreateServerTcpSocket(const SocketAddress& local_address,
uint16_t min_port, uint16_t min_port,
uint16_t max_port, uint16_t max_port,
int opts) override { int opts) override {
EXPECT_TRUE(next_server_tcp_socket_ != NULL); EXPECT_TRUE(next_server_tcp_socket_ != NULL);
AsyncPacketSocket* result = next_server_tcp_socket_; AsyncListenSocket* result = next_server_tcp_socket_;
next_server_tcp_socket_ = NULL; next_server_tcp_socket_ = NULL;
return result; return result;
} }
@ -995,7 +996,7 @@ class FakePacketSocketFactory : public rtc::PacketSocketFactory {
void set_next_udp_socket(AsyncPacketSocket* next_udp_socket) { void set_next_udp_socket(AsyncPacketSocket* next_udp_socket) {
next_udp_socket_ = next_udp_socket; next_udp_socket_ = next_udp_socket;
} }
void set_next_server_tcp_socket(AsyncPacketSocket* next_server_tcp_socket) { void set_next_server_tcp_socket(AsyncListenSocket* next_server_tcp_socket) {
next_server_tcp_socket_ = next_server_tcp_socket; next_server_tcp_socket_ = next_server_tcp_socket;
} }
void set_next_client_tcp_socket(AsyncPacketSocket* next_client_tcp_socket) { void set_next_client_tcp_socket(AsyncPacketSocket* next_client_tcp_socket) {
@ -1008,7 +1009,7 @@ class FakePacketSocketFactory : public rtc::PacketSocketFactory {
private: private:
AsyncPacketSocket* next_udp_socket_; AsyncPacketSocket* next_udp_socket_;
AsyncPacketSocket* next_server_tcp_socket_; AsyncListenSocket* next_server_tcp_socket_;
absl::optional<AsyncPacketSocket*> next_client_tcp_socket_; absl::optional<AsyncPacketSocket*> next_client_tcp_socket_;
}; };

View file

@ -71,6 +71,7 @@
#include <vector> #include <vector>
#include "absl/algorithm/container.h" #include "absl/algorithm/container.h"
#include "absl/memory/memory.h"
#include "p2p/base/p2p_constants.h" #include "p2p/base/p2p_constants.h"
#include "rtc_base/checks.h" #include "rtc_base/checks.h"
#include "rtc_base/ip_address.h" #include "rtc_base/ip_address.h"
@ -99,7 +100,6 @@ TCPPort::TCPPort(rtc::Thread* thread,
username, username,
password), password),
allow_listen_(allow_listen), allow_listen_(allow_listen),
socket_(NULL),
error_(0) { error_(0) {
// TODO(mallinath) - Set preference value as per RFC 6544. // TODO(mallinath) - Set preference value as per RFC 6544.
// http://b/issue?id=7141794 // http://b/issue?id=7141794
@ -109,7 +109,7 @@ TCPPort::TCPPort(rtc::Thread* thread,
} }
TCPPort::~TCPPort() { TCPPort::~TCPPort() {
delete socket_; listen_socket_ = nullptr;
std::list<Incoming>::iterator it; std::list<Incoming>::iterator it;
for (it = incoming_.begin(); it != incoming_.end(); ++it) for (it = incoming_.begin(); it != incoming_.end(); ++it)
delete it->socket; delete it->socket;
@ -165,16 +165,16 @@ Connection* TCPPort::CreateConnection(const Candidate& address,
} }
void TCPPort::PrepareAddress() { void TCPPort::PrepareAddress() {
if (socket_) { if (listen_socket_) {
// Socket may be in the CLOSED state if Listen() // Socket may be in the CLOSED state if Listen()
// failed, we still want to add the socket address. // failed, we still want to add the socket address.
RTC_LOG(LS_VERBOSE) << "Preparing TCP address, current state: " RTC_LOG(LS_VERBOSE) << "Preparing TCP address, current state: "
<< socket_->GetState(); << listen_socket_->GetState();
if (socket_->GetState() == rtc::AsyncPacketSocket::STATE_BOUND || if (listen_socket_->GetState() == rtc::AsyncPacketSocket::STATE_BOUND ||
socket_->GetState() == rtc::AsyncPacketSocket::STATE_CLOSED) listen_socket_->GetState() == rtc::AsyncPacketSocket::STATE_CLOSED)
AddAddress(socket_->GetLocalAddress(), socket_->GetLocalAddress(), AddAddress(listen_socket_->GetLocalAddress(),
rtc::SocketAddress(), TCP_PROTOCOL_NAME, "", listen_socket_->GetLocalAddress(), rtc::SocketAddress(),
TCPTYPE_PASSIVE_STR, LOCAL_PORT_TYPE, TCP_PROTOCOL_NAME, "", TCPTYPE_PASSIVE_STR, LOCAL_PORT_TYPE,
ICE_TYPE_PREFERENCE_HOST_TCP, 0, "", true); ICE_TYPE_PREFERENCE_HOST_TCP, 0, "", true);
} else { } else {
RTC_LOG(LS_INFO) << ToString() RTC_LOG(LS_INFO) << ToString()
@ -245,16 +245,16 @@ int TCPPort::SendTo(const void* data,
} }
int TCPPort::GetOption(rtc::Socket::Option opt, int* value) { int TCPPort::GetOption(rtc::Socket::Option opt, int* value) {
if (socket_) { if (listen_socket_) {
return socket_->GetOption(opt, value); return listen_socket_->GetOption(opt, value);
} else { } else {
return SOCKET_ERROR; return SOCKET_ERROR;
} }
} }
int TCPPort::SetOption(rtc::Socket::Option opt, int value) { int TCPPort::SetOption(rtc::Socket::Option opt, int value) {
if (socket_) { if (listen_socket_) {
return socket_->SetOption(opt, value); return listen_socket_->SetOption(opt, value);
} else { } else {
return SOCKET_ERROR; return SOCKET_ERROR;
} }
@ -272,9 +272,9 @@ ProtocolType TCPPort::GetProtocol() const {
return PROTO_TCP; return PROTO_TCP;
} }
void TCPPort::OnNewConnection(rtc::AsyncPacketSocket* socket, void TCPPort::OnNewConnection(rtc::AsyncListenSocket* socket,
rtc::AsyncPacketSocket* new_socket) { rtc::AsyncPacketSocket* new_socket) {
RTC_DCHECK(socket == socket_); RTC_DCHECK(socket == listen_socket_.get());
Incoming incoming; Incoming incoming;
incoming.addr = new_socket->GetRemoteAddress(); incoming.addr = new_socket->GetRemoteAddress();
@ -289,16 +289,16 @@ void TCPPort::OnNewConnection(rtc::AsyncPacketSocket* socket,
} }
void TCPPort::TryCreateServerSocket() { void TCPPort::TryCreateServerSocket() {
socket_ = socket_factory()->CreateServerTcpSocket( listen_socket_ = absl::WrapUnique(socket_factory()->CreateServerTcpSocket(
rtc::SocketAddress(Network()->GetBestIP(), 0), min_port(), max_port(), rtc::SocketAddress(Network()->GetBestIP(), 0), min_port(), max_port(),
false /* ssl */); false /* ssl */));
if (!socket_) { if (!listen_socket_) {
RTC_LOG(LS_WARNING) RTC_LOG(LS_WARNING)
<< ToString() << ToString()
<< ": TCP server socket creation failed; continuing anyway."; << ": TCP server socket creation failed; continuing anyway.";
return; return;
} }
socket_->SignalNewConnection.connect(this, &TCPPort::OnNewConnection); listen_socket_->SignalNewConnection.connect(this, &TCPPort::OnNewConnection);
} }
rtc::AsyncPacketSocket* TCPPort::GetIncoming(const rtc::SocketAddress& addr, rtc::AsyncPacketSocket* TCPPort::GetIncoming(const rtc::SocketAddress& addr,

View file

@ -76,7 +76,7 @@ class TCPPort : public Port {
bool payload) override; bool payload) override;
// Accepts incoming TCP connection. // Accepts incoming TCP connection.
void OnNewConnection(rtc::AsyncPacketSocket* socket, void OnNewConnection(rtc::AsyncListenSocket* socket,
rtc::AsyncPacketSocket* new_socket); rtc::AsyncPacketSocket* new_socket);
private: private:
@ -103,7 +103,7 @@ class TCPPort : public Port {
void OnReadyToSend(rtc::AsyncPacketSocket* socket); void OnReadyToSend(rtc::AsyncPacketSocket* socket);
bool allow_listen_; bool allow_listen_;
rtc::AsyncPacketSocket* socket_; std::unique_ptr<rtc::AsyncListenSocket> listen_socket_;
int error_; int error_;
std::list<Incoming> incoming_; std::list<Incoming> incoming_;

View file

@ -135,6 +135,11 @@ class RTC_EXPORT AsyncPacketSocket : public sigslot::has_slots<> {
RTC_DISALLOW_COPY_AND_ASSIGN(AsyncPacketSocket); RTC_DISALLOW_COPY_AND_ASSIGN(AsyncPacketSocket);
}; };
// TODO(bugs.webrtc.org/13065): Intended to be broken out into a separate class,
// after downstream has adapted the new name. The main feature to move from
// AsyncPacketSocket to AsyncListenSocket is the SignalNewConnection.
using AsyncListenSocket = AsyncPacketSocket;
void CopySocketInformationToPacketInfo(size_t packet_size_bytes, void CopySocketInformationToPacketInfo(size_t packet_size_bytes,
const AsyncPacketSocket& socket_from, const AsyncPacketSocket& socket_from,
bool is_connectionless, bool is_connectionless,

View file

@ -86,7 +86,7 @@ class PacketSocketFactoryWrapper : public rtc::PacketSocketFactory {
return turn_server_->CreatePeerSocket(); return turn_server_->CreatePeerSocket();
} }
rtc::AsyncPacketSocket* CreateServerTcpSocket( rtc::AsyncListenSocket* CreateServerTcpSocket(
const rtc::SocketAddress& local_address, const rtc::SocketAddress& local_address,
uint16_t min_port, uint16_t min_port,
uint16_t max_port, uint16_t max_port,