private void ReConnectToRserve() { while (RserveToken != null && !RserveToken.IsCancellationRequested) { try { if (Connection == null) { string userLogString = "empty user"; if (Parameter.Credentials != null) { userLogString = Parameter.User; } RConnection con = null; if (Parameter.UseIpAddress) { con = RConnection.Connect(Parameter.IpAddress, Parameter.Port, Parameter.Credentials); logger.Info($"Connected to RServe {Parameter.IpAddress}:{Parameter.Port} with user ({userLogString})"); } else { con = RConnection.Connect(Parameter.Hostname, Parameter.Port, Parameter.Credentials); logger.Info($"Connected to RServe {Parameter.Hostname}:{Parameter.Port} with user ({userLogString})"); } Thread.Sleep(150); //Load Workspace if (!String.IsNullOrWhiteSpace(Parameter.InitScript)) { logger.Debug("Sending InitScript to Rserve..."); con.Eval(Parameter.InitScript); logger.Debug("...InitScript done"); } Connection = con; } if (!Connection.IsConnected()) { Connection = null; } } catch (SocketException ex) { logger.Error($"Reconnect to Rserve failed (Socket error): {ex.Message}"); Connection = null; } catch (Exception ex) { logger.Error($"Reconnect to Rserve failed: {ex.Message}"); Connection = null; } finally { Thread.Sleep(5000); } } }
/// Try to get a real error message and stack trace; if that fails rethrow the original exception. private static void GetAndThrowRealError(RConnection conn, RserveException ex) { // Try to get the error message String msg; try { msg = conn.Eval("geterrmessage()").AsString; } catch { throw ex; } if (String.IsNullOrWhiteSpace(msg)) throw ex; // Try to get the stack trace // It's possible that geterrmessage() succeeds and traceback() fails. // If so just use the error message try { var tracebacks = conn.Eval("traceback()").AsStrings; var traceback = String.Join("\r\n", tracebacks); #if DEBUG msg = msg + traceback; #endif } catch { } // Throw with a helpful message throw new RserveException(msg); }