/// <inheritdoc />
        public ISuperMemoAssistant ConnectPlugin(string channel,
                                                 Guid sessionGuid)
        {
            string pluginAssemblyName = "N/A";

            try
            {
                var plugin = RemotingServicesEx.ConnectToIpcServer <ISMAPlugin>(channel);
                pluginAssemblyName = plugin.AssemblyName;

                var pluginInstance = _runningPluginMap.SafeGet(sessionGuid);

                if (pluginInstance == null)
                {
                    LogTo.Warning($"Plugin {pluginAssemblyName} unexpected for connection. Aborting");
                    return(null);
                }

                using (pluginInstance.Lock.Lock())
                    OnPluginConnected(pluginInstance, plugin);

                return(SMA.SMA.Instance);
            }
            catch (Exception ex)
            {
                LogTo.Error(ex, $"An exception occured while connecting plugin {pluginAssemblyName}");

                return(null);
            }
        }
        private void StartIpcServer()
        {
            LogTo.Debug("Starting Plugin IPC Server");

            // Generate random channel name
            IpcServerChannelName = RemotingServicesEx.GenerateIpcServerChannelName();

            IpcServer = RemotingServicesEx.CreateIpcServer <ISMAPluginManager, PluginManager>(this, IpcServerChannelName);
        }
Ejemplo n.º 3
0
        /// <summary>Instantiates a new plugin</summary>
        /// <param name="pluginEntryAssemblyFilePath"></param>
        /// <param name="sessionGuid"></param>
        /// <param name="mgrChannelName"></param>
        /// <param name="mgrProcess"></param>
        /// <param name="isDev"></param>
        /// <param name="cts"></param>
        protected PluginHostBase(
            string pluginEntryAssemblyFilePath,
            Guid sessionGuid,
            string mgrChannelName,
            Process mgrProcess,
            bool isDev,
            CancellationTokenSource cts)
        {
            this._cts = cts;

            // Connect to Plugin Manager
            var pluginMgr = RemotingServicesEx.ConnectToIpcServer <IPluginManager <ICore> >(mgrChannelName);

            if (pluginMgr == null)
            {
                Exit(PluginHostConst.ExitIpcConnectionError);
                return;
            }

            // Setup assembly resolution
            if (isDev)
            {
                AppDomain.CurrentDomain.AssemblyResolve += DevelopmentPluginAssemblyResolver;
            }

            // Load & create plugin
            _plugin = LoadAssembliesAndCreatePluginInstance(pluginEntryAssemblyFilePath);

            if (_plugin == null)
            {
                Exit(PluginHostConst.ExitNoPluginTypeFound);
                return;
            }

            // Connect plugin to Plugin Manager
            var core = pluginMgr.ConnectPlugin(
                _plugin.ChannelName,
                sessionGuid);

            if (core == null)
            {
                Exit(PluginHostConst.ExitCouldNotConnectPlugin);
                return;
            }

            // Inject properties
            InjectPropertyDependencies(_plugin, core, pluginMgr, sessionGuid, isDev);

            _plugin.OnInjected();

            // Start monitoring Plugin Manager process
            if (StartMonitoringPluginMgrProcess(mgrProcess) == false)
            {
                Exit(PluginHostConst.ExitParentExited);
            }
        }
Ejemplo n.º 4
0
        protected void StartIpcServer()
        {
            LogTo.Debug("Starting Plugin IPC Server");

            // Generate random channel name
            IpcServerChannelName = RemotingServicesEx.GenerateIpcServerChannelName();

            IpcServer =
                RemotingServicesEx.CreateIpcServer <IPluginManager <ICore>, TParent>(
                    (TParent)this, IpcServerChannelName);
        }
Ejemplo n.º 5
0
        private string StartIPCServer()
        {
            if (ServerChannel != null)
            {
                throw new InvalidOperationException("IPC Server already started");
            }

            var channelName = RemotingServicesEx.GenerateIpcServerChannelName();

            // TODO: Switch to Duplex (get callback)
            ServerChannel = RemotingServicesEx.CreateIpcServer <SMAHookCallback, SMHookEngine>(
                this,
                channelName
                );

            return(channelName);
        }
        /// <inheritdoc />
        public ICore ConnectPlugin(string channel,
                                   Guid sessionGuid)
        {
            if (channel == null)
            {
                throw new ArgumentNullException(nameof(channel));
            }

            if (sessionGuid == null)
            {
                throw new ArgumentNullException(nameof(sessionGuid));
            }

            string pluginAssemblyName = "N/A";

            try
            {
                var plugin = RemotingServicesEx.ConnectToIpcServer <IPlugin>(channel);
                pluginAssemblyName = plugin.AssemblyName;

                var pluginInstance = RunningPluginMap.SafeGet(sessionGuid);

                if (pluginInstance == null)
                {
                    LogTo.Warning($"Plugin {pluginAssemblyName} unexpected for connection. Aborting");
                    return(null);
                }

                using (pluginInstance.Lock.Lock())
                    OnPluginConnected(pluginInstance, plugin);

                return(GetCoreInstance());
            }
            catch (RemotingException ex)
            {
                LogTo.Warning(ex, $"Connection to plugin {pluginAssemblyName} failed.");

                return(null);
            }
            catch (Exception ex)
            {
                LogTo.Error(ex, $"An exception occured while connecting plugin {pluginAssemblyName}");

                return(null);
            }
        }
