| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "content/renderer/p2p/socket_client_impl.h" | 5 #include "content/renderer/p2p/socket_client_impl.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/location.h" | 8 #include "base/location.h" |
| 9 #include "base/single_thread_task_runner.h" | 9 #include "base/single_thread_task_runner.h" |
| 10 #include "base/threading/thread_task_runner_handle.h" | 10 #include "base/threading/thread_task_runner_handle.h" |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 state_(STATE_UNINITIALIZED), | 37 state_(STATE_UNINITIALIZED), |
| 38 random_socket_id_(0), | 38 random_socket_id_(0), |
| 39 next_packet_id_(0) { | 39 next_packet_id_(0) { |
| 40 crypto::RandBytes(&random_socket_id_, sizeof(random_socket_id_)); | 40 crypto::RandBytes(&random_socket_id_, sizeof(random_socket_id_)); |
| 41 } | 41 } |
| 42 | 42 |
| 43 P2PSocketClientImpl::~P2PSocketClientImpl() { | 43 P2PSocketClientImpl::~P2PSocketClientImpl() { |
| 44 CHECK(state_ == STATE_CLOSED || state_ == STATE_UNINITIALIZED); | 44 CHECK(state_ == STATE_CLOSED || state_ == STATE_UNINITIALIZED); |
| 45 } | 45 } |
| 46 | 46 |
| 47 void P2PSocketClientImpl::Init( | 47 void P2PSocketClientImpl::Init(P2PSocketType type, |
| 48 P2PSocketType type, | 48 const net::IPEndPoint& local_address, |
| 49 const net::IPEndPoint& local_address, | 49 uint16_t min_port, |
| 50 const P2PHostAndIPEndPoint& remote_address, | 50 uint16_t max_port, |
| 51 P2PSocketClientDelegate* delegate) { | 51 const P2PHostAndIPEndPoint& remote_address, |
| 52 P2PSocketClientDelegate* delegate) { |
| 52 DCHECK(delegate_task_runner_->BelongsToCurrentThread()); | 53 DCHECK(delegate_task_runner_->BelongsToCurrentThread()); |
| 53 DCHECK(delegate); | 54 DCHECK(delegate); |
| 54 // |delegate_| is only accessesed on |delegate_message_loop_|. | 55 // |delegate_| is only accessesed on |delegate_message_loop_|. |
| 55 delegate_ = delegate; | 56 delegate_ = delegate; |
| 56 | 57 |
| 57 ipc_task_runner_->PostTask( | 58 ipc_task_runner_->PostTask( |
| 58 FROM_HERE, base::Bind(&P2PSocketClientImpl::DoInit, this, type, | 59 FROM_HERE, base::Bind(&P2PSocketClientImpl::DoInit, this, type, |
| 59 local_address, remote_address)); | 60 local_address, min_port, max_port, remote_address)); |
| 60 } | 61 } |
| 61 | 62 |
| 62 void P2PSocketClientImpl::DoInit(P2PSocketType type, | 63 void P2PSocketClientImpl::DoInit(P2PSocketType type, |
| 63 const net::IPEndPoint& local_address, | 64 const net::IPEndPoint& local_address, |
| 65 uint16_t min_port, |
| 66 uint16_t max_port, |
| 64 const P2PHostAndIPEndPoint& remote_address) { | 67 const P2PHostAndIPEndPoint& remote_address) { |
| 68 DCHECK(local_address.port() == 0 || (min_port == 0 && max_port == 0)); |
| 65 DCHECK_EQ(state_, STATE_UNINITIALIZED); | 69 DCHECK_EQ(state_, STATE_UNINITIALIZED); |
| 66 state_ = STATE_OPENING; | 70 state_ = STATE_OPENING; |
| 67 socket_id_ = dispatcher_->RegisterClient(this); | 71 socket_id_ = dispatcher_->RegisterClient(this); |
| 68 dispatcher_->SendP2PMessage(new P2PHostMsg_CreateSocket( | 72 dispatcher_->SendP2PMessage(new P2PHostMsg_CreateSocket( |
| 69 type, socket_id_, local_address, remote_address)); | 73 type, socket_id_, local_address, P2PPortRange(min_port, max_port), |
| 74 remote_address)); |
| 70 } | 75 } |
| 71 | 76 |
| 72 uint64_t P2PSocketClientImpl::Send(const net::IPEndPoint& address, | 77 uint64_t P2PSocketClientImpl::Send(const net::IPEndPoint& address, |
| 73 const std::vector<char>& data, | 78 const std::vector<char>& data, |
| 74 const rtc::PacketOptions& options) { | 79 const rtc::PacketOptions& options) { |
| 75 uint64_t unique_id = GetUniqueId(random_socket_id_, ++next_packet_id_); | 80 uint64_t unique_id = GetUniqueId(random_socket_id_, ++next_packet_id_); |
| 76 if (!ipc_task_runner_->BelongsToCurrentThread()) { | 81 if (!ipc_task_runner_->BelongsToCurrentThread()) { |
| 77 ipc_task_runner_->PostTask( | 82 ipc_task_runner_->PostTask( |
| 78 FROM_HERE, base::Bind(&P2PSocketClientImpl::SendWithPacketId, this, | 83 FROM_HERE, base::Bind(&P2PSocketClientImpl::SendWithPacketId, this, |
| 79 address, data, options, unique_id)); | 84 address, data, options, unique_id)); |
| (...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 245 delegate_->OnDataReceived(address, data, timestamp); | 250 delegate_->OnDataReceived(address, data, timestamp); |
| 246 } | 251 } |
| 247 | 252 |
| 248 void P2PSocketClientImpl::Detach() { | 253 void P2PSocketClientImpl::Detach() { |
| 249 DCHECK(ipc_task_runner_->BelongsToCurrentThread()); | 254 DCHECK(ipc_task_runner_->BelongsToCurrentThread()); |
| 250 dispatcher_ = NULL; | 255 dispatcher_ = NULL; |
| 251 OnError(); | 256 OnError(); |
| 252 } | 257 } |
| 253 | 258 |
| 254 } // namespace content | 259 } // namespace content |
| OLD | NEW |