]> git.sesse.net Git - cubemap/blob - udpstream.cpp
Set the default number of files to 16384; the default is seemingly 1024, which can...
[cubemap] / udpstream.cpp
1 #include <sys/socket.h>
2 #include <sys/types.h>
3
4 #include "log.h"
5 #include "markpool.h"
6 #include "udpstream.h"
7 #include "util.h"
8
9 #ifndef SO_MAX_PACING_RATE
10 #define SO_MAX_PACING_RATE 47
11 #endif
12
13 UDPStream::UDPStream(const sockaddr_in6 &dst, MarkPool *mark_pool, uint32_t pacing_rate)
14         : dst(dst),
15           mark_pool(mark_pool),
16           fwmark(0),
17           pacing_rate(pacing_rate)
18 {
19         sock = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
20         if (sock == -1) {
21                 // Oops. Ignore this output, then.
22                 log_perror("socket");
23                 return;
24         }
25
26         if (mark_pool != NULL) {
27                 fwmark = mark_pool->get_mark();
28                 if (setsockopt(sock, SOL_SOCKET, SO_MARK, &fwmark, sizeof(fwmark)) == -1) {                          
29                         if (fwmark != 0) {
30                                 log_perror("setsockopt(SO_MARK)");
31                         }
32                 }
33         }
34         if (setsockopt(sock, SOL_SOCKET, SO_MAX_PACING_RATE, &pacing_rate, sizeof(pacing_rate)) == -1) {
35                 if (pacing_rate != ~0U) {
36                         log_perror("setsockopt(SO_MAX_PACING_RATE)");
37                 }
38         }
39 }
40
41 UDPStream::~UDPStream()
42 {
43         if (sock != -1) {
44                 safe_close(sock);
45         }
46         if (mark_pool != NULL) {
47                 mark_pool->release_mark(fwmark);
48         }
49 }
50
51 void UDPStream::send(const char *data, size_t bytes)
52 {
53         if (sock == -1) {
54                 return;
55         }
56         ssize_t err = sendto(sock, data, bytes, 0, reinterpret_cast<sockaddr *>(&dst), sizeof(dst));
57         if (err == -1) {
58                 log_perror("sendto");
59         }
60 }