void OSCQueryDevice::slot_createDevice() { const auto& cur_settings = settings(); const auto& stgs = cur_settings.deviceSpecificSettings.value<OSCQuerySpecificSettings>(); try { std::unique_ptr<ossia::net::protocol_base> ossia_settings = std::make_unique<ossia::oscquery::oscquery_mirror_protocol>( stgs.host.toStdString()); auto& p = static_cast<ossia::oscquery::oscquery_mirror_protocol&>( *ossia_settings); m_mirror = &p; if (stgs.rate) { ossia_settings = std::make_unique<ossia::net::rate_limiting_protocol>( std::chrono::milliseconds{*stgs.rate}, std::move(ossia_settings)); } // run the commands in the Qt event loop // FIXME they should be disabled upon manual disconnection m_dev = std::make_unique<ossia::net::generic_device>( std::move(ossia_settings), settings().name.toStdString()); deviceChanged(nullptr, m_dev.get()); p.set_command_callback([=] { sig_command(); }); p.set_disconnect_callback([=] { sig_disconnect(); }); p.set_fail_callback([=] { sig_disconnect(); }); setLogging_impl(Device::get_cur_logging(isLogging())); enableCallbacks(); m_connected = true; } catch (std::exception& e) { qDebug() << "Could not connect: " << e.what(); m_connected = false; if (!m_dev) m_mirror = nullptr; } catch (...) { // TODO save the reason of the non-connection. m_connected = false; if (!m_dev) m_mirror = nullptr; } connectionChanged(m_connected); }
void net_disconnect_deinit(void) { #ifndef WIN32 NET_DISCONNECT_REC *rec; time_t now, max; int first, fd; struct timeval tv; fd_set set; /* give the sockets a chance to disconnect themselves.. */ max = time(NULL)+MAX_QUIT_CLOSE_WAIT; first = 1; while (disconnects != NULL) { rec = disconnects->data; now = time(NULL); if (rec->created+MAX_QUIT_CLOSE_WAIT <= now || max <= now) { /* this one has waited enough */ net_disconnect_remove(rec); continue; } fd = g_io_channel_unix_get_fd(rec->handle); FD_ZERO(&set); FD_SET(fd, &set); tv.tv_sec = first ? 0 : max-now; tv.tv_usec = first ? 100000 : 0; if (select(fd+1, &set, NULL, NULL, &tv) > 0 && FD_ISSET(fd, &set)) { /* data coming .. check if we can close the handle */ sig_disconnect(rec); } else if (first) { /* Display the text when we have already waited for a while */ printf("Please wait, waiting for servers to close " "connections..\n"); fflush(stdout); first = 0; } } #endif }