コード例 #1
0
        protected override void Init(Lifetime lifetime)
        {
            base.Init(lifetime);
            var serializationContext = SerializationContext;

            using (UsingLocalChange())
            {
                Advise(lifetime, it =>
                {
                    if (!IsLocalChange)
                    {
                        return;
                    }

                    Wire.Send(RdId, (stream) =>
                    {
                        stream.Write((int)it.Kind);
                        WriteValueDelegate(serializationContext, stream, it.Value);

                        if (LogSend.IsTraceEnabled())
                        {
                            LogSend.Trace("Set `{0}` ({1}) :: {2} :: {3}", Location, RdId, it.Kind, it.Value.PrintToString());
                        }
                    });
                });
            }

            Wire.Advise(lifetime, this);
        }
コード例 #2
0
ファイル: SendLog.cs プロジェクト: JamesFoxChen/RabbitMQ
        public void 发送全局日志_连接初始化一次()
        {
            var sw = new Stopwatch();

            sw.Start();

            for (int i = 0; i < count; i++)
            {
                var msg = new LogQueueModel
                {
                    RequestId   = "RequestId info",
                    RequestMsg  = "RequestMsg info",
                    ResponseMsg = "ResponseMsg info",
                    Date        = DateTime.Now
                };
                LogSend.GetInstance().SendToQueue(msg);
            }
            sw.Stop();

            TimeSpan ts          = sw.Elapsed;
            string   elapsedTime = String.Format("{0:00}:时 {1:00}:分 {2:00}:秒:{3:00}:毫秒",
                                                 ts.Hours, ts.Minutes, ts.Seconds,
                                                 ts.Milliseconds / 10);

            //00:时 00:分 00:秒:22:毫秒(1000)

            Assert.AreEqual(true, true);
        }
コード例 #3
0
ファイル: RdSignal.cs プロジェクト: epeshk/rd
        public void Fire(T value)
        {
//      AssertBound(); // todo: smart assert: fail if fired before bind; this allows bindless signals in UI
            if (!Async)
            {
                AssertThreading();
            }

            AssertNullability(value);

            var wire = Parent?.Proto.Wire;

            if (wire == null && Async)
            {
                return;
            }

            //local change
            wire.NotNull(this).Send(RdId, SendContext.Of(SerializationContext, value, this), (sendContext, stream) =>
            {
                var me = sendContext.This;
                if (LogSend.IsTraceEnabled())
                {
                    LogSend.Trace("signal `{0}` ({1}):: value = {2}", me.Location, me.RdId, sendContext.Event.PrintToString());
                }

                me.myWriteValue(sendContext.SzrCtx, stream, sendContext.Event);
            });

            using (UsingDebugInfo())
                mySignal.Fire(value);
        }
コード例 #4
0
ファイル: RdCall.cs プロジェクト: LongJohnCoder/rd
        private IRdTask <TRes> StartInternal(Lifetime requestLifetime, TReq request, [NotNull] IScheduler scheduler)
        {
            AssertBound();
            if (!Async)
            {
                AssertThreading();
            }
            AssertNullability(request);

            var taskId = Proto.Identities.Next(RdId.Nil);
            var def    = Lifetime.DefineIntersection(requestLifetime, myBindLifetime);
            var task   = new WiredRdTask <TReq, TRes>(def, this, taskId, scheduler);

            Wire.Send(RdId, (writer) =>
            {
                if (LogSend.IsTraceEnabled())
                {
                    LogSend.Trace("call `{0}`::({1}) send request '{2}' : {3}", Location, RdId, taskId, request.PrintToString());
                }

                taskId.Write(writer);
                WriteRequestDelegate(SerializationContext, writer, request);
            });

            return(task);
        }
