From e0135c87cc73a550d97103d535eec68093f99475 Mon Sep 17 00:00:00 2001 From: Stephane Letz Date: Sun, 12 May 2013 22:16:06 +0200 Subject: [PATCH] Add NETWORK_RESYNCH_LATENCY parameter to resynch in netjack2. --- common/JackNetAPI.cpp | 14 +++++++------- common/JackNetDriver.cpp | 4 ++-- common/JackNetInterface.cpp | 15 +++++++++++---- common/JackNetInterface.h | 25 ++++++++++++++++++------- common/JackNetManager.cpp | 2 +- common/JackNetTool.cpp | 3 +-- common/JackNetTool.h | 4 ++-- 7 files changed, 42 insertions(+), 25 deletions(-) diff --git a/common/JackNetAPI.cpp b/common/JackNetAPI.cpp index 4cf204b1..553edf2b 100644 --- a/common/JackNetAPI.cpp +++ b/common/JackNetAPI.cpp @@ -423,11 +423,11 @@ struct JackNetExtMaster : public JackNetMasterInterface { return res; case NET_PACKET_ERROR: - // Since sync packet is incorrect, don't decode it and continue with data + // since sync packet is incorrect, don't decode it and continue with data break; default: - //decode sync + // decode sync DecodeSyncPacket(); break; } @@ -731,7 +731,7 @@ struct JackNetExtSlave : public JackNetSlaveInterface, public JackRunnableInterf bool Execute() { try { - // Keep running even in case of error + // keep running even in case of error while (fThread.GetStatus() == JackThread::kRunning) { if (Process() == SOCKET_ERROR) { return false; @@ -739,7 +739,7 @@ struct JackNetExtSlave : public JackNetSlaveInterface, public JackRunnableInterf } return false; } catch (JackNetException& e) { - // Otherwise just restart... + // otherwise just restart... e.PrintMessage(); jack_info("NetSlave is restarted"); fThread.DropRealTime(); @@ -756,18 +756,18 @@ struct JackNetExtSlave : public JackNetSlaveInterface, public JackRunnableInterf int Read() { - //receive sync (launch the cycle) + // receive sync (launch the cycle) switch (SyncRecv()) { case SOCKET_ERROR: return SOCKET_ERROR; case NET_PACKET_ERROR: - // Since sync packet is incorrect, don't decode it and continue with data + // since sync packet is incorrect, don't decode it and continue with data break; default: - //decode sync + // decode sync DecodeSyncPacket(); break; } diff --git a/common/JackNetDriver.cpp b/common/JackNetDriver.cpp index 8f71367a..d213aac4 100644 --- a/common/JackNetDriver.cpp +++ b/common/JackNetDriver.cpp @@ -557,11 +557,11 @@ namespace Jack return SOCKET_ERROR; case NET_PACKET_ERROR: - // Since sync packet is incorrect, don't decode it and continue with data + // since sync packet is incorrect, don't decode it and continue with data break; default: - //decode sync + // decode sync DecodeSyncPacket(); break; } diff --git a/common/JackNetInterface.cpp b/common/JackNetInterface.cpp index 64126a52..9f3c6ff6 100644 --- a/common/JackNetInterface.cpp +++ b/common/JackNetInterface.cpp @@ -485,17 +485,24 @@ namespace Jack if (rx_head->fDataType != 's') { jack_error("Wrong packet type : %c", rx_head->fDataType); - // Not the last packet.. + // not the last packet.. fRxHeader.fIsLastPckt = 0; return NET_PACKET_ERROR; } fCurrentCycleOffset = fTxHeader.fCycle - rx_head->fCycle; - if (fCurrentCycleOffset < fMaxCycleOffset) { + if (fCurrentCycleOffset < fMaxCycleOffset && !fSynched) { jack_info("Synching with latency = %d", fCurrentCycleOffset); return 0; } else { + if (fCurrentCycleOffset == fMaxCycleOffset) { + // when the sync offset is reached + fSynched = true; + } else if (abs(fCurrentCycleOffset - fMaxCycleOffset) >= NETWORK_RESYNCH_LATENCY) { + jack_info("Resync connection..."); + fSynched = false; + } rx_bytes = Recv(rx_head->fPacketSize, 0); fRxHeader.fIsLastPckt = rx_head->fIsLastPckt; return rx_bytes; @@ -732,7 +739,7 @@ namespace Jack } while (strcmp(host_params.fPacketType, fParams.fPacketType) && (GetPacketType(&host_params) != SLAVE_SETUP) && (--try_count > 0)); - // Time out failure.. + // time out failure.. if (try_count == 0) { jack_error("Time out error in connect"); return NET_CONNECT_ERROR; @@ -873,7 +880,7 @@ namespace Jack if (rx_head->fDataType != 's') { jack_error("Wrong packet type : %c", rx_head->fDataType); - // Not the last packet... + // not the last packet... fRxHeader.fIsLastPckt = 0; return NET_PACKET_ERROR; } diff --git a/common/JackNetInterface.h b/common/JackNetInterface.h index 35ac3c38..cd36e475 100644 --- a/common/JackNetInterface.h +++ b/common/JackNetInterface.h @@ -32,12 +32,13 @@ namespace Jack #define SLAVE_SETUP_RETRY 5 -#define MANAGER_INIT_TIMEOUT 2000000 // in usec -#define MASTER_INIT_TIMEOUT 1000000 * 10 // in usec -#define SLAVE_INIT_TIMEOUT 1000000 * 10 // in usec -#define PACKET_TIMEOUT 500000 // in usec +#define MANAGER_INIT_TIMEOUT 1000000 * 2 // in usec +#define MASTER_INIT_TIMEOUT 1000000 * 10 // in usec +#define SLAVE_INIT_TIMEOUT 1000000 * 10 // in usec +#define PACKET_TIMEOUT 500000 // in usec -#define NETWORK_MAX_LATENCY 20 +#define NETWORK_MAX_LATENCY 20 // maximun possile latency in network master/slave loop +#define NETWORK_RESYNCH_LATENCY 3 // number of cycles offset before a resync is done.... /** \Brief This class describes the basic Net Interface, used by both master and slave. @@ -137,6 +138,7 @@ namespace Jack bool fRunning; int fCurrentCycleOffset; int fMaxCycleOffset; + bool fSynched; bool Init(); bool SetParams(); @@ -161,10 +163,19 @@ namespace Jack public: - JackNetMasterInterface() : JackNetInterface(), fRunning(false), fCurrentCycleOffset(0), fMaxCycleOffset(0) + JackNetMasterInterface() + : JackNetInterface(), + fRunning(false), + fCurrentCycleOffset(0), + fMaxCycleOffset(0), + fSynched(false) {} JackNetMasterInterface(session_params_t& params, JackNetSocket& socket, const char* multicast_ip) - : JackNetInterface(params, socket, multicast_ip), fRunning(false), fCurrentCycleOffset(0), fMaxCycleOffset(0) + : JackNetInterface(params, socket, multicast_ip), + fRunning(false), + fCurrentCycleOffset(0), + fMaxCycleOffset(0), + fSynched(false) {} virtual~JackNetMasterInterface() diff --git a/common/JackNetManager.cpp b/common/JackNetManager.cpp index d58b5e73..843e3890 100644 --- a/common/JackNetManager.cpp +++ b/common/JackNetManager.cpp @@ -524,7 +524,7 @@ namespace Jack return res; case NET_PACKET_ERROR: - // Since sync packet is incorrect, don't decode it and continue with data + // Since sync packet is incorrect, don't decode it and continue with data break; default: diff --git a/common/JackNetTool.cpp b/common/JackNetTool.cpp index 2e14457b..0dfaef67 100644 --- a/common/JackNetTool.cpp +++ b/common/JackNetTool.cpp @@ -680,7 +680,7 @@ namespace Jack Cleanup(); } - if (port_num > 0) { + if (port_num > 0) { int sub_period_bytes_size; @@ -937,7 +937,6 @@ namespace Jack #endif - NetIntAudioBuffer::NetIntAudioBuffer(session_params_t* params, uint32_t nports, char* net_buffer) : NetAudioBuffer(params, nports, net_buffer) { diff --git a/common/JackNetTool.h b/common/JackNetTool.h index 5f0b13c6..d8d47d22 100644 --- a/common/JackNetTool.h +++ b/common/JackNetTool.h @@ -38,8 +38,8 @@ using namespace std; #endif #endif -#define MASTER_PROTOCOL 6 -#define SLAVE_PROTOCOL 6 +#define MASTER_PROTOCOL 6 +#define SLAVE_PROTOCOL 6 #define NET_PACKET_ERROR -2