Exemplo n.º 1
0
        public static HttpCapsule BiserDecode(byte[] enc = null, Biser.Decoder extDecoder = null)
        {
            Biser.Decoder decoder = null;
            if (extDecoder == null)
            {
                if (enc == null || enc.Length == 0)
                {
                    return(null);
                }
                decoder = new Biser.Decoder(enc);
            }
            else
            {
                if (extDecoder.CheckNull())
                {
                    return(null);
                }
                else
                {
                    decoder = extDecoder;
                }
            }

            HttpCapsule m = new HttpCapsule();



            m.Action     = decoder.GetString();
            m.EntityType = decoder.GetString();
            m.IsOk       = decoder.GetBool();
            m.Body       = decoder.GetByteArray();


            return(m);
        }
Exemplo n.º 2
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public async Task <ESyncResult> SyncEntity()
        {
            try
            {
                bool repeatSynchro;

                var toServer = new ExchangeData();

                using (var tran = SyncEngine.DBEngine.GetTransaction())
                {
                    toServer.SyncOperations          = this.GetSyncOperations(tran, out repeatSynchro);
                    toServer.LastServerSyncTimeStamp = this.GetLastServerSyncTimeStamp(tran);
                }



                //Sending Entities to server
                //var httpCapsule = await _engine._serverSender("/modules.http.GM_PersonalDevice/IDT_Actions",

                var caps = new HttpCapsule
                {
                    Action     = "SYNC",
                    EntityType = typeof(T).FullName,
                    Body       = toServer.BiserEncoder().Encode()
                };

                var httpCapsuleBt = await SyncEngine._serverSender(_entitySync.urlSync, caps.BiserEncoder().Encode());

                if (httpCapsuleBt == null)  //Synchro with error
                {
                    return(ESyncResult.ERROR);
                }

                var httpCapsule = HttpCapsule.BiserDecode(httpCapsuleBt);

                //if (httpCapsule == null)  //Synchro with error
                //    return ESyncResult.ERROR;

                //Dictionary<string, string> res = httpCapsule.Type.DeserializeJsonSimple();

                if (httpCapsule.IsOk)
                {
                    var exData = ExchangeData.BiserDecode(httpCapsule.Body);

                    if (!repeatSynchro && exData.RepeatSynchro)
                    {
                        repeatSynchro = true;
                    }

                    if (SyncEngine.Verbose)
                    {
                        Console.WriteLine($"SyncEntity<{ typeof(T).Name }> ::: server returned {exData.SyncOperations?.Count} items.");
                    }

                    if (this.UpdateLocalDatabase(exData))
                    {
                        return(await SyncEntity()); //this is a rare exeuting place, only in case if clientSideEntityID equals to existing serverSideEntityID, and even in this case it should be executed only once
                    }
                }
                else if (!httpCapsule.IsOk && httpCapsule.Action == "AUTH FAILED")
                {
                    if (SyncEngine._resetWebSession != null)
                    {
                        try
                        {
                            SyncEngine._resetWebSession?.Invoke();
                        }
                        catch
                        {}
                    }

                    return(ESyncResult.AUTH_FAIL);
                }

                //Repeat call of the procedure
                if (repeatSynchro)
                {
                    return(ESyncResult.REPEAT);
                }
            }
            catch (Exception ex)
            {
                Logger.LogException("EntitySyncingClient.SyncStrategyV1", "SyncEntity", ex, $"type: {typeof(T).Name}");

                return(ESyncResult.ERROR);
            }

            if (SyncEngine.Verbose)
            {
                Console.WriteLine($"SyncEntity<{ typeof(T).Name }> ::: finished");
            }

            return(ESyncResult.OK);
        }