M2-PT-DRP/source/events/info/InfoEvent.cpp

44 lines
1.2 KiB
C++

#include "InfoEvent.hpp"
#include <cstring>
#include <sys/socket.h>
namespace drp::event {
void InfoEvent::handle(
Context& context,
const packet::GenericPacketContent& content,
const sockaddr_storage& fromAddress,
const socklen_t fromAddressLength
) {
// get the peer information
Peer peer;
std::memcpy(&peer, content.data.data(), sizeof(Peer));
// check if the peer address is already in the map
const auto iterator = context.remotePeers.find(peer.id);
std::shared_ptr<RemotePeer> remotePeer;
if (iterator == context.remotePeers.end()) {
// if not found, create a new peer
remotePeer = std::make_shared<RemotePeer>();
remotePeer->address = fromAddress;
remotePeer->addressLength = fromAddressLength;
// update the latest discovery time
context.latestPeerDiscovery = std::chrono::high_resolution_clock::now();
} else {
// get the peer
remotePeer = iterator->second;
}
// TODO(Faraphel): interpret the timestamp and calculate average ping
// save it in the peers list
remotePeer->information = peer;
context.remotePeers[peer.id] = remotePeer;
}
}