diff options
author | hclam@chromium.org <hclam@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-11-09 02:22:43 +0000 |
---|---|---|
committer | hclam@chromium.org <hclam@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-11-09 02:22:43 +0000 |
commit | 35b9c560f122a92d671e55985fd0161293961ceb (patch) | |
tree | 919367302b47900ce396684e9e1188d70cd5eca6 /remoting/protocol | |
parent | d78482a45263f6fb46af84a766f9407e54c2ede7 (diff) | |
download | chromium_src-35b9c560f122a92d671e55985fd0161293961ceb.zip chromium_src-35b9c560f122a92d671e55985fd0161293961ceb.tar.gz chromium_src-35b9c560f122a92d671e55985fd0161293961ceb.tar.bz2 |
Implementation of InputStub for chromoting
BUG=none
TEST=none
Review URL: http://codereview.chromium.org/4533001
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@65473 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'remoting/protocol')
-rw-r--r-- | remoting/protocol/client_control_sender.cc | 36 | ||||
-rw-r--r-- | remoting/protocol/client_control_sender.h | 49 | ||||
-rw-r--r-- | remoting/protocol/client_stub.h | 2 | ||||
-rw-r--r-- | remoting/protocol/connection_to_client.cc | 18 | ||||
-rw-r--r-- | remoting/protocol/connection_to_client.h | 13 | ||||
-rw-r--r-- | remoting/protocol/input_sender.cc | 49 | ||||
-rw-r--r-- | remoting/protocol/input_sender.h | 55 | ||||
-rw-r--r-- | remoting/protocol/input_stub.h | 2 | ||||
-rw-r--r-- | remoting/protocol/util.cc | 13 | ||||
-rw-r--r-- | remoting/protocol/util.h | 12 |
10 files changed, 221 insertions, 28 deletions
diff --git a/remoting/protocol/client_control_sender.cc b/remoting/protocol/client_control_sender.cc new file mode 100644 index 0000000..8fff967 --- /dev/null +++ b/remoting/protocol/client_control_sender.cc @@ -0,0 +1,36 @@ +// Copyright (c) 2010 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. + +// This stub is thread safe because of the use of BufferedSocketWriter. +// BufferedSocketWriter buffers messages and send them on them right thread. + +#include "remoting/protocol/client_control_sender.h" + +#include "base/task.h" +#include "remoting/protocol/buffered_socket_writer.h" +#include "remoting/proto/control.pb.h" +#include "remoting/protocol/util.h" + +namespace remoting { +namespace protocol { + +ClientControlSender::ClientControlSender(net::Socket* socket) + : buffered_writer_(new BufferedSocketWriter()) { + buffered_writer_->Init(socket, NULL); +} + +ClientControlSender::~ClientControlSender() { +} + +void ClientControlSender::NotifyResolution( + const NotifyResolutionRequest* msg, Task* done) { + ControlMessage message; + message.mutable_notify_resolution()->CopyFrom(*msg); + buffered_writer_->Write(SerializeAndFrameMessage(message)); + done->Run(); + delete done; +} + +} // namespace protocol +} // namespace remoting diff --git a/remoting/protocol/client_control_sender.h b/remoting/protocol/client_control_sender.h new file mode 100644 index 0000000..6d9b06f --- /dev/null +++ b/remoting/protocol/client_control_sender.h @@ -0,0 +1,49 @@ +// Copyright (c) 2010 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. + +// Implementation of ClientStub using sockets created from jingle connection. +// It sends messages through the socket after serializing it. +// +// Object of this class can only be created by ConnectionToClient. +// +// This class can be used on any thread. + +#ifndef REMOTING_PROTOCOL_CLIENT_STUB_IMPL_H_ +#define REMOTING_PROTOCOL_CLIENT_STUB_IMPL_H_ + +#include "base/basictypes.h" +#include "base/ref_counted.h" +#include "remoting/protocol/client_stub.h" + +class Task; + +namespace net { +class Socket; +} // namespace net + +namespace remoting { +namespace protocol { + +class BufferedSocketWriter; + +class ClientControlSender : public ClientStub { + public: + // Create a stub using a socket. + explicit ClientControlSender(net::Socket* socket); + virtual ~ClientControlSender(); + + virtual void NotifyResolution(const NotifyResolutionRequest* msg, + Task* done); + private: + // Buffered socket writer holds the serialized message and send it on the + // right thread. + scoped_refptr<BufferedSocketWriter> buffered_writer_; + + DISALLOW_COPY_AND_ASSIGN(ClientControlSender); +}; + +} // namespace protocol +} // namespace remoting + +#endif // REMOTING_PROTOCOL_CLIENT_STUB_IMPL_H_ diff --git a/remoting/protocol/client_stub.h b/remoting/protocol/client_stub.h index ac1caaa..2662e95 100644 --- a/remoting/protocol/client_stub.h +++ b/remoting/protocol/client_stub.h @@ -24,7 +24,7 @@ class ClientStub { ClientStub() {} virtual ~ClientStub() {} - virtual void NotifyResolution(const NotifyResolutionRequest& msg, + virtual void NotifyResolution(const NotifyResolutionRequest* msg, Task* done) = 0; private: diff --git a/remoting/protocol/connection_to_client.cc b/remoting/protocol/connection_to_client.cc index 9925648..417600b 100644 --- a/remoting/protocol/connection_to_client.cc +++ b/remoting/protocol/connection_to_client.cc @@ -6,7 +6,7 @@ #include "google/protobuf/message.h" #include "net/base/io_buffer.h" -#include "remoting/protocol/util.h" +#include "remoting/protocol/client_control_sender.h" // TODO(hclam): Remove this header once MessageDispatcher is used. #include "remoting/base/multiple_array_input_stream.h" @@ -43,20 +43,6 @@ protocol::Session* ConnectionToClient::session() { return session_; } -void ConnectionToClient::SendInitClientMessage(int width, int height) { - DCHECK_EQ(loop_, MessageLoop::current()); - - // If we are disconnected then return. - if (!session_) - return; - - ChromotingHostMessage msg; - msg.mutable_init_client()->set_width(width); - msg.mutable_init_client()->set_height(height); - DCHECK(msg.IsInitialized()); - control_writer_.SendMessage(msg); -} - void ConnectionToClient::SendVideoPacket(const VideoPacket& packet) { DCHECK_EQ(loop_, MessageLoop::current()); @@ -87,7 +73,7 @@ ConnectionToClient::ConnectionToClient() {} void ConnectionToClient::OnSessionStateChange( protocol::Session::State state) { if (state == protocol::Session::CONNECTED) { - control_writer_.Init(session_->control_channel()); + client_stub_.reset(new ClientControlSender(session_->control_channel())); event_reader_.Init<ChromotingClientMessage>( session_->event_channel(), NewCallback(this, &ConnectionToClient::OnMessageReceived)); diff --git a/remoting/protocol/connection_to_client.h b/remoting/protocol/connection_to_client.h index ee8b1d7..769b0a8 100644 --- a/remoting/protocol/connection_to_client.h +++ b/remoting/protocol/connection_to_client.h @@ -11,7 +11,6 @@ #include "base/message_loop.h" #include "base/ref_counted.h" #include "base/scoped_ptr.h" -#include "remoting/proto/internal.pb.h" #include "remoting/protocol/message_reader.h" #include "remoting/protocol/session.h" #include "remoting/protocol/stream_writer.h" @@ -20,6 +19,8 @@ namespace remoting { namespace protocol { +class ClientStub; + // This class represents a remote viewer connected to the chromoting host // through a libjingle connection. A viewer object is responsible for sending // screen updates and other messages to the remote viewer. It is also @@ -63,9 +64,6 @@ class ConnectionToClient : // Returns the connection in use. virtual protocol::Session* session(); - // Send information to the client for initialization. - virtual void SendInitClientMessage(int width, int height); - // Send encoded update stream data to the viewer. virtual void SendVideoPacket(const VideoPacket& packet); @@ -81,6 +79,9 @@ class ConnectionToClient : // After this method is called all the send method calls will be ignored. virtual void Disconnect(); + // Return pointer to ClientStub. + virtual ClientStub* client_stub() { return client_stub_.get(); } + protected: // Protected constructor used by unit test. ConnectionToClient(); @@ -103,10 +104,12 @@ class ConnectionToClient : // The libjingle channel used to send and receive data from the remote client. scoped_refptr<protocol::Session> session_; - ControlStreamWriter control_writer_; MessageReader event_reader_; scoped_ptr<VideoWriter> video_writer_; + // ClientStub for sending messages to the client. + scoped_ptr<ClientStub> client_stub_; + // The message loop that this object runs on. MessageLoop* loop_; diff --git a/remoting/protocol/input_sender.cc b/remoting/protocol/input_sender.cc new file mode 100644 index 0000000..d7d5236 --- /dev/null +++ b/remoting/protocol/input_sender.cc @@ -0,0 +1,49 @@ +// Copyright (c) 2010 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. + +// This stub is thread safe because of the use of BufferedSocketWriter. +// BufferedSocketWriter buffers messages and send them on them right thread. + +#include "remoting/protocol/input_sender.h" + +#include "base/task.h" +#include "remoting/proto/event.pb.h" +#include "remoting/protocol/buffered_socket_writer.h" +#include "remoting/protocol/util.h" + +namespace remoting { +namespace protocol { + +InputSender::InputSender(net::Socket* socket) + : buffered_writer_(new BufferedSocketWriter()) { + buffered_writer_->Init(socket, NULL); +} + +InputSender::~InputSender() { +} + +void InputSender::InjectKeyEvent(const KeyEvent* event, Task* done) { + EventMessage message; + Event* evt = message.add_event(); + // TODO(hclam): Provide timestamp. + evt->set_timestamp(0); + evt->mutable_key()->CopyFrom(*event); + buffered_writer_->Write(SerializeAndFrameMessage(message)); + done->Run(); + delete done; +} + +void InputSender::InjectMouseEvent(const MouseEvent* event, Task* done) { + EventMessage message; + Event* evt = message.add_event(); + // TODO(hclam): Provide timestamp. + evt->set_timestamp(0); + evt->mutable_mouse()->CopyFrom(*event); + buffered_writer_->Write(SerializeAndFrameMessage(message)); + done->Run(); + delete done; +} + +} // namespace protocol +} // namespace remoting diff --git a/remoting/protocol/input_sender.h b/remoting/protocol/input_sender.h new file mode 100644 index 0000000..cbbdb06 --- /dev/null +++ b/remoting/protocol/input_sender.h @@ -0,0 +1,55 @@ +// Copyright (c) 2010 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. + +// Implementation of InputStub using sockets created from jingle connection. +// It sends messages through the socket after serializing it. +// +// Object of this class can only be created by ConnectionToHost. +// +// This class can be used on any thread. An object of socket is given to this +// class, its lifetime is strictly greater than this object. + +#ifndef REMOTING_PROTOCOL_INPUT_SENDER_H_ +#define REMOTING_PROTOCOL_INPUT_SENDER_H_ + +#include "base/basictypes.h" +#include "base/ref_counted.h" +#include "remoting/protocol/input_stub.h" + +class Task; + +namespace net { +class Socket; +} // namespace net + +namespace remoting { +namespace protocol { + +class BufferedSocketWriter; + +class InputSender : public InputStub { + public: + // Create a stub using a socket. + explicit InputSender(net::Socket* socket); + virtual ~InputSender(); + + // InputStub implementation. + virtual void InjectKeyEvent(const KeyEvent* event, Task* done); + virtual void InjectMouseEvent(const MouseEvent* event, Task* done); + + private: + // Helper method to run the task and delete it afterwards. + void RunTask(Task* done); + + // Buffered socket writer holds the serialized message and sends it on the + // right thread. + scoped_refptr<BufferedSocketWriter> buffered_writer_; + + DISALLOW_COPY_AND_ASSIGN(InputSender); +}; + +} // namespace protocol +} // namespace remoting + +#endif // REMOTING_PROTOCOL_INPUT_SENDER_H_ diff --git a/remoting/protocol/input_stub.h b/remoting/protocol/input_stub.h index 680163b..201dda3 100644 --- a/remoting/protocol/input_stub.h +++ b/remoting/protocol/input_stub.h @@ -8,8 +8,6 @@ #ifndef REMOTING_PROTOCOL_INPUT_STUB_H_ #define REMOTING_PROTOCOL_INPUT_STUB_H_ -#include "base/basictypes.h" - class Task; namespace remoting { diff --git a/remoting/protocol/util.cc b/remoting/protocol/util.cc index a12040b..cfb9001 100644 --- a/remoting/protocol/util.cc +++ b/remoting/protocol/util.cc @@ -7,9 +7,18 @@ #include "base/basictypes.h" #include "base/hash_tables.h" #include "base/logging.h" +#include "base/task.h" #include "net/base/io_buffer.h" #include "third_party/libjingle/source/talk/base/byteorder.h" +namespace { + +void DeleteMessage(google::protobuf::MessageLite* message) { + delete message; +} + +} // namespace + namespace remoting { scoped_refptr<net::IOBufferWithSize> SerializeAndFrameMessage( @@ -25,4 +34,8 @@ scoped_refptr<net::IOBufferWithSize> SerializeAndFrameMessage( return buffer; } +Task* NewDeleteMessageTask(google::protobuf::MessageLite* message) { + return NewRunnableFunction(&DeleteMessage, message); +} + } // namespace remoting diff --git a/remoting/protocol/util.h b/remoting/protocol/util.h index 9dee8b5a..8c460f0 100644 --- a/remoting/protocol/util.h +++ b/remoting/protocol/util.h @@ -2,16 +2,17 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +// This file defines utility methods used for encoding and decoding the protocol +// used in Chromoting. + #ifndef REMOTING_PROTOCOL_UTIL_H_ #define REMOTING_PROTOCOL_UTIL_H_ #include "google/protobuf/message_lite.h" #include "net/base/io_buffer.h" -// TODO(hclam): Should not include internal.pb.h. -#include "remoting/proto/internal.pb.h" -// This file defines utility methods used for encoding and decoding the protocol -// used in Chromoting. +class Task; + namespace remoting { // Serialize the Protocol Buffer message and provide sufficient framing for @@ -21,6 +22,9 @@ namespace remoting { scoped_refptr<net::IOBufferWithSize> SerializeAndFrameMessage( const google::protobuf::MessageLite& msg); +// Create a runnable task that deletes a message. +Task* NewDeleteMessageTask(google::protobuf::MessageLite* message); + } // namespace remoting #endif // REMOTING_PROTOCOL_UTIL_H_ |