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 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 if (socket_.is_connected()) | |
| 265 return net::ERR_FAILED; | |
|
wtc
2013/04/22 19:19:53
Please use net::ERR_UNEXPECTED here and on line 27
Bei Zhang
2013/04/23 17:26:53
net::UDPSocket should be used internally. I think
wtc
2013/04/23 18:01:07
I see. Thank you for the explanation.
net::UDPSoc
| |
| 266 return socket_.SetMulticastTimeToLive(ttl); | |
| 267 } | |
| 268 | |
| 269 int UDPSocket::SetMulticastLoopbackMode(bool loopback) { | |
| 270 if (socket_.is_connected()) | |
| 271 return net::ERR_FAILED; | |
| 272 return socket_.SetMulticastLoopbackMode(loopback); | |
| 273 } | |
| 274 | |
| 275 int UDPSocket::GetJoinedGroups(base::hash_set<std::string>* groups) const { | |
| 276 if (!socket_.is_connected()) | |
| 277 return net::ERR_SOCKET_NOT_CONNECTED; | |
| 278 groups->clear(); | |
| 279 groups->insert(multicast_groups_.begin(), multicast_groups_.end()); | |
| 280 return 0; | |
| 281 } | |
| 282 | |
| 225 } // namespace extensions | 283 } // namespace extensions |
| OLD | NEW |