OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2016 The WebRTC project authors. All Rights Reserved. | 2 * Copyright 2016 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/p2p/base/udptransportchannel.h" | 11 #include "webrtc/p2p/base/udptransportchannel.h" |
12 | 12 |
13 #include <string> | 13 #include <string> |
14 | 14 |
15 #include "webrtc/base/asyncudpsocket.h" | 15 #include "webrtc/base/asyncudpsocket.h" |
16 #include "webrtc/base/asyncpacketsocket.h" | 16 #include "webrtc/base/asyncpacketsocket.h" |
17 #include "webrtc/base/logging.h" | 17 #include "webrtc/base/logging.h" |
18 #include "webrtc/base/physicalsocketserver.h" | |
19 #include "webrtc/base/socketaddress.h" | 18 #include "webrtc/base/socketaddress.h" |
20 #include "webrtc/base/thread.h" | 19 #include "webrtc/base/thread.h" |
21 #include "webrtc/base/thread_checker.h" | 20 #include "webrtc/base/thread_checker.h" |
22 #include "webrtc/p2p/base/basicpacketsocketfactory.h" | |
23 #include "webrtc/p2p/base/packettransportinterface.h" | |
24 | 21 |
25 namespace cricket { | 22 namespace cricket { |
26 | 23 |
27 UdpTransportChannel::UdpTransportChannel(const std::string& transport_name) | 24 UdpTransport::UdpTransport(const std::string& transport_name, |
28 : UdpTransportChannel(transport_name, | 25 std::unique_ptr<rtc::AsyncPacketSocket> socket) |
29 rtc::Thread::Current()->socketserver()) {} | 26 : transport_name_(transport_name), socket_(std::move(socket)) { |
| 27 RTC_DCHECK(socket_); |
| 28 socket_->SignalReadPacket.connect(this, &UdpTransport::OnSocketReadPacket); |
| 29 socket_->SignalSentPacket.connect(this, &UdpTransport::OnSocketSentPacket); |
| 30 } |
30 | 31 |
31 UdpTransportChannel::UdpTransportChannel(const std::string& transport_name, | 32 UdpTransport::~UdpTransport() { |
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_); | 33 RTC_DCHECK_RUN_ON(&network_thread_checker_); |
37 } | 34 } |
38 | 35 |
39 void UdpTransportChannel::OnSocketReadPacket( | 36 rtc::SocketAddress UdpTransport::GetLocalAddress() const { |
40 rtc::AsyncPacketSocket* socket, | 37 RTC_DCHECK_RUN_ON(&network_thread_checker_); |
41 const char* data, | 38 return socket_->GetLocalAddress(); |
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 } | 39 } |
48 | 40 |
49 void UdpTransportChannel::OnSocketSentPacket(rtc::AsyncPacketSocket* socket, | 41 bool UdpTransport::SetRemoteAddress(const rtc::SocketAddress& addr) { |
50 const rtc::SentPacket& packet) { | 42 RTC_DCHECK_RUN_ON(&network_thread_checker_); |
51 RTC_DCHECK_EQ(socket_.get(), socket); | 43 if (!addr.IsComplete()) { |
52 SignalSentPacket(this, packet); | 44 LOG(LS_WARNING) << "RemoteAddress address not complete."; |
| 45 return false; |
| 46 } |
| 47 // TODO(johan): check for ipv4, other settings. |
| 48 bool prev_destination_nil = remote_address_.IsNil(); |
| 49 remote_address_ = addr; |
| 50 // Going from "didn't have destination" to "have destination" or vice versa. |
| 51 if (prev_destination_nil != remote_address_.IsNil()) { |
| 52 SignalWritableState(this); |
| 53 if (prev_destination_nil) { |
| 54 SignalReadyToSend(this); |
| 55 } |
| 56 } |
| 57 return true; |
53 } | 58 } |
54 | 59 |
55 bool UdpTransportChannel::writable() const { | 60 rtc::SocketAddress UdpTransport::GetRemoteAddress() const { |
56 return state_ == State::CONNECTED; | 61 RTC_DCHECK_RUN_ON(&network_thread_checker_); |
| 62 return remote_address_; |
57 } | 63 } |
58 | 64 |
59 int UdpTransportChannel::SendPacket(const char* data, | 65 bool UdpTransport::writable() const { |
60 size_t len, | 66 RTC_DCHECK_RUN_ON(&network_thread_checker_); |
61 const rtc::PacketOptions& options, | 67 return !remote_address_.IsNil(); |
62 int flags) { | 68 } |
| 69 |
| 70 int UdpTransport::SendPacket(const char* data, |
| 71 size_t len, |
| 72 const rtc::PacketOptions& options, |
| 73 int flags) { |
63 // No thread_checker in high frequency network function. | 74 // No thread_checker in high frequency network function. |
64 if (!remote_parameters_) { | 75 if (remote_address_.IsNil()) { |
65 LOG(LS_WARNING) << "Remote parameters not set."; | 76 LOG(LS_WARNING) << "Remote address not set."; |
66 send_error_ = ENOTCONN; | 77 send_error_ = ENOTCONN; |
67 return -1; | 78 return -1; |
68 } | 79 } |
69 const rtc::SocketAddress& remote_addr_ = *remote_parameters_; | 80 int result = |
70 int result = socket_->SendTo((const void*)data, len, remote_addr_, options); | 81 socket_->SendTo((const void*)data, len, remote_address_, options); |
71 if (result <= 0) { | 82 if (result <= 0) { |
72 LOG(LS_VERBOSE) << "SendPacket() " << result; | 83 LOG(LS_VERBOSE) << "SendPacket() " << result; |
73 } | 84 } |
74 return result; | 85 return result; |
75 } | 86 } |
76 | 87 |
77 void UdpTransportChannel::Start() { | 88 void UdpTransport::OnSocketReadPacket(rtc::AsyncPacketSocket* socket, |
78 RTC_DCHECK_RUN_ON(&network_thread_checker_); | 89 const char* data, |
79 if (socket_) { | 90 size_t len, |
80 LOG(LS_WARNING) << "Local socket already allocated."; | 91 const rtc::SocketAddress& remote_addr, |
81 return; | 92 const rtc::PacketTime& packet_time) { |
82 } | 93 // No thread_checker in high frequency network function. |
83 static constexpr uint16_t kMaxTries = 100; | 94 SignalReadPacket(this, data, len, packet_time, 0); |
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 } | 95 } |
108 | 96 |
109 void UdpTransportChannel::UpdateState() { | 97 void UdpTransport::OnSocketSentPacket(rtc::AsyncPacketSocket* socket, |
110 RTC_DCHECK_RUN_ON(&network_thread_checker_); | 98 const rtc::SentPacket& packet) { |
111 RTC_DCHECK(!(local_parameters_ && !socket_)); | 99 RTC_DCHECK_EQ(socket_.get(), socket); |
112 RTC_DCHECK(!(!local_parameters_ && socket_)); | 100 SignalSentPacket(this, packet); |
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 } | 101 } |
121 | 102 |
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 | 103 } // namespace cricket |
OLD | NEW |