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 ipc_message_loop_(dispatcher->message_loop()), | |
14 delegate_message_loop_(NULL), | |
15 socket_id_(0), delegate_(NULL), | |
16 state_(STATE_UNINITIALIZED) { | |
17 } | |
18 | |
19 P2PSocketClient::~P2PSocketClient() { | |
20 DCHECK(state_ == STATE_CLOSED || state_ == STATE_UNINITIALIZED || | |
21 state_ == STATE_ERROR); | |
22 } | |
23 | |
24 void P2PSocketClient::Init( | |
25 P2PSocketType type, const net::IPEndPoint& address, | |
26 P2PSocketClient::Delegate* delegate, | |
27 scoped_refptr<base::MessageLoopProxy> delegate_loop) { | |
28 if (!ipc_message_loop_->BelongsToCurrentThread()) { | |
29 ipc_message_loop_->PostTask( | |
30 FROM_HERE, NewRunnableMethod(this, &P2PSocketClient::Init, | |
31 type, address, delegate, delegate_loop)); | |
32 return; | |
33 } | |
34 | |
35 DCHECK_EQ(state_, STATE_UNINITIALIZED); | |
36 state_ = STATE_OPENING; | |
37 delegate_ = delegate; | |
38 delegate_message_loop_ = delegate_loop; | |
39 socket_id_ = dispatcher_->RegisterClient(this); | |
40 dispatcher_->SendP2PMessage( | |
41 new P2PHostMsg_CreateSocket(0, type, socket_id_, address)); | |
42 } | |
43 | |
44 void P2PSocketClient::Send(const net::IPEndPoint& address, | |
45 const std::vector<char>& data) { | |
46 if (!ipc_message_loop_->BelongsToCurrentThread()) { | |
47 ipc_message_loop_->PostTask( | |
48 FROM_HERE, NewRunnableMethod(this, &P2PSocketClient::Send, address, | |
49 data)); | |
50 return; | |
51 } | |
52 | |
53 // Can send data only when the socket is open. | |
54 DCHECK_EQ(state_, STATE_OPEN); | |
55 dispatcher_->SendP2PMessage( | |
56 new P2PHostMsg_Send(0, socket_id_, address, data)); | |
57 } | |
58 | |
59 void P2PSocketClient::Close() { | |
60 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); | |
61 | |
62 delegate_ = NULL; | |
63 | |
64 ipc_message_loop_->PostTask( | |
65 FROM_HERE, NewRunnableMethod(this, &P2PSocketClient::DoClose)); | |
66 } | |
67 | |
68 void P2PSocketClient::DoClose() { | |
69 if (dispatcher_) { | |
70 if (state_ == STATE_OPEN || state_ == STATE_OPENING) { | |
71 dispatcher_->SendP2PMessage(new P2PHostMsg_DestroySocket(0, socket_id_)); | |
72 } | |
73 dispatcher_->UnregisterClient(socket_id_); | |
74 } | |
75 | |
76 state_ = STATE_CLOSED; | |
77 } | |
78 | |
79 void P2PSocketClient::OnSocketCreated(const net::IPEndPoint& address) { | |
80 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); | |
81 DCHECK_EQ(state_, STATE_OPENING); | |
82 state_ = STATE_OPEN; | |
83 | |
84 delegate_message_loop_->PostTask(FROM_HERE, NewRunnableMethod( | |
85 this, &P2PSocketClient::DeliverOnSocketCreated, address)); | |
86 } | |
87 | |
88 void P2PSocketClient::DeliverOnSocketCreated(const net::IPEndPoint& address) { | |
89 if (delegate_) | |
90 delegate_->OnOpen(address); | |
91 } | |
92 | |
93 void P2PSocketClient::OnError() { | |
94 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); | |
95 state_ = STATE_ERROR; | |
96 | |
97 delegate_message_loop_->PostTask(FROM_HERE, NewRunnableMethod( | |
98 this, &P2PSocketClient::DeliverOnError)); | |
99 } | |
100 | |
101 void P2PSocketClient::DeliverOnError() { | |
102 if (delegate_) | |
103 delegate_->OnError(); | |
104 } | |
105 | |
106 void P2PSocketClient::OnDataReceived(const net::IPEndPoint& address, | |
107 const std::vector<char>& data) { | |
108 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); | |
109 DCHECK_EQ(STATE_OPEN, state_); | |
110 delegate_message_loop_->PostTask(FROM_HERE, NewRunnableMethod( | |
111 this, &P2PSocketClient::DeliverOnDataReceived, address, data)); | |
112 } | |
113 | |
114 void P2PSocketClient::DeliverOnDataReceived(const net::IPEndPoint& address, | |
115 const std::vector<char>& data) { | |
116 if (delegate_) | |
117 delegate_->OnDataReceived(address, data); | |
118 } | |
119 | |
120 void P2PSocketClient::Detach() { | |
121 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); | |
122 dispatcher_ = NULL; | |
123 OnError(); | |
124 } | |
OLD | NEW |