-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUDPClient.cpp
More file actions
61 lines (52 loc) · 1.51 KB
/
UDPClient.cpp
File metadata and controls
61 lines (52 loc) · 1.51 KB
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
61
#include "UDPClient.h"
#include "DataPack.h"
// Maybe error detection mechanism future
// on both boost code and our function
using namespace std;
using boost::asio::ip::udp;
// Constructor
UDPClient::UDPClient(boost::asio::io_service& io_service, const string dest_host, const int dest_port, const int loc_port)
: io_serv(io_service), sock(io_serv) {
size = sizeof(DataPack);
tar_host = dest_host;
tar_port = to_string(dest_port);
local_port = loc_port;
}
// Destructor, when sock is open, close it
UDPClient::~UDPClient() {
if(sock.is_open()) {
sock.close();
}
}
// Init all needed staff for client side
void UDPClient::Init() {
// Find both local & remote endpoints
dest = *FindRemote();
sender = FindLocal();
// Init socket
InitSocket();
}
// Send data structures to remote server
size_t UDPClient::Send(DataPack* send) {
return sock.send_to(boost::asio::buffer(send, size), dest);
}
// Receive message from remote server
size_t UDPClient::Receive(DataPack* recv) {
return sock.receive_from(boost::asio::buffer(recv, size), sender);
}
// Private helper methods
// Find remote endpoint based on host and port
udp::resolver::iterator UDPClient::FindRemote() {
udp::resolver resolver(io_serv);
udp::resolver::query query(udp::v4(), tar_host, tar_port);
return resolver.resolve(query);
}
// Find local endpoint
udp::endpoint UDPClient::FindLocal() {
return udp::endpoint(udp::v4(), local_port);
}
// Open IPv4 socket
void UDPClient::InitSocket() {
sock.open(udp::v4());
sock.bind(sender);
}