Ejemplo n.º 1
0
        public static SaiFrame Parse(byte[] bytes)
        {
            SaiFrame theFrame = null;

            var theFrameType = (SaiFrameType)bytes[0];

            if (theFrameType == SaiFrameType.TTS_OffsetStart)
            {
                theFrame = new SaiTtsFrameOffsetStart();
            }
            else if (theFrameType == SaiFrameType.TTS_OffsetAnswer1)
            {
                theFrame = new SaiTtsFrameOffsetAnswer1();
            }
            else if (theFrameType == SaiFrameType.TTS_OffsetAnswer2)
            {
                theFrame = new SaiTtsFrameOffsetAnswer2();
            }
            else if (theFrameType == SaiFrameType.TTS_OffsetEstimate)
            {
                theFrame = new SaiTtsFrameEstimate();
            }
            else if (theFrameType == SaiFrameType.TTS_OffsetEnd)
            {
                theFrame = new SaiTtsFrameOffsetEnd();
            }
            else if (theFrameType == SaiFrameType.TTS_AppData)
            {
                theFrame = new SaiTtsFrameAppData();
            }
            else if (theFrameType == SaiFrameType.EC_Start)
            {
                theFrame = new SaiEcFrameStart();
            }
            else if (theFrameType == SaiFrameType.EC_AppData)
            {
                theFrame = new SaiEcFrameApplication();
            }
            else if (theFrameType == SaiFrameType.EC_AppDataAskForAck)
            {
                theFrame = new SaiEcFrameAskForAck();
            }
            else if (theFrameType == SaiFrameType.EC_AppDataAcknowlegment)
            {
                theFrame = new SaiEcFrameAcknowlegment();
            }
            else
            {
                throw new InvalidOperationException(string.Format("无法解析指定的Sai帧,不可识别的类型{0}。", theFrameType));
            }

            theFrame.ParseBytes(bytes);

            return(theFrame);
        }
        public void Test1()
        {
            var frameInital = new SaiTtsFrameOffsetAnswer2();

            frameInital.SequenceNo                = 100;
            frameInital.SenderTimestamp           = 1000;
            frameInital.SenderLastRecvTimestamp   = 900;
            frameInital.ReceiverLastSendTimestamp = 800;

            var bytes = frameInital.GetBytes();

            var actual = SaiFrame.Parse(bytes) as SaiTtsFrameOffsetAnswer2;

            Assert.AreEqual(frameInital.FrameType, actual.FrameType);
            Assert.AreEqual(frameInital.SequenceNo, actual.SequenceNo);
            Assert.AreEqual(frameInital.SenderTimestamp, actual.SenderTimestamp);
            Assert.AreEqual(frameInital.SenderLastRecvTimestamp, actual.SenderLastRecvTimestamp);
            Assert.AreEqual(frameInital.ReceiverLastSendTimestamp, actual.ReceiverLastSendTimestamp);
        }
Ejemplo n.º 3
0
        protected override void  HandleTtsOffsetAnswer1Frame(SaiTtsFrameOffsetAnswer1 answer1)
        {
            // 收到Answer1,停止计时器
            LogUtility.Info(string.Format("{0}: 收到的OffsetAnswer1,停止Tint_start计时器。", this.Context.RsspEP.ID));
            this.Context.StopHandshakeTimer();

            // 更新时间戳
            this.Calculator.InitTimestamp2 = this.TTS.LocalLastRecvTimeStamp;
            this.Calculator.ResTimestamp1  = answer1.SenderLastRecvTimestamp;
            this.Calculator.ResTimestamp2  = answer1.SenderTimestamp;

            // 发起方计算偏移
            this.Calculator.EstimateInitOffset();

            // 记录日志
            LogUtility.Info(string.Format("{0}: 发起方估算时钟偏移,OffsetMin = {1}, OffsetMax = {2}",
                                          this.Context.RsspEP.ID, this.Calculator.InitiatorMinOffset, this.Calculator.InitiatorMaxOffset));

            // 发送OffsetAnswer2报文
            var seq     = (ushort)this.Context.SeqNoManager.GetAndUpdateSendSeq();
            var answer2 = new SaiTtsFrameOffsetAnswer2(seq,
                                                       TripleTimestamp.CurrentTimestamp,
                                                       this.TTS.RemoteLastSendTimestamp,
                                                       this.TTS.LocalLastRecvTimeStamp);

            this.Context.NextLayer.SendUserData(answer2.GetBytes());

            LogUtility.Info(string.Format("{0}: 发送OffsetAnswer2。", this.Context.RsspEP.ID));


            // 启动计时器,等待OffsetEst报文
            LogUtility.Info(string.Format("{0}: 启动Tint_start计时器,等待OffsetEst报文", this.Context.RsspEP.ID));
            this.Context.StartHandshakeTimer();

            // 更新状态。
            this.Context.CurrentState = new TtsWaitingforEstimateState(this);
        }
Ejemplo n.º 4
0
 protected virtual void HandleTtsOffsetAnswer2Frame(SaiTtsFrameOffsetAnswer2 frame)
 {
     LogUtility.Error(string.Format("{0}: {1}.{2} not implement!",
                                    this.Context.RsspEP.ID, this.GetType().Name,
                                    new StackFrame(0).GetMethod().Name.Split('.').Last()));
 }