Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 Loading... | |
| 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); | |
|
miket_OOO
2013/04/26 01:20:01
You don't need to do the find/erase here. You can
Bei Zhang
2013/04/26 08:07:34
We are changing to a vector. Have to lookup the it
| |
| 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/26 01:20:01
The hash_set really doesn't make any sense here. I
Bei Zhang
2013/04/26 08:07:34
I Agree. For our case, I think vector is our best
| |
| 268 if (!socket_.is_connected()) | |
| 269 return net::ERR_SOCKET_NOT_CONNECTED; | |
| 270 | |
| 271 groups->clear(); | |
| 272 groups->insert(multicast_groups_.begin(), multicast_groups_.end()); | |
| 273 return 0; | |
| 274 } | |
| 275 | |
| 225 } // namespace extensions | 276 } // namespace extensions |
| OLD | NEW |