-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathudp.cpp
More file actions
60 lines (45 loc) · 1.53 KB
/
udp.cpp
File metadata and controls
60 lines (45 loc) · 1.53 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
// This file is part of the Open Audio Live System project, a live audio environment
// Copyright (c) 2026 - Mathis DELGADO
//
// This project is distributed under the Creative Commons CC-BY-NC-SA licence. https://creativecommons.org/licenses/by-nc-sa/4.0
#include "udp.h"
#ifdef __linux__
UDPSocket::UDPSocket() {
}
UDPSocket::~UDPSocket() {
close(m_socket);
}
bool UDPSocket::init_socket(const uint32_t bound_ip, const uint16_t port) {
m_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if(m_socket < 0) {
return false;
}
enable_broadcasting();
m_self.sin_family = AF_INET;
m_self.sin_addr.s_addr = htonl(bound_ip);
m_self.sin_port = htons(port);
int bind_res = bind(m_socket, (sockaddr*)&m_self, sizeof(m_self));
if(bind_res < 0) {
std::cout << "Error no: " << errno << std::endl;
return false;
}
return true;
}
uint32_t UDPSocket::get_ip() const {
return m_self.sin_addr.s_addr;
}
uint16_t UDPSocket::get_port() const {
return m_self.sin_port;
}
void UDPSocket::enable_broadcasting() const {
int br_en = 1;
setsockopt(m_socket, SOL_SOCKET, SO_BROADCAST, &br_en, sizeof(int));
setsockopt(m_socket, SOL_SOCKET, SO_REUSEPORT, &br_en, sizeof(int));
}
void UDPSocket::set_high_prio() const {
int prio = 6;
setsockopt(m_socket, SOL_SOCKET, SO_PRIORITY, &prio, sizeof(int));
int enable = SOF_TIMESTAMPING_TX_HARDWARE | SOF_TIMESTAMPING_TX_SOFTWARE;
setsockopt(m_socket, SOL_SOCKET, SO_TIMESTAMPING, &enable, sizeof(enable));
}
#endif // __linux__