| 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 : UdpTransportChannel(transport_name, | |
| 29 rtc::Thread::Current()->socketserver()) {} | |
| 30 | |
| 31 UdpTransportChannel::UdpTransportChannel(const std::string& transport_name, | |
| 32 rtc::SocketServer* socket_server) | |
| 33 : transport_name_(transport_name), socket_server_(socket_server) {} | |
| 34 | |
| 35 UdpTransportChannel::~UdpTransportChannel() { | |
| 36 RTC_DCHECK_RUN_ON(&network_thread_checker_); | |
| 37 } | |
| 38 | |
| 39 void UdpTransportChannel::OnSocketReadPacket( | |
| 40 rtc::AsyncPacketSocket* socket, | |
| 41 const char* data, | |
| 42 size_t len, | |
| 43 const rtc::SocketAddress& remote_addr, | |
| 44 const rtc::PacketTime& packet_time) { | |
| 45 // No thread_checker in high frequency network function. | |
| 46 SignalReadPacket(this, data, len, packet_time, 0); | |
| 47 } | |
| 48 | |
| 49 void UdpTransportChannel::OnSocketSentPacket(rtc::AsyncPacketSocket* socket, | |
| 50 const rtc::SentPacket& packet) { | |
| 51 RTC_DCHECK_EQ(socket_.get(), socket); | |
| 52 SignalSentPacket(this, packet); | |
| 53 } | |
| 54 | |
| 55 bool UdpTransportChannel::writable() const { | |
| 56 return state_ == State::CONNECTED; | |
| 57 } | |
| 58 | |
| 59 int UdpTransportChannel::SendPacket(const char* data, | |
| 60 size_t len, | |
| 61 const rtc::PacketOptions& options, | |
| 62 int flags) { | |
| 63 // No thread_checker in high frequency network function. | |
| 64 if (!remote_parameters_) { | |
| 65 LOG(LS_WARNING) << "Remote parameters not set."; | |
| 66 send_error_ = ENOTCONN; | |
| 67 return -1; | |
| 68 } | |
| 69 const rtc::SocketAddress& remote_addr_ = *remote_parameters_; | |
| 70 int result = socket_->SendTo((const void*)data, len, remote_addr_, options); | |
| 71 if (result <= 0) { | |
| 72 LOG(LS_VERBOSE) << "SendPacket() " << result; | |
| 73 } | |
| 74 return result; | |
| 75 } | |
| 76 | |
| 77 void UdpTransportChannel::Start() { | |
| 78 RTC_DCHECK_RUN_ON(&network_thread_checker_); | |
| 79 if (socket_) { | |
| 80 LOG(LS_WARNING) << "Local socket already allocated."; | |
| 81 return; | |
| 82 } | |
| 83 static constexpr uint16_t kMaxTries = 100; | |
| 84 static constexpr uint16_t kMinPortNumber = 2000; | |
| 85 // TODO(johan) provide configuration option for kMinPortNumber. | |
| 86 rtc::SocketAddress socket_addr("0.0.0.0", 0); | |
| 87 // TODO(johan): Replace BasicPacketSocketFactory by something that honors RFC | |
| 88 // 3550 Section 11 port number requirements like | |
| 89 // {port_{RTP} is even, port_{RTCP} := port{RTP} + 1}. | |
| 90 rtc::BasicPacketSocketFactory socket_factory(socket_server_); | |
| 91 socket_.reset(socket_factory.CreateUdpSocket(socket_addr, kMinPortNumber, | |
| 92 kMinPortNumber + kMaxTries)); | |
| 93 if (socket_) { | |
| 94 local_parameters_ = | |
| 95 rtc::Optional<rtc::SocketAddress>(socket_->GetLocalAddress()); | |
| 96 LOG(INFO) << "Created UDP socket with addr " << local_parameters_->ipaddr() | |
| 97 << " port " << local_parameters_->port() << "."; | |
| 98 socket_->SignalReadPacket.connect(this, | |
| 99 &UdpTransportChannel::OnSocketReadPacket); | |
| 100 socket_->SignalSentPacket.connect(this, | |
| 101 &UdpTransportChannel::OnSocketSentPacket); | |
| 102 } else { | |
| 103 LOG(INFO) << "Local socket allocation failure"; | |
| 104 } | |
| 105 UpdateState(); | |
| 106 return; | |
| 107 } | |
| 108 | |
| 109 void UdpTransportChannel::UpdateState() { | |
| 110 RTC_DCHECK_RUN_ON(&network_thread_checker_); | |
| 111 RTC_DCHECK(!(local_parameters_ && !socket_)); | |
| 112 RTC_DCHECK(!(!local_parameters_ && socket_)); | |
| 113 if (!local_parameters_) { | |
| 114 SetState(State::INIT); | |
| 115 } else if (!remote_parameters_) { | |
| 116 SetState(State::CONNECTING); | |
| 117 } else { | |
| 118 SetState(State::CONNECTED); | |
| 119 } | |
| 120 } | |
| 121 | |
| 122 void UdpTransportChannel::SetRemoteParameters(const rtc::SocketAddress& addr) { | |
| 123 RTC_DCHECK_RUN_ON(&network_thread_checker_); | |
| 124 if (!addr.IsComplete()) { | |
| 125 LOG(INFO) << "remote address not complete"; | |
| 126 return; | |
| 127 } | |
| 128 // TODO(johan) check for ipv4, other settings. | |
| 129 remote_parameters_ = rtc::Optional<rtc::SocketAddress>(addr); | |
| 130 UpdateState(); | |
| 131 } | |
| 132 | |
| 133 void UdpTransportChannel::SetState(State state) { | |
| 134 RTC_DCHECK_RUN_ON(&network_thread_checker_); | |
| 135 if (state_ == state) { | |
| 136 return; | |
| 137 } | |
| 138 state_ = state; | |
| 139 if (state == State::CONNECTED) { | |
| 140 SignalWritableState(this); | |
| 141 SignalReadyToSend(this); | |
| 142 } | |
| 143 } | |
| 144 } // namespace cricket | |
| OLD | NEW |