-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReceiver.hpp
60 lines (45 loc) · 1.52 KB
/
Receiver.hpp
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#ifndef RECEIVER_HPP
#define RECEIVER_HPP
#include "Util.hpp"
#include <sstream>
#include <iostream>
#include <string>
#include <memory>
#include "Compress.hpp"
#include "Archiver.hpp"
class Receiver {
private:
io_service& io;
unsigned short port;
std::shared_ptr<udp_socket> socket;
void read(std::stringstream& ss, int reply_size);
public:
Receiver(io_service& io, const std::shared_ptr<udp_socket>& socket);
bool available();
template <typename Serializable_Items>
Serializable_Items static deserialize(std::stringstream& ss) {
const std::string decomp(decompress_string(ss.str()));
std::stringstream sss;
sss.str(decomp);
Serializable_Items items;
{
cereal::PortableBinaryInputArchive iarchive(sss); // Create an input archive
iarchive(items); // Read the data from the archive
} // flush
return items;
}
template <typename Serializable_Items>
Serializable_Items receive() {
Serializable_Items items;
int reply_size = socket->available();
if (reply_size > 0) {
std::stringstream ss;
read(ss,reply_size);
items = deserialize<Serializable_Items>(ss);
} else {
;
}
return items;
}
};
#endif