forked from RoboCup-SSL/ssl-refbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
protobufpublisher.cc
28 lines (23 loc) · 977 Bytes
/
protobufpublisher.cc
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
#include "protobufpublisher.h"
#include "configuration.h"
#include "referee.pb.h"
#include "savestate.pb.h"
#include <chrono>
#include <ctime>
#include <string>
#include <google/protobuf/io/zero_copy_stream_impl_lite.h>
ProtobufPublisher::ProtobufPublisher(const Configuration &configuration, Logger &logger) : bcast(logger, configuration.address, configuration.protobuf_port, configuration.interface) {
}
void ProtobufPublisher::publish(SaveState &state) {
// Shove in the packet timestamp.
std::chrono::microseconds diff = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now() - std::chrono::system_clock::from_time_t(0));
state.mutable_referee()->set_packet_timestamp(static_cast<uint64_t>(diff.count()));
// Serialize the packet.
std::string packet;
{
google::protobuf::io::StringOutputStream sos(&packet);
state.referee().SerializeToZeroCopyStream(&sos);
}
// Send the packet.
bcast.send(packet.data(), packet.size());
}