Ejemplo n.º 7
0
        public bool OnBrowserConnected(string extensionId, string userAgent, string channel)
        {
            if (BrowserServices.ContainsKey(channel))
            {
                return(false);
            }

            try
            {
                var hostSvc = RemotingServicesEx.ConnectToIpcServer <IBrowserHostService>(channel);

                BrowserServices[extensionId] = new RemoteBrowser(extensionId, userAgent, hostSvc);

                return(true);
            }
            catch (RemotingException)
            {
                return(false);
            }
        }
Ejemplo n.º 8
0
        private async Task <bool> TryConnection()
        {
            try
            {
                var pluginSvc = RemotingServicesEx.ConnectToIpcServer <IImportPluginService>(ImportConst.ChannelName);

                if (await _hostService.OnPluginConnected(pluginSvc, _channelName))
                {
                    var localPluginSvc = pluginSvc;

                    Task.Factory.StartNew(() => KeepAlive(localPluginSvc), TaskCreationOptions.LongRunning).RunAsync();
                    return(true);
                }
            }
            catch (RemotingException) { }
            catch (Exception ex)
            {
                LogTo.Error(ex, "Exception caught in TryConnection");
                throw;
            }

            return(false);
        }
        protected SMAPluginBase(DebuggerAttachStrategy debuggerAttachStrategy = DebuggerAttachStrategy.Never)
            : base(RemotingServicesEx.GenerateIpcServerChannelName())
        {
            switch (debuggerAttachStrategy)
            {
            case DebuggerAttachStrategy.Always:
                Debugger.Launch();
                break;

            case DebuggerAttachStrategy.InDebugConfiguration:
                AttachDebuggerIfDebug();
                break;
            }

            try
            {
                // Required for logging
                Svc.App = CreateApplication();
                Svc.SharedConfiguration = new ConfigurationService(SMAFileSystem.SharedConfigDir);

                Svc.Logger = LoggerFactory.Create(AssemblyName, Svc.SharedConfiguration, ConfigureLogger);
                ReloadAnotarLogger();

                Svc.KeyboardHotKey       = KeyboardHookService.Instance;
                Svc.KeyboardHotKeyLegacy = KeyboardHotKeyService.Instance;
                Svc.Configuration        = new PluginConfigurationService(this);
                Svc.HotKeyManager        = HotKeyManager.Instance.Initialize(Svc.Configuration, Svc.KeyboardHotKey);

                LogTo.Debug($"Plugin {AssemblyName} initialized");
            }
            catch (Exception ex)
            {
                LogTo.Error(ex, $"Exception while initializing {GetType().Name}");
                throw;
            }
        }
Ejemplo n.º 10
0
        public PluginHost(
            string pluginPackageName,
            Guid sessionGuid,
            string smaChannelName,
            Process smaProcess,
            bool isDev)
        {
            // Connect to SMA
            var pluginMgr = RemotingServicesEx.ConnectToIpcServer <ISMAPluginManager>(smaChannelName);

            if (pluginMgr == null)
            {
                Exit(HostConst.ExitIpcConnectionError);
                return;
            }

            // Get required assemblies name
            IEnumerable <string> pluginAssemblies;
            IEnumerable <string> dependenciesAssemblies;

            if (isDev)
            {
                var homePath       = new DirectoryPath(AppDomain.CurrentDomain.BaseDirectory);
                var pluginFilePath = homePath.CombineFile(pluginPackageName + ".dll");

                pluginAssemblies = new List <string>
                {
                    pluginFilePath.FullPath
                };
                dependenciesAssemblies = new List <string>();
            }

            else if (pluginMgr.GetAssembliesPathsForPlugin(
                         sessionGuid,
                         out pluginAssemblies,
                         out dependenciesAssemblies) == false)
            {
                Exit(HostConst.ExitCouldNotGetAssembliesPaths);
                return;
            }

            // Setup assembly resolution
            AppDomain.CurrentDomain.AssemblyResolve += AssemblyResolve;

            // Load & create plugin
            _plugin = LoadAssembliesAndCreatePluginInstance(
                dependenciesAssemblies,
                pluginAssemblies);

            if (_plugin == null)
            {
                Exit(HostConst.ExitNoPluginTypeFound);
                return;
            }

            // Connect plugin to SMA
            var sma = pluginMgr.ConnectPlugin(
                _plugin.ChannelName,
                sessionGuid);

            if (sma == null)
            {
                Exit(HostConst.ExitCouldNotConnectPlugin);
                return;
            }

            // Inject properties
            InjectPropertyDependencies(_plugin, sma, pluginMgr, sessionGuid, isDev);

            _plugin.OnInjected();

            // Start monitoring SMA process
            if (StartMonitoringSMAProcess(smaProcess) == false)
            {
                Exit(HostConst.ExitParentExited);
            }
        }
Ejemplo n.º 11
0
 private void StartIpcServer()
 {
     _channelName = RemotingServicesEx.GenerateIpcServerChannelName();
     RemotingServicesEx.CreateIpcServer <IBrowserHostService, BrowserHostService>(_hostService, _channelName);
 }