1 // Copyright 2019 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 #include "discovery/mdns/mdns_sender.h" 6 7 #include <algorithm> 8 #include <iostream> 9 #include <vector> 10 11 #include "discovery/mdns/mdns_writer.h" 12 #include "platform/api/udp_socket.h" 13 14 namespace openscreen { 15 namespace discovery { 16 MdnsSender(UdpSocket * socket)17MdnsSender::MdnsSender(UdpSocket* socket) : socket_(socket) { 18 OSP_DCHECK(socket_ != nullptr); 19 } 20 21 MdnsSender::~MdnsSender() = default; 22 SendMulticast(const MdnsMessage & message)23Error MdnsSender::SendMulticast(const MdnsMessage& message) { 24 const IPEndpoint& endpoint = socket_->IsIPv6() ? kMulticastSendIPv6Endpoint 25 : kMulticastSendIPv4Endpoint; 26 return SendMessage(message, endpoint); 27 } 28 SendMessage(const MdnsMessage & message,const IPEndpoint & endpoint)29Error MdnsSender::SendMessage(const MdnsMessage& message, 30 const IPEndpoint& endpoint) { 31 // Always try to write the message into the buffer even if MaxWireSize is 32 // greater than maximum message size. Domain name compression might reduce the 33 // on-the-wire size of the message sufficiently for it to fit into the buffer. 34 std::vector<uint8_t> buffer( 35 std::min(message.MaxWireSize(), kMaxMulticastMessageSize)); 36 MdnsWriter writer(buffer.data(), buffer.size()); 37 if (!writer.Write(message)) { 38 return Error::Code::kInsufficientBuffer; 39 } 40 41 socket_->SendMessage(buffer.data(), writer.offset(), endpoint); 42 return Error::Code::kNone; 43 } 44 OnSendError(UdpSocket * socket,Error error)45void MdnsSender::OnSendError(UdpSocket* socket, Error error) { 46 OSP_LOG_ERROR << "Error sending packet"; 47 } 48 49 } // namespace discovery 50 } // namespace openscreen 51