diff --git a/common/JackNetAdapter.cpp b/common/JackNetAdapter.cpp index 4c3f730d..cf18550e 100644 --- a/common/JackNetAdapter.cpp +++ b/common/JackNetAdapter.cpp @@ -36,6 +36,9 @@ namespace Jack jack_error ( "Can't init Socket API, exiting..." ); //global parametering + //we can't call JackNetSlaveInterface constructor with some parameters before + //because we don't have full parametering right now + //parameters will be parsed from the param list, and then JackNetSlaveInterface will be filled with proper values fMulticastIP = new char[16]; strcpy ( fMulticastIP, DEFAULT_MULTICAST_IP ); uint port = DEFAULT_PORT; @@ -101,12 +104,15 @@ namespace Jack } } + //set the socket parameters fSocket.SetPort ( port ); fSocket.SetAddress ( fMulticastIP, port ); + //set the audio adapter interface channel values SetInputs ( fParams.fSendAudioChannels ); SetOutputs ( fParams.fReturnAudioChannels ); + //soft buffers will be allocated later (once network initialization done) fSoftCaptureBuffer = NULL; fSoftPlaybackBuffer = NULL; } @@ -355,6 +361,8 @@ namespace Jack //read/write operations--------------------------------------------------------------- int JackNetAdapter::Read() { + //don't return -1 in case of sync recv failure + //we need the process to continue for network error detection if ( SyncRecv() == SOCKET_ERROR ) return 0; diff --git a/common/JackNetDriver.cpp b/common/JackNetDriver.cpp index 196268b4..bf4683e1 100644 --- a/common/JackNetDriver.cpp +++ b/common/JackNetDriver.cpp @@ -115,7 +115,7 @@ namespace Jack //init network if ( !JackNetSlaveInterface::Init() ) - return false;; + return false; //set global paramaters SetParams(); @@ -488,6 +488,7 @@ namespace Jack JackDriver::CycleTakeBeginTime(); //decode sync + //if there is an error, don't return -1, it will skip Write() and the network error probably won't be identified if ( DecodeSyncPacket() < 0 ) return 0; diff --git a/common/JackNetInterface.cpp b/common/JackNetInterface.cpp index 532ec6f7..6b102b28 100644 --- a/common/JackNetInterface.cpp +++ b/common/JackNetInterface.cpp @@ -67,6 +67,7 @@ namespace Jack JackNetInterface::~JackNetInterface() { jack_log ( "JackNetInterface::~JackNetInterface" ); + fSocket.Close(); delete[] fTxBuffer; delete[] fRxBuffer; @@ -247,14 +248,13 @@ namespace Jack int JackNetMasterInterface::SetRxTimeout() { + jack_log ( "JackNetMasterInterface::SetRxTimeout" ); + float time = 0; - //slow mode, very short timeout on recv - if ( fParams.fNetworkMode == 's' ) + //slow or normal mode, short timeout on recv (2 audio subcycles) + if ( ( fParams.fNetworkMode == 's' ) || ( fParams.fNetworkMode == 'n' ) ) time = 2000000.f * ( static_cast ( fParams.fFramesPerPacket ) / static_cast ( fParams.fSampleRate ) ); - //normal mode, short timeout on recv - else if ( fParams.fNetworkMode == 'n' ) - time = 4000000.f * ( static_cast ( fParams.fFramesPerPacket ) / static_cast ( fParams.fSampleRate ) ); - //fast mode, wait for the entire cycle duration + //fast mode, wait for 75% of the entire cycle duration else if ( fParams.fNetworkMode == 'f' ) time = 750000.f * ( static_cast ( fParams.fPeriodSize ) / static_cast ( fParams.fSampleRate ) ); return fSocket.SetTimeOut ( static_cast ( time ) ); @@ -302,16 +302,16 @@ namespace Jack int JackNetMasterInterface::Send ( size_t size, int flags ) { int tx_bytes; - if ( ( tx_bytes = fSocket.Send ( fTxBuffer, size, flags ) ) == SOCKET_ERROR ) + if ( ( ( tx_bytes = fSocket.Send ( fTxBuffer, size, flags ) ) == SOCKET_ERROR ) && fRunning ) { net_error_t error = fSocket.GetError(); - if ( fRunning && ( error == NET_CONN_ERROR ) ) + if ( error == NET_CONN_ERROR ) { //fatal connection issue, exit jack_error ( "'%s' : %s, exiting.", fParams.fName, StrError ( NET_ERROR_CODE ) ); Exit(); } - else if ( fRunning ) + else jack_error ( "Error in send : %s", StrError ( NET_ERROR_CODE ) ); } return tx_bytes; @@ -320,20 +320,20 @@ namespace Jack int JackNetMasterInterface::Recv ( size_t size, int flags ) { int rx_bytes; - if ( ( rx_bytes = fSocket.Recv ( fRxBuffer, size, flags ) ) == SOCKET_ERROR ) + if ( ( ( rx_bytes = fSocket.Recv ( fRxBuffer, size, flags ) ) == SOCKET_ERROR ) && fRunning ) { net_error_t error = fSocket.GetError(); //no data isn't really a network error, so just return 0 avalaible read bytes if ( error == NET_NO_DATA ) return 0; - else if ( fRunning && ( error == NET_CONN_ERROR ) ) + else if ( error == NET_CONN_ERROR ) { //fatal connection issue, exit jack_error ( "'%s' : %s, exiting.", fParams.fName, StrError ( NET_ERROR_CODE ) ); //ask to the manager to properly remove the master Exit(); } - else if ( fRunning ) + else jack_error ( "Error in receive : %s", StrError ( NET_ERROR_CODE ) ); } return rx_bytes; @@ -494,6 +494,8 @@ namespace Jack // JackNetSlaveInterface ************************************************************************************************ + uint JackNetSlaveInterface::fSlaveCounter = 0; + bool JackNetSlaveInterface::Init() { jack_log ( "JackNetSlaveInterface::Init()" ); @@ -533,7 +535,6 @@ namespace Jack //utility session_params_t params; int rx_bytes = 0; - unsigned char loop = 0; //socket if ( fSocket.NewSocket() == SOCKET_ERROR ) @@ -551,7 +552,7 @@ namespace Jack jack_error ( "Can't set timeout : %s", StrError ( NET_ERROR_CODE ) ); //disable local loop - if ( fSocket.SetOption ( IPPROTO_IP, IP_MULTICAST_LOOP, &loop, sizeof ( loop ) ) == SOCKET_ERROR ) + if ( fSocket.SetLocalLoop() == SOCKET_ERROR ) jack_error ( "Can't disable multicast loop : %s", StrError ( NET_ERROR_CODE ) ); //send 'AVAILABLE' until 'SLAVE_SETUP' received diff --git a/common/JackNetInterface.h b/common/JackNetInterface.h index 96176fbd..a0ae39ae 100644 --- a/common/JackNetInterface.h +++ b/common/JackNetInterface.h @@ -130,6 +130,8 @@ namespace Jack class EXPORT JackNetSlaveInterface : public JackNetInterface { protected: + static uint fSlaveCounter; + bool Init(); net_status_t GetNetMaster(); net_status_t SendStartToMaster(); @@ -144,12 +146,18 @@ namespace Jack public: JackNetSlaveInterface() : JackNetInterface() - {} + { + fSlaveCounter++; + } JackNetSlaveInterface ( const char* ip, int port ) : JackNetInterface ( ip, port ) - {} + { + fSlaveCounter++; + } ~JackNetSlaveInterface() { - SocketAPIEnd(); + //close Socket API with the last slave + if ( --fSlaveCounter == 0 ) + SocketAPIEnd(); } }; } diff --git a/common/JackNetManager.cpp b/common/JackNetManager.cpp index 894a07ad..a5899b70 100644 --- a/common/JackNetManager.cpp +++ b/common/JackNetManager.cpp @@ -322,6 +322,7 @@ namespace Jack break; case JackTransportNetStarting : jack_info ( "'%s' is ready to roll..", fParams.fName ); + break; case JackTransportRolling : jack_info ( "'%s' is rolling.", fParams.fName ); break; @@ -362,7 +363,7 @@ namespace Jack //copy to TxBuffer memcpy ( fTxData, &fSendTransportData, sizeof ( net_transport_data_t ) ); } - //then others + //then others (freewheel etc.) //... return 0; } @@ -571,7 +572,7 @@ namespace Jack } //bind the socket to the local port - if ( fSocket.Bind () == SOCKET_ERROR ) + if ( fSocket.Bind() == SOCKET_ERROR ) { jack_error ( "Can't bind the network manager socket : %s", StrError ( NET_ERROR_CODE ) ); fSocket.Close(); diff --git a/common/JackNetUnixSocket.cpp b/common/JackNetUnixSocket.cpp index 54536a78..301e4bc5 100644 --- a/common/JackNetUnixSocket.cpp +++ b/common/JackNetUnixSocket.cpp @@ -200,13 +200,6 @@ namespace Jack return SetOption ( IPPROTO_IP, IP_ADD_MEMBERSHIP, &multicast_req, sizeof ( multicast_req ) ); } - void JackNetUnixSocket::CopyParams ( JackNetUnixSocket* socket ) - { - fPort = socket->fPort; - fSendAddr = socket->fSendAddr; - fRecvAddr = socket->fRecvAddr; - } - //options************************************************************************************************************ int JackNetUnixSocket::SetOption ( int level, int optname, const void* optval, socklen_t optlen ) { @@ -227,6 +220,7 @@ namespace Jack if ( ( us < 0 ) || ( us > 10000000 ) ) return SOCKET_ERROR; struct timeval timeout; + //less than 1sec if ( us < 1000000 ) { diff --git a/common/JackNetUnixSocket.h b/common/JackNetUnixSocket.h index 7f4a0d62..00988897 100644 --- a/common/JackNetUnixSocket.h +++ b/common/JackNetUnixSocket.h @@ -76,7 +76,6 @@ namespace Jack //utility int GetName ( char* name ); int JoinMCastGroup ( const char* mcast_ip ); - void CopyParams ( JackNetUnixSocket* socket ); //options management int SetOption ( int level, int optname, const void* optval, socklen_t optlen ); @@ -85,7 +84,7 @@ namespace Jack //timeout int SetTimeOut ( int us ); - //local loop + //disable local loop int SetLocalLoop(); //network operations diff --git a/windows/JackNetWinSocket.cpp b/windows/JackNetWinSocket.cpp index 40042fa0..42d08b45 100644 --- a/windows/JackNetWinSocket.cpp +++ b/windows/JackNetWinSocket.cpp @@ -290,8 +290,8 @@ namespace Jack //local loop********************************************************************************************************* int JackNetWinSocket::SetLocalLoop() { - char enable = 1; - return SetOption ( IPPROTO_IP, IP_MULTICAST_LOOP, &enable, sizeof ( enable ) ); + char disable = 0; + return SetOption ( IPPROTO_IP, IP_MULTICAST_LOOP, &disable, sizeof ( disable ) ); } //network operations************************************************************************************************* diff --git a/windows/JackNetWinSocket.h b/windows/JackNetWinSocket.h index 79b36e4c..d7a9982b 100644 --- a/windows/JackNetWinSocket.h +++ b/windows/JackNetWinSocket.h @@ -85,7 +85,7 @@ namespace Jack //timeout int SetTimeOut ( int usec ); - //local loop + //disable local loop int SetLocalLoop(); //network operations diff --git a/windows/libjackserver.cbp b/windows/libjackserver.cbp index 2974491f..a8bb2f69 100644 --- a/windows/libjackserver.cbp +++ b/windows/libjackserver.cbp @@ -153,7 +153,7 @@ - +