Chromium Code Reviews| Index: chrome/renderer/p2p/socket_client.cc |
| diff --git a/chrome/renderer/p2p/socket_client.cc b/chrome/renderer/p2p/socket_client.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..2e1e83daecf40f63a137cc553662817f524d38d3 |
| --- /dev/null |
| +++ b/chrome/renderer/p2p/socket_client.cc |
| @@ -0,0 +1,84 @@ |
| +// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/renderer/p2p/socket_client.h" |
| + |
| +#include "base/message_loop.h" |
| +#include "chrome/common/render_messages.h" |
| +#include "chrome/renderer/p2p/sockets_dispatcher.h" |
| +#include "ipc/ipc_message.h" |
| +#include "ipc/ipc_message_macros.h" |
| + |
| +P2PSocketClient::P2PSocketClient(P2PSocketsDispatcher* dispatcher) |
| + : dispatcher_(dispatcher), id_(0), delegate_(NULL), |
| + state_(STATE_UNINITIALIZED) { |
| +} |
| + |
| +P2PSocketClient::~P2PSocketClient() { |
| + DCHECK(state_ == STATE_CLOSED || state_ == STATE_UNINITIALIZED); |
| +} |
| + |
| +void P2PSocketClient::Init(P2PSocketType type, P2PSocketAddress address, |
| + P2PSocketClient::Delegate* delegate) { |
| + if (MessageLoop::current() != dispatcher_->message_loop()) { |
| + dispatcher_->message_loop()->PostTask( |
| + FROM_HERE, NewRunnableMethod(this, &P2PSocketClient::Init, |
| + type, address, delegate)); |
| + return; |
| + } |
| + |
| + DCHECK_EQ(state_, STATE_UNINITIALIZED); |
| + id_ = dispatcher_->RegisterClient(this); |
| + dispatcher_->Send(new ViewHostMsg_P2P_CreateSocket(0, type, id_, address)); |
| + state_ = STATE_OPENING; |
| +} |
| + |
| +void P2PSocketClient::Send(P2PSocketAddress address, |
| + const std::vector<char>& data) { |
| + if (MessageLoop::current() != dispatcher_->message_loop()) { |
| + dispatcher_->message_loop()->PostTask( |
| + FROM_HERE, NewRunnableMethod(this, &P2PSocketClient::Send, address, |
| + data)); |
| + return; |
| + } |
| + |
| + // Can send data only when the socket is open. |
| + DCHECK_EQ(state_, STATE_OPEN); |
| + dispatcher_->Send(new ViewHostMsg_P2P_Send(0, id_, address, data)); |
| +} |
| + |
| +void P2PSocketClient::Close(Task* closed_task) { |
| + if (MessageLoop::current() != dispatcher_->message_loop()) { |
| + dispatcher_->message_loop()->PostTask( |
| + FROM_HERE, NewRunnableMethod(this, &P2PSocketClient::Close, |
| + closed_task)); |
| + return; |
| + } |
| + |
| + if (state_ == STATE_OPEN || state_ == STATE_OPENING) { |
| + dispatcher_->Send(new ViewHostMsg_P2P_DestroySocket(0, id_)); |
| + } |
| + state_ = STATE_CLOSED; |
| + dispatcher_->UnregisterClient(id_); |
| + |
| + closed_task->Run(); |
| + delete closed_task; |
| +} |
| + |
| +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.
|
| + DCHECK_EQ(state_, STATE_OPENING); |
| + state_ = STATE_OPEN; |
| + delegate_->OnOpen(address); |
| +} |
| + |
| +void P2PSocketClient::OnError() { |
| + state_ = STATE_ERROR; |
| + delegate_->OnError(); |
| +} |
| + |
| +void P2PSocketClient::OnDataReceived(P2PSocketAddress address, |
| + 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.
|
| + 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
|
| + delegate_->OnDataReceived(address, data); |
| +} |