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

Side by Side Diff: extensions/browser/api/socket/udp_socket.cc

Issue 2378763003: Add ability to reuse address in chrome.sockets.udp
Patch Set: 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
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "extensions/browser/api/socket/udp_socket.h" 5 #include "extensions/browser/api/socket/udp_socket.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/callback_helpers.h" 9 #include "base/callback_helpers.h"
10 #include "base/lazy_instance.h" 10 #include "base/lazy_instance.h"
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
67 return net::ERR_CONNECTION_FAILED; 67 return net::ERR_CONNECTION_FAILED;
68 68
69 net::IPEndPoint ip_end_point; 69 net::IPEndPoint ip_end_point;
70 if (!StringAndPortToIPEndPoint(address, port, &ip_end_point)) 70 if (!StringAndPortToIPEndPoint(address, port, &ip_end_point))
71 return net::ERR_INVALID_ARGUMENT; 71 return net::ERR_INVALID_ARGUMENT;
72 72
73 int result = socket_.Open(ip_end_point.GetFamily()); 73 int result = socket_.Open(ip_end_point.GetFamily());
74 if (result != net::OK) 74 if (result != net::OK)
75 return result; 75 return result;
76 76
77 if (IsReusable()) {
78 socket_.AllowAddressReuse();
79 }
80
77 result = socket_.Bind(ip_end_point); 81 result = socket_.Bind(ip_end_point);
78 if (result != net::OK) 82 if (result != net::OK)
79 socket_.Close(); 83 socket_.Close();
80 return result; 84 return result;
81 } 85 }
82 86
83 void UDPSocket::Disconnect() { 87 void UDPSocket::Disconnect() {
84 is_connected_ = false; 88 is_connected_ = false;
85 socket_.Close(); 89 socket_.Close();
86 read_callback_.Reset(); 90 read_callback_.Reset();
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after
241 } 245 }
242 246
243 void UDPSocket::OnSendToComplete(int result) { 247 void UDPSocket::OnSendToComplete(int result) {
244 DCHECK(!send_to_callback_.is_null()); 248 DCHECK(!send_to_callback_.is_null());
245 send_to_callback_.Run(result); 249 send_to_callback_.Run(result);
246 send_to_callback_.Reset(); 250 send_to_callback_.Reset();
247 } 251 }
248 252
249 bool UDPSocket::IsBound() { return socket_.is_connected(); } 253 bool UDPSocket::IsBound() { return socket_.is_connected(); }
250 254
255 bool UDPSocket::IsReusable() {
256 // Only ResumableUDPSocket can be reusable
257 return false;
258 }
259
251 int UDPSocket::JoinGroup(const std::string& address) { 260 int UDPSocket::JoinGroup(const std::string& address) {
252 net::IPAddress ip; 261 net::IPAddress ip;
253 if (!ip.AssignFromIPLiteral(address)) 262 if (!ip.AssignFromIPLiteral(address))
254 return net::ERR_ADDRESS_INVALID; 263 return net::ERR_ADDRESS_INVALID;
255 264
256 std::string normalized_address = ip.ToString(); 265 std::string normalized_address = ip.ToString();
257 std::vector<std::string>::iterator find_result = std::find( 266 std::vector<std::string>::iterator find_result = std::find(
258 multicast_groups_.begin(), multicast_groups_.end(), normalized_address); 267 multicast_groups_.begin(), multicast_groups_.end(), normalized_address);
259 if (find_result != multicast_groups_.end()) 268 if (find_result != multicast_groups_.end())
260 return net::ERR_ADDRESS_INVALID; 269 return net::ERR_ADDRESS_INVALID;
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
298 } 307 }
299 308
300 const std::vector<std::string>& UDPSocket::GetJoinedGroups() const { 309 const std::vector<std::string>& UDPSocket::GetJoinedGroups() const {
301 return multicast_groups_; 310 return multicast_groups_;
302 } 311 }
303 312
304 ResumableUDPSocket::ResumableUDPSocket(const std::string& owner_extension_id) 313 ResumableUDPSocket::ResumableUDPSocket(const std::string& owner_extension_id)
305 : UDPSocket(owner_extension_id), 314 : UDPSocket(owner_extension_id),
306 persistent_(false), 315 persistent_(false),
307 buffer_size_(0), 316 buffer_size_(0),
308 paused_(false) {} 317 paused_(false),
318 allow_address_reuse_(false) {}
309 319
310 bool ResumableUDPSocket::IsPersistent() const { return persistent(); } 320 bool ResumableUDPSocket::IsPersistent() const { return persistent(); }
311 321
322 bool ResumableUDPSocket::IsReusable() {
323 return allow_address_reuse();
324 }
325
312 } // namespace extensions 326 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698