-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReceiver.cpp
57 lines (49 loc) · 1.44 KB
/
Receiver.cpp
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
#include "Util.hpp"
#include <sstream>
#include <iostream>
#include <string>
#define ASIO_STANDALONE
#include "asio.hpp"
#include <vector>
#include "Receiver.hpp"
#include "Compress.hpp"
#include <memory>
void Receiver::read(std::stringstream& ss, int reply_size) {
std::vector<char> reply(reply_size);
udp_endpoint sender_endpoint;
int reply_length = socket->receive_from(
asio::buffer(reply.data(), reply_size), sender_endpoint);
auto addr = sender_endpoint.address();
auto port = sender_endpoint.port();
//std::cout << "Received " << reply_length << " on " << addr << ":" << port << "\n";
//std::cout << "Versus reply_size " << reply_size << "\n";
ss.write(reply.data(), reply_length); // reply data to stream
}
Receiver::Receiver(io_service& io, const std::shared_ptr<udp_socket>& socket) :
io(io),
port(port),
socket(socket)
//socket(io, udp_endpoint(asio::ip::udp::v4(), port))
{
}
bool Receiver::available() {
return socket->available() > 0;
}
/*
int main(int argc, char* argv[]) {
io_service io;
Receiver receiver(io,2000);
while (1) {
if (receiver.available()) {
Forces fs;
fs = receiver.receive<Forces>();
std::cout << "Server received:\n";
for (const auto& f: fs) {
std::cout << "Server reads " << f.id << "\n";
}
std::cout << "Server receive end\n";
}
}
return 0;
}
*/