From 49cb493b7e4c09f1fba28ab82d07655426c2bdcd Mon Sep 17 00:00:00 2001
From: Thulinma <jaron@vietors.com>
Date: Wed, 14 Sep 2016 19:39:58 +0200
Subject: [PATCH] Improved output connect to input timeout behaviour

---
 src/output/output.cpp | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/src/output/output.cpp b/src/output/output.cpp
index 618c7af4..b103cf65 100644
--- a/src/output/output.cpp
+++ b/src/output/output.cpp
@@ -167,22 +167,30 @@ namespace Mist {
     if (statsPage.getData()){
       statsPage.finish();
     }
-    statsPage = IPC::sharedClient(SHM_STATISTICS, STAT_EX_SIZE, true);
     if (nProxy.userClient.getData()){
       nProxy.userClient.finish();
     }
     char userPageName[NAME_BUFFER_SIZE];
     snprintf(userPageName, NAME_BUFFER_SIZE, SHM_USERS, streamName.c_str());
-    nProxy.userClient = IPC::sharedClient(userPageName, PLAY_EX_SIZE, true);
+    unsigned int attempts = 0;
+    while (!nProxy.userClient.isAlive() && ++attempts < 20 && Util::streamAlive(streamName)){
+      nProxy.userClient = IPC::sharedClient(userPageName, PLAY_EX_SIZE, true);
+    }
+    if (!nProxy.userClient.isAlive()){
+      FAIL_MSG("Could not register as client for %s", streamName.c_str());
+      onFail();
+      return;
+    }
     char pageId[NAME_BUFFER_SIZE];
     snprintf(pageId, NAME_BUFFER_SIZE, SHM_STREAM_INDEX, streamName.c_str());
     nProxy.metaPages.clear();
     nProxy.metaPages[0].init(pageId, DEFAULT_STRM_PAGE_SIZE);
     if (!nProxy.metaPages[0].mapped){
-      FAIL_MSG("Could not connect to server for %s", streamName.c_str());
+      FAIL_MSG("Could not connect to data for %s", streamName.c_str());
       onFail();
       return;
     }
+    statsPage = IPC::sharedClient(SHM_STATISTICS, STAT_EX_SIZE, true);
     stats(true);
     updateMeta();
     selectDefaultTracks();