コード例 #5
0
        public override void OnWireReceived(UnsafeReader reader)
        {
            var taskId = RdId.Read(reader);
            var value  = ReadRequestDelegate(SerializationContext, reader);

            if (LogReceived.IsTraceEnabled())
            {
                LogReceived.Trace("endpoint `{0}`::({1}), taskId={2}, request = {3}", Location, RdId, taskId, value.PrintToString());
            }


            RdTask <TRes> rdTask;

            using (UsingDebugInfo()) //now supports only sync handlers
            {
                try
                {
                    rdTask = Handler(myBindLifetime, value);
                }
                catch (OperationCanceledException)
                {
                    rdTask = RdTask <TRes> .Cancelled();
                }
                catch (Exception e)
                {
                    rdTask = RdTask <TRes> .Faulted(e);
                }
            }

            rdTask.Result.Advise(myBindLifetime, result =>
            {
                if (LogSend.IsTraceEnabled())
                {
                    LogSend.Trace("endpoint `{0}`::({1}), taskId={2}, response = {3}", Location, RdId, taskId, result.PrintToString());
                }

                RdTaskResult <TRes> validatedResult;
                try
                {
                    if (result.Status == RdTaskStatus.Success)
                    {
                        AssertNullability(result.Result);
                    }
                    validatedResult = result;
                }
                catch (Exception e)
                {
                    LogSend.Error(e);
                    validatedResult = RdTaskResult <TRes> .Faulted(e);
                }

                Wire.Send(RdId, (writer) =>
                {
                    taskId.Write(writer);
                    RdTaskResult <TRes> .Write(WriteResponseDelegate, SerializationContext, writer, validatedResult);
                });
            });
        }
コード例 #6
0
        protected override void Init(Lifetime lifetime)
        {
            base.Init(lifetime);

            var serializationContext = SerializationContext;

            if (!OptimizeNested)
            {
                Change.Advise(lifetime, v =>
                {
                    if (IsLocalChange)
                    {
                        v.IdentifyPolymorphic(Proto.Identities, Proto.Identities.Next(RdId));
                    }
                });
            }

            Advise(lifetime, v =>
            {
                if (!IsLocalChange)
                {
                    return;
                }
                if (IsMaster)
                {
                    myMasterVersion++;
                }

                Wire.Send(RdId, SendContext.Of(serializationContext, v, this), (sendContext, writer) =>
                {
                    var sContext = sendContext.SzrCtx;
                    var evt      = sendContext.Event;
                    var me       = sendContext.This;
                    writer.Write(me.myMasterVersion);
                    me.WriteValueDelegate(sContext, writer, evt);
                    if (LogSend.IsTraceEnabled())
                    {
                        LogSend.Trace("property `{0}` ({1}):: ver = {2}, value = {3}", me.Location, me.RdId, me.myMasterVersion,
                                      me.Value.PrintToString());
                    }
                });
            });


            Wire.Advise(lifetime, this);


            if (!OptimizeNested)
            {
                this.View(lifetime, (lf, v) =>
                {
                    v.BindPolymorphic(lf, this, "$");
                });
            }
        }
コード例 #7
0
ファイル: RdList.cs プロジェクト: epeshk/rd
        protected override void Init(Lifetime lifetime)
        {
            base.Init(lifetime);

            var serializationContext = SerializationContext;

            using (UsingLocalChange())
            {
                Advise(lifetime, it =>
                {
                    if (!IsLocalChange)
                    {
                        return;
                    }

                    if (!OptimizeNested && it.Kind != AddUpdateRemove.Remove)
                    {
                        it.NewValue.IdentifyPolymorphic(Proto.Identities, Proto.Identities.Next(RdId));
                    }

                    Wire.Send(RdId, SendContext.Of(serializationContext, it, this), (sendContext, stream) =>
                    {
                        var sContext = sendContext.SzrCtx;
                        var evt      = sendContext.Event;
                        var me       = sendContext.This;

                        stream.Write((me.myNextVersion++ << versionedFlagShift) | (long)evt.Kind);

                        stream.Write(evt.Index);
                        if (evt.Kind != AddUpdateRemove.Remove)
                        {
                            me.WriteValueDelegate(sContext, stream, evt.NewValue);
                        }

                        if (LogSend.IsTraceEnabled())
                        {
                            LogSend.Trace("list `{0}` ({1}) :: {2} :: index={3} :: version = {4}{5}", me.Location, me.RdId, evt.Kind, evt.Index
                                          , me.myNextVersion - 1
                                          , evt.Kind != AddUpdateRemove.Remove  ? " :: value = " + evt.NewValue.PrintToString() : "");
                        }
                    });
                });
            }

            Wire.Advise(lifetime, this);


            if (!OptimizeNested) //means values must be bindable
            {
                this.View(lifetime, (lf, index, value) =>
                {
                    value.BindPolymorphic(lf, this, "[" + index + "]"); //todo name will be not unique when you add elements in the middle of the list
                });
            }
        }
