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

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

Issue 12684008: Multicast socket API (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Minor fixes Created 7 years, 8 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "chrome/browser/extensions/api/socket/udp_socket.h" 5 #include "chrome/browser/extensions/api/socket/udp_socket.h"
6 6
7 #include "chrome/browser/extensions/api/api_resource.h" 7 #include "chrome/browser/extensions/api/api_resource.h"
8 #include "net/base/ip_endpoint.h" 8 #include "net/base/ip_endpoint.h"
9 #include "net/base/net_errors.h" 9 #include "net/base/net_errors.h"
10 #include "net/udp/datagram_socket.h" 10 #include "net/udp/datagram_socket.h"
(...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after
215 recv_from_callback_.Run(result, io_buffer, ip, port); 215 recv_from_callback_.Run(result, io_buffer, ip, port);
216 recv_from_callback_.Reset(); 216 recv_from_callback_.Reset();
217 } 217 }
218 218
219 void UDPSocket::OnSendToComplete(int result) { 219 void UDPSocket::OnSendToComplete(int result) {
220 DCHECK(!send_to_callback_.is_null()); 220 DCHECK(!send_to_callback_.is_null());
221 send_to_callback_.Run(result); 221 send_to_callback_.Run(result);
222 send_to_callback_.Reset(); 222 send_to_callback_.Reset();
223 } 223 }
224 224
225 int UDPSocket::JoinGroup(const std::string& address) {
226 net::IPAddressNumber ip;
227 if (!net::ParseIPLiteralToNumber(address, &ip))
228 return net::ERR_ADDRESS_INVALID;
229
230 std::string normalized_address = net::IPAddressToString(ip);
231 base::hash_set<std::string>::iterator find_result =
232 multicast_groups_.find(normalized_address);
233 if (find_result != multicast_groups_.end())
234 return net::ERR_ADDRESS_INVALID;
235
236 int rv = socket_.JoinGroup(ip);
237 if (rv == 0)
238 multicast_groups_.insert(normalized_address);
239 return rv;
240 }
241
242 int UDPSocket::LeaveGroup(const std::string& address) {
243 net::IPAddressNumber ip;
244 if (!net::ParseIPLiteralToNumber(address, &ip))
245 return net::ERR_ADDRESS_INVALID;
246
247 std::string normalized_address = net::IPAddressToString(ip);
248 base::hash_set<std::string>::iterator find_result =
249 multicast_groups_.find(normalized_address);
250 if (find_result == multicast_groups_.end())
251 return net::ERR_ADDRESS_INVALID;
252
253 int rv = socket_.LeaveGroup(ip);
254 if (rv == 0)
255 multicast_groups_.erase(find_result);
256 return rv;
257 }
258
259 int UDPSocket::SetMulticastTimeToLive(int ttl) {
260 return socket_.SetMulticastTimeToLive(ttl);
261 }
262
263 int UDPSocket::SetMulticastLoopbackMode(bool loopback) {
264 return socket_.SetMulticastLoopbackMode(loopback);
265 }
266
267 int UDPSocket::GetJoinedGroups(base::hash_set<std::string>* groups) const {
miket_OOO 2013/04/25 21:52:12 Same here -- why hash_set?
268 if (!socket_.is_connected())
269 return net::ERR_SOCKET_NOT_CONNECTED;
270
271 groups->clear();
miket_OOO 2013/04/25 21:52:12 This method signature is a bit odd. Why require th
Bei Zhang 2013/04/25 23:56:21 To avoid the copy of the container. Clear would be
272 groups->insert(multicast_groups_.begin(), multicast_groups_.end());
273 return 0;
274 }
275
225 } // namespace extensions 276 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698