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/renderer/p2p/socket_dispatcher.h" | |
9 #include "content/common/p2p_messages.h" | |
10 | |
11 P2PSocketClient::P2PSocketClient(P2PSocketDispatcher* dispatcher) | |
12 : dispatcher_(dispatcher), socket_id_(0), delegate_(NULL), | |
13 state_(STATE_UNINITIALIZED) { | |
14 } | |
15 | |
16 P2PSocketClient::~P2PSocketClient() { | |
17 DCHECK(state_ == STATE_CLOSED || state_ == STATE_UNINITIALIZED); | |
18 } | |
19 | |
20 void P2PSocketClient::Init(P2PSocketType type, P2PSocketAddress address, | |
21 P2PSocketClient::Delegate* delegate) { | |
22 if (MessageLoop::current() != dispatcher_->message_loop()) { | |
23 dispatcher_->message_loop()->PostTask( | |
Alpha Left Google
2011/03/02 21:56:10
In chromium code it's transitioning to use Message
Sergey Ulanov
2011/03/02 23:56:43
Fixed. Also added Detach() method here for the cas
| |
24 FROM_HERE, NewRunnableMethod(this, &P2PSocketClient::Init, | |
25 type, address, delegate)); | |
26 return; | |
27 } | |
28 | |
29 DCHECK_EQ(state_, STATE_UNINITIALIZED); | |
30 socket_id_ = dispatcher_->RegisterClient(this); | |
31 dispatcher_->SendP2PMessage( | |
32 new P2PHostMsg_CreateSocket(0, type, socket_id_, address)); | |
33 state_ = STATE_OPENING; | |
34 } | |
35 | |
36 void P2PSocketClient::Send(P2PSocketAddress address, | |
37 const std::vector<char>& data) { | |
38 if (MessageLoop::current() != dispatcher_->message_loop()) { | |
39 dispatcher_->message_loop()->PostTask( | |
40 FROM_HERE, NewRunnableMethod(this, &P2PSocketClient::Send, address, | |
41 data)); | |
42 return; | |
43 } | |
44 | |
45 // Can send data only when the socket is open. | |
46 DCHECK_EQ(state_, STATE_OPEN); | |
47 dispatcher_->SendP2PMessage( | |
48 new P2PHostMsg_Send(0, socket_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_->SendP2PMessage(new P2PHostMsg_DestroySocket(0, socket_id_)); | |
61 } | |
62 state_ = STATE_CLOSED; | |
63 dispatcher_->UnregisterClient(socket_id_); | |
64 | |
65 closed_task->Run(); | |
66 delete closed_task; | |
67 } | |
68 | |
69 void P2PSocketClient::OnSocketCreated(P2PSocketAddress address) { | |
70 DCHECK_EQ(MessageLoop::current(), dispatcher_->message_loop()); | |
71 DCHECK_EQ(state_, STATE_OPENING); | |
72 state_ = STATE_OPEN; | |
73 delegate_->OnOpen(address); | |
74 } | |
75 | |
76 void P2PSocketClient::OnError() { | |
77 DCHECK_EQ(MessageLoop::current(), dispatcher_->message_loop()); | |
78 state_ = STATE_ERROR; | |
79 delegate_->OnError(); | |
80 } | |
81 | |
82 void P2PSocketClient::OnDataReceived(P2PSocketAddress address, | |
83 const std::vector<char>& data) { | |
84 DCHECK_EQ(MessageLoop::current(), dispatcher_->message_loop()); | |
85 DCHECK_EQ(STATE_OPEN, state_); | |
86 delegate_->OnDataReceived(address, data); | |
87 } | |
OLD | NEW |