| 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_DATAGRAM_SOCKET_H_ | 5 #ifndef REMOTING_PROTOCOL_FAKE_DATAGRAM_SOCKET_H_ |
| 6 #define REMOTING_PROTOCOL_FAKE_DATAGRAM_SOCKET_H_ | 6 #define REMOTING_PROTOCOL_FAKE_DATAGRAM_SOCKET_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 // AppendInputPacket(). Pending reads are supported, so if there is a pending | 28 // AppendInputPacket(). Pending reads are supported, so if there is a pending |
| 29 // read AppendInputPacket() calls the read callback. | 29 // read AppendInputPacket() calls the read callback. |
| 30 // | 30 // |
| 31 // Two fake sockets can be connected to each other using the | 31 // Two fake sockets can be connected to each other using the |
| 32 // PairWith() method, e.g.: a->PairWith(b). After this all data | 32 // PairWith() method, e.g.: a->PairWith(b). After this all data |
| 33 // written to |a| can be read from |b| and vice versa. Two connected | 33 // written to |a| can be read from |b| and vice versa. Two connected |
| 34 // sockets |a| and |b| must be created and used on the same thread. | 34 // sockets |a| and |b| must be created and used on the same thread. |
| 35 class FakeDatagramSocket : public net::Socket { | 35 class FakeDatagramSocket : public net::Socket { |
| 36 public: | 36 public: |
| 37 FakeDatagramSocket(); | 37 FakeDatagramSocket(); |
| 38 virtual ~FakeDatagramSocket(); | 38 ~FakeDatagramSocket() override; |
| 39 | 39 |
| 40 const std::vector<std::string>& written_packets() const { | 40 const std::vector<std::string>& written_packets() const { |
| 41 return written_packets_; | 41 return written_packets_; |
| 42 } | 42 } |
| 43 | 43 |
| 44 void AppendInputPacket(const std::string& data); | 44 void AppendInputPacket(const std::string& data); |
| 45 | 45 |
| 46 // Current position in the input in number of packets, i.e. number of finished | 46 // Current position in the input in number of packets, i.e. number of finished |
| 47 // Read() calls. | 47 // Read() calls. |
| 48 int input_pos() const { return input_pos_; } | 48 int input_pos() const { return input_pos_; } |
| 49 | 49 |
| 50 // Pairs the socket with |peer_socket|. Deleting either of the paired sockets | 50 // Pairs the socket with |peer_socket|. Deleting either of the paired sockets |
| 51 // unpairs them. | 51 // unpairs them. |
| 52 void PairWith(FakeDatagramSocket* peer_socket); | 52 void PairWith(FakeDatagramSocket* peer_socket); |
| 53 | 53 |
| 54 base::WeakPtr<FakeDatagramSocket> GetWeakPtr(); | 54 base::WeakPtr<FakeDatagramSocket> GetWeakPtr(); |
| 55 | 55 |
| 56 // net::Socket implementation. | 56 // net::Socket implementation. |
| 57 virtual int Read(net::IOBuffer* buf, int buf_len, | 57 int Read(net::IOBuffer* buf, |
| 58 const net::CompletionCallback& callback) override; | 58 int buf_len, |
| 59 virtual int Write(net::IOBuffer* buf, int buf_len, | 59 const net::CompletionCallback& callback) override; |
| 60 const net::CompletionCallback& callback) override; | 60 int Write(net::IOBuffer* buf, |
| 61 virtual int SetReceiveBufferSize(int32 size) override; | 61 int buf_len, |
| 62 virtual int SetSendBufferSize(int32 size) override; | 62 const net::CompletionCallback& callback) override; |
| 63 int SetReceiveBufferSize(int32 size) override; |
| 64 int SetSendBufferSize(int32 size) override; |
| 63 | 65 |
| 64 private: | 66 private: |
| 65 int CopyReadData(net::IOBuffer* buf, int buf_len); | 67 int CopyReadData(net::IOBuffer* buf, int buf_len); |
| 66 | 68 |
| 67 base::WeakPtr<FakeDatagramSocket> peer_socket_; | 69 base::WeakPtr<FakeDatagramSocket> peer_socket_; |
| 68 | 70 |
| 69 scoped_refptr<net::IOBuffer> read_buffer_; | 71 scoped_refptr<net::IOBuffer> read_buffer_; |
| 70 int read_buffer_size_; | 72 int read_buffer_size_; |
| 71 net::CompletionCallback read_callback_; | 73 net::CompletionCallback read_callback_; |
| 72 | 74 |
| 73 std::vector<std::string> written_packets_; | 75 std::vector<std::string> written_packets_; |
| 74 std::vector<std::string> input_packets_; | 76 std::vector<std::string> input_packets_; |
| 75 int input_pos_; | 77 int input_pos_; |
| 76 | 78 |
| 77 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | 79 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 78 base::WeakPtrFactory<FakeDatagramSocket> weak_factory_; | 80 base::WeakPtrFactory<FakeDatagramSocket> weak_factory_; |
| 79 | 81 |
| 80 DISALLOW_COPY_AND_ASSIGN(FakeDatagramSocket); | 82 DISALLOW_COPY_AND_ASSIGN(FakeDatagramSocket); |
| 81 }; | 83 }; |
| 82 | 84 |
| 83 class FakeDatagramChannelFactory : public DatagramChannelFactory { | 85 class FakeDatagramChannelFactory : public DatagramChannelFactory { |
| 84 public: | 86 public: |
| 85 FakeDatagramChannelFactory(); | 87 FakeDatagramChannelFactory(); |
| 86 virtual ~FakeDatagramChannelFactory(); | 88 ~FakeDatagramChannelFactory() override; |
| 87 | 89 |
| 88 void set_asynchronous_create(bool asynchronous_create) { | 90 void set_asynchronous_create(bool asynchronous_create) { |
| 89 asynchronous_create_ = asynchronous_create; | 91 asynchronous_create_ = asynchronous_create; |
| 90 } | 92 } |
| 91 | 93 |
| 92 void set_fail_create(bool fail_create) { fail_create_ = fail_create; } | 94 void set_fail_create(bool fail_create) { fail_create_ = fail_create; } |
| 93 | 95 |
| 94 // Pair with |peer_factory|. Once paired the factory will be automatically | 96 // Pair with |peer_factory|. Once paired the factory will be automatically |
| 95 // pairing created sockets with the sockets with the same name from the peer | 97 // pairing created sockets with the sockets with the same name from the peer |
| 96 // factory. | 98 // factory. |
| 97 void PairWith(FakeDatagramChannelFactory* peer_factory); | 99 void PairWith(FakeDatagramChannelFactory* peer_factory); |
| 98 | 100 |
| 99 // Can be used to retrieve FakeDatagramSocket created by this factory, e.g. to | 101 // Can be used to retrieve FakeDatagramSocket created by this factory, e.g. to |
| 100 // feed data into it. The caller doesn't get ownership of the result. Returns | 102 // feed data into it. The caller doesn't get ownership of the result. Returns |
| 101 // NULL if the socket doesn't exist. | 103 // NULL if the socket doesn't exist. |
| 102 FakeDatagramSocket* GetFakeChannel(const std::string& name); | 104 FakeDatagramSocket* GetFakeChannel(const std::string& name); |
| 103 | 105 |
| 104 // DatagramChannelFactory interface. | 106 // DatagramChannelFactory interface. |
| 105 virtual void CreateChannel(const std::string& name, | 107 void CreateChannel(const std::string& name, |
| 106 const ChannelCreatedCallback& callback) override; | 108 const ChannelCreatedCallback& callback) override; |
| 107 virtual void CancelChannelCreation(const std::string& name) override; | 109 void CancelChannelCreation(const std::string& name) override; |
| 108 | 110 |
| 109 private: | 111 private: |
| 110 typedef std::map<std::string, base::WeakPtr<FakeDatagramSocket> > ChannelsMap; | 112 typedef std::map<std::string, base::WeakPtr<FakeDatagramSocket> > ChannelsMap; |
| 111 | 113 |
| 112 void NotifyChannelCreated(scoped_ptr<FakeDatagramSocket> owned_socket, | 114 void NotifyChannelCreated(scoped_ptr<FakeDatagramSocket> owned_socket, |
| 113 const std::string& name, | 115 const std::string& name, |
| 114 const ChannelCreatedCallback& callback); | 116 const ChannelCreatedCallback& callback); |
| 115 | 117 |
| 116 base::WeakPtr<FakeDatagramChannelFactory> peer_factory_; | 118 base::WeakPtr<FakeDatagramChannelFactory> peer_factory_; |
| 117 | 119 |
| 118 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | 120 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 119 bool asynchronous_create_; | 121 bool asynchronous_create_; |
| 120 ChannelsMap channels_; | 122 ChannelsMap channels_; |
| 121 | 123 |
| 122 bool fail_create_; | 124 bool fail_create_; |
| 123 | 125 |
| 124 base::WeakPtrFactory<FakeDatagramChannelFactory> weak_factory_; | 126 base::WeakPtrFactory<FakeDatagramChannelFactory> weak_factory_; |
| 125 | 127 |
| 126 DISALLOW_COPY_AND_ASSIGN(FakeDatagramChannelFactory); | 128 DISALLOW_COPY_AND_ASSIGN(FakeDatagramChannelFactory); |
| 127 }; | 129 }; |
| 128 | 130 |
| 129 } // namespace protocol | 131 } // namespace protocol |
| 130 } // namespace remoting | 132 } // namespace remoting |
| 131 | 133 |
| 132 #endif // REMOTING_PROTOCOL_FAKE_DATAGRAM_SOCKET_H_ | 134 #endif // REMOTING_PROTOCOL_FAKE_DATAGRAM_SOCKET_H_ |
| OLD | NEW |