From f1cbf630d5fcaf77a40dee00ed0284b547063fdb Mon Sep 17 00:00:00 2001 From: moret Date: Thu, 31 Jul 2008 07:43:17 +0000 Subject: [PATCH] Minor changes in netjack2 git-svn-id: http://subversion.jackaudio.org/jack/jack2/trunk/jackmp@2751 0c269be4-1314-0410-8aa9-9f06e86f4224 --- common/JackNetDriver.cpp | 26 ++++++++-------- common/JackNetManager.cpp | 57 ++++++++++++++++-------------------- common/JackNetManager.h | 1 - common/JackNetTool.cpp | 7 ++--- common/JackNetUnixSocket.cpp | 7 +++-- common/JackNetUnixSocket.h | 2 +- windows/JackNetWinSocket.cpp | 5 ++-- windows/JackNetWinSocket.h | 2 +- 8 files changed, 52 insertions(+), 55 deletions(-) diff --git a/common/JackNetDriver.cpp b/common/JackNetDriver.cpp index beef243b..19666241 100644 --- a/common/JackNetDriver.cpp +++ b/common/JackNetDriver.cpp @@ -185,7 +185,7 @@ namespace Jack jack_log ( "JackNetDriver::GetNetMaster()" ); //utility session_params_t params; - int ms_timeout = 2000; + float ms_timeout = 2000.f; int rx_bytes = 0; //socket @@ -536,24 +536,24 @@ namespace Jack for ( audio_port_index = 0; audio_port_index < fCaptureChannels; audio_port_index++ ) fNetAudioCaptureBuffer->SetBuffer ( audio_port_index, GetInputBuffer ( audio_port_index ) ); -#ifdef JACK_MONITOR - fMeasureId = 0; -#endif - //receive sync (launch the cycle) do { - rx_bytes = Recv ( fParams.fMtu, 0 ); - //no sync received during timeout, - //if it's a connection issue, send will detect it, so don't skip the cycle (return 0) + rx_bytes = Recv ( sizeof ( packet_header_t ), 0 ); + //connection issue, send will detect it, so don't skip the cycle (return 0) if ( rx_bytes == SOCKET_ERROR ) return 0; } while ( !rx_bytes && ( rx_head->fDataType != 's' ) ); +#ifdef JACK_MONITOR + fMeasureId = 0; + fMeasure[fMeasureId++] = ( ( float ) ( GetMicroSeconds() - JackDriver::fBeginDateUst ) / ( float ) fEngineControl->fPeriodUsecs ) * 100.f; +#endif //take the time at the beginning of the cycle JackDriver::CycleTakeBeginTime(); + //audio, midi or sync if driver is late if ( fParams.fSendMidiChannels || fParams.fSendAudioChannels ) { @@ -569,6 +569,7 @@ namespace Jack { case 'm': //midi rx_bytes = Recv ( rx_bytes, 0 ); + fRxHeader.fCycle = rx_head->fCycle; fRxHeader.fIsLastPckt = rx_head->fIsLastPckt; fNetMidiCaptureBuffer->RenderFromNetwork ( rx_head->fSubCycle, rx_bytes - sizeof ( packet_header_t ) ); if ( ++recvd_midi_pckt == rx_head->fNMidiPckt ) @@ -620,16 +621,17 @@ namespace Jack fNetAudioPlaybackBuffer->SetBuffer ( audio_port_index, GetOutputBuffer ( audio_port_index ) ); #ifdef JACK_MONITOR - fMeasure[fMeasureId++] = ( ( float ) ( GetMicroSeconds() - JackDriver::fBeginDateUst ) / ( float ) fEngineControl->fPeriodUsecs ) * 100.f; + fMeasure[fMeasureId++] = ( ( float ) ( GetMicroSeconds() - fMonTimeRef ) / ( float ) fEngineControl->fPeriodUsecs ) * 100.f; #endif //sync fTxHeader.fDataType = 's'; if ( !fParams.fSendMidiChannels && !fParams.fSendAudioChannels ) fTxHeader.fIsLastPckt = 'y'; - memset ( fTxData, 0, fPayloadSize ); - SetSyncPacket(); - tx_bytes = Send ( fParams.fMtu, 0 ); + memcpy ( fTxBuffer, &fTxHeader, sizeof ( packet_header_t ) ); + //memset ( fTxData, 0, fPayloadSize ); + //SetSyncPacket(); + tx_bytes = Send ( sizeof ( packet_header_t ), 0 ); if ( tx_bytes == SOCKET_ERROR ) return tx_bytes; diff --git a/common/JackNetManager.cpp b/common/JackNetManager.cpp index 21488e38..373b3493 100644 --- a/common/JackNetManager.cpp +++ b/common/JackNetManager.cpp @@ -58,7 +58,6 @@ namespace Jack fSocket.CopyParams ( &fMasterManager->fSocket ); fNSubProcess = fParams.fPeriodSize / fParams.fFramesPerPacket; fClientName = const_cast ( fParams.fName ); - fNetJumpCnt = 0; fJackClient = NULL; fRunning = false; fSyncState = 1; @@ -164,7 +163,7 @@ namespace Jack { jack_log ( "JackNetMaster::Init, ID %u.", fParams.fID ); session_params_t params; - int msec_timeout = 1000; + float msec_timeout = 1000.f; uint attempt = 0; int rx_bytes = 0; @@ -322,7 +321,7 @@ namespace Jack { if ( fParams.fTransportSync ) { - //set the TransportData + //TODO : set the TransportData //copy to TxBuffer memcpy ( fTxData, &fTransportData, sizeof ( net_transport_data_t ) ); @@ -336,14 +335,14 @@ namespace Jack if ( ( tx_bytes = fSocket.Send ( buffer, size, flags ) ) == SOCKET_ERROR ) { net_error_t error = fSocket.GetError(); - if ( error == NET_CONN_ERROR ) + if ( fRunning && ( error == NET_CONN_ERROR ) ) { //fatal connection issue, exit jack_error ( "'%s' : %s, please check network connection with '%s'.", fParams.fName, StrError ( NET_ERROR_CODE ), fParams.fSlaveNetName ); Exit(); } - else + else if ( fRunning ) jack_error ( "Error in send : %s", StrError ( NET_ERROR_CODE ) ); } return tx_bytes; @@ -357,16 +356,11 @@ namespace Jack net_error_t error = fSocket.GetError(); if ( error == NET_NO_DATA ) { - //too much receive failure, react - if ( ++fNetJumpCnt < 100 ) - return 0; - else - { - jack_error ( "No data from %s...", fParams.fName ); - fNetJumpCnt = 0; - } + //no data isn't really a network error, so just return 0 avalaible read bytes + jack_error ( "No data from %s...", fParams.fName ); + return 0; } - else if ( error == NET_CONN_ERROR ) + else if ( fRunning && ( error == NET_CONN_ERROR ) ) { //fatal connection issue, exit jack_error ( "'%s' : %s, network connection with '%s' broken, exiting.", @@ -374,7 +368,7 @@ namespace Jack //ask to the manager to properly remove the master Exit(); } - else + else if ( fRunning ) jack_error ( "Error in receive : %s", StrError ( NET_ERROR_CODE ) ); } return rx_bytes; @@ -391,7 +385,9 @@ namespace Jack if ( !fRunning ) return 0; - int tx_bytes, rx_bytes, copy_size; + int tx_bytes = 0; + int rx_bytes = 0; + int copy_size = 0; size_t midi_recvd_pckt = 0; fTxHeader.fCycle++; fTxHeader.fSubCycle = 0; @@ -427,9 +423,10 @@ namespace Jack fTxHeader.fDataType = 's'; if ( !fParams.fSendMidiChannels && !fParams.fSendAudioChannels ) fTxHeader.fIsLastPckt = 'y'; - memset ( fTxData, 0, fPayloadSize ); - SetSyncPacket(); - tx_bytes = Send ( fTxBuffer, fParams.fMtu, 0 ); + memcpy ( fTxBuffer, &fTxHeader, sizeof ( packet_header_t ) ); + //memset ( fTxData, 0, fPayloadSize ); + //SetSyncPacket(); + tx_bytes = Send ( fTxBuffer, sizeof ( packet_header_t ), 0 ); if ( tx_bytes == SOCKET_ERROR ) return tx_bytes; @@ -479,13 +476,11 @@ namespace Jack //receive -------------------------------------------------------------------------------------------------------------------- //sync - do - { - rx_bytes = Recv ( fParams.fMtu, 0 ); - if ( rx_bytes == SOCKET_ERROR ) - return rx_bytes; - } - while ( !rx_bytes && ( rx_head->fDataType != 's' ) ); + rx_bytes == Recv ( sizeof ( packet_header_t ), 0 ); + //if there is no data to be read, there could be stg wron with the slave, so just finish the cycle while returning 0 to start a new one + //if it's a network error, just return SOCKET_ERROR + if ( ( rx_bytes == 0 ) || ( rx_bytes == SOCKET_ERROR ) ) + return rx_bytes; #ifdef JACK_MONITOR fMeasure[fMeasureId++] = ( ( ( float ) ( jack_get_time() - begin_time ) ) / ( float ) fPeriodUsecs ) * 100.f; @@ -495,7 +490,8 @@ namespace Jack { do { - if ( ( rx_bytes = Recv ( fParams.fMtu, MSG_PEEK ) ) == SOCKET_ERROR ) + rx_bytes = Recv ( fParams.fMtu, MSG_PEEK ); + if ( ( rx_bytes == 0 ) || ( rx_bytes == SOCKET_ERROR ) ) return rx_bytes; if ( rx_bytes && ( rx_head->fDataStream == 'r' ) && ( rx_head->fID == fParams.fID ) ) { @@ -508,7 +504,6 @@ namespace Jack fNetMidiPlaybackBuffer->RenderFromNetwork ( rx_head->fSubCycle, rx_bytes - sizeof ( packet_header_t ) ); if ( ++midi_recvd_pckt == rx_head->fNMidiPckt ) fNetMidiPlaybackBuffer->RenderToJackPorts(); - fNetJumpCnt = 0; break; case 'a': //audio rx_bytes = Recv ( fAudioRxLen, 0 ); @@ -518,11 +513,11 @@ namespace Jack fRxHeader.fSubCycle = rx_head->fSubCycle; fRxHeader.fIsLastPckt = rx_head->fIsLastPckt; fNetAudioPlaybackBuffer->RenderToJackPorts ( rx_head->fSubCycle ); - fNetJumpCnt = 0; break; case 's': //sync rx_bytes = Recv ( rx_bytes, 0 ); - jack_error ( "NetMaster receive sync packets instead of data." ); + fRxHeader.fCycle = rx_head->fCycle; + fRxHeader.fSubCycle = rx_head->fSubCycle; return 0; } } @@ -620,7 +615,7 @@ namespace Jack { jack_log ( "JackNetMasterManager::Run" ); //utility variables - int msec_timeout = 2000; + float msec_timeout = 2000.f; int attempt = 0; //data diff --git a/common/JackNetManager.h b/common/JackNetManager.h index d475d76e..44575d2e 100644 --- a/common/JackNetManager.h +++ b/common/JackNetManager.h @@ -44,7 +44,6 @@ namespace Jack session_params_t fParams; JackNetSocket fSocket; uint fNSubProcess; - int fNetJumpCnt; bool fRunning; //jack client diff --git a/common/JackNetTool.cpp b/common/JackNetTool.cpp index bd41fe25..0c048ea0 100644 --- a/common/JackNetTool.cpp +++ b/common/JackNetTool.cpp @@ -359,10 +359,9 @@ namespace Jack EXPORT int SetRxTimeout ( JackNetSocket* socket, session_params_t* params ) { - //time in ms : 1,25 * 'one packet time' - float time = 1250 * ( static_cast ( params->fFramesPerPacket ) / static_cast ( params->fSampleRate ) ); - int ms = ( int ) time; - return ( socket->SetTimeOut ( ms ) == SOCKET_ERROR ) ? SOCKET_ERROR : ms; + //time in ms, 1.25x cycle duration + float time = 1250 * ( static_cast ( params->fPeriodSize ) / static_cast ( params->fSampleRate ) ); + return ( socket->SetTimeOut ( time ) == SOCKET_ERROR ) ? SOCKET_ERROR : 0; } // Packet ******************************************************************************************************* diff --git a/common/JackNetUnixSocket.cpp b/common/JackNetUnixSocket.cpp index b9147aae..ed402280 100644 --- a/common/JackNetUnixSocket.cpp +++ b/common/JackNetUnixSocket.cpp @@ -37,6 +37,7 @@ namespace Jack JackNetUnixSocket::JackNetUnixSocket() { fSockfd = 0; + fPort = 0; fSendAddr.sin_family = AF_INET; fSendAddr.sin_addr.s_addr = htonl ( INADDR_ANY ); memset ( &fSendAddr.sin_zero, 0, 8 ); @@ -198,7 +199,7 @@ namespace Jack } //timeout************************************************************************************************************* - int JackNetUnixSocket::SetTimeOut ( int& msec ) + int JackNetUnixSocket::SetTimeOut ( float& msec ) { //negative timeout, or exceeding 10s, return if ( ( msec < 0 ) || ( msec > 10000 ) ) @@ -208,13 +209,13 @@ namespace Jack if ( msec < 1000 ) { timeout.tv_sec = 0; - timeout.tv_usec = msec * 1000; + timeout.tv_usec = ( int ) ( msec * 1000.f ); return SetOption ( SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof ( timeout ) ); } //more than 1sec if ( msec >= 1000 ) { - float sec = static_cast( msec ) / 1000.0f; + float sec = msec / 1000.0f; timeout.tv_sec = ( int ) sec; float usec = ( sec - timeout.tv_sec ) * 1000000; timeout.tv_usec = ( int ) usec; diff --git a/common/JackNetUnixSocket.h b/common/JackNetUnixSocket.h index 2623a1e5..244cef4a 100644 --- a/common/JackNetUnixSocket.h +++ b/common/JackNetUnixSocket.h @@ -80,7 +80,7 @@ namespace Jack int GetOption ( int level, int optname, void* optval, socklen_t* optlen ); //timeout - int SetTimeOut ( int& msec ); + int SetTimeOut ( float& msec ); //local loop int SetLocalLoop(); diff --git a/windows/JackNetWinSocket.cpp b/windows/JackNetWinSocket.cpp index 4adc1cf8..a511614d 100644 --- a/windows/JackNetWinSocket.cpp +++ b/windows/JackNetWinSocket.cpp @@ -263,12 +263,13 @@ namespace Jack } //tiemout************************************************************************************************************ - int JackNetWinSocket::SetTimeOut ( int& msec ) + int JackNetWinSocket::SetTimeOut ( float& msec ) { //negative timeout, or exceeding 10s, return if ( ( msec < 0 ) || ( msec > 10000 ) ) return -1; - return SetOption ( SOL_SOCKET, SO_RCVTIMEO, &msec, sizeof ( msec ) ); + int time = ( int ) msec; + return SetOption ( SOL_SOCKET, SO_RCVTIMEO, &time, sizeof ( time ) ); } //local loop********************************************************************************************************* diff --git a/windows/JackNetWinSocket.h b/windows/JackNetWinSocket.h index 39dea83f..51c9fd58 100644 --- a/windows/JackNetWinSocket.h +++ b/windows/JackNetWinSocket.h @@ -81,7 +81,7 @@ namespace Jack int GetOption ( int level, int optname, void* optval, SOCKLEN* optlen ); //timeout - int SetTimeOut ( int& msec ); + int SetTimeOut ( float& msec ); //local loop int SetLocalLoop();