/** * <summary> * Creates a connected facade and returns it. Called either from constructor or inside * a write lock.</summary> * * <param name="srvs">List of server addresses that this method will try to connect to.</param> * <returns>Connected client facade.</returns> * <exception cref="GridClientServerUnreachableException">If none of the servers can be reached.</exception> */ private C connect(ICollection <IPEndPoint> srvs) { Dbg.Assert(guard.IsReaderLockHeld); if (srvs.Count == 0) { throw new GridClientServerUnreachableException("Failed to establish connection to the grid node (address " + "list is empty)."); } IOException cause = null; foreach (IPEndPoint srv in srvs) { try { C cnn = connect(srv); Dbg.WriteLine("Connection successfully opened: " + srv); return(cnn); } catch (IOException e) { Dbg.WriteLine("Unable to connect to grid node [srvAddr=" + srv + ", msg=" + e.Message + ']'); cause = e; } } A.NotNull(cause, "cause"); throw new GridClientServerUnreachableException("Failed to connect to any of the servers in list: " + String.Join <IPEndPoint>(",", srvs), cause); }
/** * <summary> * Handles communication connection failure. Tries to reconnect to any of the servers specified and * throws an exception if none of the servers can be reached.</summary> * * <param name="node">Remote node for which this connection belongs to.</param> * <param name="conn">Facade that caused the exception.</param> * <param name="e">Exception.</param> */ public void onFacadeFailed(IGridClientNode node, C conn, GridClientConnectionResetException e) { Dbg.WriteLine("Connection with remote node was terminated" + "[node=" + node + ", srvAddr=" + conn.ServerAddress + ", errMsg=" + e.Message + ']'); guard.AcquireWriterLock(Timeout.Infinite); try { IPEndPoint srv = conn.ServerAddress; C cached; if (conns.TryGetValue(srv, out cached) && cached.Equals(conn)) { conns.Remove(srv); } } finally { guard.ReleaseWriterLock(); } /* Close connection to failed node outside the writer lock. */ closeSilent(conn, false); }
/** * <summary> * Silent connection close.</summary> * * <param name="conn">Connection to close.</param> * <param name="waitCompletion">If <c>true</c> will wait for all pending requests to be proceeded.</param> */ private static void closeSilent(C conn, bool waitCompletion) { U.DoSilent<Exception>(() => conn.Close(waitCompletion), e => Dbg.WriteLine("Failed to close connection [conn=" + conn + ", e=" + e.Message + "]")); }
/** * <summary> * Handles communication connection failure. Tries to reconnect to any of the servers specified and * throws an exception if none of the servers can be reached.</summary> * * <param name="node">Remote node for which this connection belongs to.</param> * <param name="conn">Facade that caused the exception.</param> * <param name="e">Exception.</param> */ public void onFacadeFailed(IGridClientNode node, C conn, GridClientConnectionResetException e) { Dbg.WriteLine("Connection with remote node was terminated" + "[node=" + node + ", srvAddr=" + conn.ServerAddress + ", errMsg=" + e.Message + ']'); guard.AcquireWriterLock(Timeout.Infinite); try { IPEndPoint srv = conn.ServerAddress; C cached; if (conns.TryGetValue(srv, out cached) && cached.Equals(conn)) conns.Remove(srv); } finally { guard.ReleaseWriterLock(); } /* Close connection to failed node outside the writer lock. */ closeSilent(conn, false); }
/** * <summary> * Silent connection close.</summary> * * <param name="conn">Connection to close.</param> * <param name="waitCompletion">If <c>true</c> will wait for all pending requests to be proceeded.</param> */ private static void closeSilent(C conn, bool waitCompletion) { U.DoSilent <Exception>(() => conn.Close(waitCompletion), e => Dbg.WriteLine("Failed to close connection [conn=" + conn + ", e=" + e.Message + "]")); }