fake_stream_socket.h 5.17 KB
Newer Older
1 2 3 4 5 6 7 8
// 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>
9
#include <memory>
10 11
#include <string>

12
#include "base/macros.h"
13
#include "base/memory/weak_ptr.h"
14
#include "base/optional.h"
15
#include "net/base/completion_callback.h"
16
#include "remoting/protocol/p2p_stream_socket.h"
17 18 19 20 21 22 23 24 25
#include "remoting/protocol/stream_channel_factory.h"

namespace base {
class SingleThreadTaskRunner;
}

namespace remoting {
namespace protocol {

26
// FakeStreamSocket implement P2PStreamSocket interface. All data written to
27 28 29 30 31 32 33 34 35
// FakeStreamSocket is stored in a buffer returned by written_data(). Read()
// reads data from another buffer that can be set with AppendInputData().
// 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.
36
class FakeStreamSocket : public P2PStreamSocket {
37 38
 public:
  FakeStreamSocket();
39
  ~FakeStreamSocket() override;
40 41 42 43 44 45 46 47 48 49

  // Returns all data written to the socket.
  const std::string& written_data() const { return written_data_; }

  // Sets maximum number of bytes written by each Write() call.
  void set_write_limit(int write_limit) { write_limit_ = write_limit; }

  // Enables asynchronous Write().
  void set_async_write(bool async_write) { async_write_ = async_write; }

50 51
  // Set error codes for the next Write() call. Once returned the
  // value is automatically reset to net::OK .
52 53 54 55 56
  void set_next_write_error(int error) { next_write_error_ = error; }

  // Appends |data| to the read buffer.
  void AppendInputData(const std::string& data);

57 58
  // Causes Read() to fail with |error| once the read buffer is exhausted. If
  // there is a currently pending Read, it is interrupted.
59
  void SetReadError(int error);
60

61 62 63 64 65 66 67 68 69 70 71 72
  // Pairs the socket with |peer_socket|. Deleting either of the paired sockets
  // unpairs them.
  void PairWith(FakeStreamSocket* peer_socket);

  // Current input position in bytes.
  int input_pos() const { return input_pos_; }

  // True if a Read() call is currently pending.
  bool read_pending() const { return !read_callback_.is_null(); }

  base::WeakPtr<FakeStreamSocket> GetWeakPtr();

73 74
  // P2PStreamSocket interface.
  int Read(const scoped_refptr<net::IOBuffer>& buf, int buf_len,
75
           const net::CompletionCallback& callback) override;
76
  int Write(const scoped_refptr<net::IOBuffer>& buf, int buf_len,
77
            const net::CompletionCallback& callback) override;
78 79

 private:
80
  void DoAsyncWrite(const scoped_refptr<net::IOBuffer>& buf, int buf_len,
81
                    const net::CompletionCallback& callback);
82
  void DoWrite(const scoped_refptr<net::IOBuffer>& buf, int buf_len);
83

84 85 86 87
  bool async_write_ = false;
  bool write_pending_ = false;
  int write_limit_ = 0;
  int next_write_error_ = 0;
88

89
  base::Optional<int> next_read_error_;
90
  scoped_refptr<net::IOBuffer> read_buffer_;
91
  int read_buffer_size_ = 0;
92 93 94 95 96
  net::CompletionCallback read_callback_;
  base::WeakPtr<FakeStreamSocket> peer_socket_;

  std::string written_data_;
  std::string input_data_;
97
  int input_pos_ = 0;
98 99 100 101 102 103 104 105 106 107 108

  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();
109
  ~FakeStreamChannelFactory() override;
110 111 112 113 114 115 116

  void set_asynchronous_create(bool asynchronous_create) {
    asynchronous_create_ = asynchronous_create;
  }

  void set_fail_create(bool fail_create) { fail_create_ = fail_create; }

117 118 119
  // Enables asynchronous Write() on created channels.
  void set_async_write(bool async_write) { async_write_ = async_write; }

120 121
  FakeStreamSocket* GetFakeChannel(const std::string& name);

122 123 124 125
  // Pairs the socket with |peer_socket|. Deleting either of the paired sockets
  // unpairs them.
  void PairWith(FakeStreamChannelFactory* peer_factory);

126
  // ChannelFactory interface.
127 128 129
  void CreateChannel(const std::string& name,
                     const ChannelCreatedCallback& callback) override;
  void CancelChannelCreation(const std::string& name) override;
130 131

 private:
132
  void NotifyChannelCreated(std::unique_ptr<FakeStreamSocket> owned_channel,
133 134 135 136
                            const std::string& name,
                            const ChannelCreatedCallback& callback);

  scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
137 138 139 140
  bool asynchronous_create_ = false;
  std::map<std::string, base::WeakPtr<FakeStreamSocket>> channels_;

  bool fail_create_ = false;
141

142
  bool async_write_ = false;
143

144
  base::WeakPtr<FakeStreamChannelFactory> peer_factory_;
145 146 147 148 149 150 151 152 153
  base::WeakPtrFactory<FakeStreamChannelFactory> weak_factory_;

  DISALLOW_COPY_AND_ASSIGN(FakeStreamChannelFactory);
};

}  // namespace protocol
}  // namespace remoting

#endif  // REMOTING_PROTOCOL_FAKE_STREAM_SOCKET_H_