M2-PT-DRP/source/events/search/SearchEvent.cpp

54 lines
1.3 KiB
C++

#include "SearchEvent.hpp"
#include <cerrno>
#include <sys/socket.h>
#include <cstring>
#include <iostream>
#include <ostream>
#include "../../tasks/types.hpp"
#include "../../events/types.hpp"
namespace drp {
void event::SearchEvent::handle(
Context& context,
const packet::GenericPacketContent& content,
sockaddr* fromAddress,
const socklen_t fromAddressLength
) {
packet::GenericPacket packet {};
packet::GenericPacketContent packetContent {};
// create the packet header (available to read for everyone)
packet.channel = 0;
packet.securityMode = static_cast<std::uint8_t>(packet::SecurityMode::PLAIN);
// insert our information into the packet
packetContent.eventType = static_cast<std::uint8_t>(EventType::INFO);
std::memcpy(&packetContent.data, &context.me, sizeof(context.me));
packet.setContent(packetContent);
// TODO(Faraphel): send back the timestamp too
// broadcast our information
if (sendto(
context.socket,
&packet,
sizeof(packet),
0,
fromAddress,
fromAddressLength
) == -1) {
std::cerr << "[Receiver] Could not send information: " << strerror(errno) << std::endl;
return;
}
std::cout << "[Receiver] Sent information." << std::endl;
}
}