OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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/renderer/p2p/socket_client.h" | 5 #include "chrome/renderer/p2p/socket_client.h" |
6 | 6 |
7 #include "base/message_loop_proxy.h" | 7 #include "base/message_loop_proxy.h" |
8 #include "chrome/renderer/p2p/socket_dispatcher.h" | 8 #include "chrome/renderer/p2p/socket_dispatcher.h" |
9 #include "content/common/p2p_messages.h" | 9 #include "content/common/p2p_messages.h" |
10 | 10 |
11 P2PSocketClient::P2PSocketClient(P2PSocketDispatcher* dispatcher) | 11 P2PSocketClient::P2PSocketClient(P2PSocketDispatcher* dispatcher) |
12 : dispatcher_(dispatcher), | 12 : dispatcher_(dispatcher), |
13 ipc_message_loop_(dispatcher->message_loop()), | 13 ipc_message_loop_(dispatcher->message_loop()), |
14 delegate_message_loop_(NULL), | 14 delegate_message_loop_(NULL), |
15 socket_id_(0), delegate_(NULL), | 15 socket_id_(0), delegate_(NULL), |
16 state_(STATE_UNINITIALIZED) { | 16 state_(STATE_UNINITIALIZED) { |
17 } | 17 } |
18 | 18 |
19 P2PSocketClient::~P2PSocketClient() { | 19 P2PSocketClient::~P2PSocketClient() { |
20 DCHECK(state_ == STATE_CLOSED || state_ == STATE_UNINITIALIZED || | 20 DCHECK(state_ == STATE_CLOSED || state_ == STATE_UNINITIALIZED || |
21 state_ == STATE_ERROR); | 21 state_ == STATE_ERROR); |
22 } | 22 } |
23 | 23 |
24 void P2PSocketClient::Init( | 24 void P2PSocketClient::Init( |
25 P2PSocketType type, const P2PSocketAddress& address, | 25 P2PSocketType type, const net::IPEndPoint& address, |
26 P2PSocketClient::Delegate* delegate, | 26 P2PSocketClient::Delegate* delegate, |
27 scoped_refptr<base::MessageLoopProxy> delegate_loop) { | 27 scoped_refptr<base::MessageLoopProxy> delegate_loop) { |
28 if (!ipc_message_loop_->BelongsToCurrentThread()) { | 28 if (!ipc_message_loop_->BelongsToCurrentThread()) { |
29 ipc_message_loop_->PostTask( | 29 ipc_message_loop_->PostTask( |
30 FROM_HERE, NewRunnableMethod(this, &P2PSocketClient::Init, | 30 FROM_HERE, NewRunnableMethod(this, &P2PSocketClient::Init, |
31 type, address, delegate, delegate_loop)); | 31 type, address, delegate, delegate_loop)); |
32 return; | 32 return; |
33 } | 33 } |
34 | 34 |
35 DCHECK_EQ(state_, STATE_UNINITIALIZED); | 35 DCHECK_EQ(state_, STATE_UNINITIALIZED); |
36 state_ = STATE_OPENING; | 36 state_ = STATE_OPENING; |
37 delegate_ = delegate; | 37 delegate_ = delegate; |
38 delegate_message_loop_ = delegate_loop; | 38 delegate_message_loop_ = delegate_loop; |
39 socket_id_ = dispatcher_->RegisterClient(this); | 39 socket_id_ = dispatcher_->RegisterClient(this); |
40 dispatcher_->SendP2PMessage( | 40 dispatcher_->SendP2PMessage( |
41 new P2PHostMsg_CreateSocket(0, type, socket_id_, address)); | 41 new P2PHostMsg_CreateSocket(0, type, socket_id_, address)); |
42 } | 42 } |
43 | 43 |
44 void P2PSocketClient::Send(const P2PSocketAddress& address, | 44 void P2PSocketClient::Send(const net::IPEndPoint& address, |
45 const std::vector<char>& data) { | 45 const std::vector<char>& data) { |
46 if (!ipc_message_loop_->BelongsToCurrentThread()) { | 46 if (!ipc_message_loop_->BelongsToCurrentThread()) { |
47 ipc_message_loop_->PostTask( | 47 ipc_message_loop_->PostTask( |
48 FROM_HERE, NewRunnableMethod(this, &P2PSocketClient::Send, address, | 48 FROM_HERE, NewRunnableMethod(this, &P2PSocketClient::Send, address, |
49 data)); | 49 data)); |
50 return; | 50 return; |
51 } | 51 } |
52 | 52 |
53 // Can send data only when the socket is open. | 53 // Can send data only when the socket is open. |
54 DCHECK_EQ(state_, STATE_OPEN); | 54 DCHECK_EQ(state_, STATE_OPEN); |
(...skipping 14 matching lines...) Expand all Loading... |
69 if (dispatcher_) { | 69 if (dispatcher_) { |
70 if (state_ == STATE_OPEN || state_ == STATE_OPENING) { | 70 if (state_ == STATE_OPEN || state_ == STATE_OPENING) { |
71 dispatcher_->SendP2PMessage(new P2PHostMsg_DestroySocket(0, socket_id_)); | 71 dispatcher_->SendP2PMessage(new P2PHostMsg_DestroySocket(0, socket_id_)); |
72 } | 72 } |
73 dispatcher_->UnregisterClient(socket_id_); | 73 dispatcher_->UnregisterClient(socket_id_); |
74 } | 74 } |
75 | 75 |
76 state_ = STATE_CLOSED; | 76 state_ = STATE_CLOSED; |
77 } | 77 } |
78 | 78 |
79 void P2PSocketClient::OnSocketCreated(const P2PSocketAddress& address) { | 79 void P2PSocketClient::OnSocketCreated(const net::IPEndPoint& address) { |
80 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); | 80 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); |
81 DCHECK_EQ(state_, STATE_OPENING); | 81 DCHECK_EQ(state_, STATE_OPENING); |
82 state_ = STATE_OPEN; | 82 state_ = STATE_OPEN; |
83 | 83 |
84 delegate_message_loop_->PostTask(FROM_HERE, NewRunnableMethod( | 84 delegate_message_loop_->PostTask(FROM_HERE, NewRunnableMethod( |
85 this, &P2PSocketClient::DeliverOnSocketCreated, address)); | 85 this, &P2PSocketClient::DeliverOnSocketCreated, address)); |
86 } | 86 } |
87 | 87 |
88 void P2PSocketClient::DeliverOnSocketCreated(const P2PSocketAddress& address) { | 88 void P2PSocketClient::DeliverOnSocketCreated(const net::IPEndPoint& address) { |
89 if (delegate_) | 89 if (delegate_) |
90 delegate_->OnOpen(address); | 90 delegate_->OnOpen(address); |
91 } | 91 } |
92 | 92 |
93 void P2PSocketClient::OnError() { | 93 void P2PSocketClient::OnError() { |
94 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); | 94 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); |
95 state_ = STATE_ERROR; | 95 state_ = STATE_ERROR; |
96 | 96 |
97 delegate_message_loop_->PostTask(FROM_HERE, NewRunnableMethod( | 97 delegate_message_loop_->PostTask(FROM_HERE, NewRunnableMethod( |
98 this, &P2PSocketClient::DeliverOnError)); | 98 this, &P2PSocketClient::DeliverOnError)); |
99 } | 99 } |
100 | 100 |
101 void P2PSocketClient::DeliverOnError() { | 101 void P2PSocketClient::DeliverOnError() { |
102 if (delegate_) | 102 if (delegate_) |
103 delegate_->OnError(); | 103 delegate_->OnError(); |
104 } | 104 } |
105 | 105 |
106 void P2PSocketClient::OnDataReceived(const P2PSocketAddress& address, | 106 void P2PSocketClient::OnDataReceived(const net::IPEndPoint& address, |
107 const std::vector<char>& data) { | 107 const std::vector<char>& data) { |
108 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); | 108 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); |
109 DCHECK_EQ(STATE_OPEN, state_); | 109 DCHECK_EQ(STATE_OPEN, state_); |
110 delegate_message_loop_->PostTask(FROM_HERE, NewRunnableMethod( | 110 delegate_message_loop_->PostTask(FROM_HERE, NewRunnableMethod( |
111 this, &P2PSocketClient::DeliverOnDataReceived, address, data)); | 111 this, &P2PSocketClient::DeliverOnDataReceived, address, data)); |
112 } | 112 } |
113 | 113 |
114 void P2PSocketClient::DeliverOnDataReceived(const P2PSocketAddress& address, | 114 void P2PSocketClient::DeliverOnDataReceived(const net::IPEndPoint& address, |
115 const std::vector<char>& data) { | 115 const std::vector<char>& data) { |
116 if (delegate_) | 116 if (delegate_) |
117 delegate_->OnDataReceived(address, data); | 117 delegate_->OnDataReceived(address, data); |
118 } | 118 } |
119 | 119 |
120 void P2PSocketClient::Detach() { | 120 void P2PSocketClient::Detach() { |
121 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); | 121 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); |
122 dispatcher_ = NULL; | 122 dispatcher_ = NULL; |
123 OnError(); | 123 OnError(); |
124 } | 124 } |
OLD | NEW |