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 "content/renderer/p2p/socket_client.h" | 5 #include "content/renderer/p2p/socket_client.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/message_loop_proxy.h" | 8 #include "base/message_loop_proxy.h" |
9 #include "content/common/p2p_messages.h" | 9 #include "content/common/p2p_messages.h" |
10 #include "content/renderer/p2p/socket_dispatcher.h" | 10 #include "content/renderer/p2p/socket_dispatcher.h" |
(...skipping 10 matching lines...) Expand all Loading... | |
21 | 21 |
22 P2PSocketClient::~P2PSocketClient() { | 22 P2PSocketClient::~P2PSocketClient() { |
23 DCHECK(state_ == STATE_CLOSED || state_ == STATE_UNINITIALIZED); | 23 DCHECK(state_ == STATE_CLOSED || state_ == STATE_UNINITIALIZED); |
24 } | 24 } |
25 | 25 |
26 void P2PSocketClient::Init( | 26 void P2PSocketClient::Init( |
27 P2PSocketType type, | 27 P2PSocketType type, |
28 const net::IPEndPoint& local_address, | 28 const net::IPEndPoint& local_address, |
29 const net::IPEndPoint& remote_address, | 29 const net::IPEndPoint& remote_address, |
30 P2PSocketClient::Delegate* delegate) { | 30 P2PSocketClient::Delegate* delegate) { |
31 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); | |
tommi (sloooow) - chröme
2012/09/20 11:48:44
if the delegate and ipc threads are not always the
perkj_chrome
2012/09/20 12:22:32
On 2012/09/20 11:48:44, tommi wrote:
> if the dele
| |
32 // |delegate_| is only accessesed on |delegate_message_loop_|. | |
33 delegate_ = delegate; | |
tommi (sloooow) - chröme
2012/09/20 11:48:44
...and since you post that task from the ipc threa
perkj_chrome
2012/09/20 12:22:32
Humm- Stupid me. Sorry - should not have sent out
| |
34 | |
31 if (!ipc_message_loop_->BelongsToCurrentThread()) { | 35 if (!ipc_message_loop_->BelongsToCurrentThread()) { |
tommi (sloooow) - chröme
2012/09/20 11:48:44
btw, maybe you could add a note here (assuming tha
perkj_chrome
2012/09/20 12:22:32
In chrome now, they are not the same.
But I don't
| |
32 ipc_message_loop_->PostTask( | 36 ipc_message_loop_->PostTask( |
33 FROM_HERE, base::Bind(&P2PSocketClient::Init, this, type, local_address, | 37 FROM_HERE, base::Bind(&P2PSocketClient::Init, this, type, local_address, |
34 remote_address, delegate)); | 38 remote_address, delegate)); |
tommi (sloooow) - chröme
2012/09/20 11:48:44
in order to not use the |delegate| parameter on th
perkj_chrome
2012/09/20 12:22:32
Done.
| |
35 return; | 39 return; |
36 } | 40 } |
37 | 41 |
38 DCHECK_EQ(state_, STATE_UNINITIALIZED); | 42 DCHECK_EQ(state_, STATE_UNINITIALIZED); |
tommi (sloooow) - chröme
2012/09/20 11:48:44
and then here, you can DCHECK that delegate_ is no
perkj_chrome
2012/09/20 12:22:32
Done.
| |
39 state_ = STATE_OPENING; | 43 state_ = STATE_OPENING; |
40 delegate_ = delegate; | |
41 socket_id_ = dispatcher_->RegisterClient(this); | 44 socket_id_ = dispatcher_->RegisterClient(this); |
42 dispatcher_->SendP2PMessage(new P2PHostMsg_CreateSocket( | 45 dispatcher_->SendP2PMessage(new P2PHostMsg_CreateSocket( |
43 type, socket_id_, local_address, remote_address)); | 46 type, socket_id_, local_address, remote_address)); |
44 } | 47 } |
45 | 48 |
46 void P2PSocketClient::Send(const net::IPEndPoint& address, | 49 void P2PSocketClient::Send(const net::IPEndPoint& address, |
47 const std::vector<char>& data) { | 50 const std::vector<char>& data) { |
48 if (!ipc_message_loop_->BelongsToCurrentThread()) { | 51 if (!ipc_message_loop_->BelongsToCurrentThread()) { |
49 ipc_message_loop_->PostTask( | 52 ipc_message_loop_->PostTask( |
50 FROM_HERE, base::Bind(&P2PSocketClient::Send, this, address, data)); | 53 FROM_HERE, base::Bind(&P2PSocketClient::Send, this, address, data)); |
(...skipping 10 matching lines...) Expand all Loading... | |
61 void P2PSocketClient::Close() { | 64 void P2PSocketClient::Close() { |
62 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); | 65 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); |
63 | 66 |
64 delegate_ = NULL; | 67 delegate_ = NULL; |
65 | 68 |
66 ipc_message_loop_->PostTask( | 69 ipc_message_loop_->PostTask( |
67 FROM_HERE, base::Bind(&P2PSocketClient::DoClose, this)); | 70 FROM_HERE, base::Bind(&P2PSocketClient::DoClose, this)); |
68 } | 71 } |
69 | 72 |
70 void P2PSocketClient::DoClose() { | 73 void P2PSocketClient::DoClose() { |
74 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); | |
71 if (dispatcher_) { | 75 if (dispatcher_) { |
72 if (state_ == STATE_OPEN || state_ == STATE_OPENING || | 76 if (state_ == STATE_OPEN || state_ == STATE_OPENING || |
73 state_ == STATE_ERROR) { | 77 state_ == STATE_ERROR) { |
74 dispatcher_->SendP2PMessage(new P2PHostMsg_DestroySocket(socket_id_)); | 78 dispatcher_->SendP2PMessage(new P2PHostMsg_DestroySocket(socket_id_)); |
75 } | 79 } |
76 dispatcher_->UnregisterClient(socket_id_); | 80 dispatcher_->UnregisterClient(socket_id_); |
77 } | 81 } |
78 | 82 |
79 state_ = STATE_CLOSED; | 83 state_ = STATE_CLOSED; |
80 } | 84 } |
81 | 85 |
82 void P2PSocketClient::set_delegate(Delegate* delegate) { | 86 void P2PSocketClient::set_delegate(Delegate* delegate) { |
83 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); | 87 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); |
84 delegate_ = delegate; | 88 delegate_ = delegate; |
85 } | 89 } |
86 | 90 |
87 void P2PSocketClient::OnSocketCreated(const net::IPEndPoint& address) { | 91 void P2PSocketClient::OnSocketCreated(const net::IPEndPoint& address) { |
88 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); | 92 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); |
89 DCHECK_EQ(state_, STATE_OPENING); | 93 DCHECK_EQ(state_, STATE_OPENING); |
90 state_ = STATE_OPEN; | 94 state_ = STATE_OPEN; |
91 | 95 |
92 delegate_message_loop_->PostTask( | 96 delegate_message_loop_->PostTask( |
93 FROM_HERE, | 97 FROM_HERE, |
94 base::Bind(&P2PSocketClient::DeliverOnSocketCreated, this, address)); | 98 base::Bind(&P2PSocketClient::DeliverOnSocketCreated, this, address)); |
95 } | 99 } |
96 | 100 |
97 void P2PSocketClient::DeliverOnSocketCreated(const net::IPEndPoint& address) { | 101 void P2PSocketClient::DeliverOnSocketCreated(const net::IPEndPoint& address) { |
102 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); | |
98 if (delegate_) | 103 if (delegate_) |
99 delegate_->OnOpen(address); | 104 delegate_->OnOpen(address); |
100 } | 105 } |
101 | 106 |
102 void P2PSocketClient::OnIncomingTcpConnection(const net::IPEndPoint& address) { | 107 void P2PSocketClient::OnIncomingTcpConnection(const net::IPEndPoint& address) { |
103 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); | 108 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); |
104 DCHECK_EQ(state_, STATE_OPEN); | 109 DCHECK_EQ(state_, STATE_OPEN); |
105 | 110 |
106 scoped_refptr<P2PSocketClient> new_client = new P2PSocketClient(dispatcher_); | 111 scoped_refptr<P2PSocketClient> new_client = new P2PSocketClient(dispatcher_); |
107 new_client->socket_id_ = dispatcher_->RegisterClient(new_client); | 112 new_client->socket_id_ = dispatcher_->RegisterClient(new_client); |
108 new_client->state_ = STATE_OPEN; | 113 new_client->state_ = STATE_OPEN; |
109 new_client->delegate_message_loop_ = delegate_message_loop_; | 114 new_client->delegate_message_loop_ = delegate_message_loop_; |
110 | 115 |
111 dispatcher_->SendP2PMessage(new P2PHostMsg_AcceptIncomingTcpConnection( | 116 dispatcher_->SendP2PMessage(new P2PHostMsg_AcceptIncomingTcpConnection( |
112 socket_id_, address, new_client->socket_id_)); | 117 socket_id_, address, new_client->socket_id_)); |
113 | 118 |
114 delegate_message_loop_->PostTask( | 119 delegate_message_loop_->PostTask( |
115 FROM_HERE, base::Bind(&P2PSocketClient::DeliverOnIncomingTcpConnection, | 120 FROM_HERE, base::Bind(&P2PSocketClient::DeliverOnIncomingTcpConnection, |
116 this, address, new_client)); | 121 this, address, new_client)); |
117 } | 122 } |
118 | 123 |
119 void P2PSocketClient::DeliverOnIncomingTcpConnection( | 124 void P2PSocketClient::DeliverOnIncomingTcpConnection( |
120 const net::IPEndPoint& address, scoped_refptr<P2PSocketClient> new_client) { | 125 const net::IPEndPoint& address, scoped_refptr<P2PSocketClient> new_client) { |
126 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); | |
121 if (delegate_) | 127 if (delegate_) |
122 delegate_->OnIncomingTcpConnection(address, new_client); | 128 delegate_->OnIncomingTcpConnection(address, new_client); |
123 } | 129 } |
124 | 130 |
125 void P2PSocketClient::OnError() { | 131 void P2PSocketClient::OnError() { |
126 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); | 132 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); |
127 state_ = STATE_ERROR; | 133 state_ = STATE_ERROR; |
128 | 134 |
129 delegate_message_loop_->PostTask( | 135 delegate_message_loop_->PostTask( |
130 FROM_HERE, base::Bind(&P2PSocketClient::DeliverOnError, this)); | 136 FROM_HERE, base::Bind(&P2PSocketClient::DeliverOnError, this)); |
131 } | 137 } |
132 | 138 |
133 void P2PSocketClient::DeliverOnError() { | 139 void P2PSocketClient::DeliverOnError() { |
140 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); | |
134 if (delegate_) | 141 if (delegate_) |
135 delegate_->OnError(); | 142 delegate_->OnError(); |
136 } | 143 } |
137 | 144 |
138 void P2PSocketClient::OnDataReceived(const net::IPEndPoint& address, | 145 void P2PSocketClient::OnDataReceived(const net::IPEndPoint& address, |
139 const std::vector<char>& data) { | 146 const std::vector<char>& data) { |
140 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); | 147 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); |
141 DCHECK_EQ(STATE_OPEN, state_); | 148 DCHECK_EQ(STATE_OPEN, state_); |
142 delegate_message_loop_->PostTask( | 149 delegate_message_loop_->PostTask( |
143 FROM_HERE, | 150 FROM_HERE, |
144 base::Bind(&P2PSocketClient::DeliverOnDataReceived, this, address, data)); | 151 base::Bind(&P2PSocketClient::DeliverOnDataReceived, this, address, data)); |
145 } | 152 } |
146 | 153 |
147 void P2PSocketClient::DeliverOnDataReceived(const net::IPEndPoint& address, | 154 void P2PSocketClient::DeliverOnDataReceived(const net::IPEndPoint& address, |
148 const std::vector<char>& data) { | 155 const std::vector<char>& data) { |
156 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); | |
149 if (delegate_) | 157 if (delegate_) |
150 delegate_->OnDataReceived(address, data); | 158 delegate_->OnDataReceived(address, data); |
151 } | 159 } |
152 | 160 |
153 void P2PSocketClient::Detach() { | 161 void P2PSocketClient::Detach() { |
154 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); | 162 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); |
155 dispatcher_ = NULL; | 163 dispatcher_ = NULL; |
156 OnError(); | 164 OnError(); |
157 } | 165 } |
158 | 166 |
159 } // namespace content | 167 } // namespace content |
OLD | NEW |