-/// @brief Listen for all device sockets and fill can_messages_queue with them.
-/// Reading blocks until message arrive on listened sockets.
-///
-/// @return 0 if ok -1 if not
-int can_bus_t::can_reader()
-{
- fd_set rfds;
- int sock_max = INVALID_SOCKET;
-
- FD_ZERO(&rfds);
-
- for(auto& can_dev : can_devices_)
- {
- FD_SET(can_dev.second->get_socket().socket(), &rfds);
- if (sock_max < can_dev.second->get_socket().socket())
- sock_max = can_dev.second->get_socket().socket();
- }
-
- int ret;
- while(is_reading_)
- {
- ret = select(sock_max + 1, &rfds, nullptr, nullptr, nullptr);
-
- if(ret == -1)
- perror("select()");
- else if(ret > 0)
- {
- for(const auto& s: can_devices_)
- {
- if(FD_ISSET(s.second->get_socket().socket(), &rfds))
- {
- can_message_t msg;
- s.second->get_socket() >> msg;
- std::lock_guard<std::mutex> can_message_lock(get_can_message_mutex());
- { push_new_can_message(msg); }
- get_new_can_message_cv().notify_one();
- }
- }
- }
- else
- printf("Timeout\n");
- }
- return 0;
-}
-