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/logging.h" | |
17 #include "webrtc/base/physicalsocketserver.h" | |
18 #include "webrtc/base/socketaddress.h" | |
19 #include "webrtc/base/thread.h" | |
20 #include "webrtc/base/thread_checker.h" | |
21 #include "webrtc/p2p/base/basicpacketsocketfactory.h" | |
22 | |
23 namespace cricket { | |
24 | |
25 UdpTransportChannel::UdpTransportChannel(const std::string& transport_name, | |
26 int component) | |
27 : UdpTransportChannel(transport_name, | |
28 component, | |
29 rtc::Thread::Current()->socketserver()) {} | |
30 | |
31 UdpTransportChannel::UdpTransportChannel(const std::string& transport_name, | |
32 int component, | |
33 rtc::SocketServer* socket_server) | |
34 : transport_name_(transport_name), | |
35 component_(component), | |
36 socket_server_(socket_server) { | |
37 RTC_DCHECK_RUN_ON(&network_thread_checker_); | |
Taylor Brandstetter
2016/11/01 21:32:48
This isn't necessary, since the constructor is whe
johan
2016/11/02 15:14:04
Done.
| |
38 UpdateDebugName(); | |
39 } | |
40 | |
41 UdpTransportChannel::~UdpTransportChannel() { | |
42 RTC_DCHECK_RUN_ON(&network_thread_checker_); | |
43 } | |
44 | |
45 void UdpTransportChannel::OnSocketReadPacket( | |
46 rtc::AsyncPacketSocket* socket, | |
47 const char* data, | |
48 size_t len, | |
49 const rtc::SocketAddress& remote_addr, | |
50 const rtc::PacketTime& packet_time) { | |
51 // No thread_checker in high frequency network function. | |
Taylor Brandstetter
2016/11/01 21:32:48
I think a DCHECK should be fine, even if it's high
johan
2016/11/02 15:14:04
The thread checker's CalledOnValidThread() method
Taylor Brandstetter
2016/11/04 00:17:41
Ok, fair enough.
| |
52 SignalReadPacket(this, data, len, packet_time, 0); | |
53 } | |
54 | |
55 void UdpTransportChannel::OnSocketSentPacket(rtc::AsyncPacketSocket* socket, | |
56 const rtc::SentPacket& packet) { | |
57 RTC_DCHECK_EQ(socket_.get(), socket); | |
58 SignalSentPacket(this, packet); | |
59 } | |
60 | |
61 void UdpTransportChannel::UpdateDebugName() { | |
62 debug_name_ = transport_name_ + " " + std::to_string(component_); | |
pthatcher1
2016/11/02 06:19:37
Just do "return transport_name_".
johan
2016/11/02 15:14:04
Done.
| |
63 } | |
64 | |
65 bool UdpTransportChannel::writable() const { | |
66 return state_ == STATE_COMPLETED; | |
67 } | |
68 | |
69 int UdpTransportChannel::SendPacket(const char* data, | |
70 size_t len, | |
71 const rtc::PacketOptions& options, | |
72 int flags) { | |
73 // No thread_checker in high frequency network function. | |
74 if (!remote_parameters_) { | |
75 LOG(LS_WARNING) << "Remote parameters not set."; | |
76 send_error_ = ENOTCONN; | |
77 return -1; | |
78 } | |
79 const rtc::SocketAddress& remote_addr_ = remote_parameters_->address(); | |
80 int result = socket_->SendTo((const void*)data, len, remote_addr_, options); | |
81 if (result <= 0) { | |
82 LOG(LS_VERBOSE) << "SendPacket() " << result; | |
83 } | |
84 return result; | |
85 } | |
86 | |
87 void UdpTransportChannel::TryAllocateSocket() { | |
pthatcher1
2016/11/02 06:19:37
I'd call this CreateSocket, since what it does it
johan
2016/11/02 15:14:04
Done.
| |
88 RTC_DCHECK_RUN_ON(&network_thread_checker_); | |
89 if ((state_ == STATE_CONNECTING) || (state_ == STATE_COMPLETED)) { | |
90 LOG(LS_WARNING) << "Local Socket already allocated."; | |
Taylor Brandstetter
2016/11/01 21:32:48
nit: Don't need to capitalize Socket
johan
2016/11/02 15:14:04
Acknowledged.
| |
91 return; | |
92 } | |
93 static constexpr uint16_t kMaxTries = 100; | |
94 static constexpr uint16_t kMinPortNumber = 2000; | |
95 // TODO(johan) provide configuration option for kMinPortNumber. | |
96 rtc::SocketAddress socket_addr("0.0.0.0", 0); | |
97 // TODO(johan): Replace BasicPacketSocketFactory by something that honors RFC | |
98 // 3550 Section 11 port number requirements like | |
99 // {port_{RTP} is even, port_{RTCP} := port{RTP} + 1}. | |
100 // This could be a SetSocket(...) method for sockets allocated at a higher | |
101 // control level. | |
pthatcher1
2016/11/02 06:19:37
I think it should be done at the RtpSender/RtpRece
| |
102 rtc::BasicPacketSocketFactory socket_factory(socket_server_); | |
103 socket_.reset(socket_factory.CreateUdpSocket(socket_addr, kMinPortNumber, | |
104 kMinPortNumber + kMaxTries)); | |
105 if (socket_) { | |
106 uint16_t port = socket_->GetLocalAddress().port(); | |
107 LOG(INFO) << "Allocated Rtp socket with local port " << port; | |
pthatcher1
2016/11/02 06:19:37
I'd say "Created UDP socket with port "
johan
2016/11/02 15:14:04
Done.
| |
108 socket_->SignalReadPacket.connect(this, | |
109 &UdpTransportChannel::OnSocketReadPacket); | |
110 socket_->SignalSentPacket.connect(this, | |
111 &UdpTransportChannel::OnSocketSentPacket); | |
112 socket_addr.SetPort(port); | |
113 local_parameters_ = rtc::Optional<Candidate>(Candidate()); | |
114 local_parameters_->set_address(socket_addr); | |
115 } else { | |
116 LOG(INFO) << "Local socket allocation failure"; | |
117 } | |
118 UpdateState(); | |
119 return; | |
120 } | |
121 | |
122 bool UdpTransportChannel::IsLocalConsistent() { | |
123 if (!local_parameters_) { | |
124 return true; | |
125 } | |
126 if (!socket_) { | |
127 LOG(INFO) << "socket_ is false"; | |
Taylor Brandstetter
2016/11/01 21:32:48
nit: "null" instead of "false"
Also, should this e
johan
2016/11/02 15:14:03
Inlined into UpdateState() as Peter suggests.
| |
128 return false; | |
129 } | |
130 return true; | |
131 } | |
132 | |
133 bool UdpTransportChannel::IsRemoteConsistent() { | |
134 if (!remote_parameters_) { | |
135 return true; | |
136 } | |
137 if (!remote_parameters_->address().IsComplete()) { | |
Taylor Brandstetter
2016/11/01 21:32:48
Could you check for this when AddRemoteCandidate i
johan
2016/11/02 15:14:04
Can do that.
Side effect: state does not change to
| |
138 LOG(INFO) << "remote_addr_ not complete"; | |
Taylor Brandstetter
2016/11/01 21:32:48
Don't have something called "remote_addr_" any mor
johan
2016/11/02 15:14:04
Acknowledged.
| |
139 return false; | |
140 } | |
141 return true; | |
142 } | |
pthatcher1
2016/11/02 06:19:37
The above two methods are only used once. I'd pre
johan
2016/11/02 15:14:03
Inlining IsLocalConsistent() in UpdateState().
Doi
| |
143 | |
144 void UdpTransportChannel::UpdateState() { | |
145 RTC_DCHECK_RUN_ON(&network_thread_checker_); | |
146 TransportChannelState state; | |
147 if (!IsLocalConsistent()) { | |
148 state = STATE_FAILED; | |
149 } else if (!IsRemoteConsistent()) { | |
150 state = STATE_FAILED; | |
151 } else if (!local_parameters_ && !remote_parameters_) { | |
152 state = STATE_INIT; | |
153 } else if (local_parameters_ && !remote_parameters_) { | |
154 state = STATE_CONNECTING; | |
155 } else if (local_parameters_ && remote_parameters_) { | |
156 state = STATE_COMPLETED; | |
157 } else { | |
158 state = STATE_FAILED; | |
Taylor Brandstetter
2016/11/01 21:32:48
This makes the TransportChannelState seem redundan
johan
2016/11/02 15:14:04
May be redundant, but usually state dependent logi
Taylor Brandstetter
2016/11/04 00:17:41
I guess it's a matter of preference, so I'll accep
| |
159 } | |
160 SetTransportChannelState(state); | |
161 } | |
162 | |
163 void UdpTransportChannel::AddRemoteCandidate(const Candidate& candidate) { | |
164 RTC_DCHECK_RUN_ON(&network_thread_checker_); | |
165 // TODO(johan) check for ipv4, other settings. | |
166 remote_parameters_ = rtc::Optional<Candidate>(candidate); | |
167 UpdateState(); | |
168 } | |
169 | |
170 const Candidate& UdpTransportChannel::GetLocalCandidate() { | |
171 return *local_parameters_; | |
Taylor Brandstetter
2016/11/01 21:32:48
I think this will crash if local_parameters_ isn't
johan
2016/11/02 15:14:03
Oups, adding default + unit test.
| |
172 } | |
173 | |
174 void UdpTransportChannel::SetTransportChannelState( | |
175 TransportChannelState state) { | |
176 RTC_DCHECK_RUN_ON(&network_thread_checker_); | |
177 if (state_ != state) { | |
178 state_ = state; | |
pthatcher1
2016/11/02 06:19:37
Please use early returns.
johan
2016/11/02 15:14:03
Ok, returning early if (state_ == state) and resol
| |
179 if (state == STATE_COMPLETED) { | |
180 SignalWritableState(this); | |
181 SignalReadyToSend(this); | |
182 } | |
183 } | |
184 } | |
185 } // namespace cricket | |
OLD | NEW |