Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1252)

Side by Side Diff: remoting/protocol/fake_datagram_socket.h

Issue 580243003: Cleanup Fake* classes in remoting/protocol (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@sctp
Patch Set: Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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);
45
46 // Current position in the input in number of packets, i.e. number of finished
47 // Read() calls.
48 int input_pos() const { return input_pos_; }
49
50 // Pairs the socket with |peer_socket|. Deleting either of the paired sockets
51 // unpairs them.
52 void PairWith(FakeDatagramSocket* peer_socket);
53
54 base::WeakPtr<FakeDatagramSocket> GetWeakPtr();
55
56 // net::Socket implementation.
57 virtual int Read(net::IOBuffer* buf, int buf_len,
58 const net::CompletionCallback& callback) OVERRIDE;
59 virtual int Write(net::IOBuffer* buf, int buf_len,
60 const net::CompletionCallback& callback) OVERRIDE;
61 virtual int SetReceiveBufferSize(int32 size) OVERRIDE;
62 virtual int SetSendBufferSize(int32 size) OVERRIDE;
63
64 private:
65 int CopyReadData(net::IOBuffer* buf, int buf_len);
66
67 base::WeakPtr<FakeDatagramSocket> peer_socket_;
68
69 scoped_refptr<net::IOBuffer> read_buffer_;
70 int read_buffer_size_;
71 net::CompletionCallback read_callback_;
72
73 std::vector<std::string> written_packets_;
74 std::vector<std::string> input_packets_;
75 int input_pos_;
76
77 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
78 base::WeakPtrFactory<FakeDatagramSocket> weak_factory_;
79
80 DISALLOW_COPY_AND_ASSIGN(FakeDatagramSocket);
81 };
82
83 class FakeDatagramChannelFactory : public DatagramChannelFactory {
84 public:
85 FakeDatagramChannelFactory();
86 virtual ~FakeDatagramChannelFactory();
87
88 void set_asynchronous_create(bool asynchronous_create) {
89 asynchronous_create_ = asynchronous_create;
90 }
91
92 void set_fail_create(bool fail_create) { fail_create_ = fail_create; }
93
94 // Pair with |peer_factory|. Paired factories automatically pair sockets
95 // they create.
96 void PairWith(FakeDatagramChannelFactory* peer_factory);
97
98 // Can be used to retrieve FakeDatagramSocket created by this factory, e.g. to
99 // feed data into it. The caller doesn't get ownership of the result.
Wez 2014/09/23 17:07:38 nit: Also indicate that it's valid to call this fo
Sergey Ulanov 2014/09/23 19:06:09 Done.
100 FakeDatagramSocket* GetFakeChannel(const std::string& name);
101
102 // DatagramChannelFactory interface.
103 virtual void CreateChannel(const std::string& name,
104 const ChannelCreatedCallback& callback) OVERRIDE;
105 virtual void CancelChannelCreation(const std::string& name) OVERRIDE;
106
107 private:
108 typedef std::map<std::string, base::WeakPtr<FakeDatagramSocket> > ChannelsMap;
109
110 void NotifyChannelCreated(scoped_ptr<FakeDatagramSocket> owned_socket,
111 const std::string& name,
112 const ChannelCreatedCallback& callback);
113
114 base::WeakPtr<FakeDatagramChannelFactory> peer_factory_;
115
116 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
117 bool asynchronous_create_;
118 ChannelsMap channels_;
119
120 bool fail_create_;
121
122 base::WeakPtrFactory<FakeDatagramChannelFactory> weak_factory_;
123
124 DISALLOW_COPY_AND_ASSIGN(FakeDatagramChannelFactory);
125 };
126
127 } // namespace protocol
128 } // namespace remoting
129
130 #endif // REMOTING_PROTOCOL_FAKE_DATAGRAM_SOCKET_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698