OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 NET_DNS_MDNS_CLIENT_IMPL_H_ | 5 #ifndef NET_DNS_MDNS_CLIENT_IMPL_H_ |
6 #define NET_DNS_MDNS_CLIENT_IMPL_H_ | 6 #define NET_DNS_MDNS_CLIENT_IMPL_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <string> | 9 #include <string> |
10 #include <utility> | 10 #include <utility> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/cancelable_callback.h" | 13 #include "base/cancelable_callback.h" |
14 #include "base/memory/scoped_vector.h" | 14 #include "base/memory/scoped_vector.h" |
15 #include "base/observer_list.h" | 15 #include "base/observer_list.h" |
16 #include "net/base/io_buffer.h" | 16 #include "net/base/io_buffer.h" |
17 #include "net/base/ip_endpoint.h" | 17 #include "net/base/ip_endpoint.h" |
18 #include "net/dns/mdns_cache.h" | 18 #include "net/dns/mdns_cache.h" |
19 #include "net/dns/mdns_client.h" | 19 #include "net/dns/mdns_client.h" |
20 #include "net/udp/datagram_server_socket.h" | 20 #include "net/udp/datagram_server_socket.h" |
21 #include "net/udp/udp_server_socket.h" | 21 #include "net/udp/udp_server_socket.h" |
22 #include "net/udp/udp_socket.h" | 22 #include "net/udp/udp_socket.h" |
23 | 23 |
24 namespace net { | 24 namespace net { |
25 | 25 |
26 // A connection to the network for multicast DNS clients. It reads data into | 26 // A connection to the network for multicast DNS clients. It reads data into |
27 // DnsResponse objects and alerts the delegate that a packet has been received. | 27 // DnsResponse objects and alerts the delegate that a packet has been received. |
28 class NET_EXPORT_PRIVATE MDnsConnection { | 28 class NET_EXPORT_PRIVATE MDnsConnection { |
29 public: | 29 public: |
30 class Socket { | |
szym
2013/11/27 00:28:09
When compared to the more direct route of using Da
Vitaly Buka (NO REVIEWS)
2013/11/27 00:45:45
1. Cleaner tests because no need to mock unnecessa
szym
2013/11/27 02:25:28
True, but less _test_ code is not a reason to slow
| |
31 public: | |
32 virtual ~Socket() {}; | |
33 | |
34 virtual int RecvFrom(IOBuffer* buf, | |
35 int buf_len, | |
36 IPEndPoint* address, | |
37 const CompletionCallback& callback) = 0; | |
38 virtual int SendTo(IOBuffer* buf, | |
szym
2013/11/27 07:37:44
Does not take an address, so this should be |Send|
Vitaly Buka (NO REVIEWS)
2013/11/27 08:45:37
This one does not, that was a point, make implemen
| |
39 int buf_len, | |
40 const CompletionCallback& callback) = 0; | |
41 }; | |
42 | |
30 class SocketFactory { | 43 class SocketFactory { |
31 public: | 44 public: |
32 virtual ~SocketFactory() {} | 45 virtual ~SocketFactory() {} |
33 | 46 virtual void CreateSockets(ScopedVector<Socket>* sockets) = 0; |
34 virtual scoped_ptr<DatagramServerSocket> CreateSocket() = 0; | |
35 | 47 |
36 static scoped_ptr<SocketFactory> CreateDefault(); | 48 static scoped_ptr<SocketFactory> CreateDefault(); |
37 }; | 49 }; |
38 | 50 |
39 class Delegate { | 51 class Delegate { |
40 public: | 52 public: |
41 // Handle an mDNS packet buffered in |response| with a size of |bytes_read|. | 53 // Handle an mDNS packet buffered in |response| with a size of |bytes_read|. |
42 virtual void HandlePacket(DnsResponse* response, int bytes_read) = 0; | 54 virtual void HandlePacket(DnsResponse* response, int bytes_read) = 0; |
43 virtual void OnConnectionError(int error) = 0; | 55 virtual void OnConnectionError(int error) = 0; |
44 virtual ~Delegate() {} | 56 virtual ~Delegate() {} |
45 }; | 57 }; |
46 | 58 |
47 explicit MDnsConnection(MDnsConnection::Delegate* delegate); | 59 explicit MDnsConnection(MDnsConnection::Delegate* delegate); |
48 | |
49 virtual ~MDnsConnection(); | 60 virtual ~MDnsConnection(); |
50 | 61 |
51 // Both methods return true if at least one of the socket handlers succeeded. | 62 // Both methods return true if at least one of the socket handlers succeeded. |
52 bool Init(MDnsConnection::SocketFactory* socket_factory); | 63 bool Init(MDnsConnection::SocketFactory* socket_factory); |
53 bool Send(IOBuffer* buffer, unsigned size); | 64 bool Send(IOBuffer* buffer, unsigned size); |
54 | 65 |
55 private: | 66 private: |
56 class SocketHandler { | 67 class SocketHandler { |
57 public: | 68 public: |
58 SocketHandler(MDnsConnection* connection, | 69 SocketHandler(scoped_ptr<Socket> socket, MDnsConnection* connection); |
59 const IPEndPoint& multicast_addr, | |
60 SocketFactory* socket_factory); | |
61 ~SocketHandler(); | 70 ~SocketHandler(); |
62 int Bind(); | 71 |
63 int Start(); | 72 int Start(); |
64 | |
65 int Send(IOBuffer* buffer, unsigned size); | 73 int Send(IOBuffer* buffer, unsigned size); |
66 | 74 |
67 private: | 75 private: |
68 int DoLoop(int rv); | 76 int DoLoop(int rv); |
69 void OnDatagramReceived(int rv); | 77 void OnDatagramReceived(int rv); |
70 | 78 |
71 // Callback for when sending a query has finished. | 79 // Callback for when sending a query has finished. |
72 void SendDone(int rv); | 80 void SendDone(int rv); |
73 | 81 |
74 scoped_ptr<DatagramServerSocket> socket_; | 82 scoped_ptr<Socket> socket_; |
75 | |
76 MDnsConnection* connection_; | 83 MDnsConnection* connection_; |
77 IPEndPoint recv_addr_; | 84 IPEndPoint recv_addr_; |
78 scoped_ptr<DnsResponse> response_; | 85 DnsResponse response_; |
79 IPEndPoint multicast_addr_; | 86 |
87 DISALLOW_COPY_AND_ASSIGN(SocketHandler); | |
80 }; | 88 }; |
81 | 89 |
82 // Callback for handling a datagram being received on either ipv4 or ipv6. | 90 // Callback for handling a datagram being received on either ipv4 or ipv6. |
83 void OnDatagramReceived(DnsResponse* response, | 91 void OnDatagramReceived(DnsResponse* response, |
84 const IPEndPoint& recv_addr, | 92 const IPEndPoint& recv_addr, |
85 int bytes_read); | 93 int bytes_read); |
86 | 94 |
87 void OnError(SocketHandler* loop, int error); | 95 void OnError(SocketHandler* loop, int error); |
88 | 96 |
89 // Only socket handlers which successfully bound and started are kept. | 97 // Only socket handlers which successfully bound and started are kept. |
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
287 MDnsClientImpl* client_; | 295 MDnsClientImpl* client_; |
288 | 296 |
289 bool started_; | 297 bool started_; |
290 int flags_; | 298 int flags_; |
291 | 299 |
292 DISALLOW_COPY_AND_ASSIGN(MDnsTransactionImpl); | 300 DISALLOW_COPY_AND_ASSIGN(MDnsTransactionImpl); |
293 }; | 301 }; |
294 | 302 |
295 } // namespace net | 303 } // namespace net |
296 #endif // NET_DNS_MDNS_CLIENT_IMPL_H_ | 304 #endif // NET_DNS_MDNS_CLIENT_IMPL_H_ |
OLD | NEW |