/// <summary>
        /// Handles notifications received from the driver.
        /// </summary>
        /// <param name="driverNotification">Driver notification.</param>
        /// <returns>Reply to be sent back to the driver.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="driverNotification"/> is <see langword="null"/>.</exception>
        /// <exception cref="InvalidOperationException">No handler is found for the <paramref name="driverNotification"/> given.</exception>
        protected override object NotificationsHandler(IDriverNotification driverNotification)
        {
            if (driverNotification == null)
            {
                throw new ArgumentNullException(nameof(driverNotification));
            }

            switch (driverNotification.Type)
            {
            case (int)DriverNotificationType.OpenFileInUserMode:

                Func <OpenFileInUserModeNotification, OpenFileInUserModeNotificationReply> openHandler = this.OpenFileInUserModeHandler;
                if (openHandler != null)
                {
                    string sourceFile = Marshal.PtrToStringUni(driverNotification.Data);
                    string targetFile = Marshal.PtrToStringUni(IntPtr.Add(driverNotification.Data, Marshal.SystemDefaultCharSize * (sourceFile.Length + 1)));

                    OpenFileInUserModeNotification notification = new OpenFileInUserModeNotification
                    {
                        SourceFile = sourceFile,
                        TargetFile = targetFile,
                    };

                    return(openHandler(notification));
                }

                break;

            case (int)DriverNotificationType.CloseFileHandle:

                Action <CloseFileHandleNotification> closeHandler = this.CloseFileHandleHandler;
                if (closeHandler != null)
                {
                    CloseFileHandleNotification notification = new CloseFileHandleNotification {
                        Handle = Marshal.ReadIntPtr(driverNotification.Data)
                    };
                    closeHandler(notification);

                    return(null);
                }

                break;

            case (int)DriverNotificationType.FetchFileInUserMode:

                Func <FetchFileInUserModeNotification, FetchFileInUserModeNotificationReply> fetchHandler = this.FetchFileInUserModeHandler;
                if (fetchHandler != null)
                {
                    string sourceFile = Marshal.PtrToStringUni(driverNotification.Data);
                    string targetFile = Marshal.PtrToStringUni(IntPtr.Add(driverNotification.Data, Marshal.SystemDefaultCharSize * (sourceFile.Length + 1)));

                    FetchFileInUserModeNotification notification = new FetchFileInUserModeNotification
                    {
                        SourceFile = sourceFile,
                        TargetFile = targetFile,
                    };

                    return(fetchHandler(notification));
                }

                break;
            }

            throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "No handler found for the notification of type {0}", driverNotification.Type));
        }
Exemple #2
0
        /// <summary>
        /// Downloads the remote file given.
        /// </summary>
        /// <param name="notification">Driver notification.</param>
        private FetchFileInUserModeNotificationReply FetchFileInUserModeHandler(FetchFileInUserModeNotification notification)
        {
            this.ImpersonateCurrentThread();

            string sourceFile = notification.SourceFile;
            string targetFile = PathHelper.ChangeDeviceNameToDriveLetter(notification.TargetFile);

            if (sourceFile.StartsWith("http://", StringComparison.OrdinalIgnoreCase) || sourceFile.StartsWith("https://", StringComparison.OrdinalIgnoreCase))
            {
                using (WebClient client = new WebClient())
                {
                    client.DownloadFile(sourceFile, targetFile);
                }
            }

            return new FetchFileInUserModeNotificationReply { BytesCopied = 404 };
        }
        /// <summary>
        /// Handles notifications received from the driver.
        /// </summary>
        /// <param name="driverNotification">Driver notification.</param>
        /// <returns>Reply to be sent back to the driver.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="driverNotification"/> is <see langword="null"/>.</exception>
        /// <exception cref="InvalidOperationException">No handler is found for the <paramref name="driverNotification"/> given.</exception>
        protected override object NotificationsHandler(IDriverNotification driverNotification)
        {
            if (driverNotification == null)
            {
                throw new ArgumentNullException(nameof(driverNotification));
            }

            switch (driverNotification.Type)
            {
                case (int)DriverNotificationType.OpenFileInUserMode:

                    Func<OpenFileInUserModeNotification, OpenFileInUserModeNotificationReply> openHandler = this.OpenFileInUserModeHandler;
                    if (openHandler != null)
                    {
                        string sourceFile = Marshal.PtrToStringUni(driverNotification.Data);
                        string targetFile = Marshal.PtrToStringUni(IntPtr.Add(driverNotification.Data, Marshal.SystemDefaultCharSize * (sourceFile.Length + 1)));

                        OpenFileInUserModeNotification notification = new OpenFileInUserModeNotification
                        {
                            SourceFile = sourceFile,
                            TargetFile = targetFile,
                        };

                        return openHandler(notification);
                    }

                    break;

                case (int)DriverNotificationType.CloseFileHandle:

                    Action<CloseFileHandleNotification> closeHandler = this.CloseFileHandleHandler;
                    if (closeHandler != null)
                    {
                        CloseFileHandleNotification notification = new CloseFileHandleNotification { Handle = Marshal.ReadIntPtr(driverNotification.Data) };
                        closeHandler(notification);

                        return null;
                    }

                    break;

                case (int)DriverNotificationType.FetchFileInUserMode:

                    Func<FetchFileInUserModeNotification, FetchFileInUserModeNotificationReply> fetchHandler = this.FetchFileInUserModeHandler;
                    if (fetchHandler != null)
                    {
                        string sourceFile = Marshal.PtrToStringUni(driverNotification.Data);
                        string targetFile = Marshal.PtrToStringUni(IntPtr.Add(driverNotification.Data, Marshal.SystemDefaultCharSize * (sourceFile.Length + 1)));

                        FetchFileInUserModeNotification notification = new FetchFileInUserModeNotification
                        {
                            SourceFile = sourceFile,
                            TargetFile = targetFile,
                        };

                        return fetchHandler(notification);
                    }

                    break;
            }

            throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "No handler found for the notification of type {0}", driverNotification.Type));
        }