Exemple #1
0
        private static IMegaRequest ThreadInit(MegaUser user, Action <Mega> OnSuccess, Action <int> OnError)
        {
            var initTransport = new Transport();
            var mega          = new Mega(initTransport);

            // mandatory anonymous registration
            if (user == null || (user.Email == null && user.Id == null))
            {
                user = new MegaUser();
                var req   = new MRequestCreateAnon <MResponseCreateAnon>(user);
                int?error = null;
                req.Success += (s, args) => user.Id = args.UserId;
                req.Error   += (s, e) => error = e.Error;
                initTransport.EnqueueRequest(req);
                req.ResetEvent.WaitOne();
                if (error != null)
                {
                    OnError((int)error);
                    // just to have the ResetEvent for async compatibility
                    return(new EmptyRequest());
                }
            }

            // set the credentials
            mega.User = user;

            // the login itself
            var sidRequest = new MRequestGetSid <MResponseGetSid>(mega.User);

            sidRequest.Success += (s, args) =>
            {
                mega.User.Sid = args.SessionId;
                mega.User.SetMasterKey(args.MasterKey.Value);

                var getUserRequest = new MRequestGetUser <MResponseGetUser>(mega.User);
                getUserRequest.Success += (s2, args2) =>
                {
                    mega.User.Status = args2.UserStatus;
                    mega.User.Email  = args2.Email;
                    mega.User.Id     = args2.UserId;
                    OnSuccess(mega);
                };
                getUserRequest.Error += (s2, e) =>
                {
                    OnError(e.Error);
                    sidRequest.ResetEvent.Set();
                };
                initTransport.EnqueueRequest(getUserRequest);
                getUserRequest.ResetEvent.WaitOne();
            };
            sidRequest.Error += (s, a) => OnError(a.Error);

            initTransport.EnqueueRequest(sidRequest);
            return(sidRequest);
        }
Exemple #2
0
        private static IMegaRequest ThreadInit(MegaUser user, Action<Mega> OnSuccess, Action<int> OnError)
        {
            var initTransport = new Transport();
            var mega = new Mega(initTransport);

            // mandatory anonymous registration
            if (user == null || (user.Email == null && user.Id == null))
            {
                user = new MegaUser();
                var req = new MRequestCreateAnon<MResponseCreateAnon>(user);
                int? error = null;
                req.Success += (s, args) => user.Id = args.UserId;
                req.Error += (s, e) => error = e.Error;
                initTransport.EnqueueRequest(req);
                req.ResetEvent.WaitOne();
                if (error != null)
                {
                    OnError((int)error);
                    // just to have the ResetEvent for async compatibility
                    return new EmptyRequest();
                }
            }

            // set the credentials
            mega.User = user;

            // the login itself
            var sidRequest = new MRequestGetSid<MResponseGetSid>(mega.User);
            sidRequest.Success += (s, args) =>
            {
                mega.User.Sid = args.SessionId;
                mega.User.SetMasterKey(args.MasterKey.Value);

                var getUserRequest = new MRequestGetUser<MResponseGetUser>(mega.User);
                getUserRequest.Success += (s2, args2) =>
                {
                    mega.User.Status = args2.UserStatus;
                    mega.User.Email = args2.Email;
                    mega.User.Id = args2.UserId;
                    OnSuccess(mega);
                };
                getUserRequest.Error += (s2, e) =>
                {
                    OnError(e.Error);
                    sidRequest.ResetEvent.Set();
                };
                initTransport.EnqueueRequest(getUserRequest);
                getUserRequest.ResetEvent.WaitOne();
            };
            sidRequest.Error += (s, a) => OnError(a.Error);

            initTransport.EnqueueRequest(sidRequest);
            return sidRequest;
        }