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_
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/weak_ptr.h"
14 #include "net/base/completion_callback.h"
15 #include "net/socket/socket.h"
16 #include "remoting/protocol/datagram_channel_factory.h"
19 class SingleThreadTaskRunner
;
25 // FakeDatagramSocket implement net::StreamSocket 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 net::Socket
{
38 ~FakeDatagramSocket() override
;
40 const std::vector
<std::string
>& written_packets() const {
41 return written_packets_
;
44 void AppendInputPacket(const std::string
& data
);
46 // Current position in the input in number of packets, i.e. number of finished
48 int input_pos() const { return input_pos_
; }
50 // Pairs the socket with |peer_socket|. Deleting either of the paired sockets
52 void PairWith(FakeDatagramSocket
* peer_socket
);
54 base::WeakPtr
<FakeDatagramSocket
> GetWeakPtr();
56 // net::Socket implementation.
57 int Read(net::IOBuffer
* buf
,
59 const net::CompletionCallback
& callback
) override
;
60 int Write(net::IOBuffer
* buf
,
62 const net::CompletionCallback
& callback
) override
;
63 int SetReceiveBufferSize(int32 size
) override
;
64 int SetSendBufferSize(int32 size
) override
;
67 int CopyReadData(net::IOBuffer
* buf
, int buf_len
);
69 base::WeakPtr
<FakeDatagramSocket
> peer_socket_
;
71 scoped_refptr
<net::IOBuffer
> read_buffer_
;
72 int read_buffer_size_
;
73 net::CompletionCallback read_callback_
;
75 std::vector
<std::string
> written_packets_
;
76 std::vector
<std::string
> input_packets_
;
79 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner_
;
80 base::WeakPtrFactory
<FakeDatagramSocket
> weak_factory_
;
82 DISALLOW_COPY_AND_ASSIGN(FakeDatagramSocket
);
85 class FakeDatagramChannelFactory
: public DatagramChannelFactory
{
87 FakeDatagramChannelFactory();
88 ~FakeDatagramChannelFactory() override
;
90 void set_asynchronous_create(bool asynchronous_create
) {
91 asynchronous_create_
= asynchronous_create
;
94 void set_fail_create(bool fail_create
) { fail_create_
= fail_create
; }
96 // Pair with |peer_factory|. Once paired the factory will be automatically
97 // pairing created sockets with the sockets with the same name from the peer
99 void PairWith(FakeDatagramChannelFactory
* peer_factory
);
101 // Can be used to retrieve FakeDatagramSocket created by this factory, e.g. to
102 // feed data into it. The caller doesn't get ownership of the result. Returns
103 // nullptr if the socket doesn't exist.
104 FakeDatagramSocket
* GetFakeChannel(const std::string
& name
);
106 // DatagramChannelFactory interface.
107 void CreateChannel(const std::string
& name
,
108 const ChannelCreatedCallback
& callback
) override
;
109 void CancelChannelCreation(const std::string
& name
) override
;
112 typedef std::map
<std::string
, base::WeakPtr
<FakeDatagramSocket
> > ChannelsMap
;
114 void NotifyChannelCreated(scoped_ptr
<FakeDatagramSocket
> owned_socket
,
115 const std::string
& name
,
116 const ChannelCreatedCallback
& callback
);
118 base::WeakPtr
<FakeDatagramChannelFactory
> peer_factory_
;
120 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner_
;
121 bool asynchronous_create_
;
122 ChannelsMap channels_
;
126 base::WeakPtrFactory
<FakeDatagramChannelFactory
> weak_factory_
;
128 DISALLOW_COPY_AND_ASSIGN(FakeDatagramChannelFactory
);
131 } // namespace protocol
132 } // namespace remoting
134 #endif // REMOTING_PROTOCOL_FAKE_DATAGRAM_SOCKET_H_