Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(145)

Side by Side Diff: webrtc/p2p/base/udptransportchannel.cc

Issue 2377883003: First step in providing a UdpTransportChannel. (Closed)
Patch Set: Use only one socket inside UdpTransportChannel. Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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
22 namespace cricket {
23
24 UdpTransportChannel::UdpTransportChannel(const std::string& transport_name,
25 int component)
26 : UdpTransportChannel(transport_name,
27 component,
28 rtc::Thread::Current()->socketserver()) {}
29
30 UdpTransportChannel::UdpTransportChannel(const std::string& transport_name,
31 int component,
32 rtc::SocketServer* socket_server)
33 : TransportChannelImpl(transport_name, component),
34 socket_server_(socket_server) {
35 RTC_DCHECK_RUN_ON(&network_thread_checker_);
36 }
37
38 UdpTransportChannel::~UdpTransportChannel() {
39 RTC_DCHECK_RUN_ON(&network_thread_checker_);
40 }
41
42 void UdpTransportChannel::OnSocketReadPacket(
43 rtc::AsyncPacketSocket* socket,
44 const char* data,
45 size_t len,
46 const rtc::SocketAddress& remote_addr,
47 const rtc::PacketTime& packet_time) {
48 // No thread_checker in high frequency network function.
49 SignalReadPacket(this, data, len, packet_time, 0);
50 }
51
52 int UdpTransportChannel::SendPacket(const char* data,
53 size_t len,
54 const rtc::PacketOptions& options,
55 int flags) {
56 // No thread_checker in high frequency network function.
57 if (!remote_parameters_) {
58 LOG(LS_WARNING) << "Remote parameters not set.";
59 send_error_ = ENOTCONN;
60 return -1;
61 }
62 const rtc::SocketAddress& remote_addr_ = remote_parameters_->address();
63 int result = socket_->SendTo((const void*)data, len, remote_addr_, options);
64 LOG(LS_VERBOSE) << "SendPacket() " << result;
65 return result;
66 }
67
68 bool UdpTransportChannel::TryAllocateSockets() {
69 RTC_DCHECK_RUN_ON(&network_thread_checker_);
70 static constexpr uint16_t kMaxTries = 100;
71 static constexpr uint16_t kMinPortNumber = 2000;
72 // TODO(johan) provide configuration option for kMinPortNumber.
73 rtc::SocketAddress socket_addr("0.0.0.0", 0);
74 // TODO(johan): use rtc::PacketSocketFactory.
75 for (uint16_t count = 0; count < kMaxTries; ++count) {
76 uint16_t rtpport = kMinPortNumber + (2 * count);
77 socket_addr.SetPort(rtpport);
78 socket_.reset(rtc::AsyncUDPSocket::Create(socket_server_, socket_addr));
79 if (!socket_) {
80 continue;
81 }
82 if (socket_) {
83 LOG(INFO) << "Allocated Rtp socket with local port " << rtpport;
84 socket_->SignalReadPacket.connect(
85 this, &UdpTransportChannel::OnSocketReadPacket);
86 local_parameters_ = rtc::Optional<Candidate>(Candidate());
87 local_parameters_->set_address(socket_addr);
88 return true;
89 }
90 }
91 socket_.reset();
92 LOG(INFO) << "Local socket allocation failure";
93 return false;
94 }
95
96 void UdpTransportChannel::MaybeStartGathering() {
97 RTC_DCHECK_RUN_ON(&network_thread_checker_);
98 if (gathering_state_ != kIceGatheringNew) {
99 LOG(INFO) << "candidates gathering already done, early return";
100 return;
101 }
102 SetGatheringState(kIceGatheringGathering);
103 bool gathering_success = TryAllocateSockets();
104 if (gathering_success) {
105 SignalCandidateGathered(this, *local_parameters_);
106 SetGatheringState(kIceGatheringComplete);
107 } else {
108 SetGatheringState(kIceGatheringNew);
109 }
110 UpdateState();
111 }
112
113 bool UdpTransportChannel::IsLocalConsistent() {
114 if (!local_parameters_) {
115 return true;
116 }
117 if (!socket_) {
118 LOG(INFO) << "socket_ is false";
119 return false;
120 }
121 return true;
122 }
123
124 bool UdpTransportChannel::IsRemoteConsistent() {
125 if (!remote_parameters_) {
126 return true;
127 }
128 if (!remote_parameters_->address().IsComplete()) {
129 LOG(INFO) << "remote_addr_ not complete";
130 return false;
131 }
132 return true;
133 }
134
135 void UdpTransportChannel::UpdateState() {
136 RTC_DCHECK_RUN_ON(&network_thread_checker_);
137 TransportChannelState state;
138 if (!IsLocalConsistent()) {
139 state = STATE_FAILED;
140 } else if (!IsRemoteConsistent()) {
141 state = STATE_FAILED;
142 } else if (!local_parameters_ && !remote_parameters_) {
143 state = STATE_INIT;
144 } else if (local_parameters_ && !remote_parameters_) {
145 state = STATE_CONNECTING;
146 } else if (local_parameters_ && remote_parameters_) {
147 state = STATE_COMPLETED;
148 } else {
149 state = STATE_FAILED;
150 }
151 set_writable(state == STATE_COMPLETED);
152 SetTransportChannelState(state);
153 }
154
155 void UdpTransportChannel::AddRemoteCandidate(const Candidate& candidate) {
156 RTC_DCHECK_RUN_ON(&network_thread_checker_);
157 // TODO(johan) check for ipv4, other settings.
158 remote_parameters_ = rtc::Optional<Candidate>(candidate);
159 UpdateState();
160 }
161
162 void UdpTransportChannel::SetGatheringState(IceGatheringState gathering_state) {
163 RTC_DCHECK_RUN_ON(&network_thread_checker_);
164 if (gathering_state_ != gathering_state) {
165 gathering_state_ = gathering_state;
166 SignalGatheringState(this);
167 }
168 }
169
170 void UdpTransportChannel::SetTransportChannelState(
171 TransportChannelState state) {
172 RTC_DCHECK_RUN_ON(&network_thread_checker_);
173 if (state_ != state) {
174 state_ = state;
175 SignalStateChanged(this);
176 }
177 }
178 } // namespace cricket
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698