OLD | NEW |
---|---|
(Empty) | |
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 | |
5 #ifndef REMOTING_PROTOCOL_FAKE_DATAGRAM_SOCKET_H_ | |
6 #define REMOTING_PROTOCOL_FAKE_DATAGRAM_SOCKET_H_ | |
7 | |
8 #include <map> | |
9 #include <string> | |
10 #include <vector> | |
11 | |
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" | |
17 | |
18 namespace base { | |
19 class SingleThreadTaskRunner; | |
20 } | |
21 | |
22 namespace remoting { | |
23 namespace protocol { | |
24 | |
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. | |
30 // | |
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 { | |
36 public: | |
37 FakeDatagramSocket(); | |
38 virtual ~FakeDatagramSocket(); | |
39 | |
40 const std::vector<std::string>& written_packets() const { | |
41 return written_packets_; | |
42 } | |
43 | |
44 void AppendInputPacket(const std::string& data); | |
Wez
2014/09/20 00:25:09
Consider replacing the term "input" with "to read"
Wez
2014/09/20 00:25:10
Would it be simpler to always create these things
Sergey Ulanov
2014/09/22 19:22:11
We had AppendInputPacket() before this CL. I think
Sergey Ulanov
2014/09/22 19:22:11
No. For some tests it's easier to have not connect
| |
45 int input_pos() const { return input_pos_; } | |
Wez
2014/09/20 00:25:09
What does |input_pos| mean? Is it a position in pa
Sergey Ulanov
2014/09/22 19:22:11
Added comment
| |
46 void PairWith(FakeDatagramSocket* peer_socket); | |
Wez
2014/09/20 00:25:09
Clarify that caller retains ownership of |peer_soc
Sergey Ulanov
2014/09/22 19:22:11
it's not scoped_ptr<> so it should be clear that o
Wez
2014/09/23 17:07:38
Except that there is still old code in the codebas
Sergey Ulanov
2014/09/23 19:06:08
I believe all code in src/remoting should be using
| |
47 | |
48 // net::Socket implementation. | |
49 virtual int Read(net::IOBuffer* buf, int buf_len, | |
50 const net::CompletionCallback& callback) OVERRIDE; | |
51 virtual int Write(net::IOBuffer* buf, int buf_len, | |
52 const net::CompletionCallback& callback) OVERRIDE; | |
53 | |
Wez
2014/09/20 00:25:09
nit: Remove blank line here
Sergey Ulanov
2014/09/22 19:22:11
Done.
| |
54 virtual int SetReceiveBufferSize(int32 size) OVERRIDE; | |
55 virtual int SetSendBufferSize(int32 size) OVERRIDE; | |
56 | |
57 private: | |
58 base::WeakPtr<FakeDatagramSocket> peer_socket_; | |
59 | |
60 bool read_pending_; | |
Wez
2014/09/20 00:25:10
nit: has_pending_read_ or has_read_pending_ or is_
Sergey Ulanov
2014/09/22 19:22:11
We use read_pending_ in other places as well, and
| |
61 scoped_refptr<net::IOBuffer> read_buffer_; | |
62 int read_buffer_size_; | |
63 net::CompletionCallback read_callback_; | |
64 | |
65 std::vector<std::string> written_packets_; | |
66 std::vector<std::string> input_packets_; | |
67 int input_pos_; | |
68 | |
69 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | |
70 base::WeakPtrFactory<FakeDatagramSocket> weak_factory_; | |
71 | |
72 DISALLOW_COPY_AND_ASSIGN(FakeDatagramSocket); | |
73 }; | |
74 | |
75 class FakeDatagramChannelFactory : public DatagramChannelFactory { | |
76 public: | |
77 FakeDatagramChannelFactory(); | |
78 virtual ~FakeDatagramChannelFactory(); | |
79 | |
80 void set_async_creation(bool async_creation) { | |
Wez
2014/09/20 00:25:10
nit: set_asynchronous, or set_is_asynchronous or s
Sergey Ulanov
2014/09/22 19:22:11
Done.
| |
81 async_creation_ = async_creation; | |
82 } | |
83 | |
84 void set_fail(bool fail) { fail_ = fail; } | |
Wez
2014/09/20 00:25:09
nit: set_fail_create_channel or set_fail_create?
Sergey Ulanov
2014/09/22 19:22:11
Done.
| |
85 | |
86 void PairWith(FakeDatagramChannelFactory* peer_factory); | |
Wez
2014/09/20 00:25:10
What does it mean to pair two factories?
Sergey Ulanov
2014/09/22 19:22:11
It means that the sockets they create are paired a
Wez
2014/09/23 17:07:38
Suggest tweaking comment to make clear that the cr
Sergey Ulanov
2014/09/23 19:06:08
Done.
| |
87 | |
88 FakeDatagramSocket* GetChannel(const std::string& name); | |
Wez
2014/09/20 00:25:09
What are the ownership semantics of GetChannel - l
Sergey Ulanov
2014/09/22 19:22:11
Added comment
| |
89 | |
90 // DatagramChannelFactory interface. | |
91 virtual void CreateChannel(const std::string& name, | |
92 const ChannelCreatedCallback& callback) OVERRIDE; | |
93 virtual void CancelChannelCreation(const std::string& name) OVERRIDE; | |
94 | |
95 private: | |
96 typedef std::map<std::string, FakeDatagramSocket*> ChannelsMap; | |
97 | |
98 void NotifyChannelCreated(const std::string& name, | |
99 const ChannelCreatedCallback& callback); | |
100 | |
101 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | |
102 bool async_creation_; | |
103 ChannelsMap channels_; | |
104 | |
105 base::WeakPtr<FakeDatagramChannelFactory> peer_factory_; | |
Wez
2014/09/20 00:25:10
nit: In the FakeDatagramSocket the peer weakptrfac
Sergey Ulanov
2014/09/22 19:22:11
Done.
| |
106 | |
107 bool fail_; | |
108 | |
109 base::WeakPtrFactory<FakeDatagramChannelFactory> weak_factory_; | |
110 | |
111 DISALLOW_COPY_AND_ASSIGN(FakeDatagramChannelFactory); | |
112 }; | |
113 | |
114 } // namespace protocol | |
115 } // namespace remoting | |
116 | |
117 #endif // REMOTING_PROTOCOL_FAKE_DATAGRAM_SOCKET_H_ | |
OLD | NEW |