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 if (!socket_.is_connected()) |
| 227 return net::ERR_SOCKET_NOT_CONNECTED; |
| 228 net::IPAddressNumber ip; |
| 229 if (!net::ParseIPLiteralToNumber(address, &ip)) |
| 230 return net::ERR_ADDRESS_INVALID; |
| 231 |
| 232 std::string normalized_address = net::IPAddressToString(ip); |
| 233 base::hash_set<std::string>::iterator find_result = |
| 234 multicast_groups_.find(normalized_address); |
| 235 if (find_result != multicast_groups_.end()) |
| 236 return net::ERR_ADDRESS_INVALID; |
| 237 |
| 238 int rv = socket_.JoinGroup(ip); |
| 239 if (rv == 0) |
| 240 multicast_groups_.insert(normalized_address); |
| 241 return rv; |
| 242 } |
| 243 |
| 244 int UDPSocket::LeaveGroup(const std::string& address) { |
| 245 if (!socket_.is_connected()) |
| 246 return net::ERR_SOCKET_NOT_CONNECTED; |
| 247 net::IPAddressNumber ip; |
| 248 if (!net::ParseIPLiteralToNumber(address, &ip)) |
| 249 return net::ERR_ADDRESS_INVALID; |
| 250 |
| 251 std::string normalized_address = net::IPAddressToString(ip); |
| 252 base::hash_set<std::string>::iterator find_result = |
| 253 multicast_groups_.find(normalized_address); |
| 254 if (find_result == multicast_groups_.end()) |
| 255 return net::ERR_ADDRESS_INVALID; |
| 256 |
| 257 int rv = socket_.LeaveGroup(ip); |
| 258 if (rv == 0) |
| 259 multicast_groups_.erase(find_result); |
| 260 return rv; |
| 261 } |
| 262 |
| 263 int UDPSocket::SetMulticastTimeToLive(int ttl) { |
| 264 return socket_.SetMulticastTimeToLive(ttl); |
| 265 } |
| 266 |
| 267 int UDPSocket::SetMulticastLoopbackMode(bool loopback) { |
| 268 return socket_.SetMulticastLoopbackMode(loopback); |
| 269 } |
| 270 |
| 271 int UDPSocket::GetJoinedGroups(base::hash_set<std::string>* groups) const { |
| 272 if (!socket_.is_connected()) |
| 273 return net::ERR_SOCKET_NOT_CONNECTED; |
| 274 groups->clear(); |
| 275 groups->insert(multicast_groups_.begin(), multicast_groups_.end()); |
| 276 return 0; |
| 277 } |
| 278 |
225 } // namespace extensions | 279 } // namespace extensions |
OLD | NEW |