diff options
author | sergeyu@chromium.org <sergeyu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-06-21 01:30:45 +0000 |
---|---|---|
committer | sergeyu@chromium.org <sergeyu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-06-21 01:30:45 +0000 |
commit | 83c75888e4525ed9ae0473f566b3f5768fe76504 (patch) | |
tree | 523dba76e44f18e0e0e1f2a4997e91c5373f87b2 /remoting | |
parent | c64edcc0969bba17342bc2f3864342049e83a864 (diff) | |
download | chromium_src-83c75888e4525ed9ae0473f566b3f5768fe76504.zip chromium_src-83c75888e4525ed9ae0473f566b3f5768fe76504.tar.gz chromium_src-83c75888e4525ed9ae0473f566b3f5768fe76504.tar.bz2 |
Channel adapter for Pepper P2P Transport API.
The new adapter will be used to implement protocol on top
of the Transport API.
BUG=51198
TEST=None
Review URL: http://codereview.chromium.org/7200037
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@89776 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'remoting')
-rw-r--r-- | remoting/protocol/DEPS | 2 | ||||
-rw-r--r-- | remoting/protocol/pepper_p2p_channel.cc | 170 | ||||
-rw-r--r-- | remoting/protocol/pepper_p2p_channel.h | 76 | ||||
-rw-r--r-- | remoting/remoting.gyp | 2 |
4 files changed, 250 insertions, 0 deletions
diff --git a/remoting/protocol/DEPS b/remoting/protocol/DEPS index b1c16e3..5da27db 100644 --- a/remoting/protocol/DEPS +++ b/remoting/protocol/DEPS @@ -2,6 +2,8 @@ include_rules = [ "+google/protobuf", "+jingle/glue", "+net", + "+ppapi/c", + "+ppapi/cpp", "+remoting/jingle_glue", "+third_party/protobuf/src", ] diff --git a/remoting/protocol/pepper_p2p_channel.cc b/remoting/protocol/pepper_p2p_channel.cc new file mode 100644 index 0000000..516b7b7 --- /dev/null +++ b/remoting/protocol/pepper_p2p_channel.cc @@ -0,0 +1,170 @@ +// 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 "remoting/protocol/pepper_p2p_channel.h" + +#include "base/logging.h" +#include "net/base/io_buffer.h" +#include "net/base/net_errors.h" +#include "ppapi/c/pp_errors.h" +#include "ppapi/cpp/dev/transport_dev.h" +#include "ppapi/cpp/var.h" + +namespace remoting { +namespace protocol { + +namespace { + +const char kPepperTransportUdpProtocol[] = "udp"; + +// Maps value returned by Recv() and Send() Pepper methods to net::Error. +int PPErrorToNetError(int result) { + if (result > 0) + return result; + + switch (result) { + case PP_OK: + return net::OK; + case PP_OK_COMPLETIONPENDING: + return net::ERR_IO_PENDING; + default: + return net::ERR_FAILED; + } +} + +} // namespace + +PepperP2PChannel::PepperP2PChannel( + pp::Instance* pp_instance, + const char* name, + const IncomingCandidateCallback& candidate_callback) + : candidate_callback_(candidate_callback), + get_address_pending_(false), + read_callback_(NULL), + write_callback_(NULL) { + transport_.reset( + new pp::Transport_Dev(pp_instance, name, kPepperTransportUdpProtocol)); +} + +bool PepperP2PChannel::Init() { + // This will return false when the GetNextAddress() returns an + // error. Particularly it is useful to detect when the P2P Transport + // API is not supported. + return ProcessCandidates(); +} + +PepperP2PChannel::~PepperP2PChannel() { +} + +void PepperP2PChannel::AddRemoteCandidate(const std::string& candidate) { + DCHECK(CalledOnValidThread()); + transport_->ReceiveRemoteAddress(candidate); +} + +int PepperP2PChannel::Read(net::IOBuffer* buf, int buf_len, + net::CompletionCallback* callback) { + DCHECK(CalledOnValidThread()); + DCHECK(!read_callback_); + DCHECK(!read_buffer_); + + int result = PPErrorToNetError(transport_->Recv( + buf->data(), buf_len, + pp::CompletionCallback(&PepperP2PChannel::ReadCallback, this))); + + if (result == net::ERR_IO_PENDING) { + read_callback_ = callback; + read_buffer_ = buf; + } + + return result; +} + +int PepperP2PChannel::Write(net::IOBuffer* buf, int buf_len, + net::CompletionCallback* callback) { + DCHECK(CalledOnValidThread()); + DCHECK(!write_callback_); + DCHECK(!write_buffer_); + + int result = PPErrorToNetError(transport_->Send( + buf->data(), buf_len, + pp::CompletionCallback(&PepperP2PChannel::WriteCallback, this))); + + if (result == net::ERR_IO_PENDING) { + write_callback_ = callback; + write_buffer_ = buf; + } + + return result; +} + +bool PepperP2PChannel::SetReceiveBufferSize(int32 size) { + DCHECK(CalledOnValidThread()); + NOTIMPLEMENTED(); + return false; +} + +bool PepperP2PChannel::SetSendBufferSize(int32 size) { + DCHECK(CalledOnValidThread()); + NOTIMPLEMENTED(); + return false; +} + +bool PepperP2PChannel::ProcessCandidates() { + DCHECK(CalledOnValidThread()); + DCHECK(!get_address_pending_); + + while (true) { + pp::Var address; + int result = transport_->GetNextAddress( + &address, + pp::CompletionCallback(&PepperP2PChannel::NextAddressCallback, this)); + if (result == PP_OK_COMPLETIONPENDING) { + get_address_pending_ = true; + break; + } + + if (result == PP_OK) { + candidate_callback_.Run(address.AsString()); + } else { + LOG(ERROR) << "GetNextAddress() returned an error " << result; + return false; + } + } + return true; +} + +// static +void PepperP2PChannel::NextAddressCallback(void* data, int32_t result) { + PepperP2PChannel* channel = reinterpret_cast<PepperP2PChannel*>(data); + DCHECK(channel->CalledOnValidThread()); + channel->get_address_pending_ = false; + channel->ProcessCandidates(); +} + +// static +void PepperP2PChannel::ReadCallback(void* data, int32_t result) { + PepperP2PChannel* channel = reinterpret_cast<PepperP2PChannel*>(data); + DCHECK(channel->CalledOnValidThread()); + DCHECK(channel->read_callback_); + DCHECK(channel->read_buffer_); + net::CompletionCallback* callback = channel->read_callback_; + channel->read_callback_ = NULL; + channel->read_buffer_ = NULL; + callback->Run(result); +} + +// static +void PepperP2PChannel::WriteCallback(void* data, int32_t result) { + PepperP2PChannel* channel = reinterpret_cast<PepperP2PChannel*>(data); + DCHECK(channel->CalledOnValidThread()); + DCHECK(channel->write_callback_); + DCHECK(channel->write_buffer_); + net::CompletionCallback* callback = channel->write_callback_; + channel->write_callback_ = NULL; + channel->write_buffer_ = NULL; + callback->Run(result); +} + +} // namespace protocol +} // namespace remoting diff --git a/remoting/protocol/pepper_p2p_channel.h b/remoting/protocol/pepper_p2p_channel.h new file mode 100644 index 0000000..b818bc8 --- /dev/null +++ b/remoting/protocol/pepper_p2p_channel.h @@ -0,0 +1,76 @@ +// 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. + +#ifndef REMOTING_PROTOCOL_PEPPER_P2P_CHANNEL_H_ +#define REMOTING_PROTOCOL_PEPPER_P2P_CHANNEL_H_ + +#include <string> + +#include "base/callback.h" +#include "base/memory/scoped_ptr.h" +#include "base/threading/non_thread_safe.h" +#include "net/socket/socket.h" +#include "ppapi/c/pp_stdint.h" + +namespace pp { +class Instance; +class Transport_Dev; +} // namespace pp + +namespace remoting { +namespace protocol { + +// This class create P2PChannel based on the Pepper P2P Transport API +// and provides net::Socket interface on top of it. +class PepperP2PChannel : public base::NonThreadSafe, + public net::Socket { + public: + // TODO(sergeyu): Use cricket::Candidate instead of strings to + // represent candidates. + typedef base::Callback<void(const std::string&)> IncomingCandidateCallback; + + PepperP2PChannel(pp::Instance* pp_instance, const char* name, + const IncomingCandidateCallback& candidate_callback); + virtual ~PepperP2PChannel(); + + bool Init(); + + // Adds candidate received from the peer. + void AddRemoteCandidate(const std::string& candidate); + + // net::Socket interface. + virtual int Read(net::IOBuffer* buf, int buf_len, + net::CompletionCallback* callback); + virtual int Write(net::IOBuffer* buf, int buf_len, + net::CompletionCallback* callback); + virtual bool SetReceiveBufferSize(int32 size); + virtual bool SetSendBufferSize(int32 size); + + private: + // Callbacks for PPAPI calls. + static void NextAddressCallback(void* data, int32_t result); + static void ReadCallback(void* data, int32_t result); + static void WriteCallback(void* data, int32_t result); + + bool ProcessCandidates(); + + IncomingCandidateCallback candidate_callback_; + + scoped_ptr<pp::Transport_Dev> transport_; + + bool get_address_pending_; + + net::CompletionCallback* read_callback_; + scoped_refptr<net::IOBuffer> read_buffer_; + + net::CompletionCallback* write_callback_; + scoped_refptr<net::IOBuffer> write_buffer_; + + DISALLOW_COPY_AND_ASSIGN(PepperP2PChannel); +}; + +} // namespace protocol +} // namespace remoting + +#endif // REMOTING_PROTOCOL_PEPPER_P2P_CHANNEL_H_ diff --git a/remoting/remoting.gyp b/remoting/remoting.gyp index 13cac24..a0735be 100644 --- a/remoting/remoting.gyp +++ b/remoting/remoting.gyp @@ -596,6 +596,8 @@ 'protocol/message_decoder.h', 'protocol/message_reader.cc', 'protocol/message_reader.h', + 'protocol/pepper_p2p_channel.h', + 'protocol/pepper_p2p_channel.cc', 'protocol/protobuf_video_reader.cc', 'protocol/protobuf_video_reader.h', 'protocol/protobuf_video_writer.cc', |