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 SaiTtsFrameEstimate(); frameInital.SequenceNo = 100; frameInital.SenderTimestamp = 1000; frameInital.SenderLastRecvTimestamp = 900; frameInital.ReceiverLastSendTimestamp = 800; frameInital.OffsetMax = -3000; frameInital.OffsetMin = -3100; var bytes = frameInital.GetBytes(); var actual = SaiFrame.Parse(bytes) as SaiTtsFrameEstimate; 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); Assert.AreEqual(frameInital.OffsetMax, actual.OffsetMax); Assert.AreEqual(frameInital.OffsetMin, actual.OffsetMin); }
protected override void HandleTtsOffsetAnswer2Frame(Frames.SaiTtsFrameOffsetAnswer2 answer2) { // 收到Answer2,停止计时器 this.Context.StopHandshakeTimer(); LogUtility.Info(string.Format("{0}: 收到OffsetAnswer2报文,停止Tres_start计时器。", this.Context.RsspEP.ID)); // 更新时间戳 this.Calculator.ResTimestamp3 = this.TTS.LocalLastRecvTimeStamp; this.Calculator.InitTimestamp2 = answer2.SenderLastRecvTimestamp; this.Calculator.InitTimestamp3 = answer2.SenderTimestamp; // 应答方计算时钟偏移 this.Calculator.EstimateResOffset(); LogUtility.Info(string.Format("{0}: 应答方估算的时钟偏移,OffsetMin = {1}, OffsetMax = {2}", this.Context.RsspEP.ID, this.Calculator.ResMinOffset, this.Calculator.ResMaxOffset)); // 发送OffsetEst var seq = (ushort)this.Context.SeqNoManager.GetAndUpdateSendSeq(); var estimateFrame = new SaiTtsFrameEstimate(seq, TripleTimestamp.CurrentTimestamp, TTS.RemoteLastSendTimestamp, TTS.LocalLastRecvTimeStamp, this.Calculator.ResMinOffset, this.Calculator.ResMaxOffset); this.Context.NextLayer.SendUserData(estimateFrame.GetBytes()); LogUtility.Info(string.Format("{0}: 发送OffsetEst。", this.Context.RsspEP.ID)); // 启动计时器,等待OffsetEnd报文。 LogUtility.Info(string.Format("{0}: 启动计时器,等待OffsetEnd报文。", this.Context.RsspEP.ID)); this.Context.StartHandshakeTimer(); // 更新状态。 this.Context.CurrentState = new TtsWaitingforEndState(this); }
protected override void HandleTtsEstimateFrame(SaiTtsFrameEstimate estimateFrame) { // 收到OffsetEst,停止计时器 LogUtility.Info(string.Format("{0}: 收到OffsetEst报文,停止Tint_start计时器。", this.Context.RsspEP.ID)); this.Context.StopHandshakeTimer(); // 更新时间戳 this.Calculator.ResMinOffset = estimateFrame.OffsetMin; this.Calculator.ResMaxOffset = estimateFrame.OffsetMax; // 检查估算值 var valid = this.Calculator.IsEstimationValid(); // 更新连接状态。 this.Context.Connected = valid; // 发送OffsetEnd。 var seq = (ushort)this.Context.SeqNoManager.GetAndUpdateSendSeq(); var offsetEndFrame = new SaiTtsFrameOffsetEnd(seq, TripleTimestamp.CurrentTimestamp, TTS.RemoteLastSendTimestamp, TTS.LocalLastRecvTimeStamp, valid); this.Context.NextLayer.SendUserData(offsetEndFrame.GetBytes()); // if (valid) { LogUtility.Info(string.Format("{0}: 发送OffsetEnd,时钟偏移验证通过,SAI层连接成功。", this.Context.RsspEP.ID)); } else { throw new Exception(string.Format("{0}: 时钟偏移估算无效,SAI层连接失败。", this.Context.RsspEP.ID)); } // 更新状态。 this.Context.CurrentState = new TtsConnectedState(this); }
protected virtual void HandleTtsEstimateFrame(SaiTtsFrameEstimate frame) { LogUtility.Error(string.Format("{0}: {1}.{2} not implement!", this.Context.RsspEP.ID, this.GetType().Name, new StackFrame(0).GetMethod().Name.Split('.').Last())); }