forked from sccn/liblsl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocket_utils.h
More file actions
70 lines (63 loc) · 2.78 KB
/
socket_utils.h
File metadata and controls
70 lines (63 loc) · 2.78 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
61
62
63
64
65
66
67
68
69
#ifndef SOCKET_UTILS_H
#define SOCKET_UTILS_H
#include "api_config.h"
#include <boost/system/system_error.hpp>
#include <boost/asio/detail/chrono.hpp>
namespace lsl {
inline lslboost::asio::chrono::milliseconds timeout_sec(double timeout_seconds) {
return lslboost::asio::chrono::milliseconds(static_cast<unsigned int>(1000*timeout_seconds));
}
/// Bind a socket (or acceptor) to a free port in the configured port range or throw an error otherwise.
template <class Socket, class Protocol>
uint16_t bind_port_in_range(Socket &sock, Protocol protocol) {
for (int k=0,e=api_config::get_instance()->port_range(); k<e; k++) {
try {
sock.bind(typename Protocol::endpoint(protocol,(uint16_t)(k + api_config::get_instance()->base_port())));
return k + api_config::get_instance()->base_port();
} catch (lslboost::system::system_error &) { /* port occupied */ }
}
if (api_config::get_instance()->allow_random_ports()) {
for (int k=0; k < 100; ++k) {
uint16_t port = 1025 + rand()%64000;
try {
sock.bind(typename Protocol::endpoint(protocol,port));
return port;
} catch (lslboost::system::system_error &) { /* port occupied */ }
}
}
throw std::runtime_error(
"All local ports were found occupied. You may have more open outlets on this machine "
"than your PortRange setting allows (see "
"https://labstreaminglayer.readthedocs.io/info/network-connectivity.html) or you "
"have a problem with your network configuration.");
}
/// Bind to and listen at a socket (or acceptor) on a free port in the configured port range or throw an error otherwise.
template <class Socket, class Protocol>
uint16_t bind_and_listen_to_port_in_range(Socket &sock, Protocol protocol, int backlog) {
for (int k=0,e=api_config::get_instance()->port_range(); k<e; k++) {
try {
sock.bind(typename Protocol::endpoint(protocol,(uint16_t)(k + api_config::get_instance()->base_port())));
sock.listen(backlog);
return k + api_config::get_instance()->base_port();
} catch (lslboost::system::system_error &) { /* port occupied */ }
}
if (api_config::get_instance()->allow_random_ports()) {
for (int k = 0; k < 100; ++k) {
uint16_t port = 1025 + rand()%64000;
try {
sock.bind(typename Protocol::endpoint(protocol,port));
sock.listen(backlog);
return port;
} catch (lslboost::system::system_error &) { /* port occupied */ }
}
}
throw std::runtime_error(
"All local ports were found occupied. You may have more open outlets on this machine "
"than your PortRange setting allows (see "
"https://labstreaminglayer.readthedocs.io/info/network-connectivity.html) or you "
"have a problem with your network configuration.");
}
/// Measure the endian conversion performance of this machine.
double measure_endian_performance();
}
#endif