]> git.sesse.net Git - cubemap/blob - udpstream.cpp
1e79494a3d8d302c5195ea17efeab83954eb37ea
[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 {
15         sock = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
16         if (sock == -1) {
17                 // Oops. Ignore this output, then.
18                 log_perror("socket");
19                 return;
20         }
21
22         if (setsockopt(sock, SOL_SOCKET, SO_MAX_PACING_RATE, &pacing_rate, sizeof(pacing_rate)) == -1) {
23                 if (pacing_rate != ~0U) {
24                         log_perror("setsockopt(SO_MAX_PACING_RATE)");
25                 }
26         }
27 }
28
29 UDPStream::~UDPStream()
30 {
31         if (sock != -1) {
32                 safe_close(sock);
33         }
34 }
35
36 void UDPStream::send(const char *data, size_t bytes)
37 {
38         if (sock == -1) {
39                 return;
40         }
41         ssize_t err = sendto(sock, data, bytes, 0, reinterpret_cast<sockaddr *>(&dst), sizeof(dst));
42         if (err == -1) {
43                 log_perror("sendto");
44         }
45 }