blob: 15db875890488544ca76365264c553a22e4b05a2 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef DISCOVERY_MDNS_MDNS_RECEIVER_H_
#define DISCOVERY_MDNS_MDNS_RECEIVER_H_
#include "platform/api/udp_socket.h"
#include "platform/base/error.h"
#include "platform/base/udp_packet.h"
namespace openscreen {
namespace discovery {
class MdnsMessage;
class MdnsReceiver : UdpSocket::Client {
public:
// MdnsReceiver does not own |socket| and |delegate|
// and expects that the lifetime of these objects exceeds the lifetime of
// MdnsReceiver.
explicit MdnsReceiver(UdpSocket* socket);
MdnsReceiver(const MdnsReceiver& other) = delete;
MdnsReceiver(MdnsReceiver&& other) noexcept = delete;
MdnsReceiver& operator=(const MdnsReceiver& other) = delete;
MdnsReceiver& operator=(MdnsReceiver&& other) noexcept = delete;
~MdnsReceiver() override;
void SetQueryCallback(
std::function<void(const MdnsMessage&, const IPEndpoint& src)> callback);
void SetResponseCallback(std::function<void(const MdnsMessage&)> callback);
// The receiver can be started and stopped multiple times.
// Start and Stop are both synchronous calls. When MdnsReceiver has not yet
// been started, OnRead callbacks it receives from the task runner will be
// no-ops.
void Start();
void Stop();
// UdpSocket::Client overrides.
void OnRead(UdpSocket* socket, ErrorOr<UdpPacket> packet) override;
void OnError(UdpSocket* socket, Error error) override;
void OnSendError(UdpSocket* socket, Error error) override;
private:
enum class State {
kStopped,
kRunning,
};
UdpSocket* const socket_;
std::function<void(const MdnsMessage&, const IPEndpoint& src)>
query_callback_;
std::function<void(const MdnsMessage&)> response_callback_;
State state_ = State::kStopped;
};
} // namespace discovery
} // namespace openscreen
#endif // DISCOVERY_MDNS_MDNS_RECEIVER_H_
|