Use the appropriate variant of IntToString in //remoting
[chromium-blink-merge.git] / remoting / protocol / fake_datagram_socket.h
bloba22751d05ced387270b19f20d87c9a9bd3bcd577
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.
5 #ifndef REMOTING_PROTOCOL_FAKE_DATAGRAM_SOCKET_H_
6 #define REMOTING_PROTOCOL_FAKE_DATAGRAM_SOCKET_H_
8 #include <map>
9 #include <string>
10 #include <vector>
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/weak_ptr.h"
14 #include "net/base/completion_callback.h"
15 #include "remoting/protocol/datagram_channel_factory.h"
16 #include "remoting/protocol/p2p_datagram_socket.h"
18 namespace base {
19 class SingleThreadTaskRunner;
22 namespace remoting {
23 namespace protocol {
25 // FakeDatagramSocket implement P2PStreamSocket interface. All data written to
26 // FakeDatagramSocket is stored in a buffer returned by written_packets().
27 // Read() reads data from another buffer that can be set with
28 // AppendInputPacket(). Pending reads are supported, so if there is a pending
29 // read AppendInputPacket() calls the read callback.
31 // Two fake sockets can be connected to each other using the
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
34 // sockets |a| and |b| must be created and used on the same thread.
35 class FakeDatagramSocket : public P2PDatagramSocket {
36 public:
37 FakeDatagramSocket();
38 ~FakeDatagramSocket() override;
40 const std::vector<std::string>& written_packets() const {
41 return written_packets_;
44 // Enables asynchronous Write().
45 void set_async_send(bool async_send) { async_send_ = async_send; }
47 // Set error codes for the next Write() call. Once returned the
48 // value is automatically reset to net::OK .
49 void set_next_send_error(int error) { next_send_error_ = error; }
51 void AppendInputPacket(const std::string& data);
53 // Current position in the input in number of packets, i.e. number of finished
54 // Recv() calls.
55 int input_pos() const { return input_pos_; }
57 // Pairs the socket with |peer_socket|. Deleting either of the paired sockets
58 // unpairs them.
59 void PairWith(FakeDatagramSocket* peer_socket);
61 base::WeakPtr<FakeDatagramSocket> GetWeakPtr();
63 // P2PDatagramSocket implementation.
64 int Recv(const scoped_refptr<net::IOBuffer>& buf, int buf_len,
65 const net::CompletionCallback& callback) override;
66 int Send(const scoped_refptr<net::IOBuffer>& buf, int buf_len,
67 const net::CompletionCallback& callback) override;
69 private:
70 int CopyReadData(const scoped_refptr<net::IOBuffer>& buf, int buf_len);
72 void DoAsyncSend(const scoped_refptr<net::IOBuffer>& buf, int buf_len,
73 const net::CompletionCallback& callback);
74 int DoSend(const scoped_refptr<net::IOBuffer>& buf, int buf_len);
76 bool async_send_ = false;
77 bool send_pending_ = false;
78 int next_send_error_ = 0;
80 base::WeakPtr<FakeDatagramSocket> peer_socket_;
82 scoped_refptr<net::IOBuffer> read_buffer_;
83 int read_buffer_size_;
84 net::CompletionCallback read_callback_;
86 std::vector<std::string> written_packets_;
87 std::vector<std::string> input_packets_;
88 int input_pos_;
90 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
91 base::WeakPtrFactory<FakeDatagramSocket> weak_factory_;
93 DISALLOW_COPY_AND_ASSIGN(FakeDatagramSocket);
96 class FakeDatagramChannelFactory : public DatagramChannelFactory {
97 public:
98 FakeDatagramChannelFactory();
99 ~FakeDatagramChannelFactory() override;
101 void set_asynchronous_create(bool asynchronous_create) {
102 asynchronous_create_ = asynchronous_create;
105 void set_fail_create(bool fail_create) { fail_create_ = fail_create; }
107 // Pair with |peer_factory|. Once paired the factory will be automatically
108 // pairing created sockets with the sockets with the same name from the peer
109 // factory.
110 void PairWith(FakeDatagramChannelFactory* peer_factory);
112 // Can be used to retrieve FakeDatagramSocket created by this factory, e.g. to
113 // feed data into it. The caller doesn't get ownership of the result. Returns
114 // nullptr if the socket doesn't exist.
115 FakeDatagramSocket* GetFakeChannel(const std::string& name);
117 // DatagramChannelFactory interface.
118 void CreateChannel(const std::string& name,
119 const ChannelCreatedCallback& callback) override;
120 void CancelChannelCreation(const std::string& name) override;
122 private:
123 typedef std::map<std::string, base::WeakPtr<FakeDatagramSocket> > ChannelsMap;
125 void NotifyChannelCreated(scoped_ptr<FakeDatagramSocket> owned_socket,
126 const std::string& name,
127 const ChannelCreatedCallback& callback);
129 base::WeakPtr<FakeDatagramChannelFactory> peer_factory_;
131 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
132 bool asynchronous_create_;
133 ChannelsMap channels_;
135 bool fail_create_;
137 base::WeakPtrFactory<FakeDatagramChannelFactory> weak_factory_;
139 DISALLOW_COPY_AND_ASSIGN(FakeDatagramChannelFactory);
142 } // namespace protocol
143 } // namespace remoting
145 #endif // REMOTING_PROTOCOL_FAKE_DATAGRAM_SOCKET_H_