| 62 | } |
| 63 | |
| 64 | void udp_server_endpoint_impl::init_unlocked(const endpoint_type& _local, boost::system::error_code& _error) { |
| 65 | // The caller must hold the lock |
| 66 | |
| 67 | if (unicast_socket_) { |
| 68 | if (local_ == _local) { |
| 69 | VSOMEIP_WARNING_P << instance_name_ << "Already initialized, lifecycle_idx=" << lifecycle_idx_.load(); |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | VSOMEIP_WARNING_P << instance_name_ << "Reset unicast socket, lifecycle_idx=" << lifecycle_idx_.load(); |
| 74 | unicast_socket_.reset(); |
| 75 | } |
| 76 | |
| 77 | // reset sending flag for all targets |
| 78 | for (auto& [client, endpoint_type] : targets_) { |
| 79 | endpoint_type.is_sending_ = false; |
| 80 | } |
| 81 | |
| 82 | auto socket_factory = abstract_socket_factory::get(); |
| 83 | unicast_socket_ = socket_factory->create_udp_socket(io_); |
| 84 | if (!unicast_socket_) { |
| 85 | _error = boost::asio::error::make_error_code(boost::asio::error::no_memory); |
| 86 | VSOMEIP_ERROR_P << instance_name_ << "Failed to create socket"; |
| 87 | return; |
| 88 | } |
| 89 | |
| 90 | if (!unicast_socket_->is_open()) { |
| 91 | unicast_socket_->open(_local.protocol(), _error); |
| 92 | if (_error) { |
| 93 | VSOMEIP_ERROR_P << instance_name_ << "Failed to open socket, " << _error.message(); |
| 94 | unicast_socket_.reset(); |
| 95 | return; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | boost::asio::socket_base::reuse_address opt_reuse_address(true); |
| 100 | unicast_socket_->set_option(opt_reuse_address, _error); |
| 101 | if (_error) { |
| 102 | VSOMEIP_ERROR_P << instance_name_ << "Failed to reuse address, " << _error.message(); |
| 103 | unicast_socket_.reset(); |
| 104 | return; |
| 105 | } |
| 106 | |
| 107 | #if defined(__linux__) || defined(__QNX__) |
| 108 | // If specified, bind to device |
| 109 | const std::string its_device = configuration_->get_device(); |
| 110 | if (!its_device.empty()) { |
| 111 | unicast_socket_->set_option(udp_bind_to_device{its_device}, _error); |
| 112 | if (_error) { |
| 113 | VSOMEIP_ERROR_P << instance_name_ << "Failed to bind to device \"" << its_device << "\", " << _error.message(); |
| 114 | // Non-fatal error |
| 115 | _error.clear(); |
| 116 | } |
| 117 | } |
| 118 | #endif |
| 119 | |
| 120 | unicast_socket_->bind(_local, _error); |
| 121 | if (_error) { |
nothing calls this directly
no test coverage detected