1616#include < sys/socket.h>
1717#include < system_error>
1818
19- namespace Modbus {
20- namespace TCP {
19+ namespace Modbus ::TCP {
2120
2221// * maximum number of Modbus registers (per type)
2322static constexpr int MAX_REGS = 0x10000 ;
@@ -37,8 +36,8 @@ static constexpr struct timespec SEMAPHORE_MAX_TIME = {0, 100'000};
3736Client_Poll::Client_Poll (const std::string &host,
3837 const std::string &service,
3938 modbus_mapping_t *mapping,
40- std::size_t tcp_timeout,
41- std::size_t max_clients)
39+ std::size_t tcp_timeout, // NOLINT
40+ std::size_t max_clients) // NOLINT
4241 : max_clients(max_clients), poll_fds(max_clients + 2 , {0 , 0 , 0 }) {
4342 const char *host_str = " ::" ;
4443 if (!(host.empty () || host == " any" )) host_str = host.c_str ();
@@ -50,7 +49,7 @@ Client_Poll::Client_Poll(const std::string &host,
5049 throw std::runtime_error (" failed to create modbus instance: " + error_msg);
5150 }
5251
53- modbus_mapping_t *mb_mapping;
52+ modbus_mapping_t *mb_mapping; // NOLINT
5453
5554 if (mapping == nullptr ) {
5655 // create new mapping with the maximum number of registers
@@ -69,7 +68,7 @@ Client_Poll::Client_Poll(const std::string &host,
6968
7069 // use mapping for all client ids
7170 for (std::size_t i = 0 ; i < MAX_CLIENT_IDS; ++i) {
72- this ->mappings [i] = mb_mapping;
71+ this ->mappings [i] = mb_mapping; // NOLINT
7372 }
7473
7574 listen ();
@@ -81,11 +80,11 @@ Client_Poll::Client_Poll(const std::string &host,
8180#endif
8281}
8382
84- Client_Poll::Client_Poll (const std::string &host,
85- const std::string &service,
86- modbus_mapping_t ** mappings,
87- std::size_t tcp_timeout,
88- std::size_t max_clients)
83+ Client_Poll::Client_Poll (const std::string &host,
84+ const std::string &service,
85+ std::array< modbus_mapping_t *, MAX_CLIENT_IDS> & mappings,
86+ std::size_t tcp_timeout, // NOLINT
87+ std::size_t max_clients) // NOLINT
8988 : max_clients(max_clients), poll_fds(max_clients + 2 , {0 , 0 , 0 }) {
9089 const char *host_str = " ::" ;
9190 if (!(host.empty () || host == " any" )) host_str = host.c_str ();
@@ -100,7 +99,7 @@ Client_Poll::Client_Poll(const std::string &host,
10099 delete_mapping = nullptr ;
101100
102101 for (std::size_t i = 0 ; i < MAX_CLIENT_IDS; ++i) {
103- if (mappings[i] == nullptr ) {
102+ if (mappings[i] == nullptr ) { // NOLINT
104103 if (delete_mapping == nullptr ) {
105104 delete_mapping = modbus_mapping_new (MAX_REGS, MAX_REGS, MAX_REGS, MAX_REGS);
106105
@@ -110,9 +109,9 @@ Client_Poll::Client_Poll(const std::string &host,
110109 throw std::runtime_error (" failed to allocate memory: " + error_msg);
111110 }
112111 }
113- this ->mappings [i] = delete_mapping;
112+ this ->mappings [i] = delete_mapping; // NOLINT
114113 } else {
115- this ->mappings [i] = mappings[i];
114+ this ->mappings [i] = mappings[i]; // NOLINT
116115 }
117116 }
118117
@@ -156,9 +155,9 @@ Client_Poll::~Client_Poll() {
156155}
157156
158157#ifdef OS_LINUX
159- void Client_Poll::set_tcp_timeout (std::size_t tcp_timeout) {
158+ void Client_Poll::set_tcp_timeout (std::size_t tcp_timeout) const {
160159 // set user timeout (~= timeout for tcp connection)
161- unsigned user_timeout = static_cast <unsigned >(tcp_timeout) * 1000 ;
160+ unsigned user_timeout = static_cast <unsigned >(tcp_timeout) * 1000 ; // NOLINT
162161 int tmp = setsockopt (server_socket, IPPROTO_TCP, TCP_USER_TIMEOUT, &user_timeout, sizeof (tcp_timeout));
163162 if (tmp != 0 ) {
164163 throw std::system_error (errno, std::generic_category (), " Failed to set socket option TCP_USER_TIMEOUT" );
@@ -172,14 +171,14 @@ void Client_Poll::set_tcp_timeout(std::size_t tcp_timeout) {
172171 }
173172
174173 // send up to 5 keepalive requests during the timeout time, but not more than one per second
175- unsigned keepintvl = std::max (static_cast <unsigned >(tcp_timeout / 5 ), 1u );
174+ unsigned keepintvl = std::max (static_cast <unsigned >(tcp_timeout / 5 ), 1u ); // NOLINT
176175 tmp = setsockopt (server_socket, IPPROTO_TCP, TCP_KEEPINTVL, &keepintvl, sizeof (keepintvl));
177176 if (tmp != 0 ) {
178177 throw std::system_error (errno, std::generic_category (), " Failed to set socket option TCP_KEEPINTVL" );
179178 }
180179
181180 // 5 keepalive requests if the timeout time is >= 5s; else send one request each second
182- unsigned keepcnt = std::min (static_cast <unsigned >(tcp_timeout), 5u );
181+ unsigned keepcnt = std::min (static_cast <unsigned >(tcp_timeout), 5u ); // NOLINT
183182 tmp = setsockopt (server_socket, IPPROTO_TCP, TCP_KEEPCNT, &keepcnt, sizeof (keepcnt));
184183 if (tmp != 0 ) {
185184 throw std::system_error (errno, std::generic_category (), " Failed to set socket option TCP_KEEPCNT" );
@@ -213,7 +212,7 @@ static inline timeout_t double_to_timeout_t(double timeout) {
213212 ret.sec = static_cast <uint32_t >(timeout);
214213
215214 double fractional = timeout - static_cast <double >(ret.sec );
216- ret.usec = static_cast <uint32_t >(fractional * 1000.0 * 1000.0 );
215+ ret.usec = static_cast <uint32_t >(fractional * 1000.0 * 1000.0 ); // NOLINT
217216
218217 return ret;
219218}
@@ -238,7 +237,7 @@ void Client_Poll::set_response_timeout(double timeout) {
238237 }
239238}
240239
241- double Client_Poll::get_byte_timeout () {
240+ [[maybe_unused]] double Client_Poll::get_byte_timeout () {
242241 timeout_t timeout {};
243242
244243 auto ret = modbus_get_byte_timeout (modbus, &timeout.sec , &timeout.usec );
@@ -248,10 +247,10 @@ double Client_Poll::get_byte_timeout() {
248247 throw std::runtime_error (" modbus_receive failed: " + error_msg + ' ' + std::to_string (errno));
249248 }
250249
251- return static_cast <double >(timeout.sec ) + (static_cast <double >(timeout.usec ) / (1000.0 * 1000.0 ));
250+ return static_cast <double >(timeout.sec ) + (static_cast <double >(timeout.usec ) / (1000.0 * 1000.0 )); // NOLINT
252251}
253252
254- double Client_Poll::get_response_timeout () {
253+ [[maybe_unused]] double Client_Poll::get_response_timeout () {
255254 timeout_t timeout {};
256255
257256 auto ret = modbus_get_response_timeout (modbus, &timeout.sec , &timeout.usec );
@@ -261,7 +260,7 @@ double Client_Poll::get_response_timeout() {
261260 throw std::runtime_error (" modbus_receive failed: " + error_msg + ' ' + std::to_string (errno));
262261 }
263262
264- return static_cast <double >(timeout.sec ) + (static_cast <double >(timeout.usec ) / (1000.0 * 1000.0 ));
263+ return static_cast <double >(timeout.sec ) + (static_cast <double >(timeout.usec ) / (1000.0 * 1000.0 )); // NOLINT
265264}
266265
267266Client_Poll::run_t Client_Poll::run (int signal_fd, bool reconnect, int timeout) {
@@ -284,7 +283,7 @@ Client_Poll::run_t Client_Poll::run(int signal_fd, bool reconnect, int timeout)
284283 }
285284
286285 // add client sockets to poll
287- for (auto con : client_addrs) {
286+ for (const auto & con : client_addrs) {
288287 auto &fd = poll_fds[i++];
289288 fd.fd = con.first ;
290289 fd.events = POLLIN;
@@ -332,9 +331,9 @@ Client_Poll::run_t Client_Poll::run(int signal_fd, bool reconnect, int timeout)
332331
333332 auto client_socket = modbus_get_socket (modbus);
334333
335- struct sockaddr_storage peer_addr;
334+ struct sockaddr_storage peer_addr; // NOLINT
336335 socklen_t len = sizeof (peer_addr);
337- tmp = getpeername (client_socket, reinterpret_cast <struct sockaddr *>(&peer_addr), &len);
336+ tmp = getpeername (client_socket, reinterpret_cast <struct sockaddr *>(&peer_addr), &len); // NOLINT
338337
339338 if (tmp < 0 ) {
340339 const std::string error_msg = modbus_strerror (errno);
@@ -345,11 +344,11 @@ Client_Poll::run_t Client_Poll::run(int signal_fd, bool reconnect, int timeout)
345344
346345 sstr << sockaddr_to_str (peer_addr);
347346 // the port entries have the same offset and size in sockaddr_in and sockaddr_in6
348- sstr << ' :' << htons (reinterpret_cast <const struct sockaddr_in *>(&peer_addr)->sin_port );
347+ sstr << ' :' << htons (reinterpret_cast <const struct sockaddr_in *>(&peer_addr)->sin_port ); // NOLINT
349348
350349 client_addrs[client_socket] = sstr.str ();
351350 std::cerr << Print_Time::iso << " INFO: [" << active_clients + 1 << " ] Modbus Server (" << sstr.str ()
352- << " ) established connection." << std::endl;
351+ << " ) established connection." << std::endl; // NOLINT
353352 } else {
354353 std::ostringstream sstr;
355354 sstr << " poll (server socket) returned unknown revent: " << fd.revents ;
@@ -380,27 +379,27 @@ Client_Poll::run_t Client_Poll::run(int signal_fd, bool reconnect, int timeout)
380379 } else if (fd.revents & POLLIN || fd.revents & POLLERR) {
381380 modbus_set_socket (modbus, fd.fd );
382381
383- uint8_t query[ MODBUS_TCP_MAX_ADU_LENGTH] ;
384- int rc = modbus_receive (modbus, query);
382+ std::array< uint8_t , MODBUS_TCP_MAX_ADU_LENGTH> query {} ;
383+ int rc = modbus_receive (modbus, query. data () );
385384 if (debug) std::cout.flush ();
386385
387386 if (rc > 0 ) {
388387 const auto CLIENT_ID = query[6 ];
389388
390389 // get mapping
391- auto mapping = mappings[CLIENT_ID];
390+ auto mapping = mappings[CLIENT_ID]; // NOLINT
392391
393392 // handle request
394393 if (semaphore) {
395394 if (!semaphore->wait (SEMAPHORE_MAX_TIME)) {
396395 std::cerr << Print_Time::iso << " WARNING: Failed to acquire semaphore '"
397- << semaphore->get_name () << " ' within 100ms." << std::endl;
396+ << semaphore->get_name () << " ' within 100ms." << std::endl; // NOLINT
398397
399398 semaphore_error_counter += SEMAPHORE_ERROR_INC;
400399
401400 if (semaphore_error_counter >= SEMAPHORE_ERROR_MAX) {
402401 std::cerr << Print_Time::iso << " ERROR: Repeatedly failed to acquire the semaphore"
403- << std::endl;
402+ << std::endl; // NOLINT
404403 close_con (client_addrs);
405404 return run_t ::semaphore;
406405 }
@@ -410,19 +409,19 @@ Client_Poll::run_t Client_Poll::run(int signal_fd, bool reconnect, int timeout)
410409 }
411410 }
412411
413- int ret = modbus_reply (modbus, query, rc, mapping);
412+ int ret = modbus_reply (modbus, query. data () , rc, mapping);
414413 if (semaphore && semaphore->is_acquired ()) semaphore->post ();
415414 if (debug) std::cout.flush ();
416415
417416 if (ret == -1 ) {
418417 std::cerr << Print_Time::iso << " ERROR: modbus_reply failed: " << modbus_strerror (errno)
419- << std::endl;
418+ << std::endl; // NOLINT
420419 close_con (client_addrs);
421420 }
422421 } else if (rc == -1 ) {
423422 if (errno != ECONNRESET) {
424423 std::cerr << Print_Time::iso << " ERROR: modbus_receive failed: " << modbus_strerror (errno)
425- << std::endl;
424+ << std::endl; // NOLINT
426425 }
427426 close_con (client_addrs);
428427 } else { // rc == 0
@@ -440,10 +439,10 @@ Client_Poll::run_t Client_Poll::run(int signal_fd, bool reconnect, int timeout)
440439 return run_t ::ok;
441440}
442441
443- std::string Client_Poll::get_listen_addr () {
444- struct sockaddr_storage sock_addr;
442+ std::string Client_Poll::get_listen_addr () const {
443+ struct sockaddr_storage sock_addr; // NOLINT
445444 socklen_t len = sizeof (sock_addr);
446- int tmp = getsockname (server_socket, reinterpret_cast <struct sockaddr *>(&sock_addr), &len);
445+ int tmp = getsockname (server_socket, reinterpret_cast <struct sockaddr *>(&sock_addr), &len); // NOLINT
447446
448447 if (tmp < 0 ) {
449448 const std::string error_msg = modbus_strerror (errno);
@@ -453,10 +452,9 @@ std::string Client_Poll::get_listen_addr() {
453452 std::ostringstream sstr;
454453 sstr << sockaddr_to_str (sock_addr);
455454 // the port entries have the same offset and size in sockaddr_in and sockaddr_in6
456- sstr << ' :' << htons (reinterpret_cast <const struct sockaddr_in *>(&sock_addr)->sin_port );
455+ sstr << ' :' << htons (reinterpret_cast <const struct sockaddr_in *>(&sock_addr)->sin_port ); // NOLINT
457456
458457 return sstr.str ();
459458}
460459
461- } // namespace TCP
462- } // namespace Modbus
460+ } // namespace Modbus::TCP
0 commit comments