fake_datagram_socket.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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
  3. // found in the LICENSE file.
  4. #ifndef REMOTING_PROTOCOL_FAKE_DATAGRAM_SOCKET_H_
  5. #define REMOTING_PROTOCOL_FAKE_DATAGRAM_SOCKET_H_
  6. #include <map>
  7. #include <memory>
  8. #include <string>
  9. #include <vector>
  10. #include "base/memory/weak_ptr.h"
  11. #include "net/base/completion_repeating_callback.h"
  12. #include "remoting/protocol/datagram_channel_factory.h"
  13. #include "remoting/protocol/p2p_datagram_socket.h"
  14. namespace base {
  15. class SingleThreadTaskRunner;
  16. }
  17. namespace remoting {
  18. namespace protocol {
  19. // FakeDatagramSocket implement P2PStreamSocket interface. All data written to
  20. // FakeDatagramSocket is stored in a buffer returned by written_packets().
  21. // Read() reads data from another buffer that can be set with
  22. // AppendInputPacket(). Pending reads are supported, so if there is a pending
  23. // read AppendInputPacket() calls the read callback.
  24. //
  25. // Two fake sockets can be connected to each other using the
  26. // PairWith() method, e.g.: a->PairWith(b). After this all data
  27. // written to |a| can be read from |b| and vice versa. Two connected
  28. // sockets |a| and |b| must be created and used on the same thread.
  29. class FakeDatagramSocket : public P2PDatagramSocket {
  30. public:
  31. FakeDatagramSocket();
  32. FakeDatagramSocket(const FakeDatagramSocket&) = delete;
  33. FakeDatagramSocket& operator=(const FakeDatagramSocket&) = delete;
  34. ~FakeDatagramSocket() override;
  35. const std::vector<std::string>& written_packets() const {
  36. return written_packets_;
  37. }
  38. // Enables asynchronous Write().
  39. void set_async_send(bool async_send) { async_send_ = async_send; }
  40. // Set error codes for the next Write() call. Once returned the
  41. // value is automatically reset to net::OK .
  42. void set_next_send_error(int error) { next_send_error_ = error; }
  43. void AppendInputPacket(const std::string& data);
  44. // Current position in the input in number of packets, i.e. number of finished
  45. // Recv() calls.
  46. int input_pos() const { return input_pos_; }
  47. // Pairs the socket with |peer_socket|. Deleting either of the paired sockets
  48. // unpairs them.
  49. void PairWith(FakeDatagramSocket* peer_socket);
  50. base::WeakPtr<FakeDatagramSocket> GetWeakPtr();
  51. // P2PDatagramSocket implementation.
  52. int Recv(const scoped_refptr<net::IOBuffer>& buf,
  53. int buf_len,
  54. const net::CompletionRepeatingCallback& callback) override;
  55. int Send(const scoped_refptr<net::IOBuffer>& buf,
  56. int buf_len,
  57. const net::CompletionRepeatingCallback& callback) override;
  58. private:
  59. int CopyReadData(const scoped_refptr<net::IOBuffer>& buf, int buf_len);
  60. void DoAsyncSend(const scoped_refptr<net::IOBuffer>& buf,
  61. int buf_len,
  62. const net::CompletionRepeatingCallback& callback);
  63. int DoSend(const scoped_refptr<net::IOBuffer>& buf, int buf_len);
  64. bool async_send_ = false;
  65. bool send_pending_ = false;
  66. int next_send_error_ = 0;
  67. base::WeakPtr<FakeDatagramSocket> peer_socket_;
  68. scoped_refptr<net::IOBuffer> read_buffer_;
  69. int read_buffer_size_;
  70. net::CompletionRepeatingCallback read_callback_;
  71. std::vector<std::string> written_packets_;
  72. std::vector<std::string> input_packets_;
  73. int input_pos_;
  74. scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
  75. base::WeakPtrFactory<FakeDatagramSocket> weak_factory_{this};
  76. };
  77. class FakeDatagramChannelFactory : public DatagramChannelFactory {
  78. public:
  79. FakeDatagramChannelFactory();
  80. FakeDatagramChannelFactory(const FakeDatagramChannelFactory&) = delete;
  81. FakeDatagramChannelFactory& operator=(const FakeDatagramChannelFactory&) =
  82. delete;
  83. ~FakeDatagramChannelFactory() override;
  84. void set_asynchronous_create(bool asynchronous_create) {
  85. asynchronous_create_ = asynchronous_create;
  86. }
  87. void set_fail_create(bool fail_create) { fail_create_ = fail_create; }
  88. // Pair with |peer_factory|. Once paired the factory will be automatically
  89. // pairing created sockets with the sockets with the same name from the peer
  90. // factory.
  91. void PairWith(FakeDatagramChannelFactory* peer_factory);
  92. // Can be used to retrieve FakeDatagramSocket created by this factory, e.g. to
  93. // feed data into it. The caller doesn't get ownership of the result. Returns
  94. // nullptr if the socket doesn't exist.
  95. FakeDatagramSocket* GetFakeChannel(const std::string& name);
  96. // DatagramChannelFactory interface.
  97. void CreateChannel(const std::string& name,
  98. ChannelCreatedCallback callback) override;
  99. void CancelChannelCreation(const std::string& name) override;
  100. private:
  101. typedef std::map<std::string, base::WeakPtr<FakeDatagramSocket> > ChannelsMap;
  102. void NotifyChannelCreated(std::unique_ptr<FakeDatagramSocket> owned_socket,
  103. const std::string& name,
  104. ChannelCreatedCallback callback);
  105. base::WeakPtr<FakeDatagramChannelFactory> peer_factory_;
  106. scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
  107. bool asynchronous_create_;
  108. ChannelsMap channels_;
  109. bool fail_create_;
  110. base::WeakPtrFactory<FakeDatagramChannelFactory> weak_factory_{this};
  111. };
  112. } // namespace protocol
  113. } // namespace remoting
  114. #endif // REMOTING_PROTOCOL_FAKE_DATAGRAM_SOCKET_H_