Chromium Code Reviews| Index: remoting/protocol/fake_stream_socket.h |
| diff --git a/remoting/protocol/fake_stream_socket.h b/remoting/protocol/fake_stream_socket.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..457f26174fe1f349e3c36f11a4bd46c818a1dfc3 |
| --- /dev/null |
| +++ b/remoting/protocol/fake_stream_socket.h |
| @@ -0,0 +1,141 @@ |
| +// Copyright 2014 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_FAKE_STREAM_SOCKET_H_ |
| +#define REMOTING_PROTOCOL_FAKE_STREAM_SOCKET_H_ |
| + |
| +#include <map> |
| +#include <string> |
| + |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/memory/weak_ptr.h" |
| +#include "net/base/completion_callback.h" |
| +#include "net/socket/stream_socket.h" |
| +#include "remoting/protocol/stream_channel_factory.h" |
| + |
| +namespace base { |
| +class SingleThreadTaskRunner; |
| +} |
| + |
| +namespace remoting { |
| +namespace protocol { |
| + |
| +// FakeStreamSocket implement net::StreamSocket interface. All data written to |
|
Wez
2014/09/20 00:25:10
Is there really nothing in net/ that performs this
Sergey Ulanov
2014/09/22 19:22:12
No.
|
| +// FakeStreamSocket is stored in a buffer returned by written_data(). Read() |
| +// reads data from another buffer that can be set with AppendInputData(). |
|
Wez
2014/09/20 00:25:10
Similar comments re naming of "input data" apply a
Sergey Ulanov
2014/09/22 19:22:12
This is not new code, so I'd prefer to keep Append
|
| +// Pending reads are supported, so if there is a pending read AppendInputData() |
| +// calls the read callback. |
| +// |
| +// Two fake sockets can be connected to each other using the |
| +// PairWith() method, e.g.: a->PairWith(b). After this all data |
| +// written to |a| can be read from |b| and vice versa. Two connected |
| +// sockets |a| and |b| must be created and used on the same thread. |
| +class FakeStreamSocket : public net::StreamSocket { |
| + public: |
| + FakeStreamSocket(); |
| + virtual ~FakeStreamSocket(); |
| + |
| + const std::string& written_data() const { return written_data_; } |
|
Wez
2014/09/20 00:25:10
I assume that this returns a single string w/ all
Sergey Ulanov
2014/09/22 19:22:12
Done.
|
| + |
| + void set_write_limit(int write_limit) { write_limit_ = write_limit; } |
| + void set_async_write(bool async_write) { async_write_ = async_write; } |
| + void set_next_write_error(int error) { next_write_error_ = error; } |
| + void set_next_read_error(int error) { next_read_error_ = error; } |
| + void AppendInputData(const std::string& data); |
| + void PairWith(FakeStreamSocket* peer_socket); |
| + int input_pos() const { return input_pos_; } |
| + bool read_pending() const { return read_pending_; } |
|
Wez
2014/09/20 00:25:10
Document each of these, e.g. specify units of writ
Sergey Ulanov
2014/09/22 19:22:12
Done.
|
| + |
| + // net::Socket implementation. |
| + virtual int Read(net::IOBuffer* buf, int buf_len, |
| + const net::CompletionCallback& callback) OVERRIDE; |
| + virtual int Write(net::IOBuffer* buf, int buf_len, |
| + const net::CompletionCallback& callback) OVERRIDE; |
| + |
| + virtual int SetReceiveBufferSize(int32 size) OVERRIDE; |
| + virtual int SetSendBufferSize(int32 size) OVERRIDE; |
| + |
| + // net::StreamSocket interface. |
| + virtual int Connect(const net::CompletionCallback& callback) OVERRIDE; |
| + virtual void Disconnect() OVERRIDE; |
| + virtual bool IsConnected() const OVERRIDE; |
| + virtual bool IsConnectedAndIdle() const OVERRIDE; |
| + virtual int GetPeerAddress(net::IPEndPoint* address) const OVERRIDE; |
| + virtual int GetLocalAddress(net::IPEndPoint* address) const OVERRIDE; |
| + virtual const net::BoundNetLog& NetLog() const OVERRIDE; |
| + virtual void SetSubresourceSpeculation() OVERRIDE; |
| + virtual void SetOmniboxSpeculation() OVERRIDE; |
| + virtual bool WasEverUsed() const OVERRIDE; |
| + virtual bool UsingTCPFastOpen() const OVERRIDE; |
| + virtual bool WasNpnNegotiated() const OVERRIDE; |
| + virtual net::NextProto GetNegotiatedProtocol() const OVERRIDE; |
| + virtual bool GetSSLInfo(net::SSLInfo* ssl_info) OVERRIDE; |
| + |
| + private: |
| + void DoAsyncWrite(scoped_refptr<net::IOBuffer> buf, int buf_len, |
| + const net::CompletionCallback& callback); |
| + void DoWrite(net::IOBuffer* buf, int buf_len); |
| + |
| + bool async_write_; |
| + bool write_pending_; |
| + int write_limit_; |
| + int next_write_error_; |
| + |
| + int next_read_error_; |
| + bool read_pending_; |
|
Wez
2014/09/20 00:25:10
is_read_pending_ or read_is_pending_
Sergey Ulanov
2014/09/22 19:22:12
read_pending_ is used in many other places. remove
|
| + scoped_refptr<net::IOBuffer> read_buffer_; |
| + int read_buffer_size_; |
| + net::CompletionCallback read_callback_; |
| + base::WeakPtr<FakeStreamSocket> peer_socket_; |
| + |
| + std::string written_data_; |
| + std::string input_data_; |
| + int input_pos_; |
| + |
| + net::BoundNetLog net_log_; |
| + |
| + scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| + base::WeakPtrFactory<FakeStreamSocket> weak_factory_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(FakeStreamSocket); |
| +}; |
| + |
| +// StreamChannelFactory that creates FakeStreamSocket. |
| +class FakeStreamChannelFactory : public StreamChannelFactory { |
| + public: |
| + FakeStreamChannelFactory(); |
| + virtual ~FakeStreamChannelFactory(); |
| + |
| + void set_async_creation(bool async_creation) { |
| + async_creation_ = async_creation; |
| + } |
| + |
| + void set_fail(bool fail) { fail_ = fail; } |
| + |
| + FakeStreamSocket* GetChannel(const std::string& name); |
| + |
| + // ChannelFactory interface. |
| + virtual void CreateChannel(const std::string& name, |
| + const ChannelCreatedCallback& callback) OVERRIDE; |
| + virtual void CancelChannelCreation(const std::string& name) OVERRIDE; |
| + |
| + private: |
| + void NotifyChannelCreated(const std::string& name, |
| + const ChannelCreatedCallback& callback); |
| + |
| + scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| + bool async_creation_; |
| + std::map<std::string, FakeStreamSocket*> channels_; |
| + |
| + bool fail_; |
| + |
| + base::WeakPtrFactory<FakeStreamChannelFactory> weak_factory_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(FakeStreamChannelFactory); |
| +}; |
| + |
| +} // namespace protocol |
| +} // namespace remoting |
| + |
| +#endif // REMOTING_PROTOCOL_FAKE_STREAM_SOCKET_H_ |