mirror of
https://github.com/brunoherbelin/vimix.git
synced 2025-12-12 10:49:59 +01:00
Connection manager seems to work...
This commit is contained in:
@@ -10,12 +10,17 @@
|
|||||||
#include "Connection.h"
|
#include "Connection.h"
|
||||||
#include "Log.h"
|
#include "Log.h"
|
||||||
|
|
||||||
|
|
||||||
|
#ifndef NDEBUG
|
||||||
|
#define CONNECTION_DEBUG
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
Connection::Connection()
|
Connection::Connection()
|
||||||
{
|
{
|
||||||
receiver_ = nullptr;
|
receiver_ = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool Connection::init()
|
bool Connection::init()
|
||||||
{
|
{
|
||||||
// add default info for myself
|
// add default info for myself
|
||||||
@@ -34,11 +39,10 @@ bool Connection::init()
|
|||||||
// through exception runtime if fails
|
// through exception runtime if fails
|
||||||
receiver_ = new UdpListeningReceiveSocket( IpEndpointName( IpEndpointName::ANY_ADDRESS,
|
receiver_ = new UdpListeningReceiveSocket( IpEndpointName( IpEndpointName::ANY_ADDRESS,
|
||||||
connections_[0].port_handshake ), &listener_ );
|
connections_[0].port_handshake ), &listener_ );
|
||||||
|
|
||||||
// all good
|
// all good
|
||||||
trial = MAX_HANDSHAKE;
|
trial = MAX_HANDSHAKE;
|
||||||
}
|
}
|
||||||
catch(const std::runtime_error& e) {
|
catch (const std::runtime_error& e) {
|
||||||
// arg, the receiver could not be initialized
|
// arg, the receiver could not be initialized
|
||||||
// because the port was not available
|
// because the port was not available
|
||||||
receiver_ = nullptr;
|
receiver_ = nullptr;
|
||||||
@@ -49,13 +53,10 @@ bool Connection::init()
|
|||||||
|
|
||||||
// perfect, we could initialize the receiver
|
// perfect, we could initialize the receiver
|
||||||
if (receiver_!=nullptr) {
|
if (receiver_!=nullptr) {
|
||||||
|
|
||||||
// listen for answers
|
// listen for answers
|
||||||
std::thread(listen).detach();
|
std::thread(listen).detach();
|
||||||
|
|
||||||
// regularly check for available streaming hosts
|
// regularly check for available streaming hosts
|
||||||
std::thread(ask).detach();
|
std::thread(ask).detach();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return receiver_ != nullptr;
|
return receiver_ != nullptr;
|
||||||
@@ -106,14 +107,14 @@ int Connection::index(ConnectionInfo i) const
|
|||||||
|
|
||||||
void Connection::listen()
|
void Connection::listen()
|
||||||
{
|
{
|
||||||
// Log::Info("Accepting handshake on %d", Connection::manager().connections_[0].port_handshake);
|
#ifdef CONNECTION_DEBUG
|
||||||
|
Log::Info("Accepting handshake on port %d", Connection::manager().connections_[0].port_handshake);
|
||||||
|
#endif
|
||||||
Connection::manager().receiver_->Run();
|
Connection::manager().receiver_->Run();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Connection::ask()
|
void Connection::ask()
|
||||||
{
|
{
|
||||||
// Log::Info("Broadcasting handshakes with info %d", Connection::manager().connections_[0].port_handshake);
|
|
||||||
|
|
||||||
// prepare OSC PING message
|
// prepare OSC PING message
|
||||||
char buffer[IP_MTU_SIZE];
|
char buffer[IP_MTU_SIZE];
|
||||||
osc::OutboundPacketStream p( buffer, IP_MTU_SIZE );
|
osc::OutboundPacketStream p( buffer, IP_MTU_SIZE );
|
||||||
@@ -132,12 +133,6 @@ void Connection::ask()
|
|||||||
// loop infinitely
|
// loop infinitely
|
||||||
while(true)
|
while(true)
|
||||||
{
|
{
|
||||||
|
|
||||||
// set status to pending : after pong status will be confirmed
|
|
||||||
for(auto it=Connection::manager().connections_.begin(); it!=Connection::manager().connections_.end(); it++) {
|
|
||||||
(*it).status--;
|
|
||||||
}
|
|
||||||
|
|
||||||
// broadcast the PING message on every possible ports
|
// broadcast the PING message on every possible ports
|
||||||
for(auto it=handshake_ports.begin(); it!=handshake_ports.end(); it++) {
|
for(auto it=handshake_ports.begin(); it!=handshake_ports.end(); it++) {
|
||||||
IpEndpointName host( "255.255.255.255", (*it) );
|
IpEndpointName host( "255.255.255.255", (*it) );
|
||||||
@@ -149,14 +144,22 @@ void Connection::ask()
|
|||||||
// wait a bit
|
// wait a bit
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(500));
|
std::this_thread::sleep_for(std::chrono::milliseconds(500));
|
||||||
|
|
||||||
// check if status decreased
|
// check the list of connections for non responding (disconnected)
|
||||||
for(auto it=Connection::manager().connections_.begin(); it!=Connection::manager().connections_.end(); ) {
|
for(auto it=Connection::manager().connections_.begin(); it!=Connection::manager().connections_.end(); ) {
|
||||||
if ( it!=Connection::manager().connections_.begin() && (*it).status < 1 )
|
// 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 ) {
|
||||||
it = Connection::manager().connections_.erase(it);
|
it = Connection::manager().connections_.erase(it);
|
||||||
|
#ifdef CONNECTION_DEBUG
|
||||||
|
Log::Info("A connection was lost");
|
||||||
|
Connection::manager().print();
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
// loop
|
||||||
else
|
else
|
||||||
it++;
|
it++;
|
||||||
}
|
}
|
||||||
// Connection::manager().print();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -179,8 +182,6 @@ void ConnectionRequestListener::ProcessMessage( const osc::ReceivedMessage& m,
|
|||||||
osc::ReceivedMessage::const_iterator arg = m.ArgumentsBegin();
|
osc::ReceivedMessage::const_iterator arg = m.ArgumentsBegin();
|
||||||
int remote_port = (arg++)->AsInt32();
|
int remote_port = (arg++)->AsInt32();
|
||||||
|
|
||||||
// Log::Info("Receive PING from %s:%d", remote_ip.c_str(), remote_port);
|
|
||||||
|
|
||||||
// build message
|
// build message
|
||||||
char buffer[IP_MTU_SIZE];
|
char buffer[IP_MTU_SIZE];
|
||||||
osc::OutboundPacketStream p( buffer, IP_MTU_SIZE );
|
osc::OutboundPacketStream p( buffer, IP_MTU_SIZE );
|
||||||
@@ -196,7 +197,6 @@ void ConnectionRequestListener::ProcessMessage( const osc::ReceivedMessage& m,
|
|||||||
UdpTransmitSocket socket( host );
|
UdpTransmitSocket socket( host );
|
||||||
socket.Send( p.Data(), p.Size() );
|
socket.Send( p.Data(), p.Size() );
|
||||||
|
|
||||||
// Log::Info("reply PONG to %s:%d", remote_ip.c_str(), remote_port);
|
|
||||||
}
|
}
|
||||||
// pong response: add info
|
// pong response: add info
|
||||||
else if( std::strcmp( m.AddressPattern(), OSC_PREFIX OSC_PONG) == 0 ){
|
else if( std::strcmp( m.AddressPattern(), OSC_PREFIX OSC_PONG) == 0 ){
|
||||||
@@ -211,17 +211,19 @@ void ConnectionRequestListener::ProcessMessage( const osc::ReceivedMessage& m,
|
|||||||
info.port_stream_send = (arg++)->AsInt32();
|
info.port_stream_send = (arg++)->AsInt32();
|
||||||
info.port_stream_receive = (arg++)->AsInt32();
|
info.port_stream_receive = (arg++)->AsInt32();
|
||||||
|
|
||||||
|
// do we know this connection ?
|
||||||
int i = Connection::manager().index(info);
|
int i = Connection::manager().index(info);
|
||||||
if ( i < 0) {
|
if ( i < 0) {
|
||||||
// add to list
|
// a new connection! Add to list
|
||||||
info.status = 3;
|
|
||||||
Connection::manager().connections_.push_back(info);
|
Connection::manager().connections_.push_back(info);
|
||||||
// Log::Info("Received PONG from %s:%d : added", info.address_.c_str(), info.port_handshake);
|
#ifdef CONNECTION_DEBUG
|
||||||
// Connection::manager().print();
|
Log::Info("New connection added");
|
||||||
|
Connection::manager().print();
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// set high (== ALIVE)
|
// we know this connection: keep its status to ALIVE
|
||||||
Connection::manager().connections_[i].status = 3;
|
Connection::manager().connections_[i].alive = ALIVE;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,8 @@
|
|||||||
|
|
||||||
#include "NetworkToolkit.h"
|
#include "NetworkToolkit.h"
|
||||||
|
|
||||||
|
#define ALIVE 3
|
||||||
|
|
||||||
class ConnectionRequestListener : public osc::OscPacketListener {
|
class ConnectionRequestListener : public osc::OscPacketListener {
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
@@ -20,14 +22,14 @@ struct ConnectionInfo {
|
|||||||
int port_handshake;
|
int port_handshake;
|
||||||
int port_stream_send;
|
int port_stream_send;
|
||||||
int port_stream_receive;
|
int port_stream_receive;
|
||||||
int status;
|
int alive;
|
||||||
|
|
||||||
ConnectionInfo () {
|
ConnectionInfo () {
|
||||||
address_ = "localhost";
|
address_ = "localhost";
|
||||||
port_handshake = HANDSHAKE_PORT;
|
port_handshake = HANDSHAKE_PORT;
|
||||||
port_stream_send = STREAM_REQUEST_PORT;
|
port_stream_send = STREAM_REQUEST_PORT;
|
||||||
port_stream_receive = STREAM_RESPONSE_PORT;
|
port_stream_receive = STREAM_RESPONSE_PORT;
|
||||||
status = 0;
|
alive = ALIVE;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline ConnectionInfo& operator = (const ConnectionInfo& o)
|
inline ConnectionInfo& operator = (const ConnectionInfo& o)
|
||||||
|
|||||||
Reference in New Issue
Block a user