Commit f1cbf630 authored by moret's avatar moret
Browse files

Minor changes in netjack2

git-svn-id: http://subversion.jackaudio.org/jack/jack2/trunk/jackmp@2751 0c269be4-1314-0410-8aa9-9f06e86f4224
parent 69faf9a9
......@@ -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;
......
......@@ -58,7 +58,6 @@ namespace Jack
fSocket.CopyParams ( &fMasterManager->fSocket );
fNSubProcess = fParams.fPeriodSize / fParams.fFramesPerPacket;
fClientName = const_cast<char*> ( 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
......
......@@ -44,7 +44,6 @@ namespace Jack
session_params_t fParams;
JackNetSocket fSocket;
uint fNSubProcess;
int fNetJumpCnt;
bool fRunning;
//jack client
......
......@@ -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<float> ( params->fFramesPerPacket ) / static_cast<float> ( 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<float> ( params->fPeriodSize ) / static_cast<float> ( params->fSampleRate ) );
return ( socket->SetTimeOut ( time ) == SOCKET_ERROR ) ? SOCKET_ERROR : 0;
}
// Packet *******************************************************************************************************
......
......@@ -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<float>( 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;
......
......@@ -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();
......
......@@ -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*********************************************************************************************************
......
......@@ -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();
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment