Skip to content

Commit

Permalink
Fixes to connect to remote server from alsa port
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmoreno committed Dec 22, 2023
1 parent dd2eb32 commit 2e57425
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 25 deletions.
26 changes: 13 additions & 13 deletions lib/rtpclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,6 @@ rtpclient_t::rtpclient_t(std::string name) : peer(std::move(name)) {
} else {
INFO("Disconnected reason: {}. Not trying to connect again.", reason);
}
connect_to_next();
});
peer_connected_event_connection = peer.connected_event.connect(
[this](const std::string &name, rtppeer_t::status_e status) {
INFO("Connected to {}: {}", name, status);
connected_event(name, status);
});
}

Expand Down Expand Up @@ -152,9 +146,11 @@ bool for_each_address(const std::string &hostname, const std::string &port,
addrinfo *serveraddr = sockaddress_list;
for (; serveraddr != nullptr; serveraddr = serveraddr->ai_next) {
bool stop = callback(serveraddr);
if (stop)
if (stop) {
DEBUG("Stop iterating addresses");
freeaddrinfo(sockaddress_list);
return true;
return true;
}
}
freeaddrinfo(sockaddress_list);
// Could not connect to any address
Expand Down Expand Up @@ -239,22 +235,25 @@ std::optional<socket_port_sockaddr_t> connect_udp_port(int local_base_port,
*
* If both succeed, return them. Else the return is an empty optional.
*/
std::optional<control_midi_ports_t> connect_control_and_midi_sockets(
int local_base_port, const std::string &hostname, const std::string &port) {
std::optional<control_midi_ports_t>
connect_control_and_midi_sockets(int local_base_port,
const std::string &hostname,
const std::string &portname) {
std::optional<control_midi_ports_t> ret = std::nullopt;

for_each_address(hostname, port, [&](addrinfo *serveraddr) {
for_each_address(hostname, portname, [&](addrinfo *serveraddr) {
char host[NI_MAXHOST];
char port[NI_MAXSERV];
host[0] = port[0] = '\0';
getnameinfo(serveraddr->ai_addr, serveraddr->ai_addrlen, host, NI_MAXHOST,
port, NI_MAXSERV, NI_NUMERICSERV);

DEBUG("Try to connect to address: {}:{}", host, port);
DEBUG("Try to connect to address: {}:{} ({}:{})", host, port, hostname,
portname);

auto control = connect_udp_port(local_base_port, serveraddr);
if (!control) {
DEBUG("Could not connect to control port");
DEBUG("Could not connect {}:{} to control port", hostname, portname);
return false;
}

Expand Down Expand Up @@ -338,6 +337,7 @@ bool rtpclient_t::connect_to(const std::string &address,
remote_base_port + 1);
peer.connect_to(rtppeer_t::MIDI_PORT);
} else if (status == rtppeer_t::CONNECTED) {
connected_event(name, status);
connected();
}
});
Expand Down
29 changes: 17 additions & 12 deletions src/network_rtpmidi_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,23 @@ network_rtpmidi_client_t::network_rtpmidi_client_t(
router->send_midi(peer_id, mididata_t{data});
});

// disconnect_connection = peer->disconnect_event.connect(
// [this](rtpmidid::rtppeer_t::disconnect_reason_e reason) {
// DEBUG("Peer disconnected: {}. Remove rtpmidi peer and alsa port
// too.",
// reason);
// rtpmidid::poller.call_later([this] {
// router->peer_connection_loop(peer_id, [this](auto other_peer) {
// router->remove_peer(other_peer->peer_id);
// });
// router->remove_peer(peer_id);
// });
// });
connected_connection = peer->connected_event.connect(
[this](const std::string &, rtpmidid::rtppeer_t::status_e) {
DEBUG("Peer connected: {}. Add rtpmidi peer and alsa port too.",
peer->peer.remote_name);
});

disconnect_connection = peer->peer.disconnect_event.connect(
[this](rtpmidid::rtppeer_t::disconnect_reason_e reason) {
DEBUG("Peer disconnected: {}. Remove rtpmidi peer and alsa port too.",
reason);
// rtpmidid::poller.call_later([this] {
// router->peer_connection_loop(peer_id, [this](auto other_peer) {
// router->remove_peer(other_peer->peer_id);
// });
// router->remove_peer(peer_id);
// });
});
}

network_rtpmidi_client_t::~network_rtpmidi_client_t() {}
Expand Down
2 changes: 2 additions & 0 deletions src/network_rtpmidi_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ class network_rtpmidi_client_t : public midipeer_t {
rtpmidid::connection_t<const rtpmidid::io_bytes_reader &> midi_connection;
rtpmidid::connection_t<rtpmidid::rtppeer_t::disconnect_reason_e>
disconnect_connection;
rtpmidid::connection_t<const std::string &, rtpmidid::rtppeer_t::status_e>
connected_connection;

network_rtpmidi_client_t(std::shared_ptr<rtpmidid::rtpclient_t> peer);
~network_rtpmidi_client_t();
Expand Down

0 comments on commit 2e57425

Please sign in to comment.