-
Notifications
You must be signed in to change notification settings - Fork 0
/
UDPSocket.cc
108 lines (89 loc) · 2.86 KB
/
UDPSocket.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
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: UDPSocket.cc
* Author: fsilva
*
* Created on December 17, 2015, 3:08 PM
*/
#include "UDPSocket.h"
/* ::: IPv4 :::
struct sockaddr_in {
short sin_family;
unsigned short sin_port;
struct in_addr sin_addr;
char sin_zero[8];
};
struct in_addr {
unsigned long s_addr;
};
*/
UDPSocket::UDPSocket() {}
UDPSocket::UDPSocket(const UDPSocket& orig) {
}
UDPSocket::~UDPSocket() {}
bool UDPSocket::createSocket() {
sock_desc = socket(AF_INET, SOCK_DGRAM, 0);
if (sock_desc < 0)
return false;
return true;
}
bool UDPSocket::bindSocket(std::string ip_addr="", int p=0)
{
memset((char *) &l_addr, 0, sizeof (l_addr));
l_addr.sin_family = AF_INET;
l_addr.sin_addr.s_addr = !ip_addr.empty() ? inet_addr(ip_addr.c_str()) : htonl(INADDR_ANY);
l_addr.sin_port = htons(p);
if (bind(sock_desc, (struct sockaddr *) &l_addr, sizeof (l_addr)) < 0)
return false;
return true;
}
Message* UDPSocket::listenMessage()
{
socklen_t r_sock_len = sizeof(r_addr);
int recvlen = recvfrom(sock_desc, buffer, MAX_BUFSIZE, 0, (struct sockaddr *) &r_addr, &r_sock_len);
if (recvlen > 0) {
buffer[recvlen] = '\0';
//std::cout << "UDP_RAW_MSG_RECV: " << buffer << std::endl;
/* Unmarshalling msg */
PB_Eotq::MsgResources msg;
google::protobuf::io::ArrayInputStream ais(buffer, sizeof (buffer));
google::protobuf::io::CodedInputStream cis(&ais);
uint32_t msgSize = 0;
cis.ReadVarint32(&msgSize);
cis.PushLimit(msgSize);
msg.ParseFromCodedStream(&cis);
//std::cout << "RAW_MSG: " << std::endl << msgAnnounce.DebugString() << std::endl;
return new Message(msg, r_addr);
}
return NULL;
}
bool UDPSocket::sendMessage(Message &msg, struct sockaddr_in r_addr, int r_port)
{
socklen_t r_sock_len = sizeof(r_addr);
r_addr.sin_port = htons(r_port);
google::protobuf::io::ArrayOutputStream arr(buffer, sizeof(buffer));
google::protobuf::io::CodedOutputStream output(&arr);
output.WriteVarint32(msg.getByteSize());
msg.setCodedStream(&output);
if ( sendto(sock_desc, (char*)buffer, output.ByteCount(), 0, (struct sockaddr *) &r_addr, r_sock_len) == -1 ) {
std::cout << "[ERROR]: sending to" << std::endl;
return false;
}
return true;
}
void
UDPSocket::closeSocket()
{
close(sock_desc);
}
struct sockaddr_in UDPSocket::convertIP(std::string &ip_addr)
{
memset((char *) &l_addr, 0, sizeof (l_addr));
l_addr.sin_family = AF_INET;
l_addr.sin_addr.s_addr = !ip_addr.empty() ? inet_addr(ip_addr.c_str()) : htonl(INADDR_ANY);
return l_addr;
}