public TcpClientChannel TryConnect(NetworkPath netPath, int timeoutInMsec, out NetworkTransportException failureEx) { TcpClientChannel result = null; TcpClientChannel.TryOpenChannel(netPath, timeoutInMsec, out result, out failureEx); return(result); }
internal static TcpClientChannel OpenChannel(NetworkPath netPath, int timeoutInMs) { NetworkTransportException ex = null; TcpClientChannel result = null; if (TcpClientChannel.TryOpenChannel(netPath, timeoutInMs, out result, out ex)) { return(result); } throw ex; }
internal static TcpClientChannel OpenConnection(ref NetworkPath actualPath, int timeoutInMsec, bool ignoreNodeDown) { NetworkPath networkPath = actualPath; NetworkTransportException ex = null; ITcpConnector tcpConnector = Dependencies.TcpConnector; TcpClientChannel tcpClientChannel = tcpConnector.TryConnect(networkPath, timeoutInMsec, out ex); if (tcpClientChannel != null) { return tcpClientChannel; } if (!networkPath.NetworkChoiceIsMandatory) { NetworkManager.TraceError("Attempting alternate routes", new object[0]); List<NetworkPath> list = null; ExchangeNetworkMap map = NetworkManager.GetMap(); if (map != null) { list = map.EnumeratePaths(networkPath.TargetNodeName, ignoreNodeDown); if (list != null) { NetworkPath networkPath2 = null; foreach (NetworkPath networkPath3 in list) { if (string.Equals(networkPath3.NetworkName, networkPath.NetworkName, DatabaseAvailabilityGroupNetwork.NameComparison)) { networkPath2 = networkPath3; break; } } if (networkPath2 != null) { list.Remove(networkPath2); } DagNetConfig dagConfig = DagNetEnvironment.FetchNetConfig(); foreach (NetworkPath networkPath4 in list) { networkPath4.Purpose = networkPath.Purpose; networkPath4.ApplyNetworkPolicy(dagConfig); tcpClientChannel = tcpConnector.TryConnect(networkPath, timeoutInMsec, out ex); if (tcpClientChannel != null) { actualPath = networkPath4; return tcpClientChannel; } } } } } throw ex; }
public TcpClientChannel OpenChannel(string targetServerName, ISimpleBufferPool socketStreamBufferPool, IPool <SocketStreamAsyncArgs> socketStreamAsyncArgPool, SocketStream.ISocketStreamPerfCounters perfCtrs, out NetworkPath netPath) { DagNetConfig dagConfig; DagNetRoute[] array = DagNetChooser.ProposeRoutes(targetServerName, out dagConfig); TcpClientChannel tcpClientChannel = null; netPath = null; NetworkTransportException ex = null; if (array != null) { foreach (DagNetRoute dagNetRoute in array) { netPath = new NetworkPath(targetServerName, dagNetRoute.TargetIPAddr, dagNetRoute.TargetPort, dagNetRoute.SourceIPAddr); netPath.CrossSubnet = dagNetRoute.IsCrossSubnet; this.ApplySocketStreamArgs(netPath, socketStreamBufferPool, socketStreamAsyncArgPool, perfCtrs); netPath.ApplyNetworkPolicy(dagConfig); tcpClientChannel = this.TryConnect(netPath, out ex); if (tcpClientChannel != null) { break; } } } if (tcpClientChannel == null) { netPath = this.BuildDnsNetworkPath(targetServerName, (int)NetworkManager.GetReplicationPort()); this.ApplySocketStreamArgs(netPath, socketStreamBufferPool, socketStreamAsyncArgPool, perfCtrs); netPath.ApplyNetworkPolicy(dagConfig); tcpClientChannel = this.TryConnect(netPath, out ex); if (tcpClientChannel == null) { throw ex; } } return(tcpClientChannel); }
internal static bool TryOpenChannel(NetworkPath netPath, int timeoutInMs, out TcpClientChannel channel, out NetworkTransportException networkEx) { channel = null; networkEx = null; Exception ex = null; Socket socket = null; Stream stream = null; NegotiateStream negotiateStream = null; ReplayStopwatch replayStopwatch = new ReplayStopwatch(); replayStopwatch.Start(); try { socket = new Socket(netPath.TargetEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp); if (netPath.Purpose == NetworkPath.ConnectionPurpose.Seeding) { socket.ReceiveBufferSize = RegistryParameters.SeedingNetworkTransferSize; socket.SendBufferSize = RegistryParameters.SeedingNetworkTransferSize; } else { socket.ReceiveBufferSize = RegistryParameters.LogCopyNetworkTransferSize; socket.SendBufferSize = RegistryParameters.LogCopyNetworkTransferSize; } if (netPath.HasSourceEndpoint()) { socket.Bind(netPath.SourceEndPoint); } TcpClientChannel.ConnectAbandon connectAbandon = new TcpClientChannel.ConnectAbandon(socket); IAsyncResult asyncResult = socket.BeginConnect(netPath.TargetEndPoint.Address, netPath.TargetEndPoint.Port, null, connectAbandon); if (!asyncResult.AsyncWaitHandle.WaitOne(timeoutInMs, false)) { socket = null; connectAbandon.Cancel(asyncResult); TcpChannel.ThrowTimeoutException(netPath.TargetNodeName, ReplayStrings.NetworkConnectionTimeout(timeoutInMs / 1000)); } socket.EndConnect(asyncResult); long elapsedMilliseconds = replayStopwatch.ElapsedMilliseconds; ExTraceGlobals.TcpClientTracer.TraceDebug <long>(0L, "Connection took {0}ms", elapsedMilliseconds); socket.LingerState = new LingerOption(true, 0); if (!netPath.UseSocketStream || RegistryParameters.DisableSocketStream) { stream = new NetworkStream(socket, false); } else { stream = new SocketStream(socket, netPath.SocketStreamBufferPool, netPath.SocketStreamAsyncArgPool, netPath.SocketStreamPerfCounters); } negotiateStream = new NegotiateStream(stream, false); stream = null; elapsedMilliseconds = replayStopwatch.ElapsedMilliseconds; if (elapsedMilliseconds >= (long)timeoutInMs) { TcpChannel.ThrowTimeoutException(netPath.TargetNodeName, ReplayStrings.NetworkConnectionTimeout(timeoutInMs / 1000)); } int num = timeoutInMs - (int)elapsedMilliseconds; negotiateStream.WriteTimeout = num; negotiateStream.ReadTimeout = num; TcpClientChannel.AuthAbandon authAbandon = new TcpClientChannel.AuthAbandon(socket, negotiateStream); string targetName; if (netPath.UseNullSpn) { targetName = ""; } else { targetName = "HOST/" + netPath.TargetNodeName; } bool encrypt = netPath.Encrypt; ProtectionLevel protectionLevel; if (encrypt) { protectionLevel = ProtectionLevel.EncryptAndSign; } else if (RegistryParameters.DisableNetworkSigning) { protectionLevel = ProtectionLevel.None; } else { protectionLevel = ProtectionLevel.Sign; } asyncResult = negotiateStream.BeginAuthenticateAsClient(CredentialCache.DefaultNetworkCredentials, targetName, protectionLevel, TokenImpersonationLevel.Identification, null, authAbandon); if (!asyncResult.AsyncWaitHandle.WaitOne(num, false)) { negotiateStream = null; socket = null; authAbandon.Abandon(asyncResult); TcpChannel.ThrowTimeoutException(netPath.TargetNodeName, ReplayStrings.NetworkConnectionTimeout(timeoutInMs / 1000)); } negotiateStream.EndAuthenticateAsClient(asyncResult); bool flag = false; if (!negotiateStream.IsAuthenticated) { flag = true; } else if (protectionLevel != ProtectionLevel.None && !negotiateStream.IsMutuallyAuthenticated) { if (netPath.IgnoreMutualAuth || MachineName.Comparer.Equals(netPath.TargetNodeName, Environment.MachineName)) { ExTraceGlobals.TcpClientTracer.TraceDebug(0L, "Ignoring mutual auth since we are local"); } else { flag = true; } } if (!flag && encrypt && !negotiateStream.IsEncrypted) { ExTraceGlobals.TcpClientTracer.TraceError(0L, "Encryption requested, but could not be negotiated"); flag = true; } if (flag) { ExTraceGlobals.TcpClientTracer.TraceError <bool, bool, bool>(0L, "Security Negotiation failed. Auth={0},MAuth={1},Encrypt={2}", negotiateStream.IsAuthenticated, negotiateStream.IsMutuallyAuthenticated, negotiateStream.IsEncrypted); NetworkManager.ThrowException(new NetworkCommunicationException(netPath.TargetNodeName, ReplayStrings.NetworkSecurityFailed)); } ExTraceGlobals.TcpClientTracer.TraceDebug <long, bool, ProtectionLevel>(0L, "Authenticated Connection took {0}ms. Encrypt={1} ProtRequested={2}", replayStopwatch.ElapsedMilliseconds, negotiateStream.IsEncrypted, protectionLevel); channel = new TcpClientChannel(netPath.TargetNodeName, socket, negotiateStream, timeoutInMs); return(true); } catch (SocketException ex2) { ex = ex2; } catch (IOException ex3) { ex = ex3; } catch (AuthenticationException ex4) { ex = ex4; } catch (NetworkTransportException ex5) { ex = ex5; } finally { if (channel == null) { if (negotiateStream != null) { negotiateStream.Dispose(); } else if (stream != null) { stream.Dispose(); } if (socket != null) { socket.Close(); } } else { ReplayCrimsonEvents.NetworkConnectionSuccess.Log <string, IPEndPoint, IPEndPoint>(netPath.TargetNodeName, netPath.TargetEndPoint, channel.LocalEndpoint); } } ExTraceGlobals.TcpClientTracer.TraceError <Exception>(0L, "TryOpenChannel failed. Ex={0}", ex); ReplayCrimsonEvents.NetworkConnectionFailure.Log <string, IPEndPoint, IPEndPoint, string>(netPath.TargetNodeName, netPath.TargetEndPoint, netPath.SourceEndPoint, ex.ToString()); if (ex is NetworkTransportException) { networkEx = (NetworkTransportException)ex; } else { networkEx = new NetworkCommunicationException(netPath.TargetNodeName, ex.Message, ex); } return(false); }
private void TrySendStatusMessageToActive(PassiveStatusMsg.Flags statusFlags, long requestAckCounter, out NetworkTransportException sendException, out bool sendSkippedBecauseChannelBusy) { sendException = null; sendSkippedBecauseChannelBusy = false; NetworkChannel netCh = this.m_netChannel; if (this.m_asyncNetWritePending || netCh == null) { PassiveBlockMode.Tracer.TraceError((long)this.GetHashCode(), "SendStatusMessageToActive skipped because channel is busy"); sendSkippedBecauseChannelBusy = true; return; } ReplayState replayState = this.Configuration.ReplayState; long highestCompleteGen = replayState.CopyNotificationGenerationNumber; byte[] statusMsgBuf = PassiveStatusMsg.SerializeToBytes(statusFlags, requestAckCounter, (uint)highestCompleteGen, (uint)replayState.CopyGenerationNumber, (uint)replayState.InspectorGenerationNumber, (uint)replayState.ReplayGenerationNumber, this.m_isCrossSite); bool writeStarted = false; try { Exception ex = NetworkChannel.RunNetworkFunction(delegate { ExTraceGlobals.FaultInjectionTracer.TraceTest(PassiveBlockMode.ackMessageSendFailed); PassiveBlockMode.Tracer.TraceDebug <string, long>((long)this.GetHashCode(), "Sending PassiveStatusMsg({0}) Gen 0x{1:X}", this.DatabaseName, highestCompleteGen); this.m_nextPingDue = DateTime.UtcNow.AddMilliseconds((double)RegistryParameters.LogShipTimeoutInMsec); this.m_asyncNetWritePending = true; netCh.TcpChannel.Stream.BeginWrite(statusMsgBuf, 0, statusMsgBuf.Length, new AsyncCallback(this.NetWriteCompletion), netCh); writeStarted = true; }); if (ex != null) { PassiveBlockMode.Tracer.TraceError <Exception>((long)this.GetHashCode(), "SendStatusMessageToActive Failed to ping Active: {0}", ex); ReplayEventLogConstants.Tuple_NotifyActiveSendFailed.LogEvent(null, new object[] { this.DatabaseName, replayState.ReplayGenerationNumber, replayState.InspectorGenerationNumber, replayState.CopyGenerationNumber, ex.Message }); sendException = (ex as NetworkTransportException); if (sendException == null) { sendException = new NetworkCommunicationException(netCh.PartnerNodeName, ex.Message, ex); } } } finally { if (!writeStarted) { this.m_asyncNetWritePending = false; } } }
public TcpClientChannel TryConnect(NetworkPath netPath, out NetworkTransportException failureEx) { return(this.TryConnect(netPath, DagNetEnvironment.ConnectTimeoutInSec * 1000, out failureEx)); }