File: fake_udp_socket.cc

package info (click to toggle)
android-platform-tools 34.0.5-12
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 150,900 kB
  • sloc: cpp: 805,786; java: 293,500; ansic: 128,288; xml: 127,491; python: 41,481; sh: 14,245; javascript: 9,665; cs: 3,846; asm: 2,049; makefile: 1,917; yacc: 440; awk: 368; ruby: 183; sql: 140; perl: 88; lex: 67
file content (81 lines) | stat: -rw-r--r-- 2,388 bytes parent folder | download | duplicates (5)
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// 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.

#include "platform/test/fake_udp_socket.h"

#include <utility>

namespace openscreen {

FakeUdpSocket::FakeUdpSocket(TaskRunner* task_runner,
                             Client* client,
                             Version version)
    : client_(client), version_(version) {}

FakeUdpSocket::~FakeUdpSocket() = default;

bool FakeUdpSocket::IsIPv4() const {
  return version_ == UdpSocket::Version::kV4;
}

bool FakeUdpSocket::IsIPv6() const {
  return version_ == UdpSocket::Version::kV6;
}

IPEndpoint FakeUdpSocket::GetLocalEndpoint() const {
  return IPEndpoint{};
}

void FakeUdpSocket::Bind() {
  OSP_CHECK(bind_errors_.size()) << "No Bind responses queued.";
  ProcessConfigurationMethod(&bind_errors_);
}

void FakeUdpSocket::SetMulticastOutboundInterface(
    NetworkInterfaceIndex interface) {
  OSP_CHECK(set_multicast_outbound_interface_errors_.size())
      << "No SetMulticastOutboundInterface responses queued.";
  ProcessConfigurationMethod(&set_multicast_outbound_interface_errors_);
}

void FakeUdpSocket::JoinMulticastGroup(const IPAddress& address,
                                       NetworkInterfaceIndex interface) {
  OSP_CHECK(join_multicast_group_errors_.size())
      << "No JoinMulticastGroup responses queued.";
  ProcessConfigurationMethod(&join_multicast_group_errors_);
}

void FakeUdpSocket::SetDscp(DscpMode mode) {
  OSP_CHECK(set_dscp_errors_.size()) << "No SetDscp responses queued.";
  ProcessConfigurationMethod(&set_dscp_errors_);
}

void FakeUdpSocket::MockReceivePacket(UdpPacket packet) {
  if (client_) {
    client_->OnRead(this, std::move(packet));
  }
}

void FakeUdpSocket::ProcessConfigurationMethod(std::queue<Error>* errors) {
  Error error = errors->front();
  errors->pop();

  if (!error.ok() && client_) {
    client_->OnError(this, std::move(error));
  }
}

void FakeUdpSocket::SendMessage(const void* data,
                                size_t length,
                                const IPEndpoint& dest) {
  OSP_CHECK(send_errors_.size()) << "No SendMessage responses queued.";
  Error error = send_errors_.front();
  send_errors_.pop();

  if (!error.ok() && client_) {
    client_->OnSendError(this, std::move(error));
  }
}

}  // namespace openscreen