-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathudp_socket.hpp
133 lines (100 loc) · 3.02 KB
/
udp_socket.hpp
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#ifndef VI_UDP_SOCKET_HPP
#define VI_UDP_SOCKET_HPP
#include "common_defs.h"
#include "common_inc.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/unistd.h>
#include <sys/fcntl.h>
#include <arpa/inet.h>
#include <netinet/in.h>
namespace vi
{
class udp_socket
{
friend class udp_multicast_receiver;
friend class udp_multicast_sender;
private:
socket_t mSocket;
public:
udp_socket() : mSocket(-1)
{
}
~udp_socket()
{
close();
}
bool is_valid() const
{
return mSocket >= 0;
}
udp_socket& open()
{
mSocket = ::socket(AF_INET, SOCK_DGRAM, 0);
assert(mSocket >= 0);
return *this;
}
void close()
{
if (is_valid())
{
::close(mSocket);
mSocket = -1;
}
}
udp_socket& reuse_address()
{
int reuse = 1;
int result = ::setsockopt(mSocket, SOL_SOCKET, SO_REUSEADDR, (char*)&reuse, sizeof(reuse));
assert(0 == result);
return *this;
}
udp_socket& set_nonblocking()
{
int flags = ::fcntl(mSocket, F_GETFL, 0);
if (-1 == flags)
{
flags = 0;
}
int result = ::fcntl(mSocket, F_SETFL, flags | O_NONBLOCK);
assert(0 == result);
return *this;
}
udp_socket& listen(uint16_t port)
{
sockaddr_in sockAddr = {0};
sockAddr.sin_family = AF_INET;
sockAddr.sin_port = ::htons(port);
sockAddr.sin_addr.s_addr = INADDR_ANY;
int result = ::bind(mSocket, (sockaddr*)&sockAddr, sizeof(sockAddr));
assert(0 == result);
return *this;
}
udp_socket& join_multicast_group(const char* groupAddress,
const char* interfaceAddress)
{
ip_mreq group;
group.imr_multiaddr.s_addr = ::inet_addr(groupAddress);
group.imr_interface.s_addr = ::inet_addr(interfaceAddress);
int result = ::setsockopt(mSocket, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char*)&group, sizeof(group));
assert(0 == result);
return *this;
}
udp_socket& disable_loopback()
{
char loopch = 0;
int result = ::setsockopt(mSocket, IPPROTO_IP, IP_MULTICAST_LOOP, &loopch, sizeof(loopch));
assert(0 == result);
return *this;
}
udp_socket& bind_interface(const char* interfaceAddress)
{
in_addr localInterface;
localInterface.s_addr = inet_addr(interfaceAddress);
int result = ::setsockopt(mSocket, IPPROTO_IP, IP_MULTICAST_IF, (char*)&localInterface, sizeof(localInterface));
assert(0 == result);
return *this;
}
};
};
#endif