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

42 lines
1.1 KiB
C++

#include "InfoEvent.hpp"
#include <cstring>
#include <sys/socket.h>
namespace drp::event {
void InfoEvent::handle(
Context& context,
const packet::GenericPacketContent& content,
sockaddr* fromAddress,
const socklen_t fromAddressLength
) {
// check if the peer address is already in the map
std::shared_ptr<RemotePeer> remotePeer;
auto iterator = context.remotePeers.find(*fromAddress);
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;
}
// save the remote peer information
std::memcpy(&remotePeer->information, &content, sizeof(Peer));
// TODO(Faraphel): interpret the timestamp and calculate average ping
// save it in the peers list
context.remotePeers[remotePeer->address] = remotePeer;
}
}