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..0d49a58b0ed3a7bcf6d220dd4573c6af4e2f74d3 |
--- /dev/null |
+++ b/chrome/renderer/p2p/socket_client.cc |
@@ -0,0 +1,87 @@ |
+// 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/renderer/p2p/socket_dispatcher.h" |
+#include "content/common/p2p_messages.h" |
+ |
+P2PSocketClient::P2PSocketClient(P2PSocketDispatcher* dispatcher) |
+ : dispatcher_(dispatcher), socket_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( |
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
|
+ FROM_HERE, NewRunnableMethod(this, &P2PSocketClient::Init, |
+ type, address, delegate)); |
+ return; |
+ } |
+ |
+ DCHECK_EQ(state_, STATE_UNINITIALIZED); |
+ socket_id_ = dispatcher_->RegisterClient(this); |
+ dispatcher_->SendP2PMessage( |
+ new P2PHostMsg_CreateSocket(0, type, socket_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_->SendP2PMessage( |
+ new P2PHostMsg_Send(0, socket_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_->SendP2PMessage(new P2PHostMsg_DestroySocket(0, socket_id_)); |
+ } |
+ state_ = STATE_CLOSED; |
+ dispatcher_->UnregisterClient(socket_id_); |
+ |
+ closed_task->Run(); |
+ delete closed_task; |
+} |
+ |
+void P2PSocketClient::OnSocketCreated(P2PSocketAddress address) { |
+ DCHECK_EQ(MessageLoop::current(), dispatcher_->message_loop()); |
+ DCHECK_EQ(state_, STATE_OPENING); |
+ state_ = STATE_OPEN; |
+ delegate_->OnOpen(address); |
+} |
+ |
+void P2PSocketClient::OnError() { |
+ DCHECK_EQ(MessageLoop::current(), dispatcher_->message_loop()); |
+ state_ = STATE_ERROR; |
+ delegate_->OnError(); |
+} |
+ |
+void P2PSocketClient::OnDataReceived(P2PSocketAddress address, |
+ const std::vector<char>& data) { |
+ DCHECK_EQ(MessageLoop::current(), dispatcher_->message_loop()); |
+ DCHECK_EQ(STATE_OPEN, state_); |
+ delegate_->OnDataReceived(address, data); |
+} |