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.h" | |
8 #include "chrome/common/render_messages.h" | |
9 #include "chrome/renderer/p2p/sockets_dispatcher.h" | |
10 #include "ipc/ipc_message.h" | |
11 #include "ipc/ipc_message_macros.h" | |
12 | |
13 P2PSocketClient::P2PSocketClient(P2PSocketsDispatcher* dispatcher) | |
14 : dispatcher_(dispatcher), id_(0), delegate_(NULL), | |
15 state_(STATE_UNINITIALIZED) { | |
16 } | |
17 | |
18 P2PSocketClient::~P2PSocketClient() { | |
19 DCHECK(state_ == STATE_CLOSED || state_ == STATE_UNINITIALIZED); | |
20 } | |
21 | |
22 void P2PSocketClient::Init(P2PSocketType type, P2PSocketAddress address, | |
23 P2PSocketClient::Delegate* delegate) { | |
24 if (MessageLoop::current() != dispatcher_->message_loop()) { | |
25 dispatcher_->message_loop()->PostTask( | |
26 FROM_HERE, NewRunnableMethod(this, &P2PSocketClient::Init, | |
27 type, address, delegate)); | |
28 return; | |
29 } | |
30 | |
31 DCHECK_EQ(state_, STATE_UNINITIALIZED); | |
32 id_ = dispatcher_->RegisterClient(this); | |
33 dispatcher_->Send(new ViewHostMsg_P2P_CreateSocket(0, type, id_, address)); | |
34 state_ = STATE_OPENING; | |
35 } | |
36 | |
37 void P2PSocketClient::Send(P2PSocketAddress address, | |
38 const std::vector<char>& data) { | |
39 if (MessageLoop::current() != dispatcher_->message_loop()) { | |
40 dispatcher_->message_loop()->PostTask( | |
41 FROM_HERE, NewRunnableMethod(this, &P2PSocketClient::Send, address, | |
42 data)); | |
43 return; | |
44 } | |
45 | |
46 // Can send data only when the socket is open. | |
47 DCHECK_EQ(state_, STATE_OPEN); | |
48 dispatcher_->Send(new ViewHostMsg_P2P_Send(0, id_, address, data)); | |
49 } | |
50 | |
51 void P2PSocketClient::Close(Task* closed_task) { | |
52 if (MessageLoop::current() != dispatcher_->message_loop()) { | |
53 dispatcher_->message_loop()->PostTask( | |
54 FROM_HERE, NewRunnableMethod(this, &P2PSocketClient::Close, | |
55 closed_task)); | |
56 return; | |
57 } | |
58 | |
59 if (state_ == STATE_OPEN || state_ == STATE_OPENING) { | |
60 dispatcher_->Send(new ViewHostMsg_P2P_DestroySocket(0, id_)); | |
61 } | |
62 state_ = STATE_CLOSED; | |
63 dispatcher_->UnregisterClient(id_); | |
64 | |
65 closed_task->Run(); | |
66 delete closed_task; | |
67 } | |
68 | |
69 void P2PSocketClient::OnSocketCreated(P2PSocketAddress address) { | |
awong
2011/03/01 20:21:55
DCHECK the thread, here and below?
Sergey Ulanov
2011/03/02 16:12:29
Done.
| |
70 DCHECK_EQ(state_, STATE_OPENING); | |
71 state_ = STATE_OPEN; | |
72 delegate_->OnOpen(address); | |
73 } | |
74 | |
75 void P2PSocketClient::OnError() { | |
76 state_ = STATE_ERROR; | |
77 delegate_->OnError(); | |
78 } | |
79 | |
80 void P2PSocketClient::OnDataReceived(P2PSocketAddress address, | |
81 const std::vector<char>& data) { | |
awong
2011/03/01 20:21:55
spacing...maybe run cpplint on these files?
Sergey Ulanov
2011/03/02 16:12:29
Done.
| |
82 DCHECK_EQ(state_, STATE_OPEN); | |
awong
2011/03/01 20:21:55
use the expected state as the first argument here
Sergey Ulanov
2011/03/02 16:12:29
Why? Most most of remoting code has expected value
| |
83 delegate_->OnDataReceived(address, data); | |
84 } | |
OLD | NEW |