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

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

Issue 2377883003: First step in providing a UdpTransportChannel. (Closed)
Patch Set: Fix gyp build file, part 2. Created 4 years, 1 month 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/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();
pthatcher1 2016/11/04 22:58:58 Might as well just inline this.
johan 2016/11/07 17:51:52 Ack, but removed anyway due to the removal of comp
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);
pthatcher1 2016/11/04 22:58:58 This is just as high frequency as OnSocketReadPack
johan 2016/11/07 17:51:52 Yes this is high frequency. But I see less potenti
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 }
pthatcher1 2016/11/04 22:58:58 Why wouldn't this just check if socket_ is set or
johan 2016/11/07 17:51:53 Done.
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 // TODO(johan): Replace BasicPacketSocketFactory by something that honors RFC
100 // 3550 Section 11 port number requirements like
101 // {port_{RTP} is even, port_{RTCP} := port{RTP} + 1}.
102 // This could be a SetSocket(...) method for sockets allocated at a higher
103 // control level.
pthatcher1 2016/11/04 22:58:58 Can you please remove this comment?
johan 2016/11/07 17:51:53 Removing last sentence. Keeping the Todo. That is,
104 rtc::BasicPacketSocketFactory socket_factory(socket_server_);
105 socket_.reset(socket_factory.CreateUdpSocket(socket_addr, kMinPortNumber,
106 kMinPortNumber + kMaxTries));
107 if (socket_) {
108 uint16_t port = socket_->GetLocalAddress().port();
109 LOG(INFO) << "Created UDP socket with addr "
110 << socket_->GetLocalAddress().ipaddr() << " port " << port << ".";
111 socket_->SignalReadPacket.connect(this,
112 &UdpTransportChannel::OnSocketReadPacket);
113 socket_->SignalSentPacket.connect(this,
114 &UdpTransportChannel::OnSocketSentPacket);
115 socket_addr.SetPort(port);
116 local_parameters_ = rtc::Optional<rtc::SocketAddress>(socket_addr);
pthatcher1 2016/11/04 22:58:58 Why do we even store local_parameters? Why not ju
pthatcher1 2016/11/04 22:58:58 Do this only have a port in it? Then why put in a
johan 2016/11/07 17:51:53 No, this contains both local address (which is cur
johan 2016/11/07 17:51:53 socket's GetLocalAddress seems to be a bit expensi
117 } else {
118 LOG(INFO) << "Local socket allocation failure";
119 }
120 UpdateState();
121 return;
122 }
123
124 void UdpTransportChannel::UpdateState() {
125 RTC_DCHECK_RUN_ON(&network_thread_checker_);
126 UdpTransportState state;
127 if (local_parameters_ && !socket_) {
pthatcher1 2016/11/04 22:58:58 Can this even happen? The local_parameters_ aren'
johan 2016/11/07 17:51:52 With the current implementation the situation (loc
128 LOG(INFO) << "local address is set, but socket is null";
129 state = UDPTRANSPORT_STATE_FAILED;
130 } else if (!local_parameters_ && !remote_parameters_) {
131 state = UDPTRANSPORT_STATE_INIT;
132 } else if (local_parameters_ && !remote_parameters_) {
133 state = UDPTRANSPORT_STATE_CONNECTING;
134 } else if (local_parameters_ && remote_parameters_) {
135 state = UDPTRANSPORT_STATE_COMPLETED;
136 } else {
137 state = UDPTRANSPORT_STATE_FAILED;
138 }
139 SetTransportChannelState(state);
140 }
pthatcher1 2016/11/04 22:58:58 Also, I think it could be more clear as: if (!l
johan 2016/11/07 17:51:53 Ok, that's clean. FAILED state for this situation
141
142 void UdpTransportChannel::SetRemoteParameters(const rtc::SocketAddress& addr) {
143 RTC_DCHECK_RUN_ON(&network_thread_checker_);
144 if (!addr.IsComplete()) {
145 LOG(INFO) << "remote address not complete";
146 return;
147 }
148 // TODO(johan) check for ipv4, other settings.
149 remote_parameters_ = rtc::Optional<rtc::SocketAddress>(addr);
150 UpdateState();
151 }
152
153 const rtc::SocketAddress& UdpTransportChannel::local_parameters() {
pthatcher1 2016/11/04 22:58:58 Why not just make this rtc::Optional<rtc::SocketAd
johan 2016/11/07 17:51:52 That means a copy instead of a reference. But I ag
154 static const rtc::SocketAddress default_addr;
155 return local_parameters_.value_or(default_addr);
156 }
157
158 void UdpTransportChannel::SetTransportChannelState(UdpTransportState state) {
pthatcher1 2016/11/04 22:58:58 Just SetState is a better method name.
johan 2016/11/07 17:51:53 ok
159 RTC_DCHECK_RUN_ON(&network_thread_checker_);
160 if (state_ == state) {
161 return;
162 }
163 state_ = state;
164 if (state == UDPTRANSPORT_STATE_COMPLETED) {
pthatcher1 2016/11/04 22:58:58 I think CONNECTED would be better than COMPLETED.
johan 2016/11/07 17:51:53 Renaming according to your preference.
165 SignalWritableState(this);
166 SignalReadyToSend(this);
167 }
168 }
169 } // namespace cricket
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698