Ejemplo n.º 1
0
        public void Close()
        {
            lock (stateLock)
            {
                if (state != State.Started)
                {
                    return;
                }

                state = State.Closing;

                DemonitorTask(receiver);
                receiver.Stop();

                DemonitorTask(receiver);
                sender.Stop();

                socket.Send(BinaryMessage.Make(MSG_CLOSE).Complete());
                // HACK: otherwise close message may not be sent
                Thread.Sleep(TimeSpan.FromMilliseconds(5));
                socket.Close();

                state = State.Closed;
            }
            Dispatcher.Dispatch(new ExitMessage());
        }
Ejemplo n.º 2
0
        protected void AckGobData(long gobId, int frame)
        {
            var message = BinaryMessage.Make(MSG_OBJACK)
                          .UInt32((uint)gobId)
                          .Int32(frame)
                          .Complete();

            // FIXME: make it smarter
            socket.Send(message);
        }
Ejemplo n.º 3
0
        public void ReadWriteTest()
        {
            var input = BinaryMessage.Make(1)
                        .String("Test")
                        .UInt16(64)
                        .String("Test2")
                        .Complete();

            var output = new BinaryMessage(1, input.GetData());

            Assert.AreEqual(output.Type, input.Type);

            using (var reader = output.GetReader())
            {
                Assert.AreEqual("Test", reader.ReadCString());
                Assert.AreEqual(64, reader.ReadUInt16());
                Assert.AreEqual("Test2", reader.ReadCString());
            }
        }
Ejemplo n.º 4
0
        protected override void OnStart()
        {
            var last = DateTime.Now;

            while (!IsCancelled)
            {
                var to   = TimeSpan.FromMilliseconds(5000);
                var now  = DateTime.Now;
                var beat = true;

                lock (pending)
                {
                    if (pending.Count > 0)
                    {
                        to = TimeSpan.FromMilliseconds(60);
                    }
                }
                lock (thisLock)
                {
                    if (ackTime.HasValue)
                    {
                        to = ackTime.Value - now + TimeSpan.FromMilliseconds(AckThreshold);
                    }
                    if (to.TotalMilliseconds > 0)
                    {
                        Monitor.Wait(thisLock, to);
                    }
                }

                lock (pending)
                {
                    foreach (var msg in pending)
                    {
                        int txtime;

                        if (msg.RetryCount == 0)
                        {
                            txtime = 0;
                        }
                        else if (msg.RetryCount == 1)
                        {
                            txtime = 80;
                        }
                        else if (msg.RetryCount < 4)
                        {
                            txtime = 200;
                        }
                        else if (msg.RetryCount < 10)
                        {
                            txtime = 620;
                        }
                        else
                        {
                            txtime = 2000;
                        }

                        if ((now - msg.Last).TotalMilliseconds > txtime)
                        {
                            msg.Last = now;
                            msg.RetryCount++;
                            var rmsg = BinaryMessage.Make(ProtocolHandlerBase.MSG_REL)
                                       .UInt16(msg.Seq)
                                       .Byte(msg.Type)
                                       .Bytes(msg.Content)
                                       .Complete();
                            socket.Send(rmsg);
                            beat = false;
                        }
                    }
                }

                lock (thisLock)
                {
                    if (ackTime.HasValue && ((now - ackTime.Value).TotalMilliseconds >= AckThreshold))
                    {
                        socket.Send(BinaryMessage.Make(ProtocolHandlerBase.MSG_ACK).UInt16(ackSeq).Complete());
                        ackTime = null;
                        beat    = false;
                    }
                }

                if (beat)
                {
                    if ((now - last).TotalMilliseconds > KeepAliveTimeout)
                    {
                        socket.Send(BinaryMessage.Make(ProtocolHandlerBase.MSG_BEAT).Complete());
                        last = now;
                    }
                }
            }
        }