OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef REMOTING_PROTOCOL_FAKE_STREAM_SOCKET_H_ | 5 #ifndef REMOTING_PROTOCOL_FAKE_STREAM_SOCKET_H_ |
6 #define REMOTING_PROTOCOL_FAKE_STREAM_SOCKET_H_ | 6 #define REMOTING_PROTOCOL_FAKE_STREAM_SOCKET_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
12 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
13 #include "net/base/completion_callback.h" | 13 #include "net/base/completion_callback.h" |
14 #include "net/socket/stream_socket.h" | 14 #include "remoting/protocol/p2p_socket.h" |
15 #include "remoting/protocol/stream_channel_factory.h" | 15 #include "remoting/protocol/stream_channel_factory.h" |
16 | 16 |
17 namespace base { | 17 namespace base { |
18 class SingleThreadTaskRunner; | 18 class SingleThreadTaskRunner; |
19 } | 19 } |
20 | 20 |
21 namespace remoting { | 21 namespace remoting { |
22 namespace protocol { | 22 namespace protocol { |
23 | 23 |
24 // FakeStreamSocket implement net::StreamSocket interface. All data written to | 24 // FakeStreamSocket implement P2PStreamSocket interface. All data written to |
25 // FakeStreamSocket is stored in a buffer returned by written_data(). Read() | 25 // FakeStreamSocket is stored in a buffer returned by written_data(). Read() |
26 // reads data from another buffer that can be set with AppendInputData(). | 26 // reads data from another buffer that can be set with AppendInputData(). |
27 // Pending reads are supported, so if there is a pending read AppendInputData() | 27 // Pending reads are supported, so if there is a pending read AppendInputData() |
28 // calls the read callback. | 28 // calls the read callback. |
29 // | 29 // |
30 // Two fake sockets can be connected to each other using the | 30 // Two fake sockets can be connected to each other using the |
31 // PairWith() method, e.g.: a->PairWith(b). After this all data | 31 // PairWith() method, e.g.: a->PairWith(b). After this all data |
32 // written to |a| can be read from |b| and vice versa. Two connected | 32 // written to |a| can be read from |b| and vice versa. Two connected |
33 // sockets |a| and |b| must be created and used on the same thread. | 33 // sockets |a| and |b| must be created and used on the same thread. |
34 class FakeStreamSocket : public net::StreamSocket { | 34 class FakeStreamSocket : public P2PStreamSocket { |
35 public: | 35 public: |
36 FakeStreamSocket(); | 36 FakeStreamSocket(); |
37 ~FakeStreamSocket() override; | 37 ~FakeStreamSocket() override; |
38 | 38 |
39 // Returns all data written to the socket. | 39 // Returns all data written to the socket. |
40 const std::string& written_data() const { return written_data_; } | 40 const std::string& written_data() const { return written_data_; } |
41 | 41 |
42 // Sets maximum number of bytes written by each Write() call. | 42 // Sets maximum number of bytes written by each Write() call. |
43 void set_write_limit(int write_limit) { write_limit_ = write_limit; } | 43 void set_write_limit(int write_limit) { write_limit_ = write_limit; } |
44 | 44 |
(...skipping 16 matching lines...) Expand all Loading... |
61 void PairWith(FakeStreamSocket* peer_socket); | 61 void PairWith(FakeStreamSocket* peer_socket); |
62 | 62 |
63 // Current input position in bytes. | 63 // Current input position in bytes. |
64 int input_pos() const { return input_pos_; } | 64 int input_pos() const { return input_pos_; } |
65 | 65 |
66 // True if a Read() call is currently pending. | 66 // True if a Read() call is currently pending. |
67 bool read_pending() const { return !read_callback_.is_null(); } | 67 bool read_pending() const { return !read_callback_.is_null(); } |
68 | 68 |
69 base::WeakPtr<FakeStreamSocket> GetWeakPtr(); | 69 base::WeakPtr<FakeStreamSocket> GetWeakPtr(); |
70 | 70 |
71 // net::Socket implementation. | 71 // P2PStreamSocket interface. |
72 int Read(net::IOBuffer* buf, | 72 int Read(net::IOBuffer* buf, |
73 int buf_len, | 73 int buf_len, |
74 const net::CompletionCallback& callback) override; | 74 const net::CompletionCallback& callback) override; |
75 int Write(net::IOBuffer* buf, | 75 int Write(net::IOBuffer* buf, |
76 int buf_len, | 76 int buf_len, |
77 const net::CompletionCallback& callback) override; | 77 const net::CompletionCallback& callback) override; |
78 int SetReceiveBufferSize(int32 size) override; | 78 int SetReceiveBufferSize(int32 size) override; |
79 int SetSendBufferSize(int32 size) override; | 79 int SetSendBufferSize(int32 size) override; |
80 | 80 |
81 // net::StreamSocket interface. | |
82 int Connect(const net::CompletionCallback& callback) override; | |
83 void Disconnect() override; | |
84 bool IsConnected() const override; | |
85 bool IsConnectedAndIdle() const override; | |
86 int GetPeerAddress(net::IPEndPoint* address) const override; | |
87 int GetLocalAddress(net::IPEndPoint* address) const override; | |
88 const net::BoundNetLog& NetLog() const override; | |
89 void SetSubresourceSpeculation() override; | |
90 void SetOmniboxSpeculation() override; | |
91 bool WasEverUsed() const override; | |
92 bool UsingTCPFastOpen() const override; | |
93 bool WasNpnNegotiated() const override; | |
94 net::NextProto GetNegotiatedProtocol() const override; | |
95 bool GetSSLInfo(net::SSLInfo* ssl_info) override; | |
96 void GetConnectionAttempts(net::ConnectionAttempts* out) const override; | |
97 void ClearConnectionAttempts() override {} | |
98 void AddConnectionAttempts(const net::ConnectionAttempts& attempts) override { | |
99 } | |
100 | |
101 private: | 81 private: |
102 void DoAsyncWrite(scoped_refptr<net::IOBuffer> buf, int buf_len, | 82 void DoAsyncWrite(scoped_refptr<net::IOBuffer> buf, int buf_len, |
103 const net::CompletionCallback& callback); | 83 const net::CompletionCallback& callback); |
104 void DoWrite(net::IOBuffer* buf, int buf_len); | 84 void DoWrite(net::IOBuffer* buf, int buf_len); |
105 | 85 |
106 bool async_write_; | 86 bool async_write_; |
107 bool write_pending_; | 87 bool write_pending_; |
108 int write_limit_; | 88 int write_limit_; |
109 int next_write_error_; | 89 int next_write_error_; |
110 | 90 |
111 int next_read_error_; | 91 int next_read_error_; |
112 scoped_refptr<net::IOBuffer> read_buffer_; | 92 scoped_refptr<net::IOBuffer> read_buffer_; |
113 int read_buffer_size_; | 93 int read_buffer_size_; |
114 net::CompletionCallback read_callback_; | 94 net::CompletionCallback read_callback_; |
115 base::WeakPtr<FakeStreamSocket> peer_socket_; | 95 base::WeakPtr<FakeStreamSocket> peer_socket_; |
116 | 96 |
117 std::string written_data_; | 97 std::string written_data_; |
118 std::string input_data_; | 98 std::string input_data_; |
119 int input_pos_; | 99 int input_pos_; |
120 | 100 |
121 net::BoundNetLog net_log_; | |
122 | |
123 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | 101 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
124 base::WeakPtrFactory<FakeStreamSocket> weak_factory_; | 102 base::WeakPtrFactory<FakeStreamSocket> weak_factory_; |
125 | 103 |
126 DISALLOW_COPY_AND_ASSIGN(FakeStreamSocket); | 104 DISALLOW_COPY_AND_ASSIGN(FakeStreamSocket); |
127 }; | 105 }; |
128 | 106 |
129 // StreamChannelFactory that creates FakeStreamSocket. | 107 // StreamChannelFactory that creates FakeStreamSocket. |
130 class FakeStreamChannelFactory : public StreamChannelFactory { | 108 class FakeStreamChannelFactory : public StreamChannelFactory { |
131 public: | 109 public: |
132 FakeStreamChannelFactory(); | 110 FakeStreamChannelFactory(); |
(...skipping 25 matching lines...) Expand all Loading... |
158 | 136 |
159 base::WeakPtrFactory<FakeStreamChannelFactory> weak_factory_; | 137 base::WeakPtrFactory<FakeStreamChannelFactory> weak_factory_; |
160 | 138 |
161 DISALLOW_COPY_AND_ASSIGN(FakeStreamChannelFactory); | 139 DISALLOW_COPY_AND_ASSIGN(FakeStreamChannelFactory); |
162 }; | 140 }; |
163 | 141 |
164 } // namespace protocol | 142 } // namespace protocol |
165 } // namespace remoting | 143 } // namespace remoting |
166 | 144 |
167 #endif // REMOTING_PROTOCOL_FAKE_STREAM_SOCKET_H_ | 145 #endif // REMOTING_PROTOCOL_FAKE_STREAM_SOCKET_H_ |
OLD | NEW |