Example #1
0
 /// <summary>
 /// Opens the key.
 /// </summary>
 /// <param name="onFailure">A delegate specifiying what happens in case of error</param>
 /// /// <returns>Returns TRUE, if action was successful, otherwise returns FALSE</returns>
 public static bool openKey(FailureDelegate onFailure)
 {
     try     // key initializing
     {
         key = Registry.LocalMachine.CreateSubKey(keyAddr);
         return(true);
     }
     catch
     {
         onFailure();
         return(false);
     }
 }
Example #2
0
    // PUBLIC METHODS /////////////////////////////////////////////////////////////////////////

    /// <summary>
    /// Creates a new key or opens one if it doesn't exist
    /// </summary>
    /// <param name="keyAddr">The addres of the key</param>
    /// <param name="onFailure">A delegate specifiying what happens in case of error</param>
    /// <returns>Returns TRUE, if action was successful, otherwise returns FALSE</returns>
    public static bool createKey(String keyAddr, FailureDelegate onFailure)
    {
        RegistryReaderWriter.keyAddr = keyAddr;

        try     // key initializing
        {
            key = Registry.LocalMachine.CreateSubKey(keyAddr);
            return(true);
        }
        catch
        {
            onFailure();
            return(false);
        }
    }
 /// <summary>
 /// Reports a failure before adding the integers.
 /// </summary>
 /// <returns>The result of adding a and b.</returns>
 /// <param name="a">The integer a.</param>
 /// <param name="b">The integer b.</param>
 /// <param name="fail">The failure callback.</param>
 public static int AddWithFailure(int a, int b, FailureDelegate fail)
 {
     fail("my_error", "This is a test of the error functionality");
     return a + b;
 }
            /// <summary>
            /// Handles a function request.
            /// </summary>
            /// <param name="message">The message body.</param>
            private void HandleFunction(object message)
            {
                try {
                    byte[] data = (byte[])message;

                    // Receive the length of the function name.
                    int nameLength = Convert.ToInt32(Buffer.GetByte(data, 0));

                    // Receive the name of the function.
                    String name = Encoding.UTF8.GetString(data, 1, nameLength);

                    // Receive the number of arguments.
                    ushort argCount = ToUInt16(data, 1 + nameLength);

                    // Lookup the method info.
                    MethodInfo methodInfo = provider.GetType().GetMethods(BindingFlags.Static | BindingFlags.DeclaredOnly | BindingFlags.Public)
                                            .Where(m => m.Name == name)
                                            .Where(m =>
                    {
                        ParameterInfo[] p = m.GetParameters();
                        // If the number of parameters is less than the arg count, return false.
                        if (p.Length == argCount)
                        {
                            return(true);
                        }
                        else if (p.Length == argCount + 1)
                        {
                            string n = p.Last().ParameterType.FullName;
                            if (n == "Thinknode.ProgressDelegate" || n == "Thinknode.FailureDelegate")
                            {
                                return(true);
                            }
                            else
                            {
                                return(false);
                            }
                        }
                        else if (p.Length == argCount + 2)
                        {
                            string n1 = p[p.Length - 2].ParameterType.FullName;
                            string n2 = p[p.Length - 1].ParameterType.FullName;
                            if ((n1 == "Thinknode.ProgressDelegate" && n2 == "Thinknode.FailureDelegate") ||
                                (n1 == "Thinknode.FailureDelegate" && n2 == "Thinknode.ProgressDelegate"))
                            {
                                return(true);
                            }
                            else
                            {
                                return(false);
                            }
                        }
                        else
                        {
                            return(false);
                        }
                    })
                                            .First();
                    if (methodInfo == null)
                    {
                        throw new InvalidOperationException(String.Format("Public static method {0} not found.", name));
                    }

                    // Lookup parameters.
                    ParameterInfo[] parameters = methodInfo.GetParameters();

                    // Decode arguments.
                    object[] args   = new object[parameters.Length];
                    int      offset = 1 + nameLength + 2;
                    ushort   i;
                    for (i = 0; i < argCount; ++i)
                    {
                        uint argLength = ToUInt32(data, offset);
                        offset += 4;
                        byte[] arg = new byte[argLength];
                        Buffer.BlockCopy(data, offset, arg, 0, (int)argLength);
                        offset += (int)argLength;
                        var serializer = MessagePackSerializer.Get(parameters[i].ParameterType, provider.context);
                        args[i] = serializer.UnpackSingleObject(arg);
                    }

                    // Optionally add the progress and failure delegates.
                    string delName;
                    for (; i < parameters.Length; ++i)
                    {
                        delName = parameters[i].ParameterType.FullName;
                        if (delName == "Thinknode.ProgressDelegate")
                        {
                            ProgressDelegate progDel = HandleProgress;
                            args[i] = progDel;
                        }
                        else
                        {
                            FailureDelegate failDel = HandleFailure;
                            args[i] = failDel;
                        }
                    }

                    // Invoke function with arguments from function request message.
                    var    returnSerializer = MessagePackSerializer.Get(methodInfo.ReturnType, provider.context);
                    byte[] result           = returnSerializer.PackSingleObject(methodInfo.Invoke(null, args));

                    // Send result.
                    uint   length = Convert.ToUInt32(result.LongLength);
                    byte[] header = ConstructHeader(1, Action.Result, length);
                    Send(header.Concat(result).ToArray());
                    Console.WriteLine("Completed function...");

                    // Reset source.
                    cancelSource = null;
                }
                catch (Exception ex)
                {
                    HandleException(ex);
                }
            }
Example #5
0
        /// <summary>
        /// Creates a new SignInDataModel instance.
        /// </summary>
        /// <param name="success">callback on sign in success.</param>
        /// <param name="fail">callback on sign in failure.</param>
        public SignInDataModel(
            UserDataContext context,
            SignInSuccessDelegate success, 
            FailureDelegate fail)
        {
            if (success == null || fail == null)
                throw new ArgumentNullException("success | fail");

            this.userContext = context;
            this.onSuccess = success;
            this.onFail = fail;
            this.signInCommand = new RelayCommand(async () => { await SignInAsync(); }, CanSignIn);
        }