Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 /* | |
| 2 * Copyright 2016 The WebRTC project authors. All Rights Reserved. | |
| 3 * | |
| 4 * Use of this source code is governed by a BSD-style license | |
| 5 * that can be found in the LICENSE file in the root of the source | |
| 6 * tree. An additional intellectual property rights grant can be found | |
| 7 * in the file PATENTS. All contributing project authors may | |
| 8 * be found in the AUTHORS file in the root of the source tree. | |
| 9 */ | |
| 10 | |
| 11 #include "webrtc/p2p/base/udptransportchannel.h" | |
| 12 | |
| 13 #include <string> | |
| 14 | |
| 15 #include "webrtc/base/asyncudpsocket.h" | |
| 16 #include "webrtc/base/asyncpacketsocket.h" | |
| 17 #include "webrtc/base/logging.h" | |
| 18 #include "webrtc/base/physicalsocketserver.h" | |
| 19 #include "webrtc/base/socketaddress.h" | |
| 20 #include "webrtc/base/thread.h" | |
| 21 #include "webrtc/base/thread_checker.h" | |
| 22 #include "webrtc/p2p/base/basicpacketsocketfactory.h" | |
| 23 #include "webrtc/p2p/base/packettransportinterface.h" | |
| 24 | |
| 25 namespace cricket { | |
| 26 | |
| 27 UdpTransportChannel::UdpTransportChannel(const std::string& transport_name, | |
| 28 int component) | |
| 29 : UdpTransportChannel(transport_name, | |
| 30 component, | |
| 31 rtc::Thread::Current()->socketserver()) {} | |
| 32 | |
| 33 UdpTransportChannel::UdpTransportChannel(const std::string& transport_name, | |
| 34 int component, | |
| 35 rtc::SocketServer* socket_server) | |
| 36 : transport_name_(transport_name), | |
| 37 component_(component), | |
| 38 socket_server_(socket_server) { | |
| 39 UpdateDebugName(); | |
| 40 } | |
| 41 | |
| 42 UdpTransportChannel::~UdpTransportChannel() { | |
| 43 RTC_DCHECK_RUN_ON(&network_thread_checker_); | |
| 44 } | |
| 45 | |
| 46 void UdpTransportChannel::OnSocketReadPacket( | |
| 47 rtc::AsyncPacketSocket* socket, | |
| 48 const char* data, | |
| 49 size_t len, | |
| 50 const rtc::SocketAddress& remote_addr, | |
| 51 const rtc::PacketTime& packet_time) { | |
| 52 // No thread_checker in high frequency network function. | |
| 53 SignalReadPacket(this, data, len, packet_time, 0); | |
| 54 } | |
| 55 | |
| 56 void UdpTransportChannel::OnSocketSentPacket(rtc::AsyncPacketSocket* socket, | |
| 57 const rtc::SentPacket& packet) { | |
| 58 RTC_DCHECK_EQ(socket_.get(), socket); | |
| 59 SignalSentPacket(this, packet); | |
| 60 } | |
| 61 | |
| 62 void UdpTransportChannel::UpdateDebugName() { | |
| 63 debug_name_ = transport_name_ + " " + std::to_string(component_); | |
| 64 } | |
| 65 | |
| 66 bool UdpTransportChannel::writable() const { | |
| 67 return state_ == UDPTRANSPORT_STATE_COMPLETED; | |
| 68 } | |
| 69 | |
| 70 int UdpTransportChannel::SendPacket(const char* data, | |
| 71 size_t len, | |
| 72 const rtc::PacketOptions& options, | |
| 73 int flags) { | |
| 74 // No thread_checker in high frequency network function. | |
| 75 if (!remote_parameters_) { | |
| 76 LOG(LS_WARNING) << "Remote parameters not set."; | |
| 77 send_error_ = ENOTCONN; | |
| 78 return -1; | |
| 79 } | |
| 80 const rtc::SocketAddress& remote_addr_ = *remote_parameters_; | |
| 81 int result = socket_->SendTo((const void*)data, len, remote_addr_, options); | |
| 82 if (result <= 0) { | |
| 83 LOG(LS_VERBOSE) << "SendPacket() " << result; | |
| 84 } | |
| 85 return result; | |
| 86 } | |
| 87 | |
| 88 void UdpTransportChannel::CreateSocket() { | |
| 89 RTC_DCHECK_RUN_ON(&network_thread_checker_); | |
| 90 if ((state_ == UDPTRANSPORT_STATE_CONNECTING) || | |
| 91 (state_ == UDPTRANSPORT_STATE_COMPLETED)) { | |
| 92 LOG(LS_WARNING) << "Local socket already allocated."; | |
| 93 return; | |
| 94 } | |
| 95 static constexpr uint16_t kMaxTries = 100; | |
| 96 static constexpr uint16_t kMinPortNumber = 2000; | |
| 97 // TODO(johan) provide configuration option for kMinPortNumber. | |
| 98 rtc::SocketAddress socket_addr("0.0.0.0", 0); | |
| 99 // rtc::SocketAddress socket_addr("127.0.0.1", 0); | |
|
Taylor Brandstetter
2016/11/04 17:51:07
Can remove this comment
| |
| 100 // TODO(johan): Replace BasicPacketSocketFactory by something that honors RFC | |
| 101 // 3550 Section 11 port number requirements like | |
| 102 // {port_{RTP} is even, port_{RTCP} := port{RTP} + 1}. | |
| 103 // This could be a SetSocket(...) method for sockets allocated at a higher | |
| 104 // control level. | |
| 105 rtc::BasicPacketSocketFactory socket_factory(socket_server_); | |
| 106 socket_.reset(socket_factory.CreateUdpSocket(socket_addr, kMinPortNumber, | |
| 107 kMinPortNumber + kMaxTries)); | |
| 108 if (socket_) { | |
| 109 uint16_t port = socket_->GetLocalAddress().port(); | |
| 110 LOG(INFO) << "Created UDP socket with addr " | |
| 111 << socket_->GetLocalAddress().ipaddr() << " port " << port << "."; | |
| 112 socket_->SignalReadPacket.connect(this, | |
| 113 &UdpTransportChannel::OnSocketReadPacket); | |
| 114 socket_->SignalSentPacket.connect(this, | |
| 115 &UdpTransportChannel::OnSocketSentPacket); | |
| 116 socket_addr.SetPort(port); | |
| 117 local_parameters_ = rtc::Optional<rtc::SocketAddress>(socket_addr); | |
| 118 } else { | |
| 119 LOG(INFO) << "Local socket allocation failure"; | |
| 120 } | |
| 121 UpdateState(); | |
| 122 return; | |
| 123 } | |
| 124 | |
| 125 void UdpTransportChannel::UpdateState() { | |
| 126 RTC_DCHECK_RUN_ON(&network_thread_checker_); | |
| 127 UdpTransportState state; | |
| 128 if (local_parameters_ && !socket_) { | |
| 129 LOG(INFO) << "local address is set, but socket is null"; | |
| 130 state = UDPTRANSPORT_STATE_FAILED; | |
| 131 } else if (!local_parameters_ && !remote_parameters_) { | |
| 132 state = UDPTRANSPORT_STATE_INIT; | |
| 133 } else if (local_parameters_ && !remote_parameters_) { | |
| 134 state = UDPTRANSPORT_STATE_CONNECTING; | |
| 135 } else if (local_parameters_ && remote_parameters_) { | |
| 136 state = UDPTRANSPORT_STATE_COMPLETED; | |
| 137 } else { | |
| 138 state = UDPTRANSPORT_STATE_FAILED; | |
| 139 } | |
| 140 SetTransportChannelState(state); | |
| 141 } | |
| 142 | |
| 143 void UdpTransportChannel::SetRemoteParameters(const rtc::SocketAddress& addr) { | |
| 144 RTC_DCHECK_RUN_ON(&network_thread_checker_); | |
| 145 if (!addr.IsComplete()) { | |
| 146 LOG(INFO) << "remote address not complete"; | |
| 147 return; | |
| 148 } | |
| 149 // TODO(johan) check for ipv4, other settings. | |
| 150 remote_parameters_ = rtc::Optional<rtc::SocketAddress>(addr); | |
| 151 UpdateState(); | |
| 152 } | |
| 153 | |
| 154 const rtc::SocketAddress& UdpTransportChannel::local_parameters() { | |
| 155 static const rtc::SocketAddress default_addr; | |
| 156 return local_parameters_.value_or(default_addr); | |
| 157 } | |
| 158 | |
| 159 void UdpTransportChannel::SetTransportChannelState(UdpTransportState state) { | |
| 160 RTC_DCHECK_RUN_ON(&network_thread_checker_); | |
| 161 if (state_ == state) { | |
| 162 return; | |
| 163 } | |
| 164 state_ = state; | |
| 165 if (state == UDPTRANSPORT_STATE_COMPLETED) { | |
| 166 SignalWritableState(this); | |
| 167 SignalReadyToSend(this); | |
| 168 } | |
| 169 } | |
| 170 } // namespace cricket | |
| OLD | NEW |