コード例 #8
0
        public override void OnWireReceived(UnsafeReader stream)
        {
            var kind  = (AddRemove)stream.ReadInt();
            var value = ReadValueDelegate(SerializationContext, stream);

            LogSend.Trace("Set `{0}` ({1}) :: {2} :: {3}", Location, RdId, kind, value.PrintToString());

            using (UsingDebugInfo())
            {
                switch (kind)
                {
                case AddRemove.Add:
                    mySet.Add(value);
                    break;

                case AddRemove.Remove:
                    mySet.Remove(value);
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }
        }
コード例 #9
0
ファイル: RdMap.cs プロジェクト: epeshk/rd
        protected override void Init(Lifetime lifetime)
        {
            base.Init(lifetime);

            var serializationContext = SerializationContext;

            using (UsingLocalChange())
            {
                Advise(lifetime, it =>
                {
                    if (!IsLocalChange)
                    {
                        return;
                    }

                    AssertNullability(it.Key);

                    if (it.Kind != AddUpdateRemove.Remove)
                    {
                        AssertNullability(it.NewValue);
                    }

                    if (!OptimizeNested && it.Kind != AddUpdateRemove.Remove)
                    {
                        it.NewValue.IdentifyPolymorphic(Proto.Identities, Proto.Identities.Next(RdId));
                    }

                    Wire.Send(RdId, SendContext.Of(serializationContext, it, this), (sendContext, stream) =>
                    {
                        var sContext      = sendContext.SzrCtx;
                        var evt           = sendContext.Event;
                        var me            = sendContext.This;
                        var versionedFlag = me.IsMaster ? 1 << versionedFlagShift : 0;
                        stream.Write(versionedFlag | (int)evt.Kind);

                        var version = ++me.myNextVersion;
                        if (me.IsMaster)
                        {
                            me.myPendingForAck[evt.Key] = version;
                            stream.Write(version);
                        }

                        me.WriteKeyDelegate(sContext, stream, evt.Key);
                        if (evt.Kind != AddUpdateRemove.Remove)
                        {
                            me.WriteValueDelegate(sContext, stream, evt.NewValue);
                        }

                        if (LogSend.IsTraceEnabled())
                        {
                            LogSend.Trace("map `{0}` ({1}) :: {2} :: key = {3}{4}{5}", me.Location, me.RdId, evt.Kind, evt.Key.PrintToString()
                                          , me.IsMaster     ? " :: version = " + version : ""
                                          , evt.Kind != AddUpdateRemove.Remove  ? " :: value = " + evt.NewValue.PrintToString() : "");
                        }
                    });
                });
            }

            Wire.Advise(lifetime, this);


            if (!OptimizeNested) //means values must be bindable
            {
                this.View(lifetime, (lf, k, v) =>
                {
                    v.BindPolymorphic(lf, this, "[" + k + "]");
                });
            }
        }
コード例 #10
0
ファイル: RdMap.cs プロジェクト: epeshk/rd
        public override void OnWireReceived(UnsafeReader stream)
        {
            var header       = stream.ReadInt();
            var msgVersioned = (header >> versionedFlagShift) != 0;
            var opType       = header & ((1 << versionedFlagShift) - 1);

            var version = msgVersioned ? stream.ReadLong() : 0L;

            var key = ReadKeyDelegate(SerializationContext, stream);

            if (opType == Ack)
            {
                string error = null;

                if (!msgVersioned)
                {
                    error = "Received ACK while msg hasn't versioned flag set";
                }
                else if (!IsMaster)
                {
                    error = "Received ACK when not a Master";
                }
                else if (!myPendingForAck.TryGetValue(key, out var pendingVersion))
                {
                    error = "No pending for ACK";
                }
                else if (pendingVersion < version)
                {
                    error = string.Format("Pending version `{0}` < ACK version `{1}`", pendingVersion, version);
                }
                // Good scenario
                else if (pendingVersion == version)
                {
                    myPendingForAck.Remove(key);
                }
                //else do nothing, silently drop

                var isError = !string.IsNullOrEmpty(error);
                if (LogReceived.IsTraceEnabled() || isError)
                {
                    LogReceived.LogFormat(isError ? LoggingLevel.ERROR : LoggingLevel.TRACE,
                                          "map `{0}` ({1})  :: ACK :: key = {2} :: version = {3}{4}", Location, RdId, key.PrintToString(), version,
                                          isError ? " >> " + error : "");
                }
            }
            else
            {
                using (UsingDebugInfo())
                {
                    var kind = (AddUpdateRemove)opType;
                    switch (kind)
                    {
                    case AddUpdateRemove.Add:
                    case AddUpdateRemove.Update:
                        var value = ReadValueDelegate(SerializationContext, stream);
                        if (LogReceived.IsTraceEnabled())
                        {
                            LogReceived.Trace("map `{0}` ({1})  :: {2} :: key = {3}{4} :: value = {5}", Location, RdId, kind,
                                              key.PrintToString(), msgVersioned ? " :: version = " + version : "", value.PrintToString());
                        }

                        if (msgVersioned || !IsMaster || !myPendingForAck.ContainsKey(key))
                        {
                            myMap[key] = value;
                        }
                        else
                        {
                            LogReceived.Trace(">> CHANGE IGNORED");
                        }

                        break;

                    case AddUpdateRemove.Remove:
                        if (LogReceived.IsTraceEnabled())
                        {
                            LogReceived.Trace("map `{0}` ({1})  :: {2} :: key = {3}{4}", Location, RdId, kind,
                                              key.PrintToString(), msgVersioned ? " :: version = " + version : "");
                        }

                        if (msgVersioned || !IsMaster || !myPendingForAck.ContainsKey(key))
                        {
                            myMap.Remove(key);
                        }
                        else
                        {
                            LogReceived.Trace(">> CHANGE IGNORED");
                        }

                        break;

                    default:
                        throw new ArgumentOutOfRangeException(kind.ToString());
                    }
                }

                if (msgVersioned)
                {
                    Wire.Send(RdId, (innerWriter) =>
                    {
                        innerWriter.Write((1 << versionedFlagShift) | Ack);
                        innerWriter.Write(version);
                        WriteKeyDelegate.Invoke(SerializationContext, innerWriter, key);

                        if (LogSend.IsTraceEnabled())
                        {
                            LogSend.Trace("map `{0}` ({1}) :: ACK :: key = {2} :: version = {3}", Location, RdId, key.PrintToString(), version);
                        }
                    });

                    if (IsMaster)
                    {
                        LogReceived.Error("Both ends are masters: {0}", Location);
                    }
                }
            }
        }
コード例 #11
0
ファイル: RdCall.cs プロジェクト: LongJohnCoder/rd
        public override void OnWireReceived(UnsafeReader reader)
        {
            var taskId = RdId.Read(reader);
            var value  = ReadRequestDelegate(SerializationContext, reader);

            if (LogReceived.IsTraceEnabled())
            {
                LogReceived.Trace("endpoint `{0}`::({1}), taskId={2}, request = {3}", Location, RdId, taskId, value.PrintToString());
            }


            var taskLifetimeDef = myBindLifetime.CreateNested();

            //subscribe for lifetime cancellation
            new WiredLifetime(taskLifetimeDef, taskId, this, Wire);

            RdTask <TRes> rdTask;

            using (UsingDebugInfo()) //now supports only sync handlers
            {
                try
                {
                    rdTask = Handler(taskLifetimeDef.Lifetime, value);
                }
                catch (Exception e)
                {
                    rdTask = RdTask <TRes> .Faulted(e);
                }
            }



            rdTask.Result.Advise(taskLifetimeDef.Lifetime, result =>
            {
                if (LogSend.IsTraceEnabled())
                {
                    LogSend.Trace("endpoint `{0}`::({1}), taskId={2}, response = {3}", Location, RdId, taskId, result.PrintToString());
                }

                RdTaskResult <TRes> validatedResult;
                try
                {
                    if (result.Status == RdTaskStatus.Success)
                    {
                        AssertNullability(result.Result);
                    }
                    validatedResult = result;
                }
                catch (Exception e)
                {
                    LogSend.Error(e);
                    validatedResult = RdTaskResult <TRes> .Faulted(e);
                }

                Wire.Send(taskId, writer =>
                {
                    RdTaskResult <TRes> .Write(WriteResponseDelegate, SerializationContext, writer, validatedResult);
                });

                taskLifetimeDef.Terminate(); //need to terminate to unsubscribe lifetime listener - not for bindable entries
            });
        }