OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "content/renderer/p2p/port_allocator.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "content/renderer/p2p/host_address_request.h" |
| 9 #include "jingle/glue/utils.h" |
| 10 #include "net/base/ip_endpoint.h" |
| 11 |
| 12 namespace content { |
| 13 |
| 14 P2PPortAllocator::P2PPortAllocator( |
| 15 P2PSocketDispatcher* socket_dispatcher, |
| 16 talk_base::NetworkManager* network_manager, |
| 17 talk_base::PacketSocketFactory* socket_factory, |
| 18 const webkit_glue::P2PTransport::Config& config) |
| 19 : cricket::BasicPortAllocator(network_manager, socket_factory), |
| 20 socket_dispatcher_(socket_dispatcher), |
| 21 config_(config) { |
| 22 } |
| 23 |
| 24 P2PPortAllocator::~P2PPortAllocator() { |
| 25 } |
| 26 |
| 27 cricket::PortAllocatorSession* P2PPortAllocator::CreateSession( |
| 28 const std::string& name, |
| 29 const std::string& session_type) { |
| 30 return new P2PPortAllocatorSession(this, name, session_type); |
| 31 } |
| 32 |
| 33 P2PPortAllocatorSession::P2PPortAllocatorSession( |
| 34 P2PPortAllocator* allocator, |
| 35 const std::string& name, |
| 36 const std::string& session_type) |
| 37 : cricket::BasicPortAllocatorSession(allocator, name, session_type), |
| 38 allocator_(allocator) { |
| 39 } |
| 40 |
| 41 P2PPortAllocatorSession::~P2PPortAllocatorSession() { |
| 42 if (stun_address_request_) |
| 43 stun_address_request_->Cancel(); |
| 44 } |
| 45 |
| 46 void P2PPortAllocatorSession::GetPortConfigurations() { |
| 47 // Add am empty configuration synchronously, so a local connection |
| 48 // can be started immediately. |
| 49 ConfigReady(new cricket::PortConfiguration( |
| 50 talk_base::SocketAddress(), "", "", "")); |
| 51 |
| 52 ResolveStunServerAddress(); |
| 53 |
| 54 // TODO(sergeyu): Implement relay server support. |
| 55 } |
| 56 |
| 57 void P2PPortAllocatorSession::ResolveStunServerAddress() { |
| 58 if (allocator_->config_.stun_server.empty()) |
| 59 return; |
| 60 |
| 61 DCHECK(!stun_address_request_); |
| 62 stun_address_request_ = |
| 63 new P2PHostAddressRequest(allocator_->socket_dispatcher_); |
| 64 stun_address_request_->Request(allocator_->config_.stun_server, base::Bind( |
| 65 &P2PPortAllocatorSession::OnStunServerAddress, |
| 66 base::Unretained(this))); |
| 67 } |
| 68 |
| 69 void P2PPortAllocatorSession::OnStunServerAddress( |
| 70 const net::IPAddressNumber& address) { |
| 71 if (address.empty()) { |
| 72 LOG(ERROR) << "Failed to resolve STUN server address " |
| 73 << allocator_->config_.stun_server; |
| 74 return; |
| 75 } |
| 76 |
| 77 talk_base::SocketAddress socket_address; |
| 78 if (!jingle_glue::IPEndPointToSocketAddress( |
| 79 net::IPEndPoint(address, allocator_->config_.stun_server_port), |
| 80 &socket_address)) { |
| 81 return; |
| 82 } |
| 83 |
| 84 ConfigReady(new cricket::PortConfiguration(socket_address, "", "", "")); |
| 85 } |
| 86 |
| 87 } // namespace content |
OLD | NEW |