]> git.sesse.net Git - cubemap/blob - udpstream.cpp
Remove mark= from cubemap.config.sample, as the option no longer exists.
[cubemap] / udpstream.cpp
1 #include <sys/socket.h>
2 #include <sys/types.h>
3
4 #include "log.h"
5 #include "udpstream.h"
6 #include "util.h"
7
8 #ifndef SO_MAX_PACING_RATE
9 #define SO_MAX_PACING_RATE 47
10 #endif
11
12 UDPStream::UDPStream(const sockaddr_in6 &dst, uint32_t pacing_rate)
13         : dst(dst),
14           pacing_rate(pacing_rate)
15 {
16         sock = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
17         if (sock == -1) {
18                 // Oops. Ignore this output, then.
19                 log_perror("socket");
20                 return;
21         }
22
23         if (setsockopt(sock, SOL_SOCKET, SO_MAX_PACING_RATE, &pacing_rate, sizeof(pacing_rate)) == -1) {
24                 if (pacing_rate != ~0U) {
25                         log_perror("setsockopt(SO_MAX_PACING_RATE)");
26                 }
27         }
28 }
29
30 UDPStream::~UDPStream()
31 {
32         if (sock != -1) {
33                 safe_close(sock);
34         }
35 }
36
37 void UDPStream::send(const char *data, size_t bytes)
38 {
39         if (sock == -1) {
40                 return;
41         }
42         ssize_t err = sendto(sock, data, bytes, 0, reinterpret_cast<sockaddr *>(&dst), sizeof(dst));
43         if (err == -1) {
44                 log_perror("sendto");
45         }
46 }