Ejemplo n.º 1
0
            protected Base(string id, Lifetime lifetime, [NotNull] IScheduler scheduler) : base(scheduler)
            {
                Id         = id;
                Log        = Diagnostics.Log.GetLog(GetType());
                myLifetime = lifetime;
                myAcktor   = new Actor <long>(id + "-ACK", lifetime, SendAck);

                SendBuffer = new ByteBufferAsyncProcessor(id + "-Sender", Send0);
                SendBuffer.Pause(DisconnectedPauseReason);
                SendBuffer.Start();

                Connected.Advise(lifetime, value => HeartbeatAlive.Value = value);


                //when connected
                SocketProvider.Advise(lifetime, socket =>
                {
//          //todo hack for multiconnection, bring it to API
//          if (SupportsReconnect) SendBuffer.Clear();

                    var timer = StartHeartbeat();

                    SendBuffer.ReprocessUnacknowledged();
                    SendBuffer.Resume(DisconnectedPauseReason);

                    scheduler.Queue(() => { Connected.Value = true; });

                    try
                    {
                        //use current thread for receiving procedure
                        ReceiverProc(socket);
                    }
                    finally
                    {
                        scheduler.Queue(() => { Connected.Value = false; });

                        SendBuffer.Pause(DisconnectedPauseReason);

                        timer.Dispose();

                        CloseSocket(socket);
                    }
                });
            }
Ejemplo n.º 2
0
        public unsafe void TestReprocess()
        {
            long prev = 0;
            ByteBufferAsyncProcessor buffer = null;
            List <long> log = new List <long>();

            buffer = new ByteBufferAsyncProcessor("TestAsyncProcessor", 8,
                                                  delegate(byte[] data, int offset, int len, ref long seqN)
            {
                try
                {
                    fixed(byte *b = data)
                    {
                        long l = UnsafeReader.CreateReader(b, 8).ReadLong();
                        if (seqN != 0)
                        {
                            Assert.AreEqual(l, seqN);
                        }
                        seqN = l;
                        log.Add(l);

                        Assert.True(l > prev);
                        prev = l;
                    }
                }
                catch (Exception e)
                {
                    Log.Root.Error(e);
                }
            });

            buffer.ShrinkIntervalMs = 10;
            buffer.Start();

            PutLong(buffer, 1);
            PutLong(buffer, 2);
            PutLong(buffer, 3);
            PutLong(buffer, 4);

            SpinWait.SpinUntil(() => buffer.AllDataProcessed);
            Assert.AreEqual(new List <int> {
                1, 2, 3, 4
            }, log);

            buffer.Acknowledge(2);
            prev = 2;
            buffer.ReprocessUnacknowledged();

            PutLong(buffer, 5);
            SpinWait.SpinUntil(() => buffer.AllDataProcessed);
            Assert.AreEqual(new List <int> {
                1, 2, 3, 4, 3, 4, 5
            }, log);

            buffer.Acknowledge(5);
            buffer.ReprocessUnacknowledged();
            SpinWait.SpinUntil(() => buffer.AllDataProcessed);
            Assert.AreEqual(new List <int> {
                1, 2, 3, 4, 3, 4, 5
            }, log);
        }