不过该方式很少用,一般都是用udp的candidate或者turn的candidate。

调用栈

image.png

AllocationSequence::CreateTCPPorts

image.png

  1. void AllocationSequence::CreateTCPPorts() {
  2. if (IsFlagSet(PORTALLOCATOR_DISABLE_TCP)) {
  3. RTC_LOG(LS_VERBOSE) << "AllocationSequence: TCP ports disabled, skipping.";
  4. return;
  5. }
  6. std::unique_ptr<Port> port = TCPPort::Create(
  7. session_->network_thread(), session_->socket_factory(), network_,
  8. session_->allocator()->min_port(), session_->allocator()->max_port(),
  9. session_->username(), session_->password(),
  10. session_->allocator()->allow_tcp_listen());
  11. if (port) {
  12. session_->AddAllocatedPort(port.release(), this, true);
  13. // Since TCPPort is not created using shared socket, |port| will not be
  14. // added to the dequeue.
  15. }
  16. }

在TCPPort::Create中主要是调用了

TCPPort::TryCreateServerSocket

image.png

BasicPacketSocketFactory::CreateServerTcpSocket

image.png

  1. AsyncPacketSocket* BasicPacketSocketFactory::CreateServerTcpSocket(
  2. const SocketAddress& local_address,
  3. uint16_t min_port,
  4. uint16_t max_port,
  5. int opts) {
  6. // Fail if TLS is required.
  7. if (opts & PacketSocketFactory::OPT_TLS) {
  8. RTC_LOG(LS_ERROR) << "TLS support currently is not available.";
  9. return NULL;
  10. }
  11. AsyncSocket* socket =
  12. socket_factory()->CreateAsyncSocket(local_address.family(), SOCK_STREAM);
  13. if (!socket) {
  14. return NULL;
  15. }
  16. if (BindSocket(socket, local_address, min_port, max_port) < 0) {
  17. RTC_LOG(LS_ERROR) << "TCP bind failed with error " << socket->GetError();
  18. delete socket;
  19. return NULL;
  20. }
  21. // Set TCP_NODELAY (via OPT_NODELAY) for improved performance; this causes
  22. // small media packets to be sent immediately rather than being buffered up,
  23. // reducing latency.
  24. if (socket->SetOption(Socket::OPT_NODELAY, 1) != 0) {
  25. RTC_LOG(LS_ERROR) << "Setting TCP_NODELAY option failed with error "
  26. << socket->GetError();
  27. }
  28. // If using fake TLS, wrap the TCP socket in a pseudo-SSL socket.
  29. if (opts & PacketSocketFactory::OPT_TLS_FAKE) {
  30. RTC_DCHECK(!(opts & PacketSocketFactory::OPT_TLS));
  31. socket = new AsyncSSLSocket(socket);
  32. }
  33. if (opts & PacketSocketFactory::OPT_STUN)
  34. return new cricket::AsyncStunTCPSocket(socket, true);
  35. return new AsyncTCPSocket(socket, true);
  36. }

可见里面还包括了tls的处理

AllocationSequence::CreateTCPPorts
-》
BasicPortAllocatorSession::AddAllocatedPort
-》

TCPPort::PrepareAddress

image.png