Fixing Streamer and NetworkSource dialog

This commit is contained in:
brunoherbelin
2020-11-01 18:13:37 +01:00
parent 954b35032a
commit b8ebab5766
6 changed files with 48 additions and 47 deletions

View File

@@ -65,10 +65,10 @@ bool Connection::init()
// inform the application settings of our id
Settings::application.instance_id = connections_[0].port_handshake - HANDSHAKE_PORT;
// use or replace instance name from settings
if (Settings::application.instance_names.count(Settings::application.instance_id))
connections_[0].name = Settings::application.instance_names[Settings::application.instance_id];
else
Settings::application.instance_names[Settings::application.instance_id] = connections_[0].name;
// if (Settings::application.instance_names.count(Settings::application.instance_id))
// connections_[0].name = Settings::application.instance_names[Settings::application.instance_id];
// else
// Settings::application.instance_names[Settings::application.instance_id] = connections_[0].name;
// restore state of Streamer
Streaming::manager().enable( Settings::application.accept_connections );
@@ -176,11 +176,12 @@ void Connection::ask()
std::this_thread::sleep_for(std::chrono::milliseconds(500));
// check the list of connections for non responding (disconnected)
for(auto it=Connection::manager().connections_.begin(); it!=Connection::manager().connections_.end(); ) {
std::vector< ConnectionInfo >::iterator it = Connection::manager().connections_.begin();
for(it++; it!=Connection::manager().connections_.end(); ) {
// decrease life score
(*it).alive--;
// erase connection if its life score is negative (not responding too many times)
if ( it!=Connection::manager().connections_.begin() && (*it).alive < 0 ) {
if ( (*it).alive < 0 ) {
// inform streamer to cancel streaming to this client
Streaming::manager().removeStreams( (*it).name );
// remove from list
@@ -258,8 +259,8 @@ void ConnectionRequestListener::ProcessMessage( const osc::ReceivedMessage& m,
// a new connection! Add to list
Connection::manager().connections_.push_back(info);
// replace instance name in settings
int id = info.port_handshake - HANDSHAKE_PORT;
Settings::application.instance_names[id] = info.name;
// int id = info.port_handshake - HANDSHAKE_PORT;
// Settings::application.instance_names[id] = info.name;
#ifdef CONNECTION_DEBUG
Log::Info("List of connection updated:");

View File

@@ -35,6 +35,9 @@ void StreamerResponseListener::ProcessMessage( const osc::ReceivedMessage& m,
try{
if( std::strcmp( m.AddressPattern(), OSC_PREFIX OSC_STREAM_OFFER ) == 0 ){
#ifdef NETWORK_DEBUG
Log::Info("Received stream info from %s", sender);
#endif
NetworkToolkit::StreamConfig conf;
// someone is offering a stream
@@ -49,17 +52,14 @@ void StreamerResponseListener::ProcessMessage( const osc::ReceivedMessage& m,
parent_->connected_ = true;
parent_->received_config_ = true;
#ifdef NETWORK_DEBUG
Log::Info("Received stream info from %s", sender);
#endif
}
else if( std::strcmp( m.AddressPattern(), OSC_PREFIX OSC_STREAM_REJECT ) == 0 ){
parent_->connected_ = false;
parent_->received_config_ = true;
#ifdef NETWORK_DEBUG
Log::Info("Received rejection from %s", sender);
#endif
parent_->connected_ = false;
parent_->received_config_ = true;
}
}
catch( osc::Exception& e ){
@@ -113,6 +113,10 @@ void NetworkStream::connect(const std::string &nameconnection)
return;
}
// ok, we want to ask to this connected streamer to send us a stream
streamer_ = Connection::manager().info(streamer_index);
std::string listener_address = NetworkToolkit::closest_host_ip(streamer_.address);
// prepare listener to receive stream config from remote streaming manager
listener_.setParent(this);
@@ -123,7 +127,7 @@ void NetworkStream::connect(const std::string &nameconnection)
// invent a port which would be available
listener_port_ = 72000 + rand()%1000;
// try to create receiver (through exception on fail)
receiver_ = new UdpListeningReceiveSocket(IpEndpointName(Connection::manager().info().address.c_str(), listener_port_), &listener_);
receiver_ = new UdpListeningReceiveSocket(IpEndpointName(listener_address.c_str(), listener_port_), &listener_);
}
catch (const std::runtime_error&) {
receiver_ = nullptr;
@@ -135,12 +139,6 @@ void NetworkStream::connect(const std::string &nameconnection)
return;
}
// ok, we can ask to this connected streamer to send us a stream
ConnectionInfo streamer = Connection::manager().info(streamer_index);
IpEndpointName a( streamer.address.c_str(), streamer.port_stream_request );
streamer_address_ = a.address;
streamer_address_ = a.port;
// build OSC message
char buffer[IP_MTU_SIZE];
osc::OutboundPacketStream p( buffer, IP_MTU_SIZE );
@@ -152,14 +150,14 @@ void NetworkStream::connect(const std::string &nameconnection)
p << osc::EndMessage;
// send OSC message to streamer
UdpTransmitSocket socket( streamer_address_ );
UdpTransmitSocket socket( IpEndpointName(streamer_.address.c_str(), streamer_.port_stream_request) );
socket.Send( p.Data(), p.Size() );
// Now we wait for the offer from the streamer
std::thread(wait_for_stream_, receiver_).detach();
#ifdef NETWORK_DEBUG
Log::Info("Asking %s:%d for a stream", streamer.address.c_str(), streamer.port_stream_request);
Log::Info("Asking %s:%d for a stream", streamer_.address.c_str(), streamer_.port_stream_request);
Log::Info("Waiting for response at %s:%d", Connection::manager().info().address.c_str(), listener_port_);
#endif
}
@@ -181,7 +179,7 @@ void NetworkStream::disconnect()
p << osc::EndMessage;
// send OSC message to streamer
UdpTransmitSocket socket( streamer_address_ );
UdpTransmitSocket socket( IpEndpointName(streamer_.address.c_str(), streamer_.port_stream_request) );
socket.Send( p.Data(), p.Size() );
}
}

View File

@@ -7,6 +7,7 @@
#include "ip/UdpSocket.h"
#include "NetworkToolkit.h"
#include "Connection.h"
#include "StreamSource.h"
class NetworkStream;
@@ -42,7 +43,7 @@ public:
private:
// connection information
IpEndpointName streamer_address_;
ConnectionInfo streamer_;
StreamerResponseListener listener_;
UdpListeningReceiveSocket *receiver_;
std::atomic<bool> received_config_;

View File

@@ -111,14 +111,14 @@ void Settings::Save()
{
XMLElement *connectionsNode = xmlDoc.NewElement( "Connections" );
map<int, std::string>::iterator iter;
for (iter=application.instance_names.begin(); iter != application.instance_names.end(); iter++)
{
XMLElement *connection = xmlDoc.NewElement( "Instance" );
connection->SetAttribute("name", iter->second.c_str());
connection->SetAttribute("id", iter->first);
connectionsNode->InsertEndChild(connection);
}
// map<int, std::string>::iterator iter;
// for (iter=application.instance_names.begin(); iter != application.instance_names.end(); iter++)
// {
// XMLElement *connection = xmlDoc.NewElement( "Instance" );
// connection->SetAttribute("name", iter->second.c_str());
// connection->SetAttribute("id", iter->first);
// connectionsNode->InsertEndChild(connection);
// }
pRoot->InsertEndChild(connectionsNode);
}
@@ -346,13 +346,13 @@ void Settings::Load()
XMLElement * pElement = pRoot->FirstChildElement("Connections");
if (pElement)
{
XMLElement* connectionNode = pElement->FirstChildElement("Instance");
for( ; connectionNode ; connectionNode=connectionNode->NextSiblingElement())
{
int id = 0;
connectionNode->QueryIntAttribute("id", &id);
application.instance_names[id] = connectionNode->Attribute("name");
}
// XMLElement* connectionNode = pElement->FirstChildElement("Instance");
// for( ; connectionNode ; connectionNode=connectionNode->NextSiblingElement())
// {
// int id = 0;
// connectionNode->QueryIntAttribute("id", &id);
// application.instance_names[id] = connectionNode->Attribute("name");
// }
}
}

View File

@@ -179,7 +179,7 @@ struct Application
// connection settings
bool accept_connections;
std::map<int, std::string> instance_names;
// std::map<int, std::string> instance_names;
// Settings of widgets
WidgetsConfig widget;

View File

@@ -44,6 +44,9 @@ void StreamingRequestListener::ProcessMessage( const osc::ReceivedMessage& m,
if( std::strcmp( m.AddressPattern(), OSC_PREFIX OSC_STREAM_REQUEST) == 0 ){
#ifdef STREAMER_DEBUG
Log::Info("%s wants a stream.", sender);
#endif
osc::ReceivedMessage::const_iterator arg = m.ArgumentsBegin();
int reply_to_port = (arg++)->AsInt32();
const char *client_name = (arg++)->AsString();
@@ -51,19 +54,16 @@ void StreamingRequestListener::ProcessMessage( const osc::ReceivedMessage& m,
Streaming::manager().addStream(sender, reply_to_port, client_name);
else
Streaming::manager().refuseStream(sender, reply_to_port);
#ifdef STREAMER_DEBUG
Log::Info("%s wants a stream.", sender);
#endif
}
else if( std::strcmp( m.AddressPattern(), OSC_PREFIX OSC_STREAM_DISCONNECT) == 0 ){
osc::ReceivedMessage::const_iterator arg = m.ArgumentsBegin();
int port = (arg++)->AsInt32();
Streaming::manager().removeStream(sender, port);
#ifdef STREAMER_DEBUG
Log::Info("%s does not need streaming anymore.", sender);
#endif
osc::ReceivedMessage::const_iterator arg = m.ArgumentsBegin();
int port = (arg++)->AsInt32();
Streaming::manager().removeStream(sender, port);
}
}
catch( osc::Exception& e ){
@@ -247,6 +247,7 @@ void Streaming::addStream(const std::string &sender, int reply_to, const std::st
socket.Send( p.Data(), p.Size() );
#ifdef STREAMER_DEBUG
Log::Info("Replying to %s:%d", sender_ip.c_str(), reply_to);
Log::Info("Starting streaming to %s:%d", sender_ip.c_str(), conf.port);
#endif