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 "chrome/renderer/p2p/socket_client.h" |
| 6 |
| 7 #include "base/message_loop_proxy.h" |
| 8 #include "chrome/renderer/p2p/socket_dispatcher.h" |
| 9 #include "content/common/p2p_messages.h" |
| 10 |
| 11 P2PSocketClient::P2PSocketClient(P2PSocketDispatcher* dispatcher) |
| 12 : dispatcher_(dispatcher), |
| 13 message_loop_(dispatcher->message_loop()), |
| 14 socket_id_(0), delegate_(NULL), |
| 15 state_(STATE_UNINITIALIZED) { |
| 16 } |
| 17 |
| 18 P2PSocketClient::~P2PSocketClient() { |
| 19 DCHECK(state_ == STATE_CLOSED || state_ == STATE_UNINITIALIZED || |
| 20 state_ == STATE_ERROR); |
| 21 } |
| 22 |
| 23 void P2PSocketClient::Init(P2PSocketType type, P2PSocketAddress address, |
| 24 P2PSocketClient::Delegate* delegate) { |
| 25 if (!message_loop_->BelongsToCurrentThread()) { |
| 26 message_loop_->PostTask( |
| 27 FROM_HERE, NewRunnableMethod(this, &P2PSocketClient::Init, |
| 28 type, address, delegate)); |
| 29 return; |
| 30 } |
| 31 |
| 32 DCHECK_EQ(state_, STATE_UNINITIALIZED); |
| 33 socket_id_ = dispatcher_->RegisterClient(this); |
| 34 dispatcher_->SendP2PMessage( |
| 35 new P2PHostMsg_CreateSocket(0, type, socket_id_, address)); |
| 36 state_ = STATE_OPENING; |
| 37 } |
| 38 |
| 39 void P2PSocketClient::Send(P2PSocketAddress address, |
| 40 const std::vector<char>& data) { |
| 41 if (!message_loop_->BelongsToCurrentThread()) { |
| 42 message_loop_->PostTask( |
| 43 FROM_HERE, NewRunnableMethod(this, &P2PSocketClient::Send, address, |
| 44 data)); |
| 45 return; |
| 46 } |
| 47 |
| 48 // Can send data only when the socket is open. |
| 49 DCHECK_EQ(state_, STATE_OPEN); |
| 50 dispatcher_->SendP2PMessage( |
| 51 new P2PHostMsg_Send(0, socket_id_, address, data)); |
| 52 } |
| 53 |
| 54 void P2PSocketClient::Close(Task* closed_task) { |
| 55 if (!message_loop_->BelongsToCurrentThread()) { |
| 56 message_loop_->PostTask( |
| 57 FROM_HERE, NewRunnableMethod(this, &P2PSocketClient::Close, |
| 58 closed_task)); |
| 59 return; |
| 60 } |
| 61 |
| 62 if (dispatcher_) { |
| 63 if (state_ == STATE_OPEN || state_ == STATE_OPENING) { |
| 64 dispatcher_->SendP2PMessage(new P2PHostMsg_DestroySocket(0, socket_id_)); |
| 65 } |
| 66 dispatcher_->UnregisterClient(socket_id_); |
| 67 } |
| 68 |
| 69 state_ = STATE_CLOSED; |
| 70 |
| 71 closed_task->Run(); |
| 72 delete closed_task; |
| 73 } |
| 74 |
| 75 void P2PSocketClient::OnSocketCreated(P2PSocketAddress address) { |
| 76 DCHECK(message_loop_->BelongsToCurrentThread()); |
| 77 DCHECK_EQ(state_, STATE_OPENING); |
| 78 state_ = STATE_OPEN; |
| 79 delegate_->OnOpen(address); |
| 80 } |
| 81 |
| 82 void P2PSocketClient::OnError() { |
| 83 DCHECK(message_loop_->BelongsToCurrentThread()); |
| 84 state_ = STATE_ERROR; |
| 85 delegate_->OnError(); |
| 86 } |
| 87 |
| 88 void P2PSocketClient::OnDataReceived(P2PSocketAddress address, |
| 89 const std::vector<char>& data) { |
| 90 DCHECK(message_loop_->BelongsToCurrentThread()); |
| 91 DCHECK_EQ(STATE_OPEN, state_); |
| 92 delegate_->OnDataReceived(address, data); |
| 93 } |
| 94 |
| 95 void P2PSocketClient::Detach() { |
| 96 DCHECK(message_loop_->BelongsToCurrentThread()); |
| 97 delegate_ = NULL; |
| 98 state_ = STATE_ERROR; |
| 99 delegate_->OnError(); |
| 100 } |
OLD | NEW |