OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef REMOTING_PROTOCOL_FAKE_DATAGRAM_SOCKET_H_ | 5 #ifndef REMOTING_PROTOCOL_FAKE_DATAGRAM_SOCKET_H_ |
6 #define REMOTING_PROTOCOL_FAKE_DATAGRAM_SOCKET_H_ | 6 #define REMOTING_PROTOCOL_FAKE_DATAGRAM_SOCKET_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
13 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
14 #include "net/base/completion_callback.h" | 14 #include "net/base/completion_callback.h" |
15 #include "net/socket/socket.h" | |
16 #include "remoting/protocol/datagram_channel_factory.h" | 15 #include "remoting/protocol/datagram_channel_factory.h" |
| 16 #include "remoting/protocol/p2p_socket.h" |
17 | 17 |
18 namespace base { | 18 namespace base { |
19 class SingleThreadTaskRunner; | 19 class SingleThreadTaskRunner; |
20 } | 20 } |
21 | 21 |
22 namespace remoting { | 22 namespace remoting { |
23 namespace protocol { | 23 namespace protocol { |
24 | 24 |
25 // FakeDatagramSocket implement net::StreamSocket interface. All data written to | 25 // FakeDatagramSocket implement P2PStreamSocket interface. All data written to |
26 // FakeDatagramSocket is stored in a buffer returned by written_packets(). | 26 // FakeDatagramSocket is stored in a buffer returned by written_packets(). |
27 // Read() reads data from another buffer that can be set with | 27 // Read() reads data from another buffer that can be set with |
28 // AppendInputPacket(). Pending reads are supported, so if there is a pending | 28 // AppendInputPacket(). Pending reads are supported, so if there is a pending |
29 // read AppendInputPacket() calls the read callback. | 29 // read AppendInputPacket() calls the read callback. |
30 // | 30 // |
31 // Two fake sockets can be connected to each other using the | 31 // Two fake sockets can be connected to each other using the |
32 // PairWith() method, e.g.: a->PairWith(b). After this all data | 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 | 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. | 34 // sockets |a| and |b| must be created and used on the same thread. |
35 class FakeDatagramSocket : public net::Socket { | 35 class FakeDatagramSocket : public P2PDatagramSocket { |
36 public: | 36 public: |
37 FakeDatagramSocket(); | 37 FakeDatagramSocket(); |
38 ~FakeDatagramSocket() override; | 38 ~FakeDatagramSocket() override; |
39 | 39 |
40 const std::vector<std::string>& written_packets() const { | 40 const std::vector<std::string>& written_packets() const { |
41 return written_packets_; | 41 return written_packets_; |
42 } | 42 } |
43 | 43 |
44 void AppendInputPacket(const std::string& data); | 44 void AppendInputPacket(const std::string& data); |
45 | 45 |
46 // Current position in the input in number of packets, i.e. number of finished | 46 // Current position in the input in number of packets, i.e. number of finished |
47 // Read() calls. | 47 // Read() calls. |
48 int input_pos() const { return input_pos_; } | 48 int input_pos() const { return input_pos_; } |
49 | 49 |
50 // Pairs the socket with |peer_socket|. Deleting either of the paired sockets | 50 // Pairs the socket with |peer_socket|. Deleting either of the paired sockets |
51 // unpairs them. | 51 // unpairs them. |
52 void PairWith(FakeDatagramSocket* peer_socket); | 52 void PairWith(FakeDatagramSocket* peer_socket); |
53 | 53 |
54 base::WeakPtr<FakeDatagramSocket> GetWeakPtr(); | 54 base::WeakPtr<FakeDatagramSocket> GetWeakPtr(); |
55 | 55 |
56 // net::Socket implementation. | 56 // P2PDatagramSocket implementation. |
57 int Read(net::IOBuffer* buf, | 57 int Read(net::IOBuffer* buf, |
58 int buf_len, | 58 int buf_len, |
59 const net::CompletionCallback& callback) override; | 59 const net::CompletionCallback& callback) override; |
60 int Write(net::IOBuffer* buf, | 60 int Write(net::IOBuffer* buf, |
61 int buf_len, | 61 int buf_len, |
62 const net::CompletionCallback& callback) override; | 62 const net::CompletionCallback& callback) override; |
63 int SetReceiveBufferSize(int32 size) override; | 63 int SetReceiveBufferSize(int32 size) override; |
64 int SetSendBufferSize(int32 size) override; | 64 int SetSendBufferSize(int32 size) override; |
65 | 65 |
66 private: | 66 private: |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
125 | 125 |
126 base::WeakPtrFactory<FakeDatagramChannelFactory> weak_factory_; | 126 base::WeakPtrFactory<FakeDatagramChannelFactory> weak_factory_; |
127 | 127 |
128 DISALLOW_COPY_AND_ASSIGN(FakeDatagramChannelFactory); | 128 DISALLOW_COPY_AND_ASSIGN(FakeDatagramChannelFactory); |
129 }; | 129 }; |
130 | 130 |
131 } // namespace protocol | 131 } // namespace protocol |
132 } // namespace remoting | 132 } // namespace remoting |
133 | 133 |
134 #endif // REMOTING_PROTOCOL_FAKE_DATAGRAM_SOCKET_H_ | 134 #endif // REMOTING_PROTOCOL_FAKE_DATAGRAM_SOCKET_H_ |
OLD | NEW |