|
2 | 2 | #define SOCKET_UTILS_H |
3 | 3 |
|
4 | 4 | #include "api_config.h" |
5 | | -#include <boost/system/system_error.hpp> |
6 | 5 | #include <boost/asio/detail/chrono.hpp> |
| 6 | +#include <boost/asio/ip/tcp.hpp> |
| 7 | +#include <boost/asio/ip/udp.hpp> |
| 8 | + |
| 9 | +using namespace lslboost::asio::ip; |
7 | 10 |
|
8 | 11 | namespace lsl { |
9 | | - inline lslboost::asio::chrono::milliseconds timeout_sec(double timeout_seconds) { |
10 | | - return lslboost::asio::chrono::milliseconds(static_cast<unsigned int>(1000*timeout_seconds)); |
11 | | - } |
| 12 | +inline lslboost::asio::chrono::milliseconds timeout_sec(double timeout_seconds) { |
| 13 | + return lslboost::asio::chrono::milliseconds(static_cast<unsigned int>(1000 * timeout_seconds)); |
| 14 | +} |
12 | 15 |
|
13 | | - /// Bind a socket (or acceptor) to a free port in the configured port range or throw an error otherwise. |
14 | | - template <class Socket, class Protocol> |
15 | | - uint16_t bind_port_in_range(Socket &sock, Protocol protocol) { |
16 | | - for (int k=0,e=api_config::get_instance()->port_range(); k<e; k++) { |
17 | | - try { |
18 | | - sock.bind(typename Protocol::endpoint(protocol,(uint16_t)(k + api_config::get_instance()->base_port()))); |
19 | | - return k + api_config::get_instance()->base_port(); |
20 | | - } catch (lslboost::system::system_error &) { /* port occupied */ } |
21 | | - } |
22 | | - if (api_config::get_instance()->allow_random_ports()) { |
23 | | - for (int k=0; k < 100; ++k) { |
24 | | - uint16_t port = 1025 + rand()%64000; |
25 | | - try { |
26 | | - sock.bind(typename Protocol::endpoint(protocol,port)); |
27 | | - return port; |
28 | | - } catch (lslboost::system::system_error &) { /* port occupied */ } |
29 | | - } |
30 | | - } |
31 | | - throw std::runtime_error( |
32 | | - "All local ports were found occupied. You may have more open outlets on this machine " |
33 | | - "than your PortRange setting allows (see " |
34 | | - "https://labstreaminglayer.readthedocs.io/info/network-connectivity.html) or you " |
35 | | - "have a problem with your network configuration."); |
36 | | - } |
| 16 | +/// Bind a socket to a free port in the configured port range or throw an error otherwise. |
| 17 | +uint16_t bind_port_in_range(udp::socket &sock, udp protocol); |
37 | 18 |
|
38 | | - /// Bind to and listen at a socket (or acceptor) on a free port in the configured port range or throw an error otherwise. |
39 | | - template <class Socket, class Protocol> |
40 | | - uint16_t bind_and_listen_to_port_in_range(Socket &sock, Protocol protocol, int backlog) { |
41 | | - for (int k=0,e=api_config::get_instance()->port_range(); k<e; k++) { |
42 | | - try { |
43 | | - sock.bind(typename Protocol::endpoint(protocol,(uint16_t)(k + api_config::get_instance()->base_port()))); |
44 | | - sock.listen(backlog); |
45 | | - return k + api_config::get_instance()->base_port(); |
46 | | - } catch (lslboost::system::system_error &) { /* port occupied */ } |
47 | | - } |
48 | | - if (api_config::get_instance()->allow_random_ports()) { |
49 | | - for (int k = 0; k < 100; ++k) { |
50 | | - uint16_t port = 1025 + rand()%64000; |
51 | | - try { |
52 | | - sock.bind(typename Protocol::endpoint(protocol,port)); |
53 | | - sock.listen(backlog); |
54 | | - return port; |
55 | | - } catch (lslboost::system::system_error &) { /* port occupied */ } |
56 | | - } |
57 | | - } |
58 | | - throw std::runtime_error( |
59 | | - "All local ports were found occupied. You may have more open outlets on this machine " |
60 | | - "than your PortRange setting allows (see " |
61 | | - "https://labstreaminglayer.readthedocs.io/info/network-connectivity.html) or you " |
62 | | - "have a problem with your network configuration."); |
63 | | - } |
| 19 | +/// Bind and listen to an acceptor on a free port in the configured port range or throw an error. |
| 20 | +uint16_t bind_and_listen_to_port_in_range(tcp::acceptor &acc, tcp protocol, int backlog); |
64 | 21 |
|
65 | | - /// Measure the endian conversion performance of this machine. |
66 | | - double measure_endian_performance(); |
67 | | -} |
| 22 | +/// Measure the endian conversion performance of this machine. |
| 23 | +double measure_endian_performance(); |
| 24 | +} // namespace lsl |
68 | 25 |
|
69 | 26 | #endif |
70 | | - |
0 commit comments