コード例 #1
0
        void StopEndpoint(RemoteForwardBridge forwardBridge)
        {
            EventTraceActivity stopActivity = BridgeEventSource.NewActivity("RemoteForwardBridgeStop", activity);

            try
            {
                BridgeEventSource.Log.RemoteForwardBridgeStopping(stopActivity, forwardBridge.ToString());
                forwardBridge.Close();
                BridgeEventSource.Log.RemoteForwardBridgeStop(stopActivity, forwardBridge.ToString());
            }
            catch (Exception exception)
            {
                BridgeEventSource.Log.RemoteForwardBridgeStopFailure(stopActivity, exception);
                if (Fx.IsFatal(exception))
                {
                    throw;
                }
            }
        }
コード例 #2
0
        void StartEndpoint(RemoteForward remoteForward)
        {
            Uri hybridConnectionUri = null;

            EventTraceActivity stopActivity = BridgeEventSource.NewActivity("RemoteForwardBridgeStart", activity);

            stopActivity.DiagnosticsActivity.Start();

            try
            {
                BridgeEventSource.Log.RemoteForwardBridgeStarting(stopActivity, this, remoteForward);

                var rcbs = remoteForward.RelayConnectionStringBuilder ?? new RelayConnectionStringBuilder(this.config.AzureRelayConnectionString);
                rcbs.EntityPath     = remoteForward.RelayName;
                hybridConnectionUri = new Uri(rcbs.Endpoint, rcbs.EntityPath);

                RemoteForwardBridge remoteForwardBridge = null;

                try
                {
                    var remoteForwarders = new Dictionary <string, IRemoteForwarder>();
                    foreach (var binding in remoteForward.Bindings)
                    {
#if !NETFRAMEWORK
                        if (!string.IsNullOrEmpty(binding.LocalSocket))
                        {
                            if (Environment.OSVersion.Platform == PlatformID.Win32NT)
                            {
                                BridgeEventSource.Log.ThrowingException(
                                    new NotSupportedException("Unix sockets are not supported on Windows"));
                            }

                            var socketRemoteForwarder =
                                new SocketRemoteForwarder(binding.PortName, binding.LocalSocket);
                            remoteForwarders.Add(socketRemoteForwarder.PortName, socketRemoteForwarder);
                        }
                        else
#endif
                        if (binding.HostPort > 0)
                        {
                            var tcpRemoteForwarder =
                                new TcpRemoteForwarder(this.config, binding.PortName, binding.Host, binding.HostPort);
                            remoteForwarders.Add(tcpRemoteForwarder.PortName, tcpRemoteForwarder);
                        }
                        else if (binding.HostPort < 0)
                        {
                            var udpRemoteForwarder =
                                new UdpRemoteForwarder(this.config, binding.PortName, binding.Host, -binding.HostPort);
                            remoteForwarders.Add(udpRemoteForwarder.PortName, udpRemoteForwarder);
                        }
                    }

                    remoteForwardBridge         = new RemoteForwardBridge(config, rcbs, remoteForwarders);
                    remoteForwardBridge.Online += (s, e) =>
                    {
                        NotifyOnline(hybridConnectionUri, remoteForward);
                        BridgeEventSource.Log.RemoteForwardBridgeOnline(stopActivity, hybridConnectionUri, remoteForwardBridge);
                    };
                    remoteForwardBridge.Offline += (s, e) =>
                    {
                        NotifyOffline(hybridConnectionUri, remoteForward);
                        BridgeEventSource.Log.RemoteForwardBridgeOffline(stopActivity, hybridConnectionUri, remoteForwardBridge);
                    };
                    remoteForwardBridge.Connecting += (s, e) =>
                    {
                        NotifyConnecting(hybridConnectionUri, remoteForward);
                        BridgeEventSource.Log.RemoteForwardBridgeConnecting(stopActivity, hybridConnectionUri, remoteForwardBridge);
                    };
                    remoteForwardBridge.Open().Wait();

                    this.forwardBridges.Add(hybridConnectionUri.AbsoluteUri, remoteForwardBridge);

                    BridgeEventSource.Log.RemoteForwardBridgeStart(stopActivity, hybridConnectionUri.AbsoluteUri);
                }
                catch (Exception exception)
                {
                    BridgeEventSource.Log.RemoteForwardBridgeStartFailure(stopActivity, hybridConnectionUri, exception);
                    if (Fx.IsFatal(exception))
                    {
                        throw;
                    }

                    try
                    {
                        if (remoteForwardBridge != null)
                        {
                            remoteForwardBridge.Dispose();
                        }
                    }
                    catch (Exception e)
                    {
                        if (Fx.IsFatal(exception))
                        {
                            throw;
                        }
                        BridgeEventSource.Log.HandledExceptionAsWarning(this, e);
                    }

                    if (!this.config.ExitOnForwardFailure.HasValue ||
                        this.config.ExitOnForwardFailure.Value)
                    {
                        throw;
                    }
                }
            }
            finally
            {
                stopActivity.DiagnosticsActivity.Stop();
            }